Check for cfg_attr on the actual item and Debug instead of info in cfg_process

This commit is contained in:
Wyatt Herkamp 2024-03-11 06:55:04 -04:00
parent 948a2dee09
commit 0fb5d0d918

View file

@ -4,7 +4,7 @@ use syntax::{
ast::{self, Attr, HasAttrs, Meta, VariantList}, ast::{self, Attr, HasAttrs, Meta, VariantList},
AstNode, SyntaxElement, SyntaxNode, T, AstNode, SyntaxElement, SyntaxNode, T,
}; };
use tracing::{info, warn}; use tracing::{debug, warn};
use crate::{db::ExpandDatabase, MacroCallKind, MacroCallLoc}; use crate::{db::ExpandDatabase, MacroCallKind, MacroCallLoc};
@ -12,9 +12,9 @@ fn check_cfg_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase) -> O
if !attr.simple_name().as_deref().map(|v| v == "cfg")? { if !attr.simple_name().as_deref().map(|v| v == "cfg")? {
return None; return None;
} }
info!("Evaluating cfg {}", attr); debug!("Evaluating cfg {}", attr);
let cfg = cfg::CfgExpr::parse_from_attr_meta(attr.meta()?)?; let cfg = cfg::CfgExpr::parse_from_attr_meta(attr.meta()?)?;
info!("Checking cfg {:?}", cfg); debug!("Checking cfg {:?}", cfg);
let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg) != Some(false); let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg) != Some(false);
Some(enabled) Some(enabled)
} }
@ -23,10 +23,9 @@ fn check_cfg_attr_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase)
if !attr.simple_name().as_deref().map(|v| v == "cfg_attr")? { if !attr.simple_name().as_deref().map(|v| v == "cfg_attr")? {
return None; return None;
} }
info!("Evaluating cfg_attr {}", attr); debug!("Evaluating cfg_attr {}", attr);
let cfg_expr = cfg::CfgExpr::parse_from_attr_meta(attr.meta()?)?; let cfg_expr = cfg::CfgExpr::parse_from_attr_meta(attr.meta()?)?;
info!("Checking cfg_attr {:?}", cfg_expr); debug!("Checking cfg_attr {:?}", cfg_expr);
let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg_expr) != Some(false); let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg_expr) != Some(false);
Some(enabled) Some(enabled)
} }
@ -40,25 +39,22 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
for item in items { for item in items {
let field_attrs = item.attrs(); let field_attrs = item.attrs();
'attrs: for attr in field_attrs { 'attrs: for attr in field_attrs {
if let Some(enabled) = check_cfg_attr(&attr, loc, db) { if check_cfg_attr(&attr, loc, db).map(|enabled| !enabled).unwrap_or_default() {
// Rustc does not strip the attribute if it is enabled. So we will will leave it debug!("censoring type {:?}", item.syntax());
if !enabled {
info!("censoring type {:?}", item.syntax());
remove.insert(item.syntax().clone().into()); remove.insert(item.syntax().clone().into());
// We need to remove the , as well // We need to remove the , as well
add_comma(&item, remove); add_comma(&item, remove);
break 'attrs; break 'attrs;
} }
};
if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) { if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
if enabled { if enabled {
info!("Removing cfg_attr tokens {:?}", attr); debug!("Removing cfg_attr tokens {:?}", attr);
let meta = attr.meta()?; let meta = attr.meta()?;
let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?; let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?;
remove.extend(removes_from_cfg_attr); remove.extend(removes_from_cfg_attr);
} else { } else {
info!("censoring type cfg_attr {:?}", item.syntax()); debug!("censoring type cfg_attr {:?}", item.syntax());
remove.insert(attr.syntax().clone().into()); remove.insert(attr.syntax().clone().into());
continue; continue;
} }
@ -70,18 +66,18 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
fn remove_tokens_within_cfg_attr(meta: Meta) -> Option<FxHashSet<SyntaxElement>> { fn remove_tokens_within_cfg_attr(meta: Meta) -> Option<FxHashSet<SyntaxElement>> {
let mut remove: FxHashSet<SyntaxElement> = FxHashSet::default(); let mut remove: FxHashSet<SyntaxElement> = FxHashSet::default();
info!("Enabling attribute {}", meta); debug!("Enabling attribute {}", meta);
let meta_path = meta.path()?; let meta_path = meta.path()?;
info!("Removing {:?}", meta_path.syntax()); debug!("Removing {:?}", meta_path.syntax());
remove.insert(meta_path.syntax().clone().into()); remove.insert(meta_path.syntax().clone().into());
let meta_tt = meta.token_tree()?; let meta_tt = meta.token_tree()?;
info!("meta_tt {}", meta_tt); debug!("meta_tt {}", meta_tt);
// Remove the left paren // Remove the left paren
remove.insert(meta_tt.l_paren_token()?.into()); remove.insert(meta_tt.l_paren_token()?.into());
let mut found_comma = false; let mut found_comma = false;
for tt in meta_tt.token_trees_and_tokens().skip(1) { for tt in meta_tt.token_trees_and_tokens().skip(1) {
info!("Checking {:?}", tt); debug!("Checking {:?}", tt);
// Check if it is a subtree or a token. If it is a token check if it is a comma. If so, remove it and break. // Check if it is a subtree or a token. If it is a token check if it is a comma. If so, remove it and break.
match tt { match tt {
syntax::NodeOrToken::Node(node) => { syntax::NodeOrToken::Node(node) => {
@ -119,25 +115,23 @@ fn process_enum(
) -> Option<()> { ) -> Option<()> {
'variant: for variant in variants.variants() { 'variant: for variant in variants.variants() {
for attr in variant.attrs() { for attr in variant.attrs() {
if let Some(enabled) = check_cfg_attr(&attr, loc, db) { if check_cfg_attr(&attr, loc, db).map(|enabled| !enabled).unwrap_or_default() {
// Rustc does not strip the attribute if it is enabled. So we will will leave it // Rustc does not strip the attribute if it is enabled. So we will will leave it
if !enabled { debug!("censoring type {:?}", variant.syntax());
info!("censoring type {:?}", variant.syntax());
remove.insert(variant.syntax().clone().into()); remove.insert(variant.syntax().clone().into());
// We need to remove the , as well // We need to remove the , as well
add_comma(&variant, remove); add_comma(&variant, remove);
continue 'variant; continue 'variant;
}
}; };
if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) { if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
if enabled { if enabled {
info!("Removing cfg_attr tokens {:?}", attr); debug!("Removing cfg_attr tokens {:?}", attr);
let meta = attr.meta()?; let meta = attr.meta()?;
let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?; let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?;
remove.extend(removes_from_cfg_attr); remove.extend(removes_from_cfg_attr);
} else { } else {
info!("censoring type cfg_attr {:?}", variant.syntax()); debug!("censoring type cfg_attr {:?}", variant.syntax());
remove.insert(attr.syntax().clone().into()); remove.insert(attr.syntax().clone().into());
continue; continue;
} }
@ -166,31 +160,45 @@ pub(crate) fn process_cfg_attrs(
if !matches!(loc.kind, MacroCallKind::Derive { .. }) { if !matches!(loc.kind, MacroCallKind::Derive { .. }) {
return None; return None;
} }
let mut res = FxHashSet::default(); let mut remove = FxHashSet::default();
let item = ast::Item::cast(node.clone())?; let item = ast::Item::cast(node.clone())?;
for attr in item.attrs() {
if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
if enabled {
debug!("Removing cfg_attr tokens {:?}", attr);
let meta = attr.meta()?;
let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?;
remove.extend(removes_from_cfg_attr);
} else {
debug!("censoring type cfg_attr {:?}", item.syntax());
remove.insert(attr.syntax().clone().into());
continue;
}
}
}
match item { match item {
ast::Item::Struct(it) => match it.field_list()? { ast::Item::Struct(it) => match it.field_list()? {
ast::FieldList::RecordFieldList(fields) => { ast::FieldList::RecordFieldList(fields) => {
process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut res)?; process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut remove)?;
} }
ast::FieldList::TupleFieldList(fields) => { ast::FieldList::TupleFieldList(fields) => {
process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut res)?; process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut remove)?;
} }
}, },
ast::Item::Enum(it) => { ast::Item::Enum(it) => {
process_enum(it.variant_list()?, loc, db, &mut res)?; process_enum(it.variant_list()?, loc, db, &mut remove)?;
} }
ast::Item::Union(it) => { ast::Item::Union(it) => {
process_has_attrs_with_possible_comma( process_has_attrs_with_possible_comma(
it.record_field_list()?.fields(), it.record_field_list()?.fields(),
loc, loc,
db, db,
&mut res, &mut remove,
)?; )?;
} }
// FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now // FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now
_ => {} _ => {}
} }
Some(res) Some(remove)
} }