rust-analyzer/crates/hir-expand/src/cfg_process.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

328 lines
13 KiB
Rust
Raw Normal View History

2024-03-09 18:25:56 +00:00
//! Processes out #[cfg] and #[cfg_attr] attributes from the input for the derive macro
2024-03-11 15:05:59 +00:00
use std::iter::Peekable;
use cfg::{CfgAtom, CfgExpr};
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-11 15:05:59 +00:00
AstNode, NodeOrToken, SyntaxElement, SyntaxNode, T,
2024-03-08 16:10:29 +00:00
};
use tracing::{debug, warn};
2024-03-11 15:05:59 +00:00
use tt::SmolStr;
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;
}
debug!("Evaluating cfg {}", attr);
2024-03-11 15:05:59 +00:00
let cfg = parse_from_attr_meta(attr.meta()?)?;
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;
}
debug!("Evaluating cfg_attr {}", attr);
2024-03-11 15:05:59 +00:00
let cfg_expr = parse_from_attr_meta(attr.meta()?)?;
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 {
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
2024-03-11 15:05:59 +00:00
remove_possible_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 {
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 {
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-11 15:05:59 +00:00
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum CfgExprStage {
/// Stripping the CFGExpr part of the attribute
StrippigCfgExpr,
/// Found the comma after the CFGExpr. Will keep all tokens until the next comma or the end of the attribute
FoundComma,
/// Everything following the attribute. This could be another attribute or the end of the attribute.
// FIXME: cfg_attr with multiple attributes will not be handled correctly. We will only keep the first attribute
// Related Issue: https://github.com/rust-lang/rust-analyzer/issues/10110
EverythingElse,
}
/// This function creates its own set of tokens to remove. To help prevent malformed syntax as input.
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();
debug!("Enabling attribute {}", meta);
2024-03-09 18:25:56 +00:00
let meta_path = meta.path()?;
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()?;
debug!("meta_tt {}", meta_tt);
2024-03-11 15:05:59 +00:00
let mut stage = CfgExprStage::StrippigCfgExpr;
for tt in meta_tt.token_trees_and_tokens() {
debug!("Checking {:?}. Stage: {:?}", tt, stage);
match (stage, tt) {
(CfgExprStage::StrippigCfgExpr, syntax::NodeOrToken::Node(node)) => {
2024-03-09 18:25:56 +00:00
remove.insert(node.syntax().clone().into());
}
2024-03-11 15:05:59 +00:00
(CfgExprStage::StrippigCfgExpr, syntax::NodeOrToken::Token(token)) => {
2024-03-09 18:25:56 +00:00
if token.kind() == T![,] {
2024-03-11 15:05:59 +00:00
stage = CfgExprStage::FoundComma;
2024-03-09 18:25:56 +00:00
}
remove.insert(token.into());
}
2024-03-11 15:05:59 +00:00
(CfgExprStage::FoundComma, syntax::NodeOrToken::Token(token))
if (token.kind() == T![,] || token.kind() == T![')']) =>
{
// The end of the attribute or separator for the next attribute
stage = CfgExprStage::EverythingElse;
remove.insert(token.into());
}
(CfgExprStage::EverythingElse, syntax::NodeOrToken::Node(node)) => {
remove.insert(node.syntax().clone().into());
}
(CfgExprStage::EverythingElse, syntax::NodeOrToken::Token(token)) => {
remove.insert(token.into());
}
// This is an actual attribute
_ => {}
2024-03-09 18:25:56 +00:00
}
}
2024-03-11 15:05:59 +00:00
if stage != CfgExprStage::EverythingElse {
warn!("Invalid cfg_attr attribute. {:?}", meta_tt);
2024-03-09 18:25:56 +00:00
return None;
}
Some(remove)
}
2024-03-11 15:05:59 +00:00
/// Removes a possible comma after the [AstNode]
fn remove_possible_comma(item: &impl AstNode, res: &mut FxHashSet<SyntaxElement>) {
2024-03-09 18:25:56 +00:00
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() {
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 leave it
debug!("censoring type {:?}", variant.syntax());
remove.insert(variant.syntax().clone().into());
// We need to remove the , as well
2024-03-11 15:05:59 +00:00
remove_possible_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 {
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 {
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
2024-03-18 11:10:02 +00:00
if !matches!(loc.kind, MacroCallKind::Derive { .. } | MacroCallKind::DeriveAttr { .. }) {
2024-03-09 18:25:56 +00:00
return None;
}
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())?;
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) => {
process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut remove)?;
2024-03-08 16:10:29 +00:00
}
ast::FieldList::TupleFieldList(fields) => {
process_has_attrs_with_possible_comma(fields.fields(), loc, db, &mut remove)?;
2024-03-08 16:10:29 +00:00
}
},
ast::Item::Enum(it) => {
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,
&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
_ => {}
}
Some(remove)
2024-03-08 16:10:29 +00:00
}
2024-03-11 15:05:59 +00:00
/// Parses a `cfg` attribute from the meta
fn parse_from_attr_meta(meta: Meta) -> Option<CfgExpr> {
let tt = meta.token_tree()?;
let mut iter = tt.token_trees_and_tokens().skip(1).peekable();
next_cfg_expr_from_syntax(&mut iter)
}
fn next_cfg_expr_from_syntax<I>(iter: &mut Peekable<I>) -> Option<CfgExpr>
where
I: Iterator<Item = NodeOrToken<ast::TokenTree, syntax::SyntaxToken>>,
{
let name = match iter.next() {
None => return None,
Some(NodeOrToken::Token(element)) => match element.kind() {
syntax::T![ident] => SmolStr::new(element.text()),
_ => return Some(CfgExpr::Invalid),
},
Some(_) => return Some(CfgExpr::Invalid),
};
let result = match name.as_str() {
"all" | "any" | "not" => {
let mut preds = Vec::new();
let Some(NodeOrToken::Node(tree)) = iter.next() else {
return Some(CfgExpr::Invalid);
};
let mut tree_iter = tree.token_trees_and_tokens().skip(1).peekable();
while tree_iter
.peek()
.filter(
|element| matches!(element, NodeOrToken::Token(token) if (token.kind() != syntax::T![')'])),
)
.is_some()
{
let pred = next_cfg_expr_from_syntax(&mut tree_iter);
if let Some(pred) = pred {
preds.push(pred);
}
}
let group = match name.as_str() {
"all" => CfgExpr::All(preds),
"any" => CfgExpr::Any(preds),
"not" => CfgExpr::Not(Box::new(preds.pop().unwrap_or(CfgExpr::Invalid))),
_ => unreachable!(),
};
Some(group)
}
_ => match iter.peek() {
Some(NodeOrToken::Token(element)) if (element.kind() == syntax::T![=]) => {
iter.next();
match iter.next() {
Some(NodeOrToken::Token(value_token))
if (value_token.kind() == syntax::SyntaxKind::STRING) =>
{
let value = value_token.text();
let value = SmolStr::new(value.trim_matches('"'));
Some(CfgExpr::Atom(CfgAtom::KeyValue { key: name, value }))
}
_ => None,
}
}
_ => Some(CfgExpr::Atom(CfgAtom::Flag(name))),
},
};
if let Some(NodeOrToken::Token(element)) = iter.peek() {
if element.kind() == syntax::T![,] {
iter.next();
}
}
result
}
#[cfg(test)]
mod tests {
use cfg::DnfExpr;
use expect_test::{expect, Expect};
use syntax::{ast::Attr, AstNode, SourceFile};
use crate::cfg_process::parse_from_attr_meta;
fn check_dnf_from_syntax(input: &str, expect: Expect) {
let parse = SourceFile::parse(input);
let node = match parse.tree().syntax().descendants().find_map(Attr::cast) {
Some(it) => it,
None => {
let node = std::any::type_name::<Attr>();
panic!("Failed to make ast node `{node}` from text {input}")
}
};
let node = node.clone_subtree();
assert_eq!(node.syntax().text_range().start(), 0.into());
let cfg = parse_from_attr_meta(node.meta().unwrap()).unwrap();
let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
expect.assert_eq(&actual);
}
#[test]
fn cfg_from_attr() {
check_dnf_from_syntax(r#"#[cfg(test)]"#, expect![[r#"#![cfg(test)]"#]]);
check_dnf_from_syntax(r#"#[cfg(not(never))]"#, expect![[r#"#![cfg(not(never))]"#]]);
}
}