Skip to content

Commit eba24f0

Browse files
authored
Rollup merge of rust-lang#65595 - Centril:cfgspecs, r=Mark-Simulacrum
move `parse_cfgspecs` to `rustc_interface` Part of rust-lang#65324. r? @Mark-Simulacrum
2 parents 9b5b9b9 + d945f98 commit eba24f0

File tree

9 files changed

+109
-108
lines changed

9 files changed

+109
-108
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,7 @@ dependencies = [
35563556
"rustc_plugin_impl",
35573557
"rustc_privacy",
35583558
"rustc_resolve",
3559+
"rustc_target",
35593560
"rustc_traits",
35603561
"rustc_typeck",
35613562
"serialize",

src/librustc/session/config.rs

+2-63
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,19 @@ use crate::session::{early_error, early_warn, Session};
77
use crate::session::search_paths::SearchPath;
88

99
use rustc_data_structures::fx::FxHashSet;
10-
use rustc_data_structures::sync::Lrc;
1110

1211
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
1312
use rustc_target::spec::{Target, TargetTriple};
1413

1514
use syntax;
16-
use syntax::ast::{self, IntTy, UintTy, MetaItemKind};
15+
use syntax::ast::{self, IntTy, UintTy};
1716
use syntax::source_map::{FileName, FilePathMapping};
1817
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
19-
use syntax::parse::new_parser_from_source_str;
20-
use syntax::parse::token;
21-
use syntax::sess::ParseSess;
2218
use syntax::symbol::{sym, Symbol};
2319
use syntax::feature_gate::UnstableFeatures;
24-
use syntax::source_map::SourceMap;
2520

2621
use errors::emitter::HumanReadableErrorType;
27-
use errors::{ColorConfig, FatalError, Handler, SourceMapperDyn};
22+
use errors::{ColorConfig, FatalError, Handler};
2823

2924
use getopts;
3025

@@ -1854,59 +1849,6 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18541849
opts
18551850
}
18561851

1857-
struct NullEmitter;
1858-
1859-
impl errors::emitter::Emitter for NullEmitter {
1860-
fn emit_diagnostic(&mut self, _: &errors::Diagnostic) {}
1861-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
1862-
}
1863-
1864-
// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
1865-
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
1866-
syntax::with_default_globals(move || {
1867-
let cfg = cfgspecs.into_iter().map(|s| {
1868-
1869-
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
1870-
let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
1871-
let sess = ParseSess::with_span_handler(handler, cm);
1872-
let filename = FileName::cfg_spec_source_code(&s);
1873-
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
1874-
1875-
macro_rules! error {($reason: expr) => {
1876-
early_error(ErrorOutputType::default(),
1877-
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
1878-
}}
1879-
1880-
match &mut parser.parse_meta_item() {
1881-
Ok(meta_item) if parser.token == token::Eof => {
1882-
if meta_item.path.segments.len() != 1 {
1883-
error!("argument key must be an identifier");
1884-
}
1885-
match &meta_item.kind {
1886-
MetaItemKind::List(..) => {
1887-
error!(r#"expected `key` or `key="value"`"#);
1888-
}
1889-
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
1890-
error!("argument value must be a string");
1891-
}
1892-
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
1893-
let ident = meta_item.ident().expect("multi-segment cfg key");
1894-
return (ident.name, meta_item.value_str());
1895-
}
1896-
}
1897-
}
1898-
Ok(..) => {}
1899-
Err(err) => err.cancel(),
1900-
}
1901-
1902-
error!(r#"expected `key` or `key="value"`"#);
1903-
}).collect::<ast::CrateConfig>();
1904-
cfg.into_iter().map(|(a, b)| {
1905-
(a.to_string(), b.map(|b| b.to_string()))
1906-
}).collect()
1907-
})
1908-
}
1909-
19101852
pub fn get_cmd_lint_options(matches: &getopts::Matches,
19111853
error_format: ErrorOutputType)
19121854
-> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) {
@@ -2877,6 +2819,3 @@ mod dep_tracking {
28772819
}
28782820
}
28792821
}
2880-
2881-
#[cfg(test)]
2882-
mod tests;

