From b1ed887d813bf5775a16624694939fdf836f97b1 Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Mon, 30 Sep 2019 06:52:15 +0800 Subject: Introduce ra_cfg to parse and evaluate CfgExpr --- crates/ra_cfg/Cargo.toml | 14 +++++ crates/ra_cfg/src/cfg_expr.rs | 128 ++++++++++++++++++++++++++++++++++++++++++ crates/ra_cfg/src/lib.rs | 43 ++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 crates/ra_cfg/Cargo.toml create mode 100644 crates/ra_cfg/src/cfg_expr.rs create mode 100644 crates/ra_cfg/src/lib.rs (limited to 'crates/ra_cfg') diff --git a/crates/ra_cfg/Cargo.toml b/crates/ra_cfg/Cargo.toml new file mode 100644 index 000000000..b28affc3a --- /dev/null +++ b/crates/ra_cfg/Cargo.toml @@ -0,0 +1,14 @@ +[package] +edition = "2018" +name = "ra_cfg" +version = "0.1.0" +authors = ["rust-analyzer developers"] + +[dependencies] +rustc-hash = "1.0.1" + +ra_syntax = { path = "../ra_syntax" } +tt = { path = "../ra_tt", package = "ra_tt" } + +[dev-dependencies] +mbe = { path = "../ra_mbe", package = "ra_mbe" } diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs new file mode 100644 index 000000000..efeadf462 --- /dev/null +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -0,0 +1,128 @@ +use std::slice::Iter as SliceIter; + +use ra_syntax::SmolStr; +use tt::{Leaf, Subtree, TokenTree}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum CfgExpr { + Invalid, + Atom(SmolStr), + KeyValue { key: SmolStr, value: SmolStr }, + All(Vec), + Any(Vec), + Not(Box), +} + +impl CfgExpr { + /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. + pub fn fold(&self, query: &impl Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option { + match self { + CfgExpr::Invalid => None, + CfgExpr::Atom(name) => Some(query(name, None)), + CfgExpr::KeyValue { key, value } => Some(query(key, Some(value))), + 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), + } + } +} + +pub fn parse_cfg(tt: &Subtree) -> CfgExpr { + next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) +} + +fn next_cfg_expr(it: &mut SliceIter) -> Option { + let name = match it.next() { + None => return None, + Some(TokenTree::Leaf(Leaf::Ident(ident))) => ident.text.clone(), + Some(_) => return Some(CfgExpr::Invalid), + }; + + // Peek + let ret = match it.as_slice().first() { + Some(TokenTree::Leaf(Leaf::Punct(punct))) if punct.char == '=' => { + match it.as_slice().get(1) { + Some(TokenTree::Leaf(Leaf::Literal(literal))) => { + it.next(); + it.next(); + // FIXME: escape? raw string? + let value = + SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); + CfgExpr::KeyValue { key: name, value } + } + _ => return Some(CfgExpr::Invalid), + } + } + Some(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, + } + } + _ => CfgExpr::Atom(name), + }; + + // Eat comma separator + if let Some(TokenTree::Leaf(Leaf::Punct(punct))) = it.as_slice().first() { + if punct.char == ',' { + it.next(); + } + } + Some(ret) +} + +#[cfg(test)] +mod tests { + use super::*; + + use mbe::ast_to_token_tree; + use ra_syntax::ast::{self, AstNode}; + + fn assert_parse_result(input: &str, expected: CfgExpr) { + let source_file = ast::SourceFile::parse(input).ok().unwrap(); + let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); + let (tt, _) = ast_to_token_tree(&tt).unwrap(); + assert_eq!(parse_cfg(&tt), expected); + } + + #[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(not(foo))]", + CfgExpr::Not(Box::new(CfgExpr::Atom("foo".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(all(foo, bar = "baz"))]"#, + CfgExpr::All(vec![ + CfgExpr::Atom("foo".into()), + CfgExpr::KeyValue { key: "bar".into(), value: "baz".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, + CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, + ]), + ); + } +} diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs new file mode 100644 index 000000000..fa5822d8a --- /dev/null +++ b/crates/ra_cfg/src/lib.rs @@ -0,0 +1,43 @@ +//! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator +use ra_syntax::SmolStr; +use rustc_hash::{FxHashMap, FxHashSet}; + +mod cfg_expr; + +pub use cfg_expr::{parse_cfg, CfgExpr}; + +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct CfgOptions { + atoms: FxHashSet, + features: FxHashSet, + options: FxHashMap, +} + +impl CfgOptions { + pub fn check(&self, cfg: &CfgExpr) -> Option { + cfg.fold(&|key, value| match value { + None => self.atoms.contains(key), + Some(value) if key == "feature" => self.features.contains(value), + Some(value) => self.options.get(key).map_or(false, |v| v == value), + }) + } + + pub fn is_cfg_enabled(&self, attr: &tt::Subtree) -> Option { + self.check(&parse_cfg(attr)) + } + + pub fn atom(mut self, name: SmolStr) -> CfgOptions { + self.atoms.insert(name); + self + } + + pub fn feature(mut self, name: SmolStr) -> CfgOptions { + self.features.insert(name); + self + } + + pub fn option(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { + self.options.insert(key, value); + self + } +} -- cgit v1.2.3 From 43f09ad36ccc1c53c78a66274693e53161c9b2fa Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Thu, 3 Oct 2019 01:20:08 +0800 Subject: Refactor CfgOptions inside --- crates/ra_cfg/src/lib.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'crates/ra_cfg') diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs index fa5822d8a..dd81a73f4 100644 --- a/crates/ra_cfg/src/lib.rs +++ b/crates/ra_cfg/src/lib.rs @@ -1,24 +1,32 @@ //! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator use ra_syntax::SmolStr; -use rustc_hash::{FxHashMap, FxHashSet}; +use rustc_hash::FxHashSet; mod cfg_expr; pub use cfg_expr::{parse_cfg, CfgExpr}; +/// 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 +/// key-value options like `target_arch="x86"`. +/// +/// Note that for key-value options, one key can have multiple values (but not none). +/// `feature` is an example. We have both `feature="foo"` and `feature="bar"` if features +/// `foo` and `bar` are both enabled. And here, we store key-value options as a set of tuple +/// of key and value in `key_values`. +/// +/// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct CfgOptions { atoms: FxHashSet, - features: FxHashSet, - options: FxHashMap, + key_values: FxHashSet<(SmolStr, SmolStr)>, } impl CfgOptions { pub fn check(&self, cfg: &CfgExpr) -> Option { cfg.fold(&|key, value| match value { None => self.atoms.contains(key), - Some(value) if key == "feature" => self.features.contains(value), - Some(value) => self.options.get(key).map_or(false, |v| v == value), + Some(value) => self.key_values.contains(&(key.clone(), value.clone())), }) } @@ -31,13 +39,13 @@ impl CfgOptions { self } - pub fn feature(mut self, name: SmolStr) -> CfgOptions { - self.features.insert(name); + pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { + self.key_values.insert((key, value)); self } - pub fn option(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { - self.options.insert(key, value); + pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { + self.atoms.remove(name); self } } -- cgit v1.2.3 From e0100e63ae2e873f119b905ac77c3355ffb351b0 Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Thu, 3 Oct 2019 01:38:56 +0800 Subject: Optimize --- crates/ra_cfg/src/cfg_expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_cfg') diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index efeadf462..811f83048 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -15,7 +15,7 @@ pub enum CfgExpr { impl CfgExpr { /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. - pub fn fold(&self, query: &impl Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option { + pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option { match self { CfgExpr::Invalid => None, CfgExpr::Atom(name) => Some(query(name, None)), -- cgit v1.2.3 From 1067a1c5f649cc206e35b427eaa8d6553280cc96 Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Thu, 3 Oct 2019 02:02:53 +0800 Subject: Read default cfgs from rustc --- crates/ra_cfg/src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'crates/ra_cfg') diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs index dd81a73f4..e1c92fbba 100644 --- a/crates/ra_cfg/src/lib.rs +++ b/crates/ra_cfg/src/lib.rs @@ -1,4 +1,6 @@ //! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator +use std::iter::IntoIterator; + use ra_syntax::SmolStr; use rustc_hash::FxHashSet; @@ -44,6 +46,14 @@ impl CfgOptions { self } + /// Shortcut to set features + pub fn features(mut self, iter: impl IntoIterator) -> CfgOptions { + for feat in iter { + self = self.key_value("feature".into(), feat); + } + self + } + pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { self.atoms.remove(name); self -- cgit v1.2.3 From b271cb18d5ab19624e751867df6705cd1e94edbc Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Thu, 3 Oct 2019 02:50:22 +0800 Subject: Add docs --- crates/ra_cfg/src/cfg_expr.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/ra_cfg') diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index 811f83048..39d71851c 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -1,3 +1,7 @@ +//! The condition expression used in `#[cfg(..)]` attributes. +//! +//! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation + use std::slice::Iter as SliceIter; use ra_syntax::SmolStr; -- cgit v1.2.3