2024-03-09 18:25:56 +00:00
|
|
|
//! Processes out #[cfg] and #[cfg_attr] attributes from the input for the derive macro
|
2024-03-08 16:10:29 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
use syntax::{
|
2024-03-09 18:25:56 +00:00
|
|
|
ast::{self, Attr, HasAttrs, Meta, VariantList},
|
2024-03-08 16:10:29 +00:00
|
|
|
AstNode, SyntaxElement, SyntaxNode, T,
|
|
|
|
};
|
2024-03-11 10:55:04 +00:00
|
|
|
use tracing::{debug, warn};
|
2024-03-08 16:10:29 +00:00
|
|
|
|
2024-03-09 18:25:56 +00:00
|
|
|
use crate::{db::ExpandDatabase, MacroCallKind, MacroCallLoc};
|
2024-03-08 16:10:29 +00:00
|
|
|
|
2024-03-09 18:25:56 +00:00
|
|
|
fn check_cfg_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase) -> Option<bool> {
|
|
|
|
if !attr.simple_name().as_deref().map(|v| v == "cfg")? {
|
|
|
|
return None;
|
|
|
|
}
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Evaluating cfg {}", attr);
|
2024-03-09 18:25:56 +00:00
|
|
|
let cfg = cfg::CfgExpr::parse_from_attr_meta(attr.meta()?)?;
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Checking cfg {:?}", cfg);
|
2024-03-08 16:10:29 +00:00
|
|
|
let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg) != Some(false);
|
|
|
|
Some(enabled)
|
|
|
|
}
|
|
|
|
|
2024-03-09 18:25:56 +00:00
|
|
|
fn check_cfg_attr_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase) -> Option<bool> {
|
|
|
|
if !attr.simple_name().as_deref().map(|v| v == "cfg_attr")? {
|
2024-03-08 16:10:29 +00:00
|
|
|
return None;
|
|
|
|
}
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Evaluating cfg_attr {}", attr);
|
2024-03-09 18:25:56 +00:00
|
|
|
let cfg_expr = cfg::CfgExpr::parse_from_attr_meta(attr.meta()?)?;
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Checking cfg_attr {:?}", cfg_expr);
|
2024-03-09 18:25:56 +00:00
|
|
|
let enabled = db.crate_graph()[loc.krate].cfg_options.check(&cfg_expr) != Some(false);
|
|
|
|
Some(enabled)
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn process_has_attrs_with_possible_comma<I: HasAttrs>(
|
|
|
|
items: impl Iterator<Item = I>,
|
|
|
|
loc: &MacroCallLoc,
|
|
|
|
db: &dyn ExpandDatabase,
|
2024-03-09 18:25:56 +00:00
|
|
|
remove: &mut FxHashSet<SyntaxElement>,
|
2024-03-08 16:10:29 +00:00
|
|
|
) -> Option<()> {
|
|
|
|
for item in items {
|
|
|
|
let field_attrs = item.attrs();
|
|
|
|
'attrs: for attr in field_attrs {
|
2024-03-11 10:55:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-03-09 18:25:56 +00:00
|
|
|
|
|
|
|
if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
|
|
|
|
if enabled {
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Removing cfg_attr tokens {:?}", attr);
|
2024-03-09 18:25:56 +00:00
|
|
|
let meta = attr.meta()?;
|
|
|
|
let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?;
|
|
|
|
remove.extend(removes_from_cfg_attr);
|
|
|
|
} else {
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("censoring type cfg_attr {:?}", item.syntax());
|
2024-03-09 18:25:56 +00:00
|
|
|
remove.insert(attr.syntax().clone().into());
|
2024-03-08 16:10:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
2024-03-09 18:25:56 +00:00
|
|
|
|
|
|
|
fn remove_tokens_within_cfg_attr(meta: Meta) -> Option<FxHashSet<SyntaxElement>> {
|
|
|
|
let mut remove: FxHashSet<SyntaxElement> = FxHashSet::default();
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Enabling attribute {}", meta);
|
2024-03-09 18:25:56 +00:00
|
|
|
let meta_path = meta.path()?;
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Removing {:?}", meta_path.syntax());
|
2024-03-09 18:25:56 +00:00
|
|
|
remove.insert(meta_path.syntax().clone().into());
|
|
|
|
|
|
|
|
let meta_tt = meta.token_tree()?;
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("meta_tt {}", meta_tt);
|
2024-03-09 18:25:56 +00:00
|
|
|
// 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) {
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Checking {:?}", tt);
|
2024-03-09 18:25:56 +00:00
|
|
|
// 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) => {
|
|
|
|
// Remove the entire subtree
|
|
|
|
remove.insert(node.syntax().clone().into());
|
|
|
|
}
|
|
|
|
syntax::NodeOrToken::Token(token) => {
|
|
|
|
if token.kind() == T![,] {
|
|
|
|
found_comma = true;
|
|
|
|
remove.insert(token.into());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
remove.insert(token.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found_comma {
|
|
|
|
warn!("No comma found in {}", meta_tt);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// Remove the right paren
|
|
|
|
remove.insert(meta_tt.r_paren_token()?.into());
|
|
|
|
Some(remove)
|
|
|
|
}
|
|
|
|
fn add_comma(item: &impl AstNode, res: &mut FxHashSet<SyntaxElement>) {
|
|
|
|
if let Some(comma) = item.syntax().next_sibling_or_token().filter(|it| it.kind() == T![,]) {
|
|
|
|
res.insert(comma);
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 16:10:29 +00:00
|
|
|
fn process_enum(
|
|
|
|
variants: VariantList,
|
|
|
|
loc: &MacroCallLoc,
|
|
|
|
db: &dyn ExpandDatabase,
|
2024-03-09 18:25:56 +00:00
|
|
|
remove: &mut FxHashSet<SyntaxElement>,
|
2024-03-08 16:10:29 +00:00
|
|
|
) -> Option<()> {
|
2024-03-09 18:25:56 +00:00
|
|
|
'variant: for variant in variants.variants() {
|
|
|
|
for attr in variant.attrs() {
|
2024-03-11 10:55:04 +00:00
|
|
|
if check_cfg_attr(&attr, loc, db).map(|enabled| !enabled).unwrap_or_default() {
|
2024-03-09 18:25:56 +00:00
|
|
|
// Rustc does not strip the attribute if it is enabled. So we will will leave it
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("censoring type {:?}", variant.syntax());
|
|
|
|
remove.insert(variant.syntax().clone().into());
|
|
|
|
// We need to remove the , as well
|
|
|
|
add_comma(&variant, remove);
|
|
|
|
continue 'variant;
|
2024-03-09 18:25:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(enabled) = check_cfg_attr_attr(&attr, loc, db) {
|
|
|
|
if enabled {
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("Removing cfg_attr tokens {:?}", attr);
|
2024-03-09 18:25:56 +00:00
|
|
|
let meta = attr.meta()?;
|
|
|
|
let removes_from_cfg_attr = remove_tokens_within_cfg_attr(meta)?;
|
|
|
|
remove.extend(removes_from_cfg_attr);
|
|
|
|
} else {
|
2024-03-11 10:55:04 +00:00
|
|
|
debug!("censoring type cfg_attr {:?}", variant.syntax());
|
2024-03-09 18:25:56 +00:00
|
|
|
remove.insert(attr.syntax().clone().into());
|
|
|
|
continue;
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(fields) = variant.field_list() {
|
|
|
|
match fields {
|
|
|
|
ast::FieldList::RecordFieldList(fields) => {
|
2024-03-09 18:25:56 +00:00
|
|
|
process_has_attrs_with_possible_comma(fields.fields(), loc, db, remove)?;
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
|
|
|
ast::FieldList::TupleFieldList(fields) => {
|
2024-03-09 18:25:56 +00:00
|
|
|
process_has_attrs_with_possible_comma(fields.fields(), loc, db, remove)?;
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
2024-03-09 18:25:56 +00:00
|
|
|
|
2024-03-08 16:10:29 +00:00
|
|
|
pub(crate) fn process_cfg_attrs(
|
|
|
|
node: &SyntaxNode,
|
|
|
|
loc: &MacroCallLoc,
|
|
|
|
db: &dyn ExpandDatabase,
|
|
|
|
) -> Option<FxHashSet<SyntaxElement>> {
|
2024-03-09 18:25:56 +00:00
|
|
|
// FIXME: #[cfg_eval] is not implemented. But it is not stable yet
|
|
|
|
if !matches!(loc.kind, MacroCallKind::Derive { .. }) {
|
|
|
|
return None;
|
|
|
|
}
|
2024-03-11 10:55:04 +00:00
|
|
|
let mut remove = FxHashSet::default();
|
2024-03-09 18:25:56 +00:00
|
|
|
|
2024-03-08 16:10:29 +00:00
|
|
|
let item = ast::Item::cast(node.clone())?;
|
2024-03-11 10:55:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 16:10:29 +00:00
|
|
|
match item {
|
|
|
|
ast::Item::Struct(it) => match it.field_list()? {
|
|
|
|
ast::FieldList::RecordFieldList(fields) => {
|
2024-03-11 10:55:04 +00:00
|
|
|
process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut remove)?;
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
|
|
|
ast::FieldList::TupleFieldList(fields) => {
|
2024-03-11 10:55:04 +00:00
|
|
|
process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut remove)?;
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::Item::Enum(it) => {
|
2024-03-11 10:55:04 +00:00
|
|
|
process_enum(it.variant_list()?, loc, db, &mut remove)?;
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|
2024-03-09 18:25:56 +00:00
|
|
|
ast::Item::Union(it) => {
|
|
|
|
process_has_attrs_with_possible_comma(
|
|
|
|
it.record_field_list()?.fields(),
|
|
|
|
loc,
|
|
|
|
db,
|
2024-03-11 10:55:04 +00:00
|
|
|
&mut remove,
|
2024-03-09 18:25:56 +00:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
// FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now
|
2024-03-08 16:10:29 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2024-03-11 10:55:04 +00:00
|
|
|
Some(remove)
|
2024-03-08 16:10:29 +00:00
|
|
|
}
|