2019-10-02 18:50:22 +00:00
|
|
|
//! The condition expression used in `#[cfg(..)]` attributes.
|
|
|
|
//!
|
|
|
|
//! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation
|
|
|
|
|
2020-10-21 17:54:04 +00:00
|
|
|
use std::{fmt, slice::Iter as SliceIter};
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2020-08-13 08:08:11 +00:00
|
|
|
use tt::SmolStr;
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2020-10-21 11:57:12 +00:00
|
|
|
/// A simple configuration value passed in from the outside.
|
2020-10-21 17:54:04 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
2020-10-21 11:57:12 +00: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 },
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:54:04 +00:00
|
|
|
impl CfgAtom {
|
|
|
|
/// Returns `true` when the atom comes from the target specification.
|
|
|
|
///
|
|
|
|
/// If this returns `true`, then changing this atom requires changing the compilation target. If
|
|
|
|
/// it returns `false`, the atom might come from a build script or the build system.
|
|
|
|
pub fn is_target_defined(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"),
|
|
|
|
CfgAtom::KeyValue { key, value: _ } => matches!(
|
|
|
|
&**key,
|
|
|
|
"target_arch"
|
|
|
|
| "target_os"
|
|
|
|
| "target_env"
|
|
|
|
| "target_family"
|
|
|
|
| "target_endian"
|
|
|
|
| "target_pointer_width"
|
|
|
|
| "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature`
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CfgAtom {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
CfgAtom::Flag(name) => write!(f, "{}", name),
|
|
|
|
CfgAtom::KeyValue { key, value } => write!(f, "{} = \"{}\"", key, value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 22:52:15 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum CfgExpr {
|
|
|
|
Invalid,
|
2020-10-21 11:57:12 +00:00
|
|
|
Atom(CfgAtom),
|
2019-09-29 22:52:15 +00:00
|
|
|
All(Vec<CfgExpr>),
|
|
|
|
Any(Vec<CfgExpr>),
|
|
|
|
Not(Box<CfgExpr>),
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:57:12 +00:00
|
|
|
impl From<CfgAtom> for CfgExpr {
|
|
|
|
fn from(atom: CfgAtom) -> Self {
|
|
|
|
CfgExpr::Atom(atom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 22:52:15 +00:00
|
|
|
impl CfgExpr {
|
2020-07-23 14:22:17 +00:00
|
|
|
pub fn parse(tt: &tt::Subtree) -> CfgExpr {
|
|
|
|
next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
|
|
|
|
}
|
2019-09-29 22:52:15 +00:00
|
|
|
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
|
2020-10-21 11:57:12 +00:00
|
|
|
pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> {
|
2019-09-29 22:52:15 +00:00
|
|
|
match self {
|
|
|
|
CfgExpr::Invalid => None,
|
2020-10-21 11:57:12 +00:00
|
|
|
CfgExpr::Atom(atom) => Some(query(atom)),
|
2019-09-29 22:52:15 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
|
|
|
|
let name = match it.next() {
|
|
|
|
None => return None,
|
2020-07-23 14:22:17 +00:00
|
|
|
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.text.clone(),
|
2019-09-29 22:52:15 +00:00
|
|
|
Some(_) => return Some(CfgExpr::Invalid),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Peek
|
|
|
|
let ret = match it.as_slice().first() {
|
2020-07-23 14:22:17 +00:00
|
|
|
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
|
2019-09-29 22:52:15 +00:00
|
|
|
match it.as_slice().get(1) {
|
2020-07-23 14:22:17 +00:00
|
|
|
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
|
2019-09-29 22:52:15 +00:00
|
|
|
it.next();
|
|
|
|
it.next();
|
|
|
|
// FIXME: escape? raw string?
|
|
|
|
let value =
|
|
|
|
SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"'));
|
2020-10-21 11:57:12 +00:00
|
|
|
CfgAtom::KeyValue { key: name, value }.into()
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
|
|
|
_ => return Some(CfgExpr::Invalid),
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 14:22:17 +00:00
|
|
|
Some(tt::TokenTree::Subtree(subtree)) => {
|
2019-09-29 22:52:15 +00:00
|
|
|
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 11:57:12 +00:00
|
|
|
_ => CfgAtom::Flag(name).into(),
|
2019-09-29 22:52:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Eat comma separator
|
2020-07-23 14:22:17 +00:00
|
|
|
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() {
|
2019-09-29 22:52:15 +00:00
|
|
|
if punct.char == ',' {
|
|
|
|
it.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-08-13 08:17:59 +00:00
|
|
|
use mbe::ast_to_token_tree;
|
2020-08-12 16:26:51 +00:00
|
|
|
use syntax::ast::{self, AstNode};
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2020-05-21 08:48:42 +00:00
|
|
|
fn assert_parse_result(input: &str, expected: CfgExpr) {
|
2020-08-13 08:17:59 +00:00
|
|
|
let (tt, _) = {
|
|
|
|
let source_file = ast::SourceFile::parse(input).ok().unwrap();
|
|
|
|
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
|
|
|
|
ast_to_token_tree(&tt).unwrap()
|
|
|
|
};
|
2020-07-23 14:22:17 +00:00
|
|
|
let cfg = CfgExpr::parse(&tt);
|
|
|
|
assert_eq!(cfg, expected);
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cfg_expr_parser() {
|
2020-10-21 11:57:12 +00:00
|
|
|
assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into());
|
|
|
|
assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into());
|
2019-09-29 22:52:15 +00:00
|
|
|
assert_parse_result(
|
|
|
|
"#![cfg(not(foo))]",
|
2020-10-21 11:57:12 +00:00
|
|
|
CfgExpr::Not(Box::new(CfgAtom::Flag("foo".into()).into())),
|
2019-09-29 22:52:15 +00:00
|
|
|
);
|
|
|
|
assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid);
|
|
|
|
|
|
|
|
// Only take the first
|
2020-10-21 11:57:12 +00:00
|
|
|
assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgAtom::Flag("foo".into()).into());
|
2019-09-29 22:52:15 +00:00
|
|
|
|
|
|
|
assert_parse_result(
|
|
|
|
r#"#![cfg(all(foo, bar = "baz"))]"#,
|
|
|
|
CfgExpr::All(vec![
|
2020-10-21 11:57:12 +00:00
|
|
|
CfgAtom::Flag("foo".into()).into(),
|
|
|
|
CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
|
2019-09-29 22:52:15 +00:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_parse_result(
|
|
|
|
r#"#![cfg(any(not(), all(), , bar = "baz",))]"#,
|
|
|
|
CfgExpr::Any(vec![
|
|
|
|
CfgExpr::Not(Box::new(CfgExpr::Invalid)),
|
|
|
|
CfgExpr::All(vec![]),
|
|
|
|
CfgExpr::Invalid,
|
2020-10-21 11:57:12 +00:00
|
|
|
CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
|
2019-09-29 22:52:15 +00:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|