aboutsummaryrefslogtreecommitdiff
path: root/crates/cfg/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/cfg/src/lib.rs')
-rw-r--r--crates/cfg/src/lib.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs
index 35f540ac3..0b0734213 100644
--- a/crates/cfg/src/lib.rs
+++ b/crates/cfg/src/lib.rs
@@ -1,11 +1,15 @@
1//! cfg defines conditional compiling options, `cfg` attibute parser and evaluator 1//! cfg defines conditional compiling options, `cfg` attibute parser and evaluator
2 2
3mod cfg_expr; 3mod cfg_expr;
4mod dnf;
5
6use std::fmt;
4 7
5use rustc_hash::FxHashSet; 8use rustc_hash::FxHashSet;
6use tt::SmolStr; 9use tt::SmolStr;
7 10
8pub use cfg_expr::{CfgAtom, CfgExpr}; 11pub use cfg_expr::{CfgAtom, CfgExpr};
12pub use dnf::DnfExpr;
9 13
10/// Configuration options used for conditional compilition on items with `cfg` attributes. 14/// Configuration options used for conditional compilition on items with `cfg` attributes.
11/// We have two kind of options in different namespaces: atomic options like `unix`, and 15/// We have two kind of options in different namespaces: atomic options like `unix`, and
@@ -40,4 +44,110 @@ impl CfgOptions {
40 self.enabled.insert(atom.clone()); 44 self.enabled.insert(atom.clone());
41 } 45 }
42 } 46 }
47
48 pub fn apply_diff(&mut self, diff: CfgDiff) {
49 for atom in diff.enable {
50 self.enabled.insert(atom);
51 }
52
53 for atom in diff.disable {
54 self.enabled.remove(&atom);
55 }
56 }
57}
58
59pub struct CfgDiff {
60 // Invariants: No duplicates, no atom that's both in `enable` and `disable`.
61 enable: Vec<CfgAtom>,
62 disable: Vec<CfgAtom>,
63}
64
65impl CfgDiff {
66 /// Returns the total number of atoms changed by this diff.
67 pub fn len(&self) -> usize {
68 self.enable.len() + self.disable.len()
69 }
70}
71
72impl fmt::Display for CfgDiff {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 if !self.enable.is_empty() {
75 f.write_str("enable ")?;
76 for (i, atom) in self.enable.iter().enumerate() {
77 let sep = match i {
78 0 => "",
79 _ if i == self.enable.len() - 1 => " and ",
80 _ => ", ",
81 };
82 f.write_str(sep)?;
83
84 write!(f, "{}", atom)?;
85 }
86
87 if !self.disable.is_empty() {
88 f.write_str("; ")?;
89 }
90 }
91
92 if !self.disable.is_empty() {
93 f.write_str("disable ")?;
94 for (i, atom) in self.disable.iter().enumerate() {
95 let sep = match i {
96 0 => "",
97 _ if i == self.enable.len() - 1 => " and ",
98 _ => ", ",
99 };
100 f.write_str(sep)?;
101
102 write!(f, "{}", atom)?;
103 }
104 }
105
106 Ok(())
107 }
108}
109
110pub struct InactiveReason {
111 enabled: Vec<CfgAtom>,
112 disabled: Vec<CfgAtom>,
113}
114
115impl fmt::Display for InactiveReason {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 if !self.enabled.is_empty() {
118 for (i, atom) in self.enabled.iter().enumerate() {
119 let sep = match i {
120 0 => "",
121 _ if i == self.enabled.len() - 1 => " and ",
122 _ => ", ",
123 };
124 f.write_str(sep)?;
125
126 write!(f, "{}", atom)?;
127 }
128 let is_are = if self.enabled.len() == 1 { "is" } else { "are" };
129 write!(f, " {} enabled", is_are)?;
130
131 if !self.disabled.is_empty() {
132 f.write_str(" and ")?;
133 }
134 }
135
136 if !self.disabled.is_empty() {
137 for (i, atom) in self.disabled.iter().enumerate() {
138 let sep = match i {
139 0 => "",
140 _ if i == self.enabled.len() - 1 => " and ",
141 _ => ", ",
142 };
143 f.write_str(sep)?;
144
145 write!(f, "{}", atom)?;
146 }
147 let is_are = if self.disabled.len() == 1 { "is" } else { "are" };
148 write!(f, " {} disabled", is_are)?;
149 }
150
151 Ok(())
152 }
43} 153}