1 //! The condition expression used in `#[cfg(..)]` attributes.
3 //! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation
5 use std::{fmt, slice::Iter as SliceIter};
9 /// A simple configuration value passed in from the outside.
10 #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
12 /// eg. `#[cfg(test)]`
14 /// eg. `#[cfg(target_os = "linux")]`
16 /// Note that a key can have multiple values that are all considered "active" at the same time.
17 /// For example, `#[cfg(target_feature = "sse")]` and `#[cfg(target_feature = "sse2")]`.
18 KeyValue { key: SmolStr, value: SmolStr },
22 /// Returns `true` when the atom comes from the target specification.
24 /// If this returns `true`, then changing this atom requires changing the compilation target. If
25 /// it returns `false`, the atom might come from a build script or the build system.
26 pub fn is_target_defined(&self) -> bool {
28 CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"),
29 CfgAtom::KeyValue { key, value: _ } => matches!(
36 | "target_pointer_width"
37 | "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature`
43 impl fmt::Display for CfgAtom {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 CfgAtom::Flag(name) => write!(f, "{}", name),
47 CfgAtom::KeyValue { key, value } => write!(f, "{} = {:?}", key, value),
52 #[derive(Debug, Clone, PartialEq, Eq)]
61 impl From<CfgAtom> for CfgExpr {
62 fn from(atom: CfgAtom) -> Self {
68 pub fn parse(tt: &tt::Subtree) -> CfgExpr {
69 next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
71 /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
72 pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> {
74 CfgExpr::Invalid => None,
75 CfgExpr::Atom(atom) => Some(query(atom)),
76 CfgExpr::All(preds) => {
77 preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?))
79 CfgExpr::Any(preds) => {
80 preds.iter().try_fold(false, |s, pred| Some(s || pred.fold(query)?))
82 CfgExpr::Not(pred) => pred.fold(query).map(|s| !s),
87 fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
88 let name = match it.next() {
90 Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.text.clone(),
91 Some(_) => return Some(CfgExpr::Invalid),
95 let ret = match it.as_slice().first() {
96 Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
97 match it.as_slice().get(1) {
98 Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
101 // FIXME: escape? raw string?
103 SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"'));
104 CfgAtom::KeyValue { key: name, value }.into()
106 _ => return Some(CfgExpr::Invalid),
109 Some(tt::TokenTree::Subtree(subtree)) => {
111 let mut sub_it = subtree.token_trees.iter();
112 let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect();
113 match name.as_str() {
114 "all" => CfgExpr::All(subs),
115 "any" => CfgExpr::Any(subs),
116 "not" => CfgExpr::Not(Box::new(subs.pop().unwrap_or(CfgExpr::Invalid))),
117 _ => CfgExpr::Invalid,
120 _ => CfgAtom::Flag(name).into(),
123 // Eat comma separator
124 if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() {
125 if punct.char == ',' {