From 2bc4c1ff31b6ef0ca1848a7ce12f981b93dcded0 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 21 Oct 2020 13:57:12 +0200 Subject: Simplify cfg representation --- crates/cfg/src/cfg_expr.rs | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'crates/cfg/src/cfg_expr.rs') diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index 336fe25bc..db3655b74 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs @@ -6,26 +6,42 @@ use std::slice::Iter as SliceIter; use tt::SmolStr; +/// A simple configuration value passed in from the outside. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +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 }, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum CfgExpr { Invalid, - Atom(SmolStr), - KeyValue { key: SmolStr, value: SmolStr }, + Atom(CfgAtom), All(Vec), Any(Vec), Not(Box), } +impl From for CfgExpr { + fn from(atom: CfgAtom) -> Self { + CfgExpr::Atom(atom) + } +} + impl CfgExpr { pub fn parse(tt: &tt::Subtree) -> CfgExpr { next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) } /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. - pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option { + pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option { match self { CfgExpr::Invalid => None, - CfgExpr::Atom(name) => Some(query(name, None)), - CfgExpr::KeyValue { key, value } => Some(query(key, Some(value))), + CfgExpr::Atom(atom) => Some(query(atom)), CfgExpr::All(preds) => { preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?)) } @@ -54,7 +70,7 @@ fn next_cfg_expr(it: &mut SliceIter) -> Option { // FIXME: escape? raw string? let value = SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); - CfgExpr::KeyValue { key: name, value } + CfgAtom::KeyValue { key: name, value }.into() } _ => return Some(CfgExpr::Invalid), } @@ -70,7 +86,7 @@ fn next_cfg_expr(it: &mut SliceIter) -> Option { _ => CfgExpr::Invalid, } } - _ => CfgExpr::Atom(name), + _ => CfgAtom::Flag(name).into(), }; // Eat comma separator @@ -101,22 +117,22 @@ mod tests { #[test] fn test_cfg_expr_parser() { - assert_parse_result("#![cfg(foo)]", CfgExpr::Atom("foo".into())); - assert_parse_result("#![cfg(foo,)]", CfgExpr::Atom("foo".into())); + assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into()); + assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into()); assert_parse_result( "#![cfg(not(foo))]", - CfgExpr::Not(Box::new(CfgExpr::Atom("foo".into()))), + CfgExpr::Not(Box::new(CfgAtom::Flag("foo".into()).into())), ); assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid); // Only take the first - assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgExpr::Atom("foo".into())); + assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgAtom::Flag("foo".into()).into()); assert_parse_result( r#"#![cfg(all(foo, bar = "baz"))]"#, CfgExpr::All(vec![ - CfgExpr::Atom("foo".into()), - CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, + CfgAtom::Flag("foo".into()).into(), + CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(), ]), ); @@ -126,7 +142,7 @@ mod tests { CfgExpr::Not(Box::new(CfgExpr::Invalid)), CfgExpr::All(vec![]), CfgExpr::Invalid, - CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, + CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(), ]), ); } -- cgit v1.2.3 From 68b17986c7c272d9be8df9a7abb9b162329b9d65 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 21 Oct 2020 19:54:04 +0200 Subject: Implement DNF-based `#[cfg]` introspection --- crates/cfg/src/cfg_expr.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'crates/cfg/src/cfg_expr.rs') diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index db3655b74..3f12a3fa8 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs @@ -2,12 +2,12 @@ //! //! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation -use std::slice::Iter as SliceIter; +use std::{fmt, slice::Iter as SliceIter}; use tt::SmolStr; /// A simple configuration value passed in from the outside. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub enum CfgAtom { /// eg. `#[cfg(test)]` Flag(SmolStr), @@ -18,6 +18,37 @@ pub enum CfgAtom { KeyValue { key: SmolStr, value: SmolStr }, } +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), + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum CfgExpr { Invalid, -- cgit v1.2.3 From dbd6266bc91c3934176e251eedb7de381fcc1006 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 22 Oct 2020 20:08:27 +0200 Subject: Update crates/cfg/src/cfg_expr.rs Co-authored-by: Aleksey Kladov --- crates/cfg/src/cfg_expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/cfg/src/cfg_expr.rs') diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index 3f12a3fa8..283ff968f 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs @@ -44,7 +44,7 @@ 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), + CfgAtom::KeyValue { key, value } => write!(f, "{} = {:?}", key, value), } } } -- cgit v1.2.3 From a246d4f599dcf2612fa99720fb4ca98101ed93fb Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 23 Oct 2020 12:14:58 +0200 Subject: cfg: move tests to separate file that way we don't have to re-check the entire project when a test is changed --- crates/cfg/src/cfg_expr.rs | 50 ---------------------------------------------- 1 file changed, 50 deletions(-) (limited to 'crates/cfg/src/cfg_expr.rs') diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index 283ff968f..42327f1e1 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs @@ -128,53 +128,3 @@ fn next_cfg_expr(it: &mut SliceIter) -> Option { } Some(ret) } - -#[cfg(test)] -mod tests { - use super::*; - - use mbe::ast_to_token_tree; - use syntax::ast::{self, AstNode}; - - fn assert_parse_result(input: &str, expected: CfgExpr) { - 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() - }; - let cfg = CfgExpr::parse(&tt); - assert_eq!(cfg, expected); - } - - #[test] - fn test_cfg_expr_parser() { - assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into()); - assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into()); - assert_parse_result( - "#![cfg(not(foo))]", - CfgExpr::Not(Box::new(CfgAtom::Flag("foo".into()).into())), - ); - assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid); - - // Only take the first - assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgAtom::Flag("foo".into()).into()); - - assert_parse_result( - r#"#![cfg(all(foo, bar = "baz"))]"#, - CfgExpr::All(vec![ - CfgAtom::Flag("foo".into()).into(), - CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(), - ]), - ); - - assert_parse_result( - r#"#![cfg(any(not(), all(), , bar = "baz",))]"#, - CfgExpr::Any(vec![ - CfgExpr::Not(Box::new(CfgExpr::Invalid)), - CfgExpr::All(vec![]), - CfgExpr::Invalid, - CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(), - ]), - ); - } -} -- cgit v1.2.3