about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWyatt Herkamp <wherkamp@gmail.com>2024-03-11 06:55:04 -0400
committerWyatt Herkamp <wherkamp@gmail.com>2024-03-11 06:55:04 -0400
commit0fb5d0d918e1deae39f1a561707a6b06a2981925 (patch)
tree102d18c501dee1331c441041fae32b3e8a91baa3
parent948a2dee0993b77240cb369a26cfd75d350d373c (diff)
downloadrust-0fb5d0d918e1deae39f1a561707a6b06a2981925.tar.gz
rust-0fb5d0d918e1deae39f1a561707a6b06a2981925.zip
Check for cfg_attr on the actual item and Debug instead of info in cfg_process
-rw-r--r--crates/hir-expand/src/cfg_process.rs84
1 files changed, 46 insertions, 38 deletions
diff --git a/crates/hir-expand/src/cfg_process.rs b/crates/hir-expand/src/cfg_process.rs
index d67402b0b8e..1d088ada646 100644
--- a/crates/hir-expand/src/cfg_process.rs
+++ b/crates/hir-expand/src/cfg_process.rs
@@ -4,7 +4,7 @@ use syntax::{
     ast::{self, Attr, HasAttrs, Meta, VariantList},
     AstNode, SyntaxElement, SyntaxNode, T,
 };
-use tracing::{info, warn};
+use tracing::{debug, warn};
 
 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")? {
         return None;
     }
-    info!("Evaluating cfg {}", attr);
+    debug!("Evaluating cfg {}", attr);
     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);
     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")? {
         return None;
     }
-    info!("Evaluating cfg_attr {}", attr);
-
+    debug!("Evaluating cfg_attr {}", attr);
     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);
     Some(enabled)
 }
@@ -40,25 +39,22 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
     for item in items {
         let field_attrs = item.attrs();
         'attrs: for attr in field_attrs {
-            if let Some(enabled) = check_cfg_attr(&attr, loc, db) {
-                // Rustc does not strip the attribute if it is enabled. So we will will leave it
-                if !enabled {
-                    info!("censoring type {:?}", item.syntax());
-                    remove.insert(item.syntax().clone().into());
-                    // We need to remove the , as well
-                    add_comma(&item, remove);
-                    break 'attrs;
-                }
-            };
+            if check_cfg_attr(&attr, loc, db).map(|enabled| !enabled).unwrap_or_default() {
+                debug!("censoring type {:?}", item.syntax());
+                remove.insert(item.syntax().clone().into());
+                // We need to remove the , as well
+                add_comma(&item, remove);
+                break 'attrs;
+            }
 
             if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
                 if enabled {
-                    info!("Removing cfg_attr tokens {:?}", attr);
+                    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 {
-                    info!("censoring type cfg_attr {:?}", item.syntax());
+                    debug!("censoring type cfg_attr {:?}", item.syntax());
                     remove.insert(attr.syntax().clone().into());
                     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>> {
     let mut remove: FxHashSet<SyntaxElement> = FxHashSet::default();
-    info!("Enabling attribute {}", meta);
+    debug!("Enabling attribute {}", meta);
     let meta_path = meta.path()?;
-    info!("Removing {:?}", meta_path.syntax());
+    debug!("Removing {:?}", meta_path.syntax());
     remove.insert(meta_path.syntax().clone().into());
 
     let meta_tt = meta.token_tree()?;
-    info!("meta_tt {}", meta_tt);
+    debug!("meta_tt {}", meta_tt);
     // Remove the left paren
     remove.insert(meta_tt.l_paren_token()?.into());
     let mut found_comma = false;
     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.
         match tt {
             syntax::NodeOrToken::Node(node) => {
@@ -119,25 +115,23 @@ fn process_enum(
 ) -> Option<()> {
     'variant: for variant in variants.variants() {
         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
-                if !enabled {
-                    info!("censoring type {:?}", variant.syntax());
-                    remove.insert(variant.syntax().clone().into());
-                    // We need to remove the , as well
-                    add_comma(&variant, remove);
-                    continue 'variant;
-                }
+                debug!("censoring type {:?}", variant.syntax());
+                remove.insert(variant.syntax().clone().into());
+                // We need to remove the , as well
+                add_comma(&variant, remove);
+                continue 'variant;
             };
 
             if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
                 if enabled {
-                    info!("Removing cfg_attr tokens {:?}", attr);
+                    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 {
-                    info!("censoring type cfg_attr {:?}", variant.syntax());
+                    debug!("censoring type cfg_attr {:?}", variant.syntax());
                     remove.insert(attr.syntax().clone().into());
                     continue;
                 }
@@ -166,31 +160,45 @@ pub(crate) fn process_cfg_attrs(
     if !matches!(loc.kind, MacroCallKind::Derive { .. }) {
         return None;
     }
-    let mut res = FxHashSet::default();
+    let mut remove = FxHashSet::default();
 
     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 {
         ast::Item::Struct(it) => match it.field_list()? {
             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) => {
-                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) => {
-            process_enum(it.variant_list()?, loc, db, &mut res)?;
+            process_enum(it.variant_list()?, loc, db, &mut remove)?;
         }
         ast::Item::Union(it) => {
             process_has_attrs_with_possible_comma(
                 it.record_field_list()?.fields(),
                 loc,
                 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
         _ => {}
     }
-    Some(res)
+    Some(remove)
 }