diff options
Diffstat (limited to 'crates/cfg/src/cfg_expr.rs')
-rw-r--r-- | crates/cfg/src/cfg_expr.rs | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index db3655b74..3f12a3fa8 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs | |||
@@ -2,12 +2,12 @@ | |||
2 | //! | 2 | //! |
3 | //! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation | 3 | //! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation |
4 | 4 | ||
5 | use std::slice::Iter as SliceIter; | 5 | use std::{fmt, slice::Iter as SliceIter}; |
6 | 6 | ||
7 | use tt::SmolStr; | 7 | use tt::SmolStr; |
8 | 8 | ||
9 | /// A simple configuration value passed in from the outside. | 9 | /// A simple configuration value passed in from the outside. |
10 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 10 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] |
11 | pub enum CfgAtom { | 11 | pub enum CfgAtom { |
12 | /// eg. `#[cfg(test)]` | 12 | /// eg. `#[cfg(test)]` |
13 | Flag(SmolStr), | 13 | Flag(SmolStr), |
@@ -18,6 +18,37 @@ pub enum CfgAtom { | |||
18 | KeyValue { key: SmolStr, value: SmolStr }, | 18 | KeyValue { key: SmolStr, value: SmolStr }, |
19 | } | 19 | } |
20 | 20 | ||
21 | impl CfgAtom { | ||
22 | /// Returns `true` when the atom comes from the target specification. | ||
23 | /// | ||
24 | /// If this returns `true`, then changing this atom requires changing the compilation target. If | ||
25 | /// it returns `false`, the atom might come from a build script or the build system. | ||
26 | pub fn is_target_defined(&self) -> bool { | ||
27 | match self { | ||
28 | CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"), | ||
29 | CfgAtom::KeyValue { key, value: _ } => matches!( | ||
30 | &**key, | ||
31 | "target_arch" | ||
32 | | "target_os" | ||
33 | | "target_env" | ||
34 | | "target_family" | ||
35 | | "target_endian" | ||
36 | | "target_pointer_width" | ||
37 | | "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature` | ||
38 | ), | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl fmt::Display for CfgAtom { | ||
44 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
45 | match self { | ||
46 | CfgAtom::Flag(name) => write!(f, "{}", name), | ||
47 | CfgAtom::KeyValue { key, value } => write!(f, "{} = \"{}\"", key, value), | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
21 | #[derive(Debug, Clone, PartialEq, Eq)] | 52 | #[derive(Debug, Clone, PartialEq, Eq)] |
22 | pub enum CfgExpr { | 53 | pub enum CfgExpr { |
23 | Invalid, | 54 | Invalid, |