diff options
Diffstat (limited to 'crates/ra_hir/src/nameres/collector.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 40e56dfe0..f0e790e4c 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs | |||
@@ -1,11 +1,13 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use ra_cfg::CfgOptions; | ||
3 | use ra_db::FileId; | 4 | use ra_db::FileId; |
4 | use ra_syntax::{ast, SmolStr}; | 5 | use ra_syntax::{ast, SmolStr}; |
5 | use rustc_hash::FxHashMap; | 6 | use rustc_hash::FxHashMap; |
6 | use test_utils::tested_by; | 7 | use test_utils::tested_by; |
7 | 8 | ||
8 | use crate::{ | 9 | use crate::{ |
10 | attr::Attr, | ||
9 | db::DefDatabase, | 11 | db::DefDatabase, |
10 | ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, | 12 | ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, |
11 | name::MACRO_RULES, | 13 | name::MACRO_RULES, |
@@ -35,6 +37,9 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C | |||
35 | } | 37 | } |
36 | } | 38 | } |
37 | 39 | ||
40 | let crate_graph = db.crate_graph(); | ||
41 | let cfg_options = crate_graph.cfg_options(def_map.krate().crate_id()); | ||
42 | |||
38 | let mut collector = DefCollector { | 43 | let mut collector = DefCollector { |
39 | db, | 44 | db, |
40 | def_map, | 45 | def_map, |
@@ -42,6 +47,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C | |||
42 | unresolved_imports: Vec::new(), | 47 | unresolved_imports: Vec::new(), |
43 | unexpanded_macros: Vec::new(), | 48 | unexpanded_macros: Vec::new(), |
44 | macro_stack_monitor: MacroStackMonitor::default(), | 49 | macro_stack_monitor: MacroStackMonitor::default(), |
50 | cfg_options, | ||
45 | }; | 51 | }; |
46 | collector.collect(); | 52 | collector.collect(); |
47 | collector.finish() | 53 | collector.finish() |
@@ -76,8 +82,8 @@ impl MacroStackMonitor { | |||
76 | } | 82 | } |
77 | 83 | ||
78 | /// Walks the tree of module recursively | 84 | /// Walks the tree of module recursively |
79 | struct DefCollector<DB> { | 85 | struct DefCollector<'a, DB> { |
80 | db: DB, | 86 | db: &'a DB, |
81 | def_map: CrateDefMap, | 87 | def_map: CrateDefMap, |
82 | glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>, | 88 | glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>, |
83 | unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, | 89 | unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, |
@@ -86,9 +92,11 @@ struct DefCollector<DB> { | |||
86 | /// Some macro use `$tt:tt which mean we have to handle the macro perfectly | 92 | /// Some macro use `$tt:tt which mean we have to handle the macro perfectly |
87 | /// To prevent stack overflow, we add a deep counter here for prevent that. | 93 | /// To prevent stack overflow, we add a deep counter here for prevent that. |
88 | macro_stack_monitor: MacroStackMonitor, | 94 | macro_stack_monitor: MacroStackMonitor, |
95 | |||
96 | cfg_options: &'a CfgOptions, | ||
89 | } | 97 | } |
90 | 98 | ||
91 | impl<'a, DB> DefCollector<&'a DB> | 99 | impl<DB> DefCollector<'_, DB> |
92 | where | 100 | where |
93 | DB: DefDatabase, | 101 | DB: DefDatabase, |
94 | { | 102 | { |
@@ -506,7 +514,7 @@ struct ModCollector<'a, D> { | |||
506 | parent_module: Option<ParentModule<'a>>, | 514 | parent_module: Option<ParentModule<'a>>, |
507 | } | 515 | } |
508 | 516 | ||
509 | impl<DB> ModCollector<'_, &'_ mut DefCollector<&'_ DB>> | 517 | impl<DB> ModCollector<'_, &'_ mut DefCollector<'_, DB>> |
510 | where | 518 | where |
511 | DB: DefDatabase, | 519 | DB: DefDatabase, |
512 | { | 520 | { |
@@ -523,23 +531,27 @@ where | |||
523 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 531 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
524 | // any other items. | 532 | // any other items. |
525 | for item in items { | 533 | for item in items { |
526 | if let raw::RawItemKind::Import(import_id) = item.kind { | 534 | if self.is_cfg_enabled(&item.attrs) { |
527 | let import = self.raw_items[import_id].clone(); | 535 | if let raw::RawItemKind::Import(import_id) = item.kind { |
528 | if import.is_extern_crate && import.is_macro_use { | 536 | let import = self.raw_items[import_id].clone(); |
529 | self.def_collector.import_macros_from_extern_crate(self.module_id, &import); | 537 | if import.is_extern_crate && import.is_macro_use { |
538 | self.def_collector.import_macros_from_extern_crate(self.module_id, &import); | ||
539 | } | ||
530 | } | 540 | } |
531 | } | 541 | } |
532 | } | 542 | } |
533 | 543 | ||
534 | for item in items { | 544 | for item in items { |
535 | match item.kind { | 545 | if self.is_cfg_enabled(&item.attrs) { |
536 | raw::RawItemKind::Module(m) => self.collect_module(&self.raw_items[m]), | 546 | match item.kind { |
537 | raw::RawItemKind::Import(import_id) => self | 547 | raw::RawItemKind::Module(m) => self.collect_module(&self.raw_items[m]), |
538 | .def_collector | 548 | raw::RawItemKind::Import(import_id) => self |
539 | .unresolved_imports | 549 | .def_collector |
540 | .push((self.module_id, import_id, self.raw_items[import_id].clone())), | 550 | .unresolved_imports |
541 | raw::RawItemKind::Def(def) => self.define_def(&self.raw_items[def]), | 551 | .push((self.module_id, import_id, self.raw_items[import_id].clone())), |
542 | raw::RawItemKind::Macro(mac) => self.collect_macro(&self.raw_items[mac]), | 552 | raw::RawItemKind::Def(def) => self.define_def(&self.raw_items[def]), |
553 | raw::RawItemKind::Macro(mac) => self.collect_macro(&self.raw_items[mac]), | ||
554 | } | ||
543 | } | 555 | } |
544 | } | 556 | } |
545 | } | 557 | } |
@@ -702,6 +714,13 @@ where | |||
702 | self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); | 714 | self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); |
703 | } | 715 | } |
704 | } | 716 | } |
717 | |||
718 | fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { | ||
719 | attrs | ||
720 | .iter() | ||
721 | .flat_map(|attr| attr.as_cfg()) | ||
722 | .all(|cfg| self.def_collector.cfg_options.is_cfg_enabled(cfg).unwrap_or(true)) | ||
723 | } | ||
705 | } | 724 | } |
706 | 725 | ||
707 | fn is_macro_rules(path: &Path) -> bool { | 726 | fn is_macro_rules(path: &Path) -> bool { |
@@ -729,6 +748,7 @@ mod tests { | |||
729 | unresolved_imports: Vec::new(), | 748 | unresolved_imports: Vec::new(), |
730 | unexpanded_macros: Vec::new(), | 749 | unexpanded_macros: Vec::new(), |
731 | macro_stack_monitor: monitor, | 750 | macro_stack_monitor: monitor, |
751 | cfg_options: &CfgOptions::default(), | ||
732 | }; | 752 | }; |
733 | collector.collect(); | 753 | collector.collect(); |
734 | collector.finish() | 754 | collector.finish() |