Rollup merge of #65595 - Centril:cfgspecs, r=Mark-Simulacrum

move `parse_cfgspecs` to `rustc_interface`

Part of https://github.com/rust-lang/rust/pull/65324.

r? @Mark-Simulacrum
This commit is contained in:
Mazdak Farrokhzad 2019-10-20 12:40:19 +02:00 committed by GitHub
commit ed9cc70768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 107 deletions

View File

@ -3556,6 +3556,7 @@ dependencies = [
"rustc_plugin_impl", "rustc_plugin_impl",
"rustc_privacy", "rustc_privacy",
"rustc_resolve", "rustc_resolve",
"rustc_target",
"rustc_traits", "rustc_traits",
"rustc_typeck", "rustc_typeck",
"serialize", "serialize",

View File

@ -7,24 +7,19 @@
use crate::session::search_paths::SearchPath; use crate::session::search_paths::SearchPath;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel}; use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
use rustc_target::spec::{Target, TargetTriple}; use rustc_target::spec::{Target, TargetTriple};
use syntax; use syntax;
use syntax::ast::{self, IntTy, UintTy, MetaItemKind}; use syntax::ast::{self, IntTy, UintTy};
use syntax::source_map::{FileName, FilePathMapping}; use syntax::source_map::{FileName, FilePathMapping};
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION}; use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
use syntax::parse::new_parser_from_source_str;
use syntax::parse::token;
use syntax::sess::ParseSess;
use syntax::symbol::{sym, Symbol}; use syntax::symbol::{sym, Symbol};
use syntax::feature_gate::UnstableFeatures; use syntax::feature_gate::UnstableFeatures;
use syntax::source_map::SourceMap;
use errors::emitter::HumanReadableErrorType; use errors::emitter::HumanReadableErrorType;
use errors::{ColorConfig, FatalError, Handler, SourceMapperDyn}; use errors::{ColorConfig, FatalError, Handler};
use getopts; use getopts;
@ -1854,59 +1849,6 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
opts opts
} }
struct NullEmitter;
impl errors::emitter::Emitter for NullEmitter {
fn emit_diagnostic(&mut self, _: &errors::Diagnostic) {}
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
}
// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
syntax::with_default_globals(move || {
let cfg = cfgspecs.into_iter().map(|s| {
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
let sess = ParseSess::with_span_handler(handler, cm);
let filename = FileName::cfg_spec_source_code(&s);
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
macro_rules! error {($reason: expr) => {
early_error(ErrorOutputType::default(),
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
}}
match &mut parser.parse_meta_item() {
Ok(meta_item) if parser.token == token::Eof => {
if meta_item.path.segments.len() != 1 {
error!("argument key must be an identifier");
}
match &meta_item.kind {
MetaItemKind::List(..) => {
error!(r#"expected `key` or `key="value"`"#);
}
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
error!("argument value must be a string");
}
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
let ident = meta_item.ident().expect("multi-segment cfg key");
return (ident.name, meta_item.value_str());
}
}
}
Ok(..) => {}
Err(err) => err.cancel(),
}
error!(r#"expected `key` or `key="value"`"#);
}).collect::<ast::CrateConfig>();
cfg.into_iter().map(|(a, b)| {
(a.to_string(), b.map(|b| b.to_string()))
}).collect()
})
}
pub fn get_cmd_lint_options(matches: &getopts::Matches, pub fn get_cmd_lint_options(matches: &getopts::Matches,
error_format: ErrorOutputType) error_format: ErrorOutputType)
-> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) { -> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) {
@ -2877,6 +2819,3 @@ pub fn stable_hash(
} }
} }
} }
#[cfg(test)]
mod tests;

View File

