rust-analyzer/crates/cfg/src/lib.rs

44 lines
1.4 KiB
Rust
Raw Normal View History

2020-08-13 08:19:09 +00:00
//! cfg defines conditional compiling options, `cfg` attibute parser and evaluator
2019-11-12 12:41:02 +00:00
mod cfg_expr;
2019-10-02 17:20:08 +00:00
use rustc_hash::FxHashSet;
2020-08-13 08:15:45 +00:00
use tt::SmolStr;
2020-10-21 11:57:12 +00:00
pub use cfg_expr::{CfgAtom, CfgExpr};
2019-10-02 17:20:08 +00:00
/// 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 {
2020-10-21 11:57:12 +00:00
enabled: FxHashSet<CfgAtom>,
}
impl CfgOptions {
pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
2020-10-21 11:57:12 +00:00
cfg.fold(&|atom| self.enabled.contains(atom))
}
pub fn insert_atom(&mut self, key: SmolStr) {
2020-10-21 11:57:12 +00:00
self.enabled.insert(CfgAtom::Flag(key));
}
pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
2020-10-21 11:57:12 +00:00
self.enabled.insert(CfgAtom::KeyValue { key, value });
2019-10-02 18:02:53 +00:00
}
pub fn append(&mut self, other: &CfgOptions) {
2020-10-21 11:57:12 +00:00
for atom in &other.enabled {
self.enabled.insert(atom.clone());
}
}
}