//! cfg defines conditional compiling options, `cfg` attribute parser and evaluator #![warn(rust_2018_idioms, unused_lifetimes)] mod cfg_expr; mod dnf; #[cfg(test)] mod tests; use std::fmt; use rustc_hash::FxHashSet; use tt::SmolStr; pub use cfg_expr::{CfgAtom, CfgExpr}; pub use dnf::DnfExpr; /// Configuration options used for conditional compilation 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: #[derive(Clone, PartialEq, Eq, Default)] pub struct CfgOptions { enabled: FxHashSet, } impl fmt::Debug for CfgOptions { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut items = self .enabled .iter() .map(|atom| match atom { CfgAtom::Flag(it) => it.to_string(), CfgAtom::KeyValue { key, value } => format!("{key}={value}"), }) .collect::>(); items.sort(); f.debug_tuple("CfgOptions").field(&items).finish() } } impl CfgOptions { pub fn check(&self, cfg: &CfgExpr) -> Option { cfg.fold(&|atom| self.enabled.contains(atom)) } pub fn insert_atom(&mut self, key: SmolStr) { self.enabled.insert(CfgAtom::Flag(key)); } pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { self.enabled.insert(CfgAtom::KeyValue { key, value }); } pub fn difference<'a>( &'a self, other: &'a CfgOptions, ) -> impl Iterator + 'a { self.enabled.difference(&other.enabled) } pub fn apply_diff(&mut self, diff: CfgDiff) { for atom in diff.enable { self.enabled.insert(atom); } for atom in diff.disable { self.enabled.remove(&atom); } } pub fn get_cfg_keys(&self) -> impl Iterator { self.enabled.iter().map(|it| match it { CfgAtom::Flag(key) => key, CfgAtom::KeyValue { key, .. } => key, }) } pub fn get_cfg_values<'a>( &'a self, cfg_key: &'a str, ) -> impl Iterator + 'a { self.enabled.iter().filter_map(move |it| match it { CfgAtom::KeyValue { key, value } if cfg_key == key => Some(value), _ => None, }) } } impl Extend for CfgOptions { fn extend>(&mut self, iter: T) { iter.into_iter().for_each(|cfg_flag| _ = self.enabled.insert(cfg_flag)); } } impl IntoIterator for CfgOptions { type Item = as IntoIterator>::Item; type IntoIter = as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { as IntoIterator>::into_iter(self.enabled) } } impl<'a> IntoIterator for &'a CfgOptions { type Item = <&'a FxHashSet as IntoIterator>::Item; type IntoIter = <&'a FxHashSet as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { <&FxHashSet as IntoIterator>::into_iter(&self.enabled) } } #[derive(Default, Clone, Debug, PartialEq, Eq)] pub struct CfgDiff { // Invariants: No duplicates, no atom that's both in `enable` and `disable`. enable: Vec, disable: Vec, } impl CfgDiff { /// Create a new CfgDiff. Will return None if the same item appears more than once in the set /// of both. pub fn new(enable: Vec, disable: Vec) -> Option { let mut occupied = FxHashSet::default(); if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) { // was present return None; } Some(CfgDiff { enable, disable }) } /// Returns the total number of atoms changed by this diff. pub fn len(&self) -> usize { self.enable.len() + self.disable.len() } pub fn is_empty(&self) -> bool { self.len() == 0 } } impl fmt::Display for CfgDiff { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if !self.enable.is_empty() { f.write_str("enable ")?; for (i, atom) in self.enable.iter().enumerate() { let sep = match i { 0 => "", _ if i == self.enable.len() - 1 => " and ", _ => ", ", }; f.write_str(sep)?; atom.fmt(f)?; } if !self.disable.is_empty() { f.write_str("; ")?; } } if !self.disable.is_empty() { f.write_str("disable ")?; for (i, atom) in self.disable.iter().enumerate() { let sep = match i { 0 => "", _ if i == self.enable.len() - 1 => " and ", _ => ", ", }; f.write_str(sep)?; atom.fmt(f)?; } } Ok(()) } } pub struct InactiveReason { enabled: Vec, disabled: Vec, } impl fmt::Display for InactiveReason { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if !self.enabled.is_empty() { for (i, atom) in self.enabled.iter().enumerate() { let sep = match i { 0 => "", _ if i == self.enabled.len() - 1 => " and ", _ => ", ", }; f.write_str(sep)?; atom.fmt(f)?; } let is_are = if self.enabled.len() == 1 { "is" } else { "are" }; write!(f, " {is_are} enabled")?; if !self.disabled.is_empty() { f.write_str(" and ")?; } } if !self.disabled.is_empty() { for (i, atom) in self.disabled.iter().enumerate() { let sep = match i { 0 => "", _ if i == self.disabled.len() - 1 => " and ", _ => ", ", }; f.write_str(sep)?; atom.fmt(f)?; } let is_are = if self.disabled.len() == 1 { "is" } else { "are" }; write!(f, " {is_are} disabled")?; } Ok(()) } }