1 //! cfg defines conditional compiling options, `cfg` attribute parser and evaluator
10 use rustc_hash::FxHashSet;
13 pub use cfg_expr::{CfgAtom, CfgExpr};
16 /// Configuration options used for conditional compilation on items with `cfg` attributes.
17 /// We have two kind of options in different namespaces: atomic options like `unix`, and
18 /// key-value options like `target_arch="x86"`.
20 /// Note that for key-value options, one key can have multiple values (but not none).
21 /// `feature` is an example. We have both `feature="foo"` and `feature="bar"` if features
22 /// `foo` and `bar` are both enabled. And here, we store key-value options as a set of tuple
23 /// of key and value in `key_values`.
25 /// See: <https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options>
26 #[derive(Clone, PartialEq, Eq, Default)]
27 pub struct CfgOptions {
28 enabled: FxHashSet<CfgAtom>,
31 impl fmt::Debug for CfgOptions {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 .map(|atom| match atom {
37 CfgAtom::Flag(it) => it.to_string(),
38 CfgAtom::KeyValue { key, value } => format!("{}={}", key, value),
42 f.debug_tuple("CfgOptions").field(&items).finish()
47 pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
48 cfg.fold(&|atom| self.enabled.contains(atom))
51 pub fn insert_atom(&mut self, key: SmolStr) {
52 self.enabled.insert(CfgAtom::Flag(key));
55 pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
56 self.enabled.insert(CfgAtom::KeyValue { key, value });
59 pub fn apply_diff(&mut self, diff: CfgDiff) {
60 for atom in diff.enable {
61 self.enabled.insert(atom);
64 for atom in diff.disable {
65 self.enabled.remove(&atom);
69 pub fn get_cfg_keys(&self) -> impl Iterator<Item = &SmolStr> {
70 self.enabled.iter().map(|x| match x {
71 CfgAtom::Flag(key) => key,
72 CfgAtom::KeyValue { key, .. } => key,
76 pub fn get_cfg_values<'a>(
79 ) -> impl Iterator<Item = &'a SmolStr> + 'a {
80 self.enabled.iter().filter_map(move |x| match x {
81 CfgAtom::KeyValue { key, value } if cfg_key == key => Some(value),
87 #[derive(Clone, Debug, PartialEq, Eq)]
89 // Invariants: No duplicates, no atom that's both in `enable` and `disable`.
91 disable: Vec<CfgAtom>,
95 /// Create a new CfgDiff. Will return None if the same item appears more than once in the set
97 pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
98 let mut occupied = FxHashSet::default();
99 for item in enable.iter().chain(disable.iter()) {
100 if !occupied.insert(item) {
106 Some(CfgDiff { enable, disable })
109 /// Returns the total number of atoms changed by this diff.
110 pub fn len(&self) -> usize {
111 self.enable.len() + self.disable.len()
114 pub fn is_empty(&self) -> bool {
119 impl fmt::Display for CfgDiff {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 if !self.enable.is_empty() {
122 f.write_str("enable ")?;
123 for (i, atom) in self.enable.iter().enumerate() {
126 _ if i == self.enable.len() - 1 => " and ",
131 write!(f, "{}", atom)?;
134 if !self.disable.is_empty() {
139 if !self.disable.is_empty() {
140 f.write_str("disable ")?;
141 for (i, atom) in self.disable.iter().enumerate() {
144 _ if i == self.enable.len() - 1 => " and ",
149 write!(f, "{}", atom)?;
157 pub struct InactiveReason {
158 enabled: Vec<CfgAtom>,
159 disabled: Vec<CfgAtom>,
162 impl fmt::Display for InactiveReason {
163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 if !self.enabled.is_empty() {
165 for (i, atom) in self.enabled.iter().enumerate() {
168 _ if i == self.enabled.len() - 1 => " and ",
173 write!(f, "{}", atom)?;
175 let is_are = if self.enabled.len() == 1 { "is" } else { "are" };
176 write!(f, " {} enabled", is_are)?;
178 if !self.disabled.is_empty() {
179 f.write_str(" and ")?;
183 if !self.disabled.is_empty() {
184 for (i, atom) in self.disabled.iter().enumerate() {
187 _ if i == self.disabled.len() - 1 => " and ",
192 write!(f, "{}", atom)?;
194 let is_are = if self.disabled.len() == 1 { "is" } else { "are" };
195 write!(f, " {} disabled", is_are)?;