src/librustc_driver/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn run_compiler(
167167
};
168168

169169
let sopts = config::build_session_options(&matches);
170-
let cfg = config::parse_cfgspecs(matches.opt_strs("cfg"));
170+
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
171171

172172
let mut dummy_config = |sopts, cfg, diagnostic_output| {
173173
let mut config = interface::Config {

src/librustc_interface/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rustc_codegen_utils = { path = "../librustc_codegen_utils" }
2727
rustc_metadata = { path = "../librustc_metadata" }
2828
rustc_mir = { path = "../librustc_mir" }
2929
rustc_passes = { path = "../librustc_passes" }
30+
rustc_target = { path = "../librustc_target" }
3031
rustc_typeck = { path = "../librustc_typeck" }
3132
rustc_lint = { path = "../librustc_lint" }
3233
rustc_errors = { path = "../librustc_errors" }

src/librustc_interface/interface.rs

+60-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use crate::util;
33
pub use crate::passes::BoxedResolver;
44

55
use rustc::lint;
6-
use rustc::session::config::{self, Input};
6+
use rustc::session::early_error;
7+
use rustc::session::config::{self, Input, ErrorOutputType};
78
use rustc::session::{DiagnosticOutput, Session};
89
use rustc::util::common::ErrorReported;
910
use rustc_codegen_utils::codegen_backend::CodegenBackend;
@@ -14,9 +15,13 @@ use rustc_metadata::cstore::CStore;
1415
use std::path::PathBuf;
1516
use std::result;
1617
use std::sync::{Arc, Mutex};
17-
use syntax;
18-
use syntax::source_map::{FileLoader, SourceMap};
18+
use syntax::{self, parse};
19+
use syntax::ast::{self, MetaItemKind};
20+
use syntax::parse::token;
21+
use syntax::source_map::{FileName, FilePathMapping, FileLoader, SourceMap};
22+
use syntax::sess::ParseSess;
1923
use syntax_pos::edition;
24+
use rustc_errors::{Diagnostic, emitter::Emitter, Handler, SourceMapperDyn};
2025

2126
pub type Result<T> = result::Result<T, ErrorReported>;
2227

@@ -60,6 +65,58 @@ impl Compiler {
6065
}
6166
}
6267

68+
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
69+
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
70+
struct NullEmitter;
71+
impl Emitter for NullEmitter {
72+
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
73+
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
74+
}
75+
76+
syntax::with_default_globals(move || {
77+
let cfg = cfgspecs.into_iter().map(|s| {
78+
79+
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
80+
let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
81+
let sess = ParseSess::with_span_handler(handler, cm);
82+
let filename = FileName::cfg_spec_source_code(&s);
83+
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
84+
85+
macro_rules! error {($reason: expr) => {
86+
early_error(ErrorOutputType::default(),
87+
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
88+
}}
89+
90+
match &mut parser.parse_meta_item() {
91+
Ok(meta_item) if parser.token == token::Eof => {
92+
if meta_item.path.segments.len() != 1 {
93+
error!("argument key must be an identifier");
94+
}
95+
match &meta_item.kind {
96+
MetaItemKind::List(..) => {
97+
error!(r#"expected `key` or `key="value"`"#);
98+
}
99+
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
100+
error!("argument value must be a string");
101+
}
102+
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
103+
let ident = meta_item.ident().expect("multi-segment cfg key");
104+
return (ident.name, meta_item.value_str());
105+
}
106+
}
107+
}
108+
Ok(..) => {}
109+
Err(err) => err.cancel(),
110+
}
111+
112+
error!(r#"expected `key` or `key="value"`"#);
113+
}).collect::<ast::CrateConfig>();
114+
cfg.into_iter().map(|(a, b)| {
115+
(a.to_string(), b.map(|b| b.to_string()))
116+
}).collect()
117+
})
118+
}
119+
63120
/// The compiler configuration
64121
pub struct Config {
65122
/// Command line options

src/librustc_interface/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ pub mod util;
1818
mod proc_macro_decls;
1919

2020
pub use interface::{run_compiler, Config};
21+
22+
#[cfg(test)]
23+
mod tests;

src/librustc/session/config/tests.rs renamed to src/librustc_interface/tests.rs

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
use getopts;
2-
use crate::lint;
3-
use crate::middle::cstore;
4-
use crate::session::config::{
5-
build_configuration,
6-
build_session_options,
7-
to_crate_config,
8-
parse_cfgspecs,
9-
};
10-
use crate::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
11-
use crate::session::build_session;
12-
use crate::session::search_paths::SearchPath;
1+
extern crate getopts;
2+
3+
use crate::interface::parse_cfgspecs;
4+
5+
use rustc::lint;
6+
use rustc::middle::cstore;
7+
use rustc::session::config::{build_configuration, build_session_options, to_crate_config};
8+
use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
9+
use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
10+
use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes};
11+
use rustc::session::build_session;
12+
use rustc::session::search_paths::SearchPath;
1313
use std::collections::{BTreeMap, BTreeSet};
1414
use std::iter::FromIterator;
1515
use std::path::PathBuf;
16-
use super::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
1716
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
1817
use syntax::symbol::sym;
1918
use syntax::edition::{Edition, DEFAULT_EDITION};
2019
use syntax;
21-
use super::Options;
2220
use rustc_data_structures::fx::FxHashSet;
21+
use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry};
2322

