mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Introduce ra_cfg to parse and evaluate CfgExpr
This commit is contained in:
parent
ffe179a736
commit
b1ed887d81
11 changed files with 315 additions and 27 deletions
43
crates/ra_cfg/src/lib.rs
Normal file
43
crates/ra_cfg/src/lib.rs
Normal file
|
@ -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<SmolStr>,
|
||||
features: FxHashSet<SmolStr>,
|
||||
options: FxHashMap<SmolStr, SmolStr>,
|
||||
}
|
||||
|
||||
impl CfgOptions {
|
||||
pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
|
||||
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<bool> {
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue