Skip to content

Commit 810af61

Browse files
committed
Add is_third_party library config
1 parent 0062004 commit 810af61

File tree

6 files changed

+47
-39
lines changed

6 files changed

+47
-39
lines changed

vhdl_lang/src/config.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct Config {
2525
pub struct LibraryConfig {
2626
name: String,
2727
patterns: Vec<String>,
28+
pub(crate) is_third_party: bool,
2829
}
2930

3031
impl LibraryConfig {
@@ -156,11 +157,23 @@ impl Config {
156157
patterns.push(path);
157158
}
158159

160+
let mut is_third_party = false;
161+
if let Some(opt) = lib.get("is_third_party") {
162+
if let Some(opt) = opt.as_bool() {
163+
is_third_party = opt;
164+
} else {
165+
return Err(format!(
166+
"Expected is_third_party to be boolean for library {name}"
167+
));
168+
}
169+
}
170+
159171
libraries.insert(
160172
name.to_owned(),
161173
LibraryConfig {
162174
name: name.to_owned(),
163175
patterns,
176+
is_third_party,
164177
},
165178
);
166179
}
@@ -199,13 +212,7 @@ impl Config {
199212
&library.name
200213
)));
201214
} else {
202-
self.libraries.insert(
203-
library.name.clone(),
204-
LibraryConfig {
205-
name: library.name.clone(),
206-
patterns: library.patterns.clone(),
207-
},
208-
);
215+
self.libraries.insert(library.name.clone(), library.clone());
209216
}
210217
}
211218
}

