|
| 1 | +use std::os::windows::process; |
| 2 | + |
| 3 | +use mbe::syntax_node_to_token_tree; |
| 4 | +use rustc_hash::FxHashSet; |
| 5 | +use syntax::{ |
| 6 | + ast::{self, Attr, FieldList, HasAttrs, RecordFieldList, TupleFieldList, Variant, VariantList}, |
| 7 | + AstNode, SyntaxElement, SyntaxNode, T, |
| 8 | +}; |
| 9 | +use tracing::info; |
| 10 | + |
| 11 | +use crate::{db::ExpandDatabase, span_map::SpanMap, MacroCallLoc}; |
| 12 | + |
| 13 | +fn check_cfg_attr( |
| 14 | + attr: &Attr, |
| 15 | + loc: &MacroCallLoc, |
| 16 | + span_map: &SpanMap, |
| 17 | + db: &dyn ExpandDatabase, |
| 18 | +) -> Option<bool> { |
| 19 | + attr.simple_name().as_deref().map(|v| v == "cfg")?; |
| 20 | + info!("Checking cfg attr {:?}", attr); |
| 21 | + let Some(tt) = attr.token_tree() else { |
| 22 | + info!("cfg attr has no expr {:?}", attr); |
| 23 | + return Some(true); |
| 24 | + }; |
| 25 | + info!("Checking cfg {:?}", tt); |
| 26 | + let tt = tt.syntax().clone(); |
| 27 | + // Convert to a tt::Subtree |
| 28 | + let tt = syntax_node_to_token_tree(&tt, span_map, loc.call_site); |
| 29 | + let cfg = cfg::CfgExpr::parse(&tt); |
| 30 | + let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg) != Some(false); |
| 31 | + Some(enabled) |
| 32 | +} |
| 33 | +enum CfgAttrResult { |
| 34 | + Enabled(Attr), |
| 35 | + Disabled, |
| 36 | +} |
| 37 | + |
| 38 | +fn check_cfg_attr_attr( |
| 39 | + attr: &Attr, |
| 40 | + loc: &MacroCallLoc, |
| 41 | + span_map: &SpanMap, |
| 42 | + db: &dyn ExpandDatabase, |
| 43 | +) -> Option<CfgAttrResult> { |
| 44 | + attr.simple_name().as_deref().map(|v| v == "cfg_attr")?; |
| 45 | + info!("Checking cfg_attr attr {:?}", attr); |
| 46 | + let Some(tt) = attr.token_tree() else { |
| 47 | + info!("cfg_attr attr has no expr {:?}", attr); |
| 48 | + return None; |
| 49 | + }; |
| 50 | + info!("Checking cfg_attr {:?}", tt); |
| 51 | + let tt = tt.syntax().clone(); |
| 52 | + // Convert to a tt::Subtree |
| 53 | + let tt = syntax_node_to_token_tree(&tt, span_map, loc.call_site); |
| 54 | + let cfg = cfg::CfgExpr::parse(&tt); |
| 55 | + let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg) != Some(false); |
| 56 | + if enabled { |
| 57 | + // FIXME: Add the internal attribute |
| 58 | + Some(CfgAttrResult::Enabled(attr.clone())) |
| 59 | + } else { |
| 60 | + Some(CfgAttrResult::Disabled) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn process_has_attrs_with_possible_comma<I: HasAttrs>( |
| 65 | + items: impl Iterator<Item = I>, |
| 66 | + loc: &MacroCallLoc, |
| 67 | + span_map: &SpanMap, |
| 68 | + db: &dyn ExpandDatabase, |
| 69 | + res: &mut FxHashSet<SyntaxElement>, |
| 70 | +) -> Option<()> { |
| 71 | + for item in items { |
| 72 | + let field_attrs = item.attrs(); |
| 73 | + 'attrs: for attr in field_attrs { |
| 74 | + let Some(enabled) = check_cfg_attr(&attr, loc, span_map, db) else { |
| 75 | + continue; |
| 76 | + }; |
| 77 | + if enabled { |
| 78 | + //FIXME: Should we remove the cfg_attr? |
| 79 | + } else { |
| 80 | + info!("censoring type {:?}", item.syntax()); |
| 81 | + res.insert(item.syntax().clone().into()); |
| 82 | + // We need to remove the , as well |
| 83 | + if let Some(comma) = item.syntax().next_sibling_or_token() { |
| 84 | + if comma.kind() == T![,] { |
| 85 | + res.insert(comma.into()); |
| 86 | + } |
| 87 | + } |
| 88 | + break 'attrs; |
| 89 | + } |
| 90 | + let Some(attr_result) = check_cfg_attr_attr(&attr, loc, span_map, db) else { |
| 91 | + continue; |
| 92 | + }; |
| 93 | + match attr_result { |
| 94 | + CfgAttrResult::Enabled(attr) => { |
| 95 | + //FIXME: Replace the attribute with the internal attribute |
| 96 | + } |
| 97 | + CfgAttrResult::Disabled => { |
| 98 | + info!("censoring type {:?}", item.syntax()); |
| 99 | + res.insert(attr.syntax().clone().into()); |
| 100 | + continue; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + Some(()) |
| 106 | +} |
| 107 | +fn process_enum( |
| 108 | + variants: VariantList, |
| 109 | + loc: &MacroCallLoc, |
| 110 | + span_map: &SpanMap, |
| 111 | + db: &dyn ExpandDatabase, |
| 112 | + res: &mut FxHashSet<SyntaxElement>, |
| 113 | +) -> Option<()> { |
| 114 | + for variant in variants.variants() { |
| 115 | + 'attrs: for attr in variant.attrs() { |
| 116 | + if !check_cfg_attr(&attr, loc, span_map, db)? { |
| 117 | + info!("censoring variant {:?}", variant.syntax()); |
| 118 | + res.insert(variant.syntax().clone().into()); |
| 119 | + if let Some(comma) = variant.syntax().next_sibling_or_token() { |
| 120 | + if comma.kind() == T![,] { |
| 121 | + res.insert(comma.into()); |
| 122 | + } |
| 123 | + } |
| 124 | + break 'attrs; |
| 125 | + } |
| 126 | + } |
| 127 | + if let Some(fields) = variant.field_list() { |
| 128 | + match fields { |
| 129 | + ast::FieldList::RecordFieldList(fields) => { |
| 130 | + process_has_attrs_with_possible_comma(fields.fields(), loc, span_map, db, res)?; |
| 131 | + } |
| 132 | + ast::FieldList::TupleFieldList(fields) => { |
| 133 | + process_has_attrs_with_possible_comma(fields.fields(), loc, span_map, db, res)?; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + Some(()) |
| 139 | +} |
| 140 | +/// Handle |
| 141 | +pub(crate) fn process_cfg_attrs( |
| 142 | + node: &SyntaxNode, |
| 143 | + loc: &MacroCallLoc, |
| 144 | + span_map: &SpanMap, |
| 145 | + db: &dyn ExpandDatabase, |
| 146 | +) -> Option<FxHashSet<SyntaxElement>> { |
| 147 | + let mut res = FxHashSet::default(); |
| 148 | + let item = ast::Item::cast(node.clone())?; |
| 149 | + match item { |
| 150 | + ast::Item::Struct(it) => match it.field_list()? { |
| 151 | + ast::FieldList::RecordFieldList(fields) => { |
| 152 | + process_has_attrs_with_possible_comma( |
| 153 | + fields.fields(), |
| 154 | + loc, |
| 155 | + span_map, |
| 156 | + db, |
| 157 | + &mut res, |
| 158 | + )?; |
| 159 | + } |
| 160 | + ast::FieldList::TupleFieldList(fields) => { |
| 161 | + process_has_attrs_with_possible_comma( |
| 162 | + fields.fields(), |
| 163 | + loc, |
| 164 | + span_map, |
| 165 | + db, |
| 166 | + &mut res, |
| 167 | + )?; |
| 168 | + } |
| 169 | + }, |
| 170 | + ast::Item::Enum(it) => { |
| 171 | + process_enum(it.variant_list()?, loc, span_map, db, &mut res)?; |
| 172 | + } |
| 173 | + // FIXME: Implement for other items |
| 174 | + _ => {} |
| 175 | + } |
| 176 | + |
| 177 | + Some(res) |
| 178 | +} |
0 commit comments