@ -167,7 +167,7 @@ pub fn run_compiler(
}; };
let sopts = config::build_session_options(&matches); let sopts = config::build_session_options(&matches);
let cfg = config::parse_cfgspecs(matches.opt_strs("cfg")); let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
let mut dummy_config = |sopts, cfg, diagnostic_output| { let mut dummy_config = |sopts, cfg, diagnostic_output| {
let mut config = interface::Config { let mut config = interface::Config {

View File

@ -27,6 +27,7 @@ rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_metadata = { path = "../librustc_metadata" } rustc_metadata = { path = "../librustc_metadata" }
rustc_mir = { path = "../librustc_mir" } rustc_mir = { path = "../librustc_mir" }
rustc_passes = { path = "../librustc_passes" } rustc_passes = { path = "../librustc_passes" }
rustc_target = { path = "../librustc_target" }
rustc_typeck = { path = "../librustc_typeck" } rustc_typeck = { path = "../librustc_typeck" }
rustc_lint = { path = "../librustc_lint" } rustc_lint = { path = "../librustc_lint" }
rustc_errors = { path = "../librustc_errors" } rustc_errors = { path = "../librustc_errors" }

View File

@ -3,7 +3,8 @@
pub use crate::passes::BoxedResolver; pub use crate::passes::BoxedResolver;
use rustc::lint; use rustc::lint;
use rustc::session::config::{self, Input}; use rustc::session::early_error;
use rustc::session::config::{self, Input, ErrorOutputType};
use rustc::session::{DiagnosticOutput, Session}; use rustc::session::{DiagnosticOutput, Session};
use rustc::util::common::ErrorReported; use rustc::util::common::ErrorReported;
use rustc_codegen_utils::codegen_backend::CodegenBackend; use rustc_codegen_utils::codegen_backend::CodegenBackend;
@ -14,9 +15,13 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::result; use std::result;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use syntax; use syntax::{self, parse};
use syntax::source_map::{FileLoader, SourceMap}; use syntax::ast::{self, MetaItemKind};
use syntax::parse::token;
use syntax::source_map::{FileName, FilePathMapping, FileLoader, SourceMap};
use syntax::sess::ParseSess;
use syntax_pos::edition; use syntax_pos::edition;
use rustc_errors::{Diagnostic, emitter::Emitter, Handler, SourceMapperDyn};
pub type Result<T> = result::Result<T, ErrorReported>; pub type Result<T> = result::Result<T, ErrorReported>;
@ -60,6 +65,58 @@ pub fn output_file(&self) -> &Option<PathBuf> {
} }
} }
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
struct NullEmitter;
impl Emitter for NullEmitter {
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
}
syntax::with_default_globals(move || {
let cfg = cfgspecs.into_iter().map(|s| {
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
let sess = ParseSess::with_span_handler(handler, cm);
let filename = FileName::cfg_spec_source_code(&s);
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
macro_rules! error {($reason: expr) => {
early_error(ErrorOutputType::default(),
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
}}
match &mut parser.parse_meta_item() {
Ok(meta_item) if parser.token == token::Eof => {
if meta_item.path.segments.len() != 1 {
error!("argument key must be an identifier");
}
match &meta_item.kind {
MetaItemKind::List(..) => {
error!(r#"expected `key` or `key="value"`"#);
}
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
error!("argument value must be a string");
}
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
let ident = meta_item.ident().expect("multi-segment cfg key");
return (ident.name, meta_item.value_str());
}
}
}
Ok(..) => {}
Err(err) => err.cancel(),
}
error!(r#"expected `key` or `key="value"`"#);
}).collect::<ast::CrateConfig>();
cfg.into_iter().map(|(a, b)| {
(a.to_string(), b.map(|b| b.to_string()))
}).collect()
})
}
/// The compiler configuration /// The compiler configuration
pub struct Config { pub struct Config {
/// Command line options /// Command line options

View File

@ -18,3 +18,6 @@
mod proc_macro_decls; mod proc_macro_decls;
pub use interface::{run_compiler, Config}; pub use interface::{run_compiler, Config};
#[cfg(test)]
mod tests;

View File

@ -1,25 +1,24 @@
use getopts; extern crate getopts;
use crate::lint;
use crate::middle::cstore; use crate::interface::parse_cfgspecs;
use crate::session::config::{
build_configuration, use rustc::lint;
build_session_options, use rustc::middle::cstore;
to_crate_config, use rustc::session::config::{build_configuration, build_session_options, to_crate_config};
parse_cfgspecs, use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
}; use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
use crate::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry}; use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes};
use crate::session::build_session; use rustc::session::build_session;
use crate::session::search_paths::SearchPath; use rustc::session::search_paths::SearchPath;
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path::PathBuf; use std::path::PathBuf;
use super::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel}; use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
use syntax::symbol::sym; use syntax::symbol::sym;
use syntax::edition::{Edition, DEFAULT_EDITION}; use syntax::edition::{Edition, DEFAULT_EDITION};
use syntax; use syntax;
use super::Options;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry};
pub fn build_session_options_and_crate_config( pub fn build_session_options_and_crate_config(
matches: &getopts::Matches, matches: &getopts::Matches,
@ -30,22 +29,23 @@ pub fn build_session_options_and_crate_config(
) )
} }
impl ExternEntry { fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
fn new_public<S: Into<String>, where
I: IntoIterator<Item = Option<S>>>(locations: I) -> ExternEntry { S: Into<String>,
let locations: BTreeSet<_> = locations.into_iter().map(|o| o.map(|s| s.into())) I: IntoIterator<Item = Option<S>>,
.collect(); {
let locations: BTreeSet<_> = locations.into_iter().map(|o| o.map(|s| s.into()))
.collect();
ExternEntry { ExternEntry {
locations, locations,
is_private_dep: false is_private_dep: false
}
} }
} }
fn optgroups() -> getopts::Options { fn optgroups() -> getopts::Options {
let mut opts = getopts::Options::new(); let mut opts = getopts::Options::new();
for group in super::rustc_optgroups() { for group in rustc_optgroups() {
(group.apply)(&mut opts); (group.apply)(&mut opts);
} }
return opts; return opts;
@ -63,7 +63,7 @@ fn test_switch_implies_cfg_test() {
Ok(m) => m, Ok(m) => m,
Err(f) => panic!("test_switch_implies_cfg_test: {}", f), Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
}; };
let registry = errors::registry::Registry::new(&[]); let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches); let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry); let sess = build_session(sessopts, None, registry);
let cfg = build_configuration(&sess, to_crate_config(cfg)); let cfg = build_configuration(&sess, to_crate_config(cfg));
@ -81,7 +81,7 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
Ok(m) => m, Ok(m) => m,
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f), Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
}; };
let registry = errors::registry::Registry::new(&[]); let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches); let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry); let sess = build_session(sessopts, None, registry);
let cfg = build_configuration(&sess, to_crate_config(cfg)); let cfg = build_configuration(&sess, to_crate_config(cfg));
@ -95,7 +95,7 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
fn test_can_print_warnings() { fn test_can_print_warnings() {
syntax::with_default_globals(|| { syntax::with_default_globals(|| {
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap(); let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
let registry = errors::registry::Registry::new(&[]); let registry = registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches); let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry); let sess = build_session(sessopts, None, registry);
assert!(!sess.diagnostic().can_emit_warnings()); assert!(!sess.diagnostic().can_emit_warnings());
@ -105,7 +105,7 @@ fn test_can_print_warnings() {
let matches = optgroups() let matches = optgroups()
.parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]) .parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()])
.unwrap(); .unwrap();
let registry = errors::registry::Registry::new(&[]); let registry = registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches); let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry); let sess = build_session(sessopts, None, registry);
assert!(sess.diagnostic().can_emit_warnings()); assert!(sess.diagnostic().can_emit_warnings());
@ -113,7 +113,7 @@ fn test_can_print_warnings() {
syntax::with_default_globals(|| { syntax::with_default_globals(|| {
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap(); let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
let registry = errors::registry::Registry::new(&[]); let registry = registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches); let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry); let sess = build_session(sessopts, None, registry);
assert!(sess.diagnostic().can_emit_warnings()); assert!(sess.diagnostic().can_emit_warnings());
@ -172,33 +172,33 @@ fn test_externs_tracking_hash_different_construction_order() {
v1.externs = Externs::new(mk_map(vec![ v1.externs = Externs::new(mk_map(vec![
( (
String::from("a"), String::from("a"),
ExternEntry::new_public(vec![Some("b"), Some("c")]) new_public_extern_entry(vec![Some("b"), Some("c")])
), ),
( (
String::from("d"), String::from("d"),
ExternEntry::new_public(vec![Some("e"), Some("f")]) new_public_extern_entry(vec![Some("e"), Some("f")])
), ),
])); ]));
v2.externs = Externs::new(mk_map(vec![ v2.externs = Externs::new(mk_map(vec![
( (
String::from("d"), String::from("d"),
ExternEntry::new_public(vec![Some("e"), Some("f")]) new_public_extern_entry(vec![Some("e"), Some("f")])
), ),
( (
String::from("a"), String::from("a"),
ExternEntry::new_public(vec![Some("b"), Some("c")]) new_public_extern_entry(vec![Some("b"), Some("c")])
), ),
])); ]));
v3.externs = Externs::new(mk_map(vec![ v3.externs = Externs::new(mk_map(vec![
( (
String::from("a"), String::from("a"),
ExternEntry::new_public(vec![Some("b"), Some("c")]) new_public_extern_entry(vec![Some("b"), Some("c")])
), ),
( (
String::from("d"), String::from("d"),
ExternEntry::new_public(vec![Some("f"), Some("e")]) new_public_extern_entry(vec![Some("f"), Some("e")])
), ),
])); ]));
@ -282,9 +282,9 @@ fn test_search_paths_tracking_hash_different_order() {
let mut v3 = Options::default(); let mut v3 = Options::default();
let mut v4 = Options::default(); let mut v4 = Options::default();
const JSON: super::ErrorOutputType = super::ErrorOutputType::Json { const JSON: ErrorOutputType = ErrorOutputType::Json {
pretty: false, pretty: false,
json_rendered: super::HumanReadableErrorType::Default(super::ColorConfig::Never), json_rendered: HumanReadableErrorType::Default(ColorConfig::Never),
}; };
// Reference // Reference
@ -455,7 +455,7 @@ fn test_codegen_options_tracking_hash() {
opts.cg.codegen_units = Some(42); opts.cg.codegen_units = Some(42);
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.cg.remark = super::Passes::Some(vec![String::from("pass1"), String::from("pass2")]); opts.cg.remark = Passes::Some(vec![String::from("pass1"), String::from("pass2")]);
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.cg.save_temps = true; opts.cg.save_temps = true;

View File

@ -329,7 +329,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
let config = interface::Config { let config = interface::Config {
opts: sessopts, opts: sessopts,
crate_cfg: config::parse_cfgspecs(cfgs), crate_cfg: interface::parse_cfgspecs(cfgs),
input, input,
input_path: cpath, input_path: cpath,
output_file: None, output_file: None,

View File

@ -67,7 +67,7 @@ pub fn run(options: Options) -> i32 {
cfgs.push("doctest".to_owned()); cfgs.push("doctest".to_owned());
let config = interface::Config { let config = interface::Config {
opts: sessopts, opts: sessopts,
crate_cfg: config::parse_cfgspecs(cfgs), crate_cfg: interface::parse_cfgspecs(cfgs),
input, input,
input_path: None, input_path: None,
output_file: None, output_file: None,