Simplify cfg representation

This commit is contained in:
Jonas Schievink 2020-10-21 13:57:12 +02:00
parent 20d369a826
commit 2bc4c1ff31
3 changed files with 41 additions and 31 deletions

View file

@ -6,26 +6,42 @@ use std::slice::Iter as SliceIter;
use tt::SmolStr; 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)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum CfgExpr { pub enum CfgExpr {
Invalid, Invalid,
Atom(SmolStr), Atom(CfgAtom),
KeyValue { key: SmolStr, value: SmolStr },
All(Vec<CfgExpr>), All(Vec<CfgExpr>),
Any(Vec<CfgExpr>), Any(Vec<CfgExpr>),
Not(Box<CfgExpr>), Not(Box<CfgExpr>),
} }
impl From<CfgAtom> for CfgExpr {
fn from(atom: CfgAtom) -> Self {
CfgExpr::Atom(atom)
}
}
impl CfgExpr { impl CfgExpr {
pub fn parse(tt: &tt::Subtree) -> CfgExpr { pub fn parse(tt: &tt::Subtree) -> CfgExpr {
next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
} }
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> { pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> {
match self { match self {
CfgExpr::Invalid => None, CfgExpr::Invalid => None,
CfgExpr::Atom(name) => Some(query(name, None)), CfgExpr::Atom(atom) => Some(query(atom)),
CfgExpr::KeyValue { key, value } => Some(query(key, Some(value))),
CfgExpr::All(preds) => { CfgExpr::All(preds) => {
preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?)) preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?))
} }
@ -54,7 +70,7 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
// FIXME: escape? raw string? // FIXME: escape? raw string?
let value = let value =
SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); 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), _ => return Some(CfgExpr::Invalid),
} }
@ -70,7 +86,7 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
_ => CfgExpr::Invalid, _ => CfgExpr::Invalid,
} }
} }
_ => CfgExpr::Atom(name), _ => CfgAtom::Flag(name).into(),
}; };
// Eat comma separator // Eat comma separator
@ -101,22 +117,22 @@ mod tests {
#[test] #[test]
fn test_cfg_expr_parser() { fn test_cfg_expr_parser() {
assert_parse_result("#![cfg(foo)]", CfgExpr::Atom("foo".into())); assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into());
assert_parse_result("#![cfg(foo,)]", CfgExpr::Atom("foo".into())); assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into());
assert_parse_result( assert_parse_result(
"#![cfg(not(foo))]", "#![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); assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid);
// Only take the first // 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( assert_parse_result(
r#"#![cfg(all(foo, bar = "baz"))]"#, r#"#![cfg(all(foo, bar = "baz"))]"#,
CfgExpr::All(vec![ CfgExpr::All(vec![
CfgExpr::Atom("foo".into()), CfgAtom::Flag("foo".into()).into(),
CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
]), ]),
); );
@ -126,7 +142,7 @@ mod tests {
CfgExpr::Not(Box::new(CfgExpr::Invalid)), CfgExpr::Not(Box::new(CfgExpr::Invalid)),
CfgExpr::All(vec![]), CfgExpr::All(vec![]),
CfgExpr::Invalid, CfgExpr::Invalid,
CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
]), ]),
); );
} }

View file

@ -5,7 +5,7 @@ mod cfg_expr;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use tt::SmolStr; use tt::SmolStr;
pub use cfg_expr::CfgExpr; pub use cfg_expr::{CfgAtom, CfgExpr};
/// Configuration options used for conditional compilition on items with `cfg` attributes. /// Configuration options used for conditional compilition on items with `cfg` attributes.
/// We have two kind of options in different namespaces: atomic options like `unix`, and /// We have two kind of options in different namespaces: atomic options like `unix`, and
@ -19,33 +19,25 @@ pub use cfg_expr::CfgExpr;
/// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options /// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CfgOptions { pub struct CfgOptions {
atoms: FxHashSet<SmolStr>, enabled: FxHashSet<CfgAtom>,
key_values: FxHashSet<(SmolStr, SmolStr)>,
} }
impl CfgOptions { impl CfgOptions {
pub fn check(&self, cfg: &CfgExpr) -> Option<bool> { pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
cfg.fold(&|key, value| match value { cfg.fold(&|atom| self.enabled.contains(atom))
None => self.atoms.contains(key),
Some(value) => self.key_values.contains(&(key.clone(), value.clone())),
})
} }
pub fn insert_atom(&mut self, key: SmolStr) { pub fn insert_atom(&mut self, key: SmolStr) {
self.atoms.insert(key); self.enabled.insert(CfgAtom::Flag(key));
} }
pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
self.key_values.insert((key, value)); self.enabled.insert(CfgAtom::KeyValue { key, value });
} }
pub fn append(&mut self, other: &CfgOptions) { pub fn append(&mut self, other: &CfgOptions) {
for atom in &other.atoms { for atom in &other.enabled {
self.atoms.insert(atom.clone()); self.enabled.insert(atom.clone());
}
for (key, value) in &other.key_values {
self.key_values.insert((key.clone(), value.clone()));
} }
} }
} }

View file

@ -1,6 +1,6 @@
//! See `CargoTargetSpec` //! See `CargoTargetSpec`
use cfg::CfgExpr; use cfg::{CfgAtom, CfgExpr};
use ide::{FileId, RunnableKind, TestId}; use ide::{FileId, RunnableKind, TestId};
use project_model::{self, TargetKind}; use project_model::{self, TargetKind};
use vfs::AbsPathBuf; use vfs::AbsPathBuf;
@ -160,7 +160,9 @@ impl CargoTargetSpec {
/// Fill minimal features needed /// Fill minimal features needed
fn required_features(cfg_expr: &CfgExpr, features: &mut Vec<String>) { fn required_features(cfg_expr: &CfgExpr, features: &mut Vec<String>) {
match cfg_expr { match cfg_expr {
CfgExpr::KeyValue { key, value } if key == "feature" => features.push(value.to_string()), CfgExpr::Atom(CfgAtom::KeyValue { key, value }) if key == "feature" => {
features.push(value.to_string())
}
CfgExpr::All(preds) => { CfgExpr::All(preds) => {
preds.iter().for_each(|cfg| required_features(cfg, features)); preds.iter().for_each(|cfg| required_features(cfg, features));
} }