Skip to content

Commit be023eb

Browse files
committed
move config.rs to libsyntax_expand
1 parent 5011ec7 commit be023eb

File tree

31 files changed

+259
-175
lines changed

31 files changed

+259
-175
lines changed

src/librustc/session/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use syntax::expand::allocator::AllocatorKind;
2727
use syntax::feature_gate::{self, AttributeType};
2828
use syntax::json::JsonEmitter;
2929
use syntax::source_map;
30-
use syntax::sess::ParseSess;
30+
use syntax::sess::{ParseSess, ProcessCfgMod};
3131
use syntax::symbol::Symbol;
3232
use syntax_pos::{MultiSpan, Span};
3333
use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
@@ -952,6 +952,7 @@ pub fn build_session(
952952
sopts: config::Options,
953953
local_crate_source_file: Option<PathBuf>,
954954
registry: errors::registry::Registry,
955+
process_cfg_mod: ProcessCfgMod,
955956
) -> Session {
956957
let file_path_mapping = sopts.file_path_mapping();
957958

@@ -962,6 +963,7 @@ pub fn build_session(
962963
Lrc::new(source_map::SourceMap::new(file_path_mapping)),
963964
DiagnosticOutput::Default,
964965
Default::default(),
966+
process_cfg_mod,
965967
)
966968
}
967969

@@ -1040,6 +1042,7 @@ pub fn build_session_with_source_map(
10401042
source_map: Lrc<source_map::SourceMap>,
10411043
diagnostics_output: DiagnosticOutput,
10421044
lint_caps: FxHashMap<lint::LintId, lint::Level>,
1045+
process_cfg_mod: ProcessCfgMod,
10431046
) -> Session {
10441047
// FIXME: This is not general enough to make the warning lint completely override
10451048
// normal diagnostic warnings, since the warning lint can also be denied and changed
@@ -1080,7 +1083,14 @@ pub fn build_session_with_source_map(
10801083
},
10811084
);
10821085

1083-
build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map, lint_caps)
1086+
build_session_(
1087+
sopts,
1088+
local_crate_source_file,
1089+
diagnostic_handler,
1090+
source_map,
1091+
lint_caps,
1092+
process_cfg_mod,
1093+
)
10841094
}
10851095

