rust/crates/cfg/src/cfg_expr.rs

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

124 lines
4.1 KiB
Rust
Raw Normal View History

2019-10-02 13:50:22 -05:00
//! The condition expression used in `#[cfg(..)]` attributes.
//!
//! See: <https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation>
2019-10-02 13:50:22 -05:00
use std::{fmt, slice::Iter as SliceIter};
2020-08-13 03:08:11 -05:00
use tt::SmolStr;
2020-10-21 06:57:12 -05:00
/// A simple configuration value passed in from the outside.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
2020-10-21 06:57:12 -05:00
pub enum CfgAtom {
/// eg. `#[cfg(test)]`
Flag(SmolStr),
/// eg. `#[cfg(target_os = "linux")]`
///
/// Note that a key can have multiple values that are all considered "active" at the same time.
/// For example, `#[cfg(target_feature = "sse")]` and `#[cfg(target_feature = "sse2")]`.
KeyValue { key: SmolStr, value: SmolStr },
}
impl fmt::Display for CfgAtom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
2022-03-21 03:43:36 -05:00
CfgAtom::Flag(name) => name.fmt(f),
CfgAtom::KeyValue { key, value } => write!(f, "{key} = {value:?}"),
}
}
}
2021-02-27 08:59:52 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-08-31 06:10:16 -05:00
#[cfg_attr(test, derive(derive_arbitrary::Arbitrary))]
pub enum CfgExpr {
Invalid,
2020-10-21 06:57:12 -05:00
Atom(CfgAtom),
All(Vec<CfgExpr>),
Any(Vec<CfgExpr>),
Not(Box<CfgExpr>),
}
2020-10-21 06:57:12 -05:00
impl From<CfgAtom> for CfgExpr {
fn from(atom: CfgAtom) -> Self {
CfgExpr::Atom(atom)
}
}
impl CfgExpr {
2023-01-31 04:49:49 -06:00
pub fn parse<S>(tt: &tt::Subtree<S>) -> CfgExpr {
2020-07-23 09:22:17 -05:00
next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
}
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
2020-10-21 06:57:12 -05:00
pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> {
match self {
CfgExpr::Invalid => None,
2020-10-21 06:57:12 -05:00
CfgExpr::Atom(atom) => Some(query(atom)),
CfgExpr::All(preds) => {
preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?))
}
CfgExpr::Any(preds) => {
preds.iter().try_fold(false, |s, pred| Some(s || pred.fold(query)?))
}
CfgExpr::Not(pred) => pred.fold(query).map(|s| !s),
}
}
}
2023-01-31 04:49:49 -06:00
fn next_cfg_expr<S>(it: &mut SliceIter<'_, tt::TokenTree<S>>) -> Option<CfgExpr> {
let name = match it.next() {
None => return None,
2020-07-23 09:22:17 -05:00
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.text.clone(),
Some(_) => return Some(CfgExpr::Invalid),
};
// Peek
let ret = match it.as_slice().first() {
2020-07-23 09:22:17 -05:00
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
match it.as_slice().get(1) {
2020-07-23 09:22:17 -05:00
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
it.next();
it.next();
// FIXME: escape? raw string?
let value =
SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"'));
2020-10-21 06:57:12 -05:00
CfgAtom::KeyValue { key: name, value }.into()
}
_ => return Some(CfgExpr::Invalid),
}
}
2020-07-23 09:22:17 -05:00
Some(tt::TokenTree::Subtree(subtree)) => {
it.next();
let mut sub_it = subtree.token_trees.iter();
let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect();
match name.as_str() {
"all" => CfgExpr::All(subs),
"any" => CfgExpr::Any(subs),
"not" => CfgExpr::Not(Box::new(subs.pop().unwrap_or(CfgExpr::Invalid))),
_ => CfgExpr::Invalid,
}
}
2020-10-21 06:57:12 -05:00
_ => CfgAtom::Flag(name).into(),
};
// Eat comma separator
2020-07-23 09:22:17 -05:00
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() {
if punct.char == ',' {
it.next();
}
}
Some(ret)
}
2021-08-30 15:26:35 -05:00
#[cfg(test)]
impl arbitrary::Arbitrary<'_> for CfgAtom {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
2021-08-31 06:11:17 -05:00
if u.arbitrary()? {
Ok(CfgAtom::Flag(String::arbitrary(u)?.into()))
} else {
Ok(CfgAtom::KeyValue {
2021-08-30 15:26:35 -05:00
key: String::arbitrary(u)?.into(),
value: String::arbitrary(u)?.into(),
2021-08-31 06:11:17 -05:00
})
2021-08-30 15:26:35 -05:00
}
}
}