vhdl_lang/src/lint/dead_code.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::data::DiagnosticHandler;
1818
use crate::data::Symbol;
1919
use crate::syntax::TokenAccess;
2020
use crate::AnyEntKind;
21+
use crate::Config;
2122
use crate::Design;
2223
use crate::Diagnostic;
2324
use crate::EntRef;
@@ -206,6 +207,7 @@ impl UnusedDeclarationsLinter {
206207
pub fn lint(
207208
&mut self,
208209
root: &DesignRoot,
210+
config: &Config,
209211
analyzed_units: &[UnitId],
210212
diagnostics: &mut dyn DiagnosticHandler,
211213
) {
@@ -229,11 +231,6 @@ impl UnusedDeclarationsLinter {
229231
let key = (unit.library_name().clone(), unit.primary_name().clone());
230232

231233
if let Some(library) = root.get_lib(unit.library_name()) {
232-
if *library.name() == root.symbol_utf8("ieee") {
233-
// Do not check dead code in ieee library
234-
continue;
235-
}
236-
237234
self.diagnostics.entry(key).or_insert_with(|| {
238235
find_unused_declarations(root, library, unit.primary_name())
239236
.into_iter()
@@ -248,8 +245,12 @@ impl UnusedDeclarationsLinter {
248245
}
249246
}
250247

251-
for unit_diagnostics in self.diagnostics.values() {
252-
diagnostics.append(unit_diagnostics.iter().cloned());
248+
for ((library_name, _), unit_diagnostics) in self.diagnostics.iter() {
249+
if let Some(library_config) = config.get_library(&library_name.name_utf8()) {
250+
if !library_config.is_third_party {
251+
diagnostics.append(unit_diagnostics.iter().cloned());
252+
}
253+
}
253254
}
254255
}
255256
}

vhdl_lang/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ fn main() {
7070
let iterations = 10;
7171
println!("Running {iterations} iterations for benchmarking");
7272
for _ in 0..(iterations - 1) {
73-
let mut project = Project::from_config(&config, &mut NullMessages);
73+
let mut project = Project::from_config(config.clone(), &mut NullMessages);
7474
project.analyse();
7575
}
7676
iterations
7777
} else {
7878
1
7979
};
8080

81-
let mut project = Project::from_config(&config, &mut msg_printer);
81+
let mut project = Project::from_config(config, &mut msg_printer);
8282
let mut diagnostics = project.analyse();
8383
let duration = start.elapsed().unwrap() / iterations;
8484

vhdl_lang/src/project.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::path::{Path, PathBuf};
1616

1717
pub struct Project {
1818
parser: VHDLParser,
19+
config: Config,
1920
root: DesignRoot,
2021
files: FnvHashMap<PathBuf, SourceFile>,
2122
empty_libraries: FnvHashSet<Symbol>,
@@ -31,24 +32,24 @@ impl Project {
3132
empty_libraries: FnvHashSet::default(),
3233
parser,
3334
lint: Default::default(),
35+
config: Config::default(),
3436
}
3537
}
3638

3739
/// Create instance from given configuration.
3840
/// Files referred by configuration are parsed into corresponding libraries.
39-
pub fn from_config(config: &Config, messages: &mut dyn MessageHandler) -> Project {
41+
pub fn from_config(config: Config, messages: &mut dyn MessageHandler) -> Project {
4042
let mut project = Project::new();
41-
42-
let files = project.load_files_from_config(config, messages);
43+
let files = project.load_files_from_config(&config, messages);
4344
project.parse_and_add_files(files, messages);
44-
45+
project.config = config;
4546
project
4647
}
4748

4849
/// Replace active project configuration.
4950
/// The design state is reset, new files are added and parsed. Existing source files will be
5051
/// kept and parsed from in-memory source (required for incremental document updates).
51-
pub fn update_config(&mut self, config: &Config, messages: &mut dyn MessageHandler) {
52+
pub fn update_config(&mut self, config: Config, messages: &mut dyn MessageHandler) {
5253
self.parser = VHDLParser::default();
5354
self.root = DesignRoot::new(self.parser.symbols.clone());
5455

@@ -61,7 +62,7 @@ impl Project {
6162
// Files might already be part of self.files, these have to be parsed
6263
// from in-memory source. New files can be parsed as usual.
6364
let (known_files, new_files) = self
64-
.load_files_from_config(config, messages)
65+
.load_files_from_config(&config, messages)
6566
.into_iter()
6667
.partition(|(file_name, _library_names)| self.files.contains_key(file_name));
6768

@@ -75,6 +76,7 @@ impl Project {
7576
}
7677
}
7778

79+
self.config = config;
7880
self.parse_and_add_files(new_files, messages);
7981
}
8082

@@ -227,7 +229,7 @@ impl Project {
227229

228230
let analyzed_units = self.root.analyze(&mut diagnostics);
229231
self.lint
230-
.lint(&self.root, &analyzed_units, &mut diagnostics);
232+
.lint(&self.root, &self.config, &analyzed_units, &mut diagnostics);
231233

232234
diagnostics
233235
}
@@ -384,15 +386,15 @@ lib.files = ['file.vhd']
384386

385387
let config = Config::from_str(config_str, root.path()).unwrap();
386388
let mut messages = Vec::new();
387-
let mut project = Project::from_config(&config, &mut messages);
389+
let mut project = Project::from_config(config, &mut messages);
388390
assert_eq!(messages, vec![]);
389391
check_no_diagnostics(&project.analyse());
390392
}
391393

392394
#[test]
393395
fn unmapped_libraries_are_analyzed() {
394396
let mut messages = Vec::new();
395-
let mut project = Project::from_config(&Config::default(), &mut messages);
397+
let mut project = Project::from_config(Config::default(), &mut messages);
396398
assert_eq!(messages, vec![]);
397399
let diagnostics = project.analyse();
398400
check_no_diagnostics(&diagnostics);
@@ -466,7 +468,7 @@ use_lib.files = ['use_file.vhd']
466468

467469
let config = Config::from_str(config_str, root.path()).unwrap();
468470
let mut messages = Vec::new();
469-
let mut project = Project::from_config(&config, &mut messages);
471+
let mut project = Project::from_config(config, &mut messages);
470472
assert_eq!(messages, vec![]);
471473
check_no_diagnostics(&project.analyse());
472474
}
@@ -516,7 +518,7 @@ lib2.files = ['file2.vhd']
516518

517519
let config = Config::from_str(config_str, &root).unwrap();
518520
let mut messages = Vec::new();
519-
let mut project = Project::from_config(&config, &mut messages);
521+
let mut project = Project::from_config(config, &mut messages);
520522
assert_eq!(messages, vec![]);
521523
check_no_diagnostics(&project.analyse());
522524

@@ -621,7 +623,7 @@ lib.files = ['file2.vhd']
621623
let config2 = Config::from_str(config_str2, &root).unwrap();
622624

623625
let mut messages = Vec::new();
624-
let mut project = Project::from_config(&config1, &mut messages);
626+
let mut project = Project::from_config(config1, &mut messages);
625627
assert_eq!(messages, vec![]);
626628

627629
// Invalid library should only be reported in source1
@@ -631,7 +633,7 @@ lib.files = ['file2.vhd']
631633
assert_eq!(diagnostics[1].pos.source, source1); // No declaration
632634

633635
// Change configuration file
634-
project.update_config(&config2, &mut messages);
636+
project.update_config(config2, &mut messages);
635637
assert_eq!(messages, vec![]);
636638

637639
// Invalid library should only be reported in source2

vhdl_libraries/vhdl_ls.toml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[libraries]
2-
std.files = [
3-
'std/*.vhd',
4-
]
5-
ieee.files = [
6-
'ieee2008/*.vhdl',
7-
'synopsys/*.vhdl',
8-
'vital2000/*.vhdl',
9-
]
2+
3+
std.files = ['std/*.vhd']
4+
std.is_third_party = true
5+
6+
ieee.files = ['ieee2008/*.vhdl', 'synopsys/*.vhdl', 'vital2000/*.vhdl']
7+
ieee.is_third_party = true

vhdl_ls/src/vhdl_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl VHDLServer {
110110
pub fn initialize_request(&mut self, init_params: InitializeParams) -> InitializeResult {
111111
self.config_file = self.root_uri_config_file(&init_params);
112112
let config = self.load_config();
113-
self.project = Project::from_config(&config, &mut self.message_filter());
113+
self.project = Project::from_config(config, &mut self.message_filter());
114114
self.init_params = Some(init_params);
115115
let trigger_chars: Vec<String> = r".".chars().map(|ch| ch.to_string()).collect();
116116

@@ -255,7 +255,7 @@ impl VHDLServer {
255255
let config = self.load_config();
256256

257257
self.project
258-
.update_config(&config, &mut self.message_filter());
258+
.update_config(config, &mut self.message_filter());
259259
self.publish_diagnostics();
260260
}
261261
}

0 commit comments

Comments
 (0)