mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Refactor CfgOptions inside
This commit is contained in:
parent
a49ad47e5a
commit
43f09ad36c
3 changed files with 20 additions and 13 deletions
|
@ -1,24 +1,32 @@
|
||||||
//! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator
|
//! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator
|
||||||
use ra_syntax::SmolStr;
|
use ra_syntax::SmolStr;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
mod cfg_expr;
|
mod cfg_expr;
|
||||||
|
|
||||||
pub use cfg_expr::{parse_cfg, CfgExpr};
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||||
pub struct CfgOptions {
|
pub struct CfgOptions {
|
||||||
atoms: FxHashSet<SmolStr>,
|
atoms: FxHashSet<SmolStr>,
|
||||||
features: FxHashSet<SmolStr>,
|
key_values: FxHashSet<(SmolStr, SmolStr)>,
|
||||||
options: FxHashMap<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(&|key, value| match value {
|
||||||
None => self.atoms.contains(key),
|
None => self.atoms.contains(key),
|
||||||
Some(value) if key == "feature" => self.features.contains(value),
|
Some(value) => self.key_values.contains(&(key.clone(), value.clone())),
|
||||||
Some(value) => self.options.get(key).map_or(false, |v| v == value),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,13 +39,13 @@ impl CfgOptions {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn feature(mut self, name: SmolStr) -> CfgOptions {
|
pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions {
|
||||||
self.features.insert(name);
|
self.key_values.insert((key, value));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn option(mut self, key: SmolStr, value: SmolStr) -> CfgOptions {
|
pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions {
|
||||||
self.options.insert(key, value);
|
self.atoms.remove(name);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -563,9 +563,9 @@ fn cfg_test() {
|
||||||
"main": ("/main.rs", ["std"]),
|
"main": ("/main.rs", ["std"]),
|
||||||
"std": ("/lib.rs", [], CfgOptions::default()
|
"std": ("/lib.rs", [], CfgOptions::default()
|
||||||
.atom("test".into())
|
.atom("test".into())
|
||||||
.feature("foo".into())
|
.key_value("feature".into(), "foo".into())
|
||||||
.feature("bar".into())
|
.key_value("feature".into(), "bar".into())
|
||||||
.option("opt".into(), "42".into())
|
.key_value("opt".into(), "42".into())
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -94,7 +94,6 @@ impl MockAnalysis {
|
||||||
assert!(path.starts_with('/'));
|
assert!(path.starts_with('/'));
|
||||||
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
||||||
let file_id = FileId(i as u32 + 1);
|
let file_id = FileId(i as u32 + 1);
|
||||||
// FIXME: cfg options
|
|
||||||
let cfg_options = CfgOptions::default();
|
let cfg_options = CfgOptions::default();
|
||||||
if path == "/lib.rs" || path == "/main.rs" {
|
if path == "/lib.rs" || path == "/main.rs" {
|
||||||
root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018, cfg_options));
|
root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018, cfg_options));
|
||||||
|
|
Loading…
Reference in a new issue