Skip to main content

cairo_lang_plugins/plugins/
config.rs

1use cairo_lang_defs::patcher::PatchBuilder;
2use cairo_lang_defs::plugin::{
3    MacroPlugin, MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile, PluginResult,
4};
5use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
6use cairo_lang_filesystem::ids::SmolStrId;
7use cairo_lang_syntax::attribute::structured::{
8    Attribute, AttributeArg, AttributeArgVariant, AttributeStructurize,
9};
10use cairo_lang_syntax::node::helpers::{BodyItems, GetIdentifier, QueryAttrs};
11use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode, ast};
12use cairo_lang_utils::try_extract_matches;
13use itertools::Itertools;
14use salsa::Database;
15
16/// Represents a predicate tree used to evaluate configuration attributes to handle nested
17/// predicates, such as logical `not` operations, and evaluate them based on a given set of
18/// configuration flags (`CfgSet`).
19#[derive(Debug, Clone)]
20enum PredicateTree {
21    Cfg(Cfg),
22    Not(Box<PredicateTree>),
23    And(Vec<PredicateTree>),
24    Or(Vec<PredicateTree>),
25}
26
27impl PredicateTree {
28    /// Evaluates the predicate tree against the provided configuration set (`CfgSet`) by traversing
29    /// the `PredicateTree` and determines whether the predicate is satisfied by the given
30    /// `cfg_set`.
31    fn evaluate(&self, cfg_set: &CfgSet) -> bool {
32        match self {
33            PredicateTree::Cfg(cfg) => cfg_set.contains(cfg),
34            PredicateTree::Not(inner) => !inner.evaluate(cfg_set),
35            PredicateTree::And(predicates) => predicates.iter().all(|p| p.evaluate(cfg_set)),
36            PredicateTree::Or(predicates) => predicates.iter().any(|p| p.evaluate(cfg_set)),
37        }
38    }
39}
40
41/// Represents a part of a configuration predicate.
42pub enum ConfigPredicatePart<'db> {
43    /// A configuration item, either a key-value pair or a simple name.
44    Cfg(Cfg),
45    /// A function call in the predicate (`not`, `and`, `or`).
46    Call(ast::ExprFunctionCall<'db>),
47}
48
49/// Plugin that enables ignoring modules not involved in the current config.
50///
51/// Mostly useful for marking test modules to prevent usage of their functionality out of tests,
52/// and reduce compilation time when the tests data isn't required.
53#[derive(Debug, Default)]
54#[non_exhaustive]
55pub struct ConfigPlugin;
56
57const CFG_ATTR: &str = "cfg";
58
59impl MacroPlugin for ConfigPlugin {
60    fn generate_code<'db>(
61        &self,
62        db: &'db dyn Database,
63        item_ast: ast::ModuleItem<'db>,
64        metadata: &MacroPluginMetadata<'_>,
65    ) -> PluginResult<'db> {
66        let mut diagnostics = vec![];
67
68        if should_drop(db, metadata.cfg_set, &item_ast, &mut diagnostics) {
69            PluginResult { code: None, diagnostics, remove_original_item: true }
70        } else if let Some(builder) =
71            handle_undropped_item(db, metadata.cfg_set, item_ast, &mut diagnostics)
72        {
73            let (content, code_mappings) = builder.build();
74            PluginResult {
75                code: Some(PluginGeneratedFile {
76                    name: "config".into(),
77                    content,
78                    code_mappings,
79                    aux_data: None,
80                    diagnostics_note: Default::default(),
81                    is_unhygienic: false,
82                }),
83                diagnostics,
84                remove_original_item: true,
85            }
86        } else {
87            PluginResult { code: None, diagnostics, remove_original_item: false }
88        }
89    }
90
91    fn declared_attributes<'db>(&self, db: &'db dyn Database) -> Vec<SmolStrId<'db>> {
92        vec![SmolStrId::from(db, CFG_ATTR)]
93    }
94}
95
96/// Extension trait for `BodyItems` filtering out items that are not included in the cfg.
97pub trait HasItemsInCfgEx<'a, Item: QueryAttrs<'a>>: BodyItems<'a, Item = Item> {
98    fn iter_items_in_cfg(
99        &self,
100        db: &'a dyn Database,
101        cfg_set: &CfgSet,
102    ) -> impl Iterator<Item = Item>;
103}
104
105impl<'a, Item: QueryAttrs<'a>, Body: BodyItems<'a, Item = Item>> HasItemsInCfgEx<'a, Item>
106    for Body
107{
108    fn iter_items_in_cfg(
109        &self,
110        db: &'a dyn Database,
111        cfg_set: &CfgSet,
112    ) -> impl Iterator<Item = Item> {
113        self.iter_items(db).filter(move |item| !should_drop(db, cfg_set, item, &mut vec![]))
114    }
115}
116
117/// Handles an item that is not dropped from the AST completely due to not matching the config.
118/// In case it includes dropped elements and needs to be rewritten, it returns the appropriate
119/// PatchBuilder. Otherwise returns `None`, and it won't be rewritten or dropped.
120fn handle_undropped_item<'a>(
121    db: &'a dyn Database,
122    cfg_set: &CfgSet,
123    item_ast: ast::ModuleItem<'a>,
124    diagnostics: &mut Vec<PluginDiagnostic<'a>>,
125) -> Option<PatchBuilder<'a>> {
126    match item_ast {
127        ast::ModuleItem::Trait(trait_item) => {
128            let body = try_extract_matches!(trait_item.body(db), ast::MaybeTraitBody::Some)?;
129            let items = get_kept_items_nodes(db, cfg_set, body.iter_items(db), diagnostics)?;
130            let mut builder = PatchBuilder::new(db, &trait_item);
131            builder.add_node(trait_item.attributes(db).as_syntax_node());
132            builder.add_node(trait_item.visibility(db).as_syntax_node());
133            builder.add_node(trait_item.trait_kw(db).as_syntax_node());
134            builder.add_node(trait_item.name(db).as_syntax_node());
135            builder.add_node(trait_item.generic_params(db).as_syntax_node());
136            builder.add_node(body.lbrace(db).as_syntax_node());
137            for item in items {
138                builder.add_node(item);
139            }
140            builder.add_node(body.rbrace(db).as_syntax_node());
141            Some(builder)
142        }
143        ast::ModuleItem::Impl(impl_item) => {
144            let body = try_extract_matches!(impl_item.body(db), ast::MaybeImplBody::Some)?;
145            let items = get_kept_items_nodes(db, cfg_set, body.iter_items(db), diagnostics)?;
146            let mut builder = PatchBuilder::new(db, &impl_item);
147            builder.add_node(impl_item.attributes(db).as_syntax_node());
148            builder.add_node(impl_item.visibility(db).as_syntax_node());
149            builder.add_node(impl_item.impl_kw(db).as_syntax_node());
150            builder.add_node(impl_item.name(db).as_syntax_node());
151            builder.add_node(impl_item.generic_params(db).as_syntax_node());
152            builder.add_node(impl_item.of_kw(db).as_syntax_node());
153            builder.add_node(impl_item.trait_path(db).as_syntax_node());
154            builder.add_node(body.lbrace(db).as_syntax_node());
155            for item in items {
156                builder.add_node(item);
157            }
158            builder.add_node(body.rbrace(db).as_syntax_node());
159            Some(builder)
160        }
161        _ => None,
162    }
163}
164
165/// Gets the list of items that should be kept in the AST.
166/// Returns `None` if all items should be kept.
167fn get_kept_items_nodes<'a, Item: QueryAttrs<'a> + TypedSyntaxNode<'a>>(
168    db: &'a dyn Database,
169    cfg_set: &CfgSet,
170    all_items: impl Iterator<Item = Item>,
171    diagnostics: &mut Vec<PluginDiagnostic<'a>>,
172) -> Option<Vec<cairo_lang_syntax::node::SyntaxNode<'a>>> {
173    let mut any_dropped = false;
174    let mut kept_items_nodes = vec![];
175    for item in all_items {
176        if should_drop(db, cfg_set, &item, diagnostics) {
177            any_dropped = true;
178        } else {
179            kept_items_nodes.push(item.as_syntax_node());
180        }
181    }
182    if any_dropped { Some(kept_items_nodes) } else { None }
183}
184
185/// Checks if the given item should be dropped from the AST.
186fn should_drop<'a, Item: QueryAttrs<'a>>(
187    db: &'a dyn Database,
188    cfg_set: &CfgSet,
189    item: &Item,
190    diagnostics: &mut Vec<PluginDiagnostic<'a>>,
191) -> bool {
192    item.query_attr(db, CFG_ATTR).any(|attr| {
193        match parse_predicate(db, attr.structurize(db), diagnostics) {
194            Some(predicate_tree) => !predicate_tree.evaluate(cfg_set),
195            None => false,
196        }
197    })
198}
199
200/// Parse `#[cfg(not(ghf)...)]` attribute arguments as a predicate matching [`Cfg`] items.
201fn parse_predicate<'a>(
202    db: &'a dyn Database,
203    attr: Attribute<'a>,
204    diagnostics: &mut Vec<PluginDiagnostic<'a>>,
205) -> Option<PredicateTree> {
206    Some(PredicateTree::And(
207        attr.args
208            .into_iter()
209            .filter_map(|arg| parse_predicate_item(db, arg, diagnostics))
210            .collect(),
211    ))
212}
213
214/// Parse single `#[cfg(...)]` attribute argument as a [`Cfg`] item.
215fn parse_predicate_item<'a>(
216    db: &'a dyn Database,
217    item: AttributeArg<'a>,
218    diagnostics: &mut Vec<PluginDiagnostic<'a>>,
219) -> Option<PredicateTree> {
220    match extract_config_predicate_part(db, &item) {
221        Some(ConfigPredicatePart::Cfg(cfg)) => Some(PredicateTree::Cfg(cfg)),
222        Some(ConfigPredicatePart::Call(call)) => {
223            let operator = call.path(db).as_syntax_node().get_text(db);
224            let args = call
225                .arguments(db)
226                .arguments(db)
227                .elements(db)
228                .map(|arg| AttributeArg::from_ast(arg, db))
229                .collect_vec();
230
231            match operator {
232                "not" => {
233                    if args.len() != 1 {
234                        diagnostics.push(PluginDiagnostic::error(
235                            call.stable_ptr(db),
236                            "`not` operator expects exactly one argument.".into(),
237                        ));
238                        None
239                    } else {
240                        Some(PredicateTree::Not(Box::new(parse_predicate_item(
241                            db,
242                            args[0].clone(),
243                            diagnostics,
244                        )?)))
245                    }
246                }
247                "and" => {
248                    if args.len() < 2 {
249                        diagnostics.push(PluginDiagnostic::error(
250                            call.stable_ptr(db),
251                            "`and` operator expects at least two arguments.".into(),
252                        ));
253                        None
254                    } else {
255                        Some(PredicateTree::And(
256                            args.into_iter()
257                                .filter_map(|arg| parse_predicate_item(db, arg, diagnostics))
258                                .collect(),
259                        ))
260                    }
261                }
262                "or" => {
263                    if args.len() < 2 {
264                        diagnostics.push(PluginDiagnostic::error(
265                            call.stable_ptr(db),
266                            "`or` operator expects at least two arguments.".into(),
267                        ));
268                        None
269                    } else {
270                        Some(PredicateTree::Or(
271                            args.into_iter()
272                                .filter_map(|arg| parse_predicate_item(db, arg, diagnostics))
273                                .collect(),
274                        ))
275                    }
276                }
277                _ => {
278                    diagnostics.push(PluginDiagnostic::error(
279                        call.stable_ptr(db),
280                        format!("Unsupported operator: `{operator}`."),
281                    ));
282                    None
283                }
284            }
285        }
286        None => {
287            diagnostics.push(PluginDiagnostic::error(
288                item.arg.stable_ptr(db).untyped(),
289                "Invalid configuration argument.".into(),
290            ));
291            None
292        }
293    }
294}
295
296/// Extracts a configuration predicate part from an attribute argument.
297fn extract_config_predicate_part<'a>(
298    db: &dyn Database,
299    arg: &AttributeArg<'a>,
300) -> Option<ConfigPredicatePart<'a>> {
301    match &arg.variant {
302        AttributeArgVariant::Unnamed(ast::Expr::Path(path)) => {
303            if let Ok(ast::PathSegment::Simple(segment)) =
304                path.segments(db).elements(db).exactly_one()
305            {
306                Some(ConfigPredicatePart::Cfg(Cfg::name(segment.identifier(db).to_string(db))))
307            } else {
308                None
309            }
310        }
311        AttributeArgVariant::Unnamed(ast::Expr::FunctionCall(call)) => {
312            Some(ConfigPredicatePart::Call(call.clone()))
313        }
314        AttributeArgVariant::Named { name, value } => {
315            let value_text = match value {
316                ast::Expr::String(terminal) => terminal.string_value(db).unwrap_or_default(),
317                ast::Expr::ShortString(terminal) => terminal.string_value(db).unwrap_or_default(),
318                _ => return None,
319            };
320
321            Some(ConfigPredicatePart::Cfg(Cfg::kv(name.text.to_string(db), value_text)))
322        }
323        _ => None,
324    }
325}