10861096
fn build_session_(
@@ -1089,6 +1099,7 @@ fn build_session_(
10891099
span_diagnostic: errors::Handler,
10901100
source_map: Lrc<source_map::SourceMap>,
10911101
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
1102+
process_cfg_mod: ProcessCfgMod,
10921103
) -> Session {
10931104
let self_profiler =
10941105
if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile {
@@ -1127,6 +1138,7 @@ fn build_session_(
11271138
let parse_sess = ParseSess::with_span_handler(
11281139
span_diagnostic,
11291140
source_map,
1141+
process_cfg_mod,
11301142
);
11311143
let sysroot = match &sopts.maybe_sysroot {
11321144
Some(sysroot) => sysroot.clone(),

src/librustc_interface/interface.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1414
use std::path::PathBuf;
1515
use std::result;
1616
use std::sync::{Arc, Mutex};
17-
use syntax::{self, parse};
1817
use syntax::ast::{self, MetaItemKind};
18+
use syntax::parse::new_parser_from_source_str;
1919
use syntax::token;
2020
use syntax::source_map::{FileName, FileLoader, SourceMap};
2121
use syntax::sess::ParseSess;
22+
use syntax_expand::config::process_configure_mod;
2223
use syntax_pos::edition;
2324

2425
pub type Result<T> = result::Result<T, ErrorReported>;
@@ -64,9 +65,9 @@ impl Compiler {
6465
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
6566
syntax::with_default_globals(move || {
6667
let cfg = cfgspecs.into_iter().map(|s| {
67-
let sess = ParseSess::with_silent_emitter();
68+
let sess = ParseSess::with_silent_emitter(process_configure_mod);
6869
let filename = FileName::cfg_spec_source_code(&s);
69-
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
70+
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
7071

7172
macro_rules! error {($reason: expr) => {
7273
early_error(ErrorOutputType::default(),

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub fn register_plugins<'a>(
181181
)
182182
});
183183

184-
let (krate, features) = syntax::config::features(
184+
let (krate, features) = syntax_expand::config::features(
185185
krate,
186186
&sess.parse_sess,
187187
sess.edition(),

src/librustc_interface/tests.rs

+24-35
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::session::config::{build_configuration, build_session_options, to_crat
88
use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
99
use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
1010
use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes};
11-
use rustc::session::build_session;
11+
use rustc::session::{build_session, Session};
1212
use rustc::session::search_paths::SearchPath;
1313
use std::collections::{BTreeMap, BTreeSet};
1414
use std::iter::FromIterator;
@@ -17,16 +17,23 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
1717
use syntax::symbol::sym;
1818
use syntax::edition::{Edition, DEFAULT_EDITION};
1919
use syntax;
20+
use syntax_expand::config::process_configure_mod;
2021
use rustc_data_structures::fx::FxHashSet;
2122
use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry};
2223

23-
pub fn build_session_options_and_crate_config(
24-
matches: &getopts::Matches,
25-
) -> (Options, FxHashSet<(String, Option<String>)>) {
26-
(
27-
build_session_options(matches),
28-
parse_cfgspecs(matches.opt_strs("cfg")),
29-
)
24+
type CfgSpecs = FxHashSet<(String, Option<String>)>;
25+
26+
fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options, CfgSpecs) {
27+
let sessopts = build_session_options(&matches);
28+
let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
29+
(sessopts, cfg)
30+
}
31+
32+
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
33+
let registry = registry::Registry::new(&[]);
34+
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
35+
let sess = build_session(sessopts, None, registry, process_configure_mod);
36+
(sess, cfg)
3037
}
3138

3239
fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
@@ -59,31 +66,19 @@ fn mk_map<K: Ord, V>(entries: Vec<(K, V)>) -> BTreeMap<K, V> {
5966
#[test]
6067
fn test_switch_implies_cfg_test() {
6168
syntax::with_default_globals(|| {
62-
let matches = &match optgroups().parse(&["--test".to_string()]) {
63-
Ok(m) => m,
64-
Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
65-
};
66-
let registry = registry::Registry::new(&[]);
67-
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
68-
let sess = build_session(sessopts, None, registry);
69+
let matches = optgroups().parse(&["--test".to_string()]).unwrap();
70+
let (sess, cfg) = mk_session(matches);
6971
let cfg = build_configuration(&sess, to_crate_config(cfg));
7072
assert!(cfg.contains(&(sym::test, None)));
7173
});
7274
}
7375

74-
// When the user supplies --test and --cfg test, don't implicitly add
75-
// another --cfg test
76+
// When the user supplies --test and --cfg test, don't implicitly add another --cfg test
7677
#[test]
7778
fn test_switch_implies_cfg_test_unless_cfg_test() {
7879
syntax::with_default_globals(|| {
79-
let matches = &match optgroups().parse(&["--test".to_string(),
80-
"--cfg=test".to_string()]) {
81-
Ok(m) => m,
82-
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
83-
};
84-
let registry = registry::Registry::new(&[]);
85-
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
86-
let sess = build_session(sessopts, None, registry);
80+
let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap();
81+
let (sess, cfg) = mk_session(matches);
8782
let cfg = build_configuration(&sess, to_crate_config(cfg));
8883
let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
8984
assert!(test_items.next().is_some());
@@ -95,27 +90,21 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
9590
fn test_can_print_warnings() {
9691
syntax::with_default_globals(|| {
9792
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
98-
let registry = registry::Registry::new(&[]);
99-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
100-
let sess = build_session(sessopts, None, registry);
93+
let (sess, _) = mk_session(matches);
10194
assert!(!sess.diagnostic().can_emit_warnings());
10295
});
10396

10497
syntax::with_default_globals(|| {
10598
let matches = optgroups()
10699
.parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()])
107100
.unwrap();
108-
let registry = registry::Registry::new(&[]);
109-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
110-
let sess = build_session(sessopts, None, registry);
101+
let (sess, _) = mk_session(matches);
111102
assert!(sess.diagnostic().can_emit_warnings());
112103
});
113104

114105
syntax::with_default_globals(|| {
115106
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
116-
let registry = registry::Registry::new(&[]);
117-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
118-
let sess = build_session(sessopts, None, registry);
107+
let (sess, _) = mk_session(matches);
119108
assert!(sess.diagnostic().can_emit_warnings());
120109
});
121110
}
@@ -704,6 +693,6 @@ fn test_edition_parsing() {
704693
let matches = optgroups()
705694
.parse(&["--edition=2018".to_string()])
706695
.unwrap();
707-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
696+
let (sessopts, _) = build_session_options_and_crate_config(matches);
708697
assert!(sessopts.edition == Edition::Edition2018)
709698
}

src/librustc_interface/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use syntax::util::lev_distance::find_best_match_for_name;
3636
use syntax::source_map::{FileLoader, RealFileLoader, SourceMap};
3737
use syntax::symbol::{Symbol, sym};
3838
use syntax::{self, ast, attr};
39+
use syntax_expand::config::process_configure_mod;
3940
use syntax_pos::edition::Edition;
4041
#[cfg(not(parallel_compiler))]
4142
use std::{thread, panic};
@@ -103,6 +104,7 @@ pub fn create_session(
103104
source_map.clone(),
104105
diagnostic_output,
105106
lint_caps,
107+
process_configure_mod,
106108
);
107109

108110
let codegen_backend = get_codegen_backend(&sess);

src/librustdoc/html/highlight.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::parse::lexer;
1616
use syntax::token::{self, Token};
1717
use syntax::sess::ParseSess;
1818
use syntax::symbol::{kw, sym};
19+
use syntax_expand::config::process_configure_mod;
1920
use syntax_pos::{Span, FileName};
2021

2122
/// Highlights `src`, returning the HTML output.
@@ -33,7 +34,7 @@ pub fn render_with_highlighting(
3334
class, tooltip).unwrap();
3435
}
3536

36-
let sess = ParseSess::with_silent_emitter();
37+
let sess = ParseSess::with_silent_emitter(process_configure_mod);
3738
let fm = sess.source_map().new_source_file(
3839
FileName::Custom(String::from("rustdoc-highlighting")),
3940
src.to_owned(),

src/librustdoc/passes/check_code_block_syntax.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use syntax::parse::lexer::{StringReader as Lexer};
33
use syntax::token;
44
use syntax::sess::ParseSess;
55
use syntax::source_map::FilePathMapping;
6+
use syntax_expand::config::process_configure_mod;
67
use syntax_pos::{InnerSpan, FileName};
78

89
use crate::clean;
@@ -27,7 +28,7 @@ struct SyntaxChecker<'a, 'tcx> {
2728

2829
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
2930
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
30-
let sess = ParseSess::new(FilePathMapping::empty());
31+
let sess = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
3132
let source_file = sess.source_map().new_source_file(
3233
FileName::Custom(String::from("doctest")),
3334
dox[code_block.code].to_owned(),

src/librustdoc/test.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::path::PathBuf;
1717
use std::process::{self, Command, Stdio};
1818
use std::str;
1919
use syntax::symbol::sym;
20+
use syntax_expand::config::process_configure_mod;
2021
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName};
2122
use tempfile::Builder as TempFileBuilder;
2223
use testing;
@@ -411,7 +412,7 @@ pub fn make_test(s: &str,
411412
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
412413
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
413414
let handler = Handler::with_emitter(false, None, box emitter);
414-
let sess = ParseSess::with_span_handler(handler, cm);
415+
let sess = ParseSess::with_span_handler(handler, cm, process_configure_mod);
415416

416417
let mut found_main = false;
417418
let mut found_extern_crate = cratename.is_none();

src/libsyntax/attr/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,12 @@ crate fn mk_attr_id() -> AttrId {
363363
}
364364

365365
pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute {
366+
mk_attr_from_item(style, AttrItem { path, tokens }, span)
367+
}
368+
369+
pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attribute {
366370
Attribute {
367-
kind: AttrKind::Normal(AttrItem { path, tokens }),
371+
kind: AttrKind::Normal(item),
368372
id: mk_attr_id(),
369373
style,
370374
span,

src/libsyntax/json/tests.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::*;
22

33
use crate::json::JsonEmitter;
44
use crate::source_map::{FilePathMapping, SourceMap};
5-
use crate::tests::Shared;
65
use crate::with_default_globals;
76

87
use errors::emitter::{ColorConfig, HumanReadableErrorType};
@@ -27,6 +26,20 @@ struct SpanTestData {
2726
pub column_end: u32,
2827
}
2928

29+
struct Shared<T> {
30+
data: Arc<Mutex<T>>,
31+
}
32+
33+
impl<T: Write> Write for Shared<T> {
34+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
35+
self.data.lock().unwrap().write(buf)
36+
}
37+
38+
fn flush(&mut self) -> io::Result<()> {
39+
self.data.lock().unwrap().flush()
40+
}
41+
}
42+
3043
/// Test the span yields correct positions in JSON.
3144
fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
3245
let expected_output = TestData { spans: vec![expected_output] };

src/libsyntax/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ pub use rustc_data_structures::thin_vec::ThinVec;
2626
use ast::AttrId;
2727
use syntax_pos::edition::Edition;
2828

29-
#[cfg(test)]
30-
mod tests;
31-
3229
pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
3330

3431
#[macro_export]
@@ -100,7 +97,6 @@ pub mod ast;
10097
pub mod attr;
10198
pub mod expand;
10299
pub mod source_map;
103-
#[macro_use] pub mod config;
104100
pub mod entry;
105101
pub mod feature_gate;
106102
pub mod mut_visit;

src/libsyntax/mut_visit.rs

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ use rustc_data_structures::sync::Lrc;
2222
use std::ops::DerefMut;
2323
use std::{panic, process, ptr};
2424

25-
#[cfg(test)]
26-
mod tests;
27-
2825
pub trait ExpectOne<A: Array> {
2926
fn expect_one(self, err: &'static str) -> A::Item;
3027
}

src/libsyntax/parse/lexer/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ use std::convert::TryInto;
1313
use rustc_data_structures::sync::Lrc;
1414
use log::debug;
1515

16-
#[cfg(test)]
17-
mod tests;
18-
1916
mod tokentrees;
2017
mod unicode_chars;
2118
mod unescape_error_reporting;
@@ -35,7 +32,8 @@ pub struct StringReader<'a> {
3532
/// Initial position, read-only.
3633
start_pos: BytePos,
3734
/// The absolute offset within the source_map of the current character.
38-
pos: BytePos,
35+
// FIXME(#64197): `pub` is needed by tests for now.
36+
pub pos: BytePos,
3937
/// Stop reading src at this index.
4038
end_src_index: usize,
4139
/// Source text to tokenize.

src/libsyntax/parse/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use std::str;
1717

1818
use log::info;
1919

20-
#[cfg(test)]
21-
mod tests;
20+
pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
2221

2322
#[macro_use]
2423
pub mod parser;

src/libsyntax/parse/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a> Parser<'a> {
268268
}
269269

270270
/// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited.
271-
crate fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> {
271+
pub fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> {
272272
self.expect(&token::OpenDelim(token::Paren))?;
273273

274274
let cfg_predicate = self.parse_meta_item()?;

0 commit comments

Comments
 (0)