@@ -16,7 +16,7 @@ use rustc_target::spec::{LinkerFlavor, SplitDebuginfo, Target, TargetTriple, Tar
16
16
17
17
use rustc_serialize:: json;
18
18
19
- use crate :: parse:: CrateConfig ;
19
+ use crate :: parse:: { CrateCheckConfig , CrateConfig } ;
20
20
use rustc_feature:: UnstableFeatures ;
21
21
use rustc_span:: edition:: { Edition , DEFAULT_EDITION , EDITION_NAME_LIST , LATEST_STABLE_EDITION } ;
22
22
use rustc_span:: source_map:: { FileName , FilePathMapping } ;
@@ -921,6 +921,7 @@ pub const fn default_lib_output() -> CrateType {
921
921
}
922
922
923
923
fn default_configuration ( sess : & Session ) -> CrateConfig {
924
+ // NOTE: This should be kept in sync with `CrateCheckConfig::fill_well_known` below.
924
925
let end = & sess. target . endian ;
925
926
let arch = & sess. target . arch ;
926
927
let wordsz = sess. target . pointer_width . to_string ( ) ;
@@ -1005,6 +1006,91 @@ pub fn to_crate_config(cfg: FxHashSet<(String, Option<String>)>) -> CrateConfig
1005
1006
cfg. into_iter ( ) . map ( |( a, b) | ( Symbol :: intern ( & a) , b. map ( |b| Symbol :: intern ( & b) ) ) ) . collect ( )
1006
1007
}
1007
1008
1009
+ /// The parsed `--check-cfg` options
1010
+ pub struct CheckCfg < T = String > {
1011
+ /// Set if `names()` checking is enabled
1012
+ pub names_checked : bool ,
1013
+ /// The union of all `names()`
1014
+ pub names_valid : FxHashSet < T > ,
1015
+ /// The set of names for which `values()` was used
1016
+ pub values_checked : FxHashSet < T > ,
1017
+ /// The set of all (name, value) pairs passed in `values()`
1018
+ pub values_valid : FxHashSet < ( T , T ) > ,
1019
+ }
1020
+
1021
+ impl < T > Default for CheckCfg < T > {
1022
+ fn default ( ) -> Self {
1023
+ CheckCfg {
1024
+ names_checked : false ,
1025
+ names_valid : FxHashSet :: default ( ) ,
1026
+ values_checked : FxHashSet :: default ( ) ,
1027
+ values_valid : FxHashSet :: default ( ) ,
1028
+ }
1029
+ }
1030
+ }
1031
+
1032
+ impl < T > CheckCfg < T > {
1033
+ fn map_data < O : Eq + Hash > ( & self , f : impl Fn ( & T ) -> O ) -> CheckCfg < O > {
1034
+ CheckCfg {
1035
+ names_checked : self . names_checked ,
1036
+ names_valid : self . names_valid . iter ( ) . map ( |a| f ( a) ) . collect ( ) ,
1037
+ values_checked : self . values_checked . iter ( ) . map ( |a| f ( a) ) . collect ( ) ,
1038
+ values_valid : self . values_valid . iter ( ) . map ( |( a, b) | ( f ( a) , f ( b) ) ) . collect ( ) ,
1039
+ }
1040
+ }
1041
+ }
1042
+
1043
+ /// Converts the crate `--check-cfg` options from `String` to `Symbol`.
1044
+ /// `rustc_interface::interface::Config` accepts this in the compiler configuration,
1045
+ /// but the symbol interner is not yet set up then, so we must convert it later.
1046
+ pub fn to_crate_check_config ( cfg : CheckCfg ) -> CrateCheckConfig {
1047
+ cfg. map_data ( |s| Symbol :: intern ( s) )
1048
+ }
1049
+
1050
+ impl CrateCheckConfig {
1051
+ /// Fills a `CrateCheckConfig` with well-known configuration names.
1052
+ pub fn fill_well_known ( & mut self ) {
1053
+ // NOTE: This should be kept in sync with `default_configuration`
1054
+ const WELL_KNOWN_NAMES : & [ Symbol ] = & [
1055
+ sym:: unix,
1056
+ sym:: windows,
1057
+ sym:: target_os,
1058
+ sym:: target_family,
1059
+ sym:: target_arch,
1060
+ sym:: target_endian,
1061
+ sym:: target_pointer_width,
1062
+ sym:: target_env,
1063
+ sym:: target_abi,
1064
+ sym:: target_vendor,
1065
+ sym:: target_thread_local,
1066
+ sym:: target_has_atomic_load_store,
1067
+ sym:: target_has_atomic,
1068
+ sym:: target_has_atomic_equal_alignment,
1069
+ sym:: panic,
1070
+ sym:: sanitize,
1071
+ sym:: debug_assertions,
1072
+ sym:: proc_macro,
1073
+ sym:: test,
1074
+ sym:: doc,
1075
+ sym:: doctest,
1076
+ sym:: feature,
1077
+ ] ;
1078
+ for & name in WELL_KNOWN_NAMES {
1079
+ self . names_valid . insert ( name) ;
1080
+ }
1081
+ }
1082
+
1083
+ /// Fills a `CrateCheckConfig` with configuration names and values that are actually active.
1084
+ pub fn fill_actual ( & mut self , cfg : & CrateConfig ) {
1085
+ for & ( k, v) in cfg {
1086
+ self . names_valid . insert ( k) ;
1087
+ if let Some ( v) = v {
1088
+ self . values_valid . insert ( ( k, v) ) ;
1089
+ }
1090
+ }
1091
+ }
1092
+ }
1093
+
1008
1094
pub fn build_configuration ( sess : & Session , mut user_cfg : CrateConfig ) -> CrateConfig {
1009
1095
// Combine the configuration requested by the session (command line) with
1010
1096
// some default and generated configuration items.
@@ -1148,6 +1234,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
1148
1234
vec ! [
1149
1235
opt:: flag_s( "h" , "help" , "Display this message" ) ,
1150
1236
opt:: multi_s( "" , "cfg" , "Configure the compilation environment" , "SPEC" ) ,
1237
+ opt:: multi( "" , "check-cfg" , "Provide list of valid cfg options for checking" , "SPEC" ) ,
1151
1238
opt:: multi_s(
1152
1239
"L" ,
1153
1240
"" ,
0 commit comments