diff options
Diffstat (limited to 'crates/ra_hir/src/nameres/collector.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index ef7dc6ebe..cef2dc9d2 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs | |||
@@ -1,3 +1,6 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use ra_cfg::CfgOptions; | ||
1 | use ra_db::FileId; | 4 | use ra_db::FileId; |
2 | use ra_syntax::{ast, SmolStr}; | 5 | use ra_syntax::{ast, SmolStr}; |
3 | use rustc_hash::FxHashMap; | 6 | use rustc_hash::FxHashMap; |
@@ -33,6 +36,9 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C | |||
33 | } | 36 | } |
34 | } | 37 | } |
35 | 38 | ||
39 | let crate_graph = db.crate_graph(); | ||
40 | let cfg_options = crate_graph.cfg_options(def_map.krate().crate_id()); | ||
41 | |||
36 | let mut collector = DefCollector { | 42 | let mut collector = DefCollector { |
37 | db, | 43 | db, |
38 | def_map, | 44 | def_map, |
@@ -40,6 +46,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C | |||
40 | unresolved_imports: Vec::new(), | 46 | unresolved_imports: Vec::new(), |
41 | unexpanded_macros: Vec::new(), | 47 | unexpanded_macros: Vec::new(), |
42 | macro_stack_monitor: MacroStackMonitor::default(), | 48 | macro_stack_monitor: MacroStackMonitor::default(), |
49 | cfg_options, | ||
43 | }; | 50 | }; |
44 | collector.collect(); | 51 | collector.collect(); |
45 | collector.finish() | 52 | collector.finish() |
@@ -74,8 +81,8 @@ impl MacroStackMonitor { | |||
74 | } | 81 | } |
75 | 82 | ||
76 | /// Walks the tree of module recursively | 83 | /// Walks the tree of module recursively |
77 | struct DefCollector<DB> { | 84 | struct DefCollector<'a, DB> { |
78 | db: DB, | 85 | db: &'a DB, |
79 | def_map: CrateDefMap, | 86 | def_map: CrateDefMap, |
80 | glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>, | 87 | glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>, |
81 | unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, | 88 | unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, |
@@ -84,9 +91,11 @@ struct DefCollector<DB> { | |||
84 | /// Some macro use `$tt:tt which mean we have to handle the macro perfectly | 91 | /// Some macro use `$tt:tt which mean we have to handle the macro perfectly |
85 | /// To prevent stack overflow, we add a deep counter here for prevent that. | 92 | /// To prevent stack overflow, we add a deep counter here for prevent that. |
86 | macro_stack_monitor: MacroStackMonitor, | 93 | macro_stack_monitor: MacroStackMonitor, |
94 | |||
95 | cfg_options: &'a CfgOptions, | ||
87 | } | 96 | } |
88 | 97 | ||
89 | impl<'a, DB> DefCollector<&'a DB> | 98 | impl<DB> DefCollector<'_, DB> |
90 | where | 99 | where |
91 | DB: DefDatabase, | 100 | DB: DefDatabase, |
92 | { | 101 | { |
@@ -504,7 +513,7 @@ struct ModCollector<'a, D> { | |||
504 | parent_module: Option<ParentModule<'a>>, | 513 | parent_module: Option<ParentModule<'a>>, |
505 | } | 514 | } |
506 | 515 | ||
507 | impl<DB> ModCollector<'_, &'_ mut DefCollector<&'_ DB>> | 516 | impl<DB> ModCollector<'_, &'_ mut DefCollector<'_, DB>> |
508 | where | 517 | where |
509 | DB: DefDatabase, | 518 | DB: DefDatabase, |
510 | { | 519 | { |
@@ -521,24 +530,27 @@ where | |||
521 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 530 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
522 | // any other items. | 531 | // any other items. |
523 | for item in items { | 532 | for item in items { |
524 | if let raw::RawItem::Import(import_id) = *item { | 533 | if self.is_cfg_enabled(&item.attrs) { |
525 | let import = self.raw_items[import_id].clone(); | 534 | if let raw::RawItemKind::Import(import_id) = item.kind { |
526 | if import.is_extern_crate && import.is_macro_use { | 535 | let import = self.raw_items[import_id].clone(); |
527 | self.def_collector.import_macros_from_extern_crate(self.module_id, &import); | 536 | if import.is_extern_crate && import.is_macro_use { |
537 | self.def_collector.import_macros_from_extern_crate(self.module_id, &import); | ||
538 | } | ||
528 | } | 539 | } |
529 | } | 540 | } |
530 | } | 541 | } |
531 | 542 | ||
532 | for item in items { | 543 | for item in items { |
533 | match *item { | 544 | if self.is_cfg_enabled(&item.attrs) { |
534 | raw::RawItem::Module(m) => self.collect_module(&self.raw_items[m]), | 545 | match item.kind { |
535 | raw::RawItem::Import(import_id) => self.def_collector.unresolved_imports.push(( | 546 | raw::RawItemKind::Module(m) => self.collect_module(&self.raw_items[m]), |
536 | self.module_id, | 547 | raw::RawItemKind::Import(import_id) => self |
537 | import_id, | 548 | .def_collector |
538 | self.raw_items[import_id].clone(), | 549 | .unresolved_imports |
539 | )), | 550 | .push((self.module_id, import_id, self.raw_items[import_id].clone())), |
540 | raw::RawItem::Def(def) => self.define_def(&self.raw_items[def]), | 551 | raw::RawItemKind::Def(def) => self.define_def(&self.raw_items[def]), |
541 | raw::RawItem::Macro(mac) => self.collect_macro(&self.raw_items[mac]), | 552 | raw::RawItemKind::Macro(mac) => self.collect_macro(&self.raw_items[mac]), |
553 | } | ||
542 | } | 554 | } |
543 | } | 555 | } |
544 | } | 556 | } |
@@ -662,7 +674,10 @@ where | |||
662 | // Case 1: macro rules, define a macro in crate-global mutable scope | 674 | // Case 1: macro rules, define a macro in crate-global mutable scope |
663 | if is_macro_rules(&mac.path) { | 675 | if is_macro_rules(&mac.path) { |
664 | if let Some(name) = &mac.name { | 676 | if let Some(name) = &mac.name { |
665 | let macro_id = MacroDefId(mac.ast_id.with_file_id(self.file_id)); | 677 | let macro_id = MacroDefId { |
678 | ast_id: mac.ast_id.with_file_id(self.file_id), | ||
679 | krate: self.def_collector.def_map.krate, | ||
680 | }; | ||
666 | let macro_ = MacroDef { id: macro_id }; | 681 | let macro_ = MacroDef { id: macro_id }; |
667 | self.def_collector.define_macro(self.module_id, name.clone(), macro_, mac.export); | 682 | self.def_collector.define_macro(self.module_id, name.clone(), macro_, mac.export); |
668 | } | 683 | } |
@@ -698,6 +713,14 @@ where | |||
698 | self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); | 713 | self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); |
699 | } | 714 | } |
700 | } | 715 | } |
716 | |||
717 | fn is_cfg_enabled(&self, attrs: &raw::Attrs) -> bool { | ||
718 | attrs.as_ref().map_or(true, |attrs| { | ||
719 | attrs | ||
720 | .iter() | ||
721 | .all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) | ||
722 | }) | ||
723 | } | ||
701 | } | 724 | } |
702 | 725 | ||
703 | fn is_macro_rules(path: &Path) -> bool { | 726 | fn is_macro_rules(path: &Path) -> bool { |
@@ -725,6 +748,7 @@ mod tests { | |||
725 | unresolved_imports: Vec::new(), | 748 | unresolved_imports: Vec::new(), |
726 | unexpanded_macros: Vec::new(), | 749 | unexpanded_macros: Vec::new(), |
727 | macro_stack_monitor: monitor, | 750 | macro_stack_monitor: monitor, |
751 | cfg_options: &CfgOptions::default(), | ||
728 | }; | 752 | }; |
729 | collector.collect(); | 753 | collector.collect(); |
730 | collector.finish() | 754 | collector.finish() |