2423
pub fn build_session_options_and_crate_config(
2524
matches: &getopts::Matches,
@@ -30,22 +29,23 @@ pub fn build_session_options_and_crate_config(
3029
)
3130
}
3231

33-
impl ExternEntry {
34-
fn new_public<S: Into<String>,
35-
I: IntoIterator<Item = Option<S>>>(locations: I) -> ExternEntry {
36-
let locations: BTreeSet<_> = locations.into_iter().map(|o| o.map(|s| s.into()))
37-
.collect();
38-
39-
ExternEntry {
40-
locations,
41-
is_private_dep: false
42-
}
32+
fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
33+
where
34+
S: Into<String>,
35+
I: IntoIterator<Item = Option<S>>,
36+
{
37+
let locations: BTreeSet<_> = locations.into_iter().map(|o| o.map(|s| s.into()))
38+
.collect();
39+
40+
ExternEntry {
41+
locations,
42+
is_private_dep: false
4343
}
4444
}
4545

4646
fn optgroups() -> getopts::Options {
4747
let mut opts = getopts::Options::new();
48-
for group in super::rustc_optgroups() {
48+
for group in rustc_optgroups() {
4949
(group.apply)(&mut opts);
5050
}
5151
return opts;
@@ -63,7 +63,7 @@ fn test_switch_implies_cfg_test() {
6363
Ok(m) => m,
6464
Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
6565
};
66-
let registry = errors::registry::Registry::new(&[]);
66+
let registry = registry::Registry::new(&[]);
6767
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
6868
let sess = build_session(sessopts, None, registry);
6969
let cfg = build_configuration(&sess, to_crate_config(cfg));
@@ -81,7 +81,7 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
8181
Ok(m) => m,
8282
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
8383
};
84-
let registry = errors::registry::Registry::new(&[]);
84+
let registry = registry::Registry::new(&[]);
8585
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
8686
let sess = build_session(sessopts, None, registry);
8787
let cfg = build_configuration(&sess, to_crate_config(cfg));
@@ -95,7 +95,7 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
9595
fn test_can_print_warnings() {
9696
syntax::with_default_globals(|| {
9797
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
98-
let registry = errors::registry::Registry::new(&[]);
98+
let registry = registry::Registry::new(&[]);
9999
let (sessopts, _) = build_session_options_and_crate_config(&matches);
100100
let sess = build_session(sessopts, None, registry);
101101
assert!(!sess.diagnostic().can_emit_warnings());
@@ -105,15 +105,15 @@ fn test_can_print_warnings() {
105105
let matches = optgroups()
106106
.parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()])
107107
.unwrap();
108-
let registry = errors::registry::Registry::new(&[]);
108+
let registry = registry::Registry::new(&[]);
109109
let (sessopts, _) = build_session_options_and_crate_config(&matches);
110110
let sess = build_session(sessopts, None, registry);
111111
assert!(sess.diagnostic().can_emit_warnings());
112112
});
113113

114114
syntax::with_default_globals(|| {
115115
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
116-
let registry = errors::registry::Registry::new(&[]);
116+
let registry = registry::Registry::new(&[]);
117117
let (sessopts, _) = build_session_options_and_crate_config(&matches);
118118
let sess = build_session(sessopts, None, registry);
119119
assert!(sess.diagnostic().can_emit_warnings());
@@ -172,33 +172,33 @@ fn test_externs_tracking_hash_different_construction_order() {
172172
v1.externs = Externs::new(mk_map(vec![
173173
(
174174
String::from("a"),
175-
ExternEntry::new_public(vec![Some("b"), Some("c")])
175+
new_public_extern_entry(vec![Some("b"), Some("c")])
176176
),
177177
(
178178
String::from("d"),
179-
ExternEntry::new_public(vec![Some("e"), Some("f")])
179+
new_public_extern_entry(vec![Some("e"), Some("f")])
180180
),
181181
]));
182182

183183
v2.externs = Externs::new(mk_map(vec![
184184
(
185185
String::from("d"),
186-
ExternEntry::new_public(vec![Some("e"), Some("f")])
186+
new_public_extern_entry(vec![Some("e"), Some("f")])
187187
),
188188
(
189189
String::from("a"),
190-
ExternEntry::new_public(vec![Some("b"), Some("c")])
190+
new_public_extern_entry(vec![Some("b"), Some("c")])
191191
),
192192
]));
193193

194194
v3.externs = Externs::new(mk_map(vec![
195195
(
196196
String::from("a"),
197-
ExternEntry::new_public(vec![Some("b"), Some("c")])
197+
new_public_extern_entry(vec![Some("b"), Some("c")])
198198
),
199199
(
200200
String::from("d"),
201-
ExternEntry::new_public(vec![Some("f"), Some("e")])
201+
new_public_extern_entry(vec![Some("f"), Some("e")])
202202
),
203203
]));
204204

@@ -282,9 +282,9 @@ fn test_search_paths_tracking_hash_different_order() {
282282
let mut v3 = Options::default();
283283
let mut v4 = Options::default();
284284

285-
const JSON: super::ErrorOutputType = super::ErrorOutputType::Json {
285+
const JSON: ErrorOutputType = ErrorOutputType::Json {
286286
pretty: false,
287-
json_rendered: super::HumanReadableErrorType::Default(super::ColorConfig::Never),
287+
json_rendered: HumanReadableErrorType::Default(ColorConfig::Never),
288288
};
289289

290290
// Reference
@@ -455,7 +455,7 @@ fn test_codegen_options_tracking_hash() {
455455
opts.cg.codegen_units = Some(42);
456456
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
457457

458-
opts.cg.remark = super::Passes::Some(vec![String::from("pass1"), String::from("pass2")]);
458+
opts.cg.remark = Passes::Some(vec![String::from("pass1"), String::from("pass2")]);
459459
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
460460

461461
opts.cg.save_temps = true;

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
329329

330330
let config = interface::Config {
331331
opts: sessopts,
332-
crate_cfg: config::parse_cfgspecs(cfgs),
332+
crate_cfg: interface::parse_cfgspecs(cfgs),
333333
input,
334334
input_path: cpath,
335335
output_file: None,

0 commit comments

Comments
 (0)