@@ -16,7 +16,7 @@ use rustc_target::spec::{LinkerFlavor, SplitDebuginfo, Target, TargetTriple, Tar
1616
1717use rustc_serialize:: json;
1818
19- use crate :: parse:: CrateConfig ;
19+ use crate :: parse:: { CrateCheckConfig , CrateConfig } ;
2020use rustc_feature:: UnstableFeatures ;
2121use rustc_span:: edition:: { Edition , DEFAULT_EDITION , EDITION_NAME_LIST , LATEST_STABLE_EDITION } ;
2222use rustc_span:: source_map:: { FileName , FilePathMapping } ;
@@ -936,6 +936,7 @@ pub const fn default_lib_output() -> CrateType {
936936}
937937
938938fn default_configuration ( sess : & Session ) -> CrateConfig {
939+ // NOTE: This should be kept in sync with `CrateCheckConfig::fill_well_known` below.
939940 let end = & sess. target . endian ;
940941 let arch = & sess. target . arch ;
941942 let wordsz = sess. target . pointer_width . to_string ( ) ;
@@ -1020,6 +1021,91 @@ pub fn to_crate_config(cfg: FxHashSet<(String, Option<String>)>) -> CrateConfig
10201021 cfg. into_iter ( ) . map ( |( a, b) | ( Symbol :: intern ( & a) , b. map ( |b| Symbol :: intern ( & b) ) ) ) . collect ( )
10211022}
10221023
1024+ /// The parsed `--check-cfg` options
1025+ pub struct CheckCfg < T = String > {
1026+ /// Set if `names()` checking is enabled
1027+ pub names_checked : bool ,
1028+ /// The union of all `names()`
1029+ pub names_valid : FxHashSet < T > ,
1030+ /// The set of names for which `values()` was used
1031+ pub values_checked : FxHashSet < T > ,
1032+ /// The set of all (name, value) pairs passed in `values()`
1033+ pub values_valid : FxHashSet < ( T , T ) > ,
1034+ }
1035+
1036+ impl < T > Default for CheckCfg < T > {
1037+ fn default ( ) -> Self {
1038+ CheckCfg {
1039+ names_checked : false ,
1040+ names_valid : FxHashSet :: default ( ) ,
1041+ values_checked : FxHashSet :: default ( ) ,
1042+ values_valid : FxHashSet :: default ( ) ,
1043+ }
1044+ }
1045+ }
1046+
1047+ impl < T > CheckCfg < T > {
1048+ fn map_data < O : Eq + Hash > ( & self , f : impl Fn ( & T ) -> O ) -> CheckCfg < O > {
1049+ CheckCfg {
1050+ names_checked : self . names_checked ,
1051+ names_valid : self . names_valid . iter ( ) . map ( |a| f ( a) ) . collect ( ) ,
1052+ values_checked : self . values_checked . iter ( ) . map ( |a| f ( a) ) . collect ( ) ,
1053+ values_valid : self . values_valid . iter ( ) . map ( |( a, b) | ( f ( a) , f ( b) ) ) . collect ( ) ,
1054+ }
1055+ }
1056+ }
1057+
1058+ /// Converts the crate `--check-cfg` options from `String` to `Symbol`.
1059+ /// `rustc_interface::interface::Config` accepts this in the compiler configuration,
1060+ /// but the symbol interner is not yet set up then, so we must convert it later.
1061+ pub fn to_crate_check_config ( cfg : CheckCfg ) -> CrateCheckConfig {
1062+ cfg. map_data ( |s| Symbol :: intern ( s) )
1063+ }
1064+
1065+ impl CrateCheckConfig {
1066+ /// Fills a `CrateCheckConfig` with well-known configuration names.
1067+ pub fn fill_well_known ( & mut self ) {
1068+ // NOTE: This should be kept in sync with `default_configuration`
1069+ const WELL_KNOWN_NAMES : & [ Symbol ] = & [
1070+ sym:: unix,
1071+ sym:: windows,
1072+ sym:: target_os,
1073+ sym:: target_family,
1074+ sym:: target_arch,
1075+ sym:: target_endian,
1076+ sym:: target_pointer_width,
1077+ sym:: target_env,
1078+ sym:: target_abi,
1079+ sym:: target_vendor,
1080+ sym:: target_thread_local,
1081+ sym:: target_has_atomic_load_store,
1082+ sym:: target_has_atomic,
1083+ sym:: target_has_atomic_equal_alignment,
1084+ sym:: panic,
1085+ sym:: sanitize,
1086+ sym:: debug_assertions,
1087+ sym:: proc_macro,
1088+ sym:: test,
1089+ sym:: doc,
1090+ sym:: doctest,
1091+ sym:: feature,
1092+ ] ;
1093+ for & name in WELL_KNOWN_NAMES {
1094+ self . names_valid . insert ( name) ;
1095+ }
1096+ }
1097+
1098+ /// Fills a `CrateCheckConfig` with configuration names and values that are actually active.
1099+ pub fn fill_actual ( & mut self , cfg : & CrateConfig ) {
1100+ for & ( k, v) in cfg {
1101+ self . names_valid . insert ( k) ;
1102+ if let Some ( v) = v {
1103+ self . values_valid . insert ( ( k, v) ) ;
1104+ }
1105+ }
1106+ }
1107+ }
1108+
10231109pub fn build_configuration ( sess : & Session , mut user_cfg : CrateConfig ) -> CrateConfig {
10241110 // Combine the configuration requested by the session (command line) with
10251111 // some default and generated configuration items.
@@ -1163,6 +1249,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
11631249 vec ! [
11641250 opt:: flag_s( "h" , "help" , "Display this message" ) ,
11651251 opt:: multi_s( "" , "cfg" , "Configure the compilation environment" , "SPEC" ) ,
1252+ opt:: multi( "" , "check-cfg" , "Provide list of valid cfg options for checking" , "SPEC" ) ,
11661253 opt:: multi_s(
11671254 "L" ,
11681255 "" ,
0 commit comments