1
Fork 0

move config.rs to libsyntax_expand

This commit is contained in:
Mazdak Farrokhzad 2019-10-10 10:26:10 +02:00
parent 5011ec7fed
commit be023ebe85
31 changed files with 259 additions and 175 deletions

View file

@ -27,7 +27,7 @@ use syntax::expand::allocator::AllocatorKind;
use syntax::feature_gate::{self, AttributeType}; use syntax::feature_gate::{self, AttributeType};
use syntax::json::JsonEmitter; use syntax::json::JsonEmitter;
use syntax::source_map; use syntax::source_map;
use syntax::sess::ParseSess; use syntax::sess::{ParseSess, ProcessCfgMod};
use syntax::symbol::Symbol; use syntax::symbol::Symbol;
use syntax_pos::{MultiSpan, Span}; use syntax_pos::{MultiSpan, Span};
use crate::util::profiling::{SelfProfiler, SelfProfilerRef}; use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
@ -952,6 +952,7 @@ pub fn build_session(
sopts: config::Options, sopts: config::Options,
local_crate_source_file: Option<PathBuf>, local_crate_source_file: Option<PathBuf>,
registry: errors::registry::Registry, registry: errors::registry::Registry,
process_cfg_mod: ProcessCfgMod,
) -> Session { ) -> Session {
let file_path_mapping = sopts.file_path_mapping(); let file_path_mapping = sopts.file_path_mapping();
@ -962,6 +963,7 @@ pub fn build_session(
Lrc::new(source_map::SourceMap::new(file_path_mapping)), Lrc::new(source_map::SourceMap::new(file_path_mapping)),
DiagnosticOutput::Default, DiagnosticOutput::Default,
Default::default(), Default::default(),
process_cfg_mod,
) )
} }
@ -1040,6 +1042,7 @@ pub fn build_session_with_source_map(
source_map: Lrc<source_map::SourceMap>, source_map: Lrc<source_map::SourceMap>,
diagnostics_output: DiagnosticOutput, diagnostics_output: DiagnosticOutput,
lint_caps: FxHashMap<lint::LintId, lint::Level>, lint_caps: FxHashMap<lint::LintId, lint::Level>,
process_cfg_mod: ProcessCfgMod,
) -> Session { ) -> Session {
// FIXME: This is not general enough to make the warning lint completely override // FIXME: This is not general enough to make the warning lint completely override
// normal diagnostic warnings, since the warning lint can also be denied and changed // normal diagnostic warnings, since the warning lint can also be denied and changed
@ -1080,7 +1083,14 @@ pub fn build_session_with_source_map(
}, },
); );
build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map, lint_caps) build_session_(
sopts,
local_crate_source_file,
diagnostic_handler,
source_map,
lint_caps,
process_cfg_mod,
)
} }
fn build_session_( fn build_session_(
@ -1089,6 +1099,7 @@ fn build_session_(
span_diagnostic: errors::Handler, span_diagnostic: errors::Handler,
source_map: Lrc<source_map::SourceMap>, source_map: Lrc<source_map::SourceMap>,
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>, driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
process_cfg_mod: ProcessCfgMod,
) -> Session { ) -> Session {
let self_profiler = let self_profiler =
if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile { if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile {
@ -1127,6 +1138,7 @@ fn build_session_(
let parse_sess = ParseSess::with_span_handler( let parse_sess = ParseSess::with_span_handler(
span_diagnostic, span_diagnostic,
source_map, source_map,
process_cfg_mod,
); );
let sysroot = match &sopts.maybe_sysroot { let sysroot = match &sopts.maybe_sysroot {
Some(sysroot) => sysroot.clone(), Some(sysroot) => sysroot.clone(),

View file

@ -14,11 +14,12 @@ use rustc_data_structures::fx::{FxHashSet, FxHashMap};
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::{self, parse};
use syntax::ast::{self, MetaItemKind}; use syntax::ast::{self, MetaItemKind};
use syntax::parse::new_parser_from_source_str;
use syntax::token; use syntax::token;
use syntax::source_map::{FileName, FileLoader, SourceMap}; use syntax::source_map::{FileName, FileLoader, SourceMap};
use syntax::sess::ParseSess; use syntax::sess::ParseSess;
use syntax_expand::config::process_configure_mod;
use syntax_pos::edition; use syntax_pos::edition;
pub type Result<T> = result::Result<T, ErrorReported>; pub type Result<T> = result::Result<T, ErrorReported>;
@ -64,9 +65,9 @@ impl Compiler {
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> { pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
syntax::with_default_globals(move || { syntax::with_default_globals(move || {
let cfg = cfgspecs.into_iter().map(|s| { let cfg = cfgspecs.into_iter().map(|s| {
let sess = ParseSess::with_silent_emitter(); let sess = ParseSess::with_silent_emitter(process_configure_mod);
let filename = FileName::cfg_spec_source_code(&s); let filename = FileName::cfg_spec_source_code(&s);
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string()); let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
macro_rules! error {($reason: expr) => { macro_rules! error {($reason: expr) => {
early_error(ErrorOutputType::default(), early_error(ErrorOutputType::default(),

View file

@ -181,7 +181,7 @@ pub fn register_plugins<'a>(
) )
}); });
let (krate, features) = syntax::config::features( let (krate, features) = syntax_expand::config::features(
krate, krate,
&sess.parse_sess, &sess.parse_sess,
sess.edition(), sess.edition(),

View file

@ -8,7 +8,7 @@ use rustc::session::config::{build_configuration, build_session_options, to_crat
use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry}; use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion}; use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes}; use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes};
use rustc::session::build_session; use rustc::session::{build_session, Session};
use rustc::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;
@ -17,16 +17,23 @@ 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 syntax_expand::config::process_configure_mod;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry}; use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry};
pub fn build_session_options_and_crate_config( type CfgSpecs = FxHashSet<(String, Option<String>)>;
matches: &getopts::Matches,
) -> (Options, FxHashSet<(String, Option<String>)>) { fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options, CfgSpecs) {
( let sessopts = build_session_options(&matches);
build_session_options(matches), let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
parse_cfgspecs(matches.opt_strs("cfg")), (sessopts, cfg)
) }
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry, process_configure_mod);
(sess, cfg)
} }
fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry 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> {
#[test] #[test]
fn test_switch_implies_cfg_test() { fn test_switch_implies_cfg_test() {
syntax::with_default_globals(|| { syntax::with_default_globals(|| {
let matches = &match optgroups().parse(&["--test".to_string()]) { let matches = optgroups().parse(&["--test".to_string()]).unwrap();
Ok(m) => m, let (sess, cfg) = mk_session(matches);
Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
};
let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
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));
assert!(cfg.contains(&(sym::test, None))); assert!(cfg.contains(&(sym::test, None)));
}); });
} }
// When the user supplies --test and --cfg test, don't implicitly add // When the user supplies --test and --cfg test, don't implicitly add another --cfg test
// another --cfg test
#[test] #[test]
fn test_switch_implies_cfg_test_unless_cfg_test() { fn test_switch_implies_cfg_test_unless_cfg_test() {
syntax::with_default_globals(|| { syntax::with_default_globals(|| {
let matches = &match optgroups().parse(&["--test".to_string(), let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap();
"--cfg=test".to_string()]) { let (sess, cfg) = mk_session(matches);
Ok(m) => m,
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
};
let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
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));
let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test); let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
assert!(test_items.next().is_some()); assert!(test_items.next().is_some());
@ -95,9 +90,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 = registry::Registry::new(&[]); let (sess, _) = mk_session(matches);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry);
assert!(!sess.diagnostic().can_emit_warnings()); assert!(!sess.diagnostic().can_emit_warnings());
}); });
@ -105,17 +98,13 @@ 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 = registry::Registry::new(&[]); let (sess, _) = mk_session(matches);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry);
assert!(sess.diagnostic().can_emit_warnings()); assert!(sess.diagnostic().can_emit_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 = registry::Registry::new(&[]); let (sess, _) = mk_session(matches);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry);
assert!(sess.diagnostic().can_emit_warnings()); assert!(sess.diagnostic().can_emit_warnings());
}); });
} }
@ -704,6 +693,6 @@ fn test_edition_parsing() {
let matches = optgroups() let matches = optgroups()
.parse(&["--edition=2018".to_string()]) .parse(&["--edition=2018".to_string()])
.unwrap(); .unwrap();
let (sessopts, _) = build_session_options_and_crate_config(&matches); let (sessopts, _) = build_session_options_and_crate_config(matches);
assert!(sessopts.edition == Edition::Edition2018) assert!(sessopts.edition == Edition::Edition2018)
} }

View file

@ -36,6 +36,7 @@ use syntax::util::lev_distance::find_best_match_for_name;
use syntax::source_map::{FileLoader, RealFileLoader, SourceMap}; use syntax::source_map::{FileLoader, RealFileLoader, SourceMap};
use syntax::symbol::{Symbol, sym}; use syntax::symbol::{Symbol, sym};
use syntax::{self, ast, attr}; use syntax::{self, ast, attr};
use syntax_expand::config::process_configure_mod;
use syntax_pos::edition::Edition; use syntax_pos::edition::Edition;
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
use std::{thread, panic}; use std::{thread, panic};
@ -103,6 +104,7 @@ pub fn create_session(
source_map.clone(), source_map.clone(),
diagnostic_output, diagnostic_output,
lint_caps, lint_caps,
process_configure_mod,
); );
let codegen_backend = get_codegen_backend(&sess); let codegen_backend = get_codegen_backend(&sess);

View file

@ -16,6 +16,7 @@ use syntax::parse::lexer;
use syntax::token::{self, Token}; use syntax::token::{self, Token};
use syntax::sess::ParseSess; use syntax::sess::ParseSess;
use syntax::symbol::{kw, sym}; use syntax::symbol::{kw, sym};
use syntax_expand::config::process_configure_mod;
use syntax_pos::{Span, FileName}; use syntax_pos::{Span, FileName};
/// Highlights `src`, returning the HTML output. /// Highlights `src`, returning the HTML output.
@ -33,7 +34,7 @@ pub fn render_with_highlighting(
class, tooltip).unwrap(); class, tooltip).unwrap();
} }
let sess = ParseSess::with_silent_emitter(); let sess = ParseSess::with_silent_emitter(process_configure_mod);
let fm = sess.source_map().new_source_file( let fm = sess.source_map().new_source_file(
FileName::Custom(String::from("rustdoc-highlighting")), FileName::Custom(String::from("rustdoc-highlighting")),
src.to_owned(), src.to_owned(),

View file

@ -3,6 +3,7 @@ use syntax::parse::lexer::{StringReader as Lexer};
use syntax::token; use syntax::token;
use syntax::sess::ParseSess; use syntax::sess::ParseSess;
use syntax::source_map::FilePathMapping; use syntax::source_map::FilePathMapping;
use syntax_expand::config::process_configure_mod;
use syntax_pos::{InnerSpan, FileName}; use syntax_pos::{InnerSpan, FileName};
use crate::clean; use crate::clean;
@ -27,7 +28,7 @@ struct SyntaxChecker<'a, 'tcx> {
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) { fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
let sess = ParseSess::new(FilePathMapping::empty()); let sess = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
let source_file = sess.source_map().new_source_file( let source_file = sess.source_map().new_source_file(
FileName::Custom(String::from("doctest")), FileName::Custom(String::from("doctest")),
dox[code_block.code].to_owned(), dox[code_block.code].to_owned(),

View file

@ -17,6 +17,7 @@ use std::path::PathBuf;
use std::process::{self, Command, Stdio}; use std::process::{self, Command, Stdio};
use std::str; use std::str;
use syntax::symbol::sym; use syntax::symbol::sym;
use syntax_expand::config::process_configure_mod;
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName}; use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName};
use tempfile::Builder as TempFileBuilder; use tempfile::Builder as TempFileBuilder;
use testing; use testing;
@ -411,7 +412,7 @@ pub fn make_test(s: &str,
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false); let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
let handler = Handler::with_emitter(false, None, box emitter); let handler = Handler::with_emitter(false, None, box emitter);
let sess = ParseSess::with_span_handler(handler, cm); let sess = ParseSess::with_span_handler(handler, cm, process_configure_mod);
let mut found_main = false; let mut found_main = false;
let mut found_extern_crate = cratename.is_none(); let mut found_extern_crate = cratename.is_none();

View file

@ -363,8 +363,12 @@ crate fn mk_attr_id() -> AttrId {
} }
pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute { pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute {
mk_attr_from_item(style, AttrItem { path, tokens }, span)
}
pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attribute {
Attribute { Attribute {
kind: AttrKind::Normal(AttrItem { path, tokens }), kind: AttrKind::Normal(item),
id: mk_attr_id(), id: mk_attr_id(),
style, style,
span, span,

View file

@ -2,7 +2,6 @@ use super::*;
use crate::json::JsonEmitter; use crate::json::JsonEmitter;
use crate::source_map::{FilePathMapping, SourceMap}; use crate::source_map::{FilePathMapping, SourceMap};
use crate::tests::Shared;
use crate::with_default_globals; use crate::with_default_globals;
use errors::emitter::{ColorConfig, HumanReadableErrorType}; use errors::emitter::{ColorConfig, HumanReadableErrorType};
@ -27,6 +26,20 @@ struct SpanTestData {
pub column_end: u32, pub column_end: u32,
} }
struct Shared<T> {
data: Arc<Mutex<T>>,
}
impl<T: Write> Write for Shared<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.data.lock().unwrap().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.data.lock().unwrap().flush()
}
}
/// Test the span yields correct positions in JSON. /// Test the span yields correct positions in JSON.
fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
let expected_output = TestData { spans: vec![expected_output] }; let expected_output = TestData { spans: vec![expected_output] };

View file

@ -26,9 +26,6 @@ pub use rustc_data_structures::thin_vec::ThinVec;
use ast::AttrId; use ast::AttrId;
use syntax_pos::edition::Edition; use syntax_pos::edition::Edition;
#[cfg(test)]
mod tests;
pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments"); pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
#[macro_export] #[macro_export]
@ -100,7 +97,6 @@ pub mod ast;
pub mod attr; pub mod attr;
pub mod expand; pub mod expand;
pub mod source_map; pub mod source_map;
#[macro_use] pub mod config;
pub mod entry; pub mod entry;
pub mod feature_gate; pub mod feature_gate;
pub mod mut_visit; pub mod mut_visit;

View file

@ -22,9 +22,6 @@ use rustc_data_structures::sync::Lrc;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::{panic, process, ptr}; use std::{panic, process, ptr};
#[cfg(test)]
mod tests;
pub trait ExpectOne<A: Array> { pub trait ExpectOne<A: Array> {
fn expect_one(self, err: &'static str) -> A::Item; fn expect_one(self, err: &'static str) -> A::Item;
} }

View file

@ -13,9 +13,6 @@ use std::convert::TryInto;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use log::debug; use log::debug;
#[cfg(test)]
mod tests;
mod tokentrees; mod tokentrees;
mod unicode_chars; mod unicode_chars;
mod unescape_error_reporting; mod unescape_error_reporting;
@ -35,7 +32,8 @@ pub struct StringReader<'a> {
/// Initial position, read-only. /// Initial position, read-only.
start_pos: BytePos, start_pos: BytePos,
/// The absolute offset within the source_map of the current character. /// The absolute offset within the source_map of the current character.
pos: BytePos, // FIXME(#64197): `pub` is needed by tests for now.
pub pos: BytePos,
/// Stop reading src at this index. /// Stop reading src at this index.
end_src_index: usize, end_src_index: usize,
/// Source text to tokenize. /// Source text to tokenize.

View file

@ -17,8 +17,7 @@ use std::str;
use log::info; use log::info;
#[cfg(test)] pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
mod tests;
#[macro_use] #[macro_use]
pub mod parser; pub mod parser;

View file

@ -268,7 +268,7 @@ impl<'a> Parser<'a> {
} }
/// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited. /// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited.
crate fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> { pub fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> {
self.expect(&token::OpenDelim(token::Paren))?; self.expect(&token::OpenDelim(token::Paren))?;
let cfg_predicate = self.parse_meta_item()?; let cfg_predicate = self.parse_meta_item()?;

View file

@ -7,8 +7,8 @@ use crate::ast::{self, Ident, Attribute, ItemKind, Mod, Crate};
use crate::parse::{new_sub_parser_from_file, DirectoryOwnership}; use crate::parse::{new_sub_parser_from_file, DirectoryOwnership};
use crate::token::{self, TokenKind}; use crate::token::{self, TokenKind};
use crate::source_map::{SourceMap, Span, DUMMY_SP, FileName}; use crate::source_map::{SourceMap, Span, DUMMY_SP, FileName};
use crate::symbol::sym;
use syntax_pos::symbol::sym;
use errors::PResult; use errors::PResult;
use std::path::{self, Path, PathBuf}; use std::path::{self, Path, PathBuf};
@ -39,17 +39,12 @@ impl<'a> Parser<'a> {
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item. /// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
pub(super) fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> { pub(super) fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
let (in_cfg, outer_attrs) = { // HACK(Centril): See documentation on `ParseSess::process_cfg_mod`.
// FIXME(Centril): This results in a cycle between config and parsing. let (in_cfg, outer_attrs) = (self.sess.process_cfg_mod)(
// Consider using dynamic dispatch via `self.sess` to disentangle the knot. self.sess,
let mut strip_unconfigured = crate::config::StripUnconfigured { self.cfg_mods,
sess: self.sess, outer_attrs,
features: None, // Don't perform gated feature checking. );
};
let mut outer_attrs = outer_attrs.to_owned();
strip_unconfigured.process_cfg_attrs(&mut outer_attrs);
(!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs)
};
let id_span = self.token.span; let id_span = self.token.span;
let id = self.parse_ident()?; let id = self.parse_ident()?;

View file

@ -939,8 +939,11 @@ impl<'a> State<'a> {
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e), |e| e.span) self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e), |e| e.span)
} }
crate fn print_mod(&mut self, _mod: &ast::Mod, pub fn print_mod(
attrs: &[ast::Attribute]) { &mut self,
_mod: &ast::Mod,
attrs: &[ast::Attribute],
) {
self.print_inner_attributes(attrs); self.print_inner_attributes(attrs);
for item in &_mod.items { for item in &_mod.items {
self.print_item(item); self.print_item(item);

View file

@ -1,7 +1,7 @@
//! Contains `ParseSess` which holds state living beyond what one `Parser` might. //! Contains `ParseSess` which holds state living beyond what one `Parser` might.
//! It also serves as an input to the parser itself. //! It also serves as an input to the parser itself.
use crate::ast::{CrateConfig, NodeId}; use crate::ast::{CrateConfig, NodeId, Attribute};
use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId}; use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
use crate::source_map::{SourceMap, FilePathMapping}; use crate::source_map::{SourceMap, FilePathMapping};
use crate::feature_gate::UnstableFeatures; use crate::feature_gate::UnstableFeatures;
@ -89,10 +89,22 @@ pub struct ParseSess {
pub gated_spans: GatedSpans, pub gated_spans: GatedSpans,
/// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors. /// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors.
pub reached_eof: Lock<bool>, pub reached_eof: Lock<bool>,
/// Process the potential `cfg` attributes on a module.
/// Also determine if the module should be included in this configuration.
///
/// HACK(Centril): This is used to break a cyclic dependency between
/// the parser and cfg-stripping as defined in `syntax_expand::config`.
/// The dependency edge from the parser comes from `parse_item_mod`.
/// A principled solution to this hack would be to implement [#64197].
///
/// [#64197]: https://github.com/rust-lang/rust/issues/64197
pub process_cfg_mod: ProcessCfgMod,
} }
pub type ProcessCfgMod = fn(&ParseSess, bool, &[Attribute]) -> (bool, Vec<Attribute>);
impl ParseSess { impl ParseSess {
pub fn new(file_path_mapping: FilePathMapping) -> Self { pub fn new(file_path_mapping: FilePathMapping, process_cfg_mod: ProcessCfgMod) -> Self {
let cm = Lrc::new(SourceMap::new(file_path_mapping)); let cm = Lrc::new(SourceMap::new(file_path_mapping));
let handler = Handler::with_tty_emitter( let handler = Handler::with_tty_emitter(
ColorConfig::Auto, ColorConfig::Auto,
@ -100,12 +112,17 @@ impl ParseSess {
None, None,
Some(cm.clone()), Some(cm.clone()),
); );
ParseSess::with_span_handler(handler, cm) ParseSess::with_span_handler(handler, cm, process_cfg_mod)
} }
pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self { pub fn with_span_handler(
handler: Handler,
source_map: Lrc<SourceMap>,
process_cfg_mod: ProcessCfgMod,
) -> Self {
Self { Self {
span_diagnostic: handler, span_diagnostic: handler,
process_cfg_mod,
unstable_features: UnstableFeatures::from_environment(), unstable_features: UnstableFeatures::from_environment(),
config: FxHashSet::default(), config: FxHashSet::default(),
edition: ExpnId::root().expn_data().edition, edition: ExpnId::root().expn_data().edition,
@ -121,10 +138,10 @@ impl ParseSess {
} }
} }
pub fn with_silent_emitter() -> Self { pub fn with_silent_emitter(process_cfg_mod: ProcessCfgMod) -> Self {
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter)); let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
ParseSess::with_span_handler(handler, cm) ParseSess::with_span_handler(handler, cm, process_cfg_mod)
} }
#[inline] #[inline]

View file

@ -23,9 +23,6 @@ use smallvec::{SmallVec, smallvec};
use std::{iter, mem}; use std::{iter, mem};
#[cfg(test)]
mod tests;
/// When the main rust parser encounters a syntax-extension invocation, it /// When the main rust parser encounters a syntax-extension invocation, it
/// parses the arguments to the invocation as a token-tree. This is a very /// parses the arguments to the invocation as a token-tree. This is a very
/// loose structure, such that all sorts of different AST-fragments can /// loose structure, such that all sorts of different AST-fragments can
@ -218,7 +215,7 @@ impl TokenStream {
self.0.len() self.0.len()
} }
pub(crate) fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream { pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
match streams.len() { match streams.len() {
0 => TokenStream::default(), 0 => TokenStream::default(),
1 => streams.pop().unwrap(), 1 => streams.pop().unwrap(),

View file

@ -47,7 +47,8 @@ crate fn is_block_doc_comment(s: &str) -> bool {
res res
} }
crate fn is_doc_comment(s: &str) -> bool { // FIXME(#64197): Try to privatize this again.
pub fn is_doc_comment(s: &str) -> bool {
(s.starts_with("///") && is_line_doc_comment(s)) || s.starts_with("//!") || (s.starts_with("///") && is_line_doc_comment(s)) || s.starts_with("//!") ||
(s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!") (s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!")
} }

View file

@ -1,20 +1,20 @@
use crate::attr::HasAttrs; use syntax::attr::HasAttrs;
use crate::feature_gate::{ use syntax::feature_gate::{
feature_err, feature_err,
EXPLAIN_STMT_ATTR_SYNTAX, EXPLAIN_STMT_ATTR_SYNTAX,
Features, Features,
get_features, get_features,
GateIssue, GateIssue,
}; };
use crate::attr; use syntax::attr;
use crate::ast; use syntax::ast;
use crate::edition::Edition; use syntax::edition::Edition;
use crate::mut_visit::*; use syntax::mut_visit::*;
use crate::parse::{self, validate_attr}; use syntax::parse::{self, validate_attr};
use crate::ptr::P; use syntax::ptr::P;
use crate::sess::ParseSess; use syntax::sess::ParseSess;
use crate::symbol::sym; use syntax::symbol::sym;
use crate::util::map_in_place::MapInPlace; use syntax::util::map_in_place::MapInPlace;
use errors::Applicability; use errors::Applicability;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -135,12 +135,11 @@ impl<'a> StripUnconfigured<'a> {
// `cfg_attr` inside of another `cfg_attr`. E.g. // `cfg_attr` inside of another `cfg_attr`. E.g.
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`. // `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
expanded_attrs.into_iter() expanded_attrs.into_iter()
.flat_map(|(item, span)| self.process_cfg_attr(ast::Attribute { .flat_map(|(item, span)| self.process_cfg_attr(attr::mk_attr_from_item(
kind: ast::AttrKind::Normal(item), attr.style,
id: attr::mk_attr_id(), item,
style: attr.style,
span, span,
})) )))
.collect() .collect()
} else { } else {
vec![] vec![]
@ -148,7 +147,7 @@ impl<'a> StripUnconfigured<'a> {
} }
/// Determines if a node with the given attributes should be included in this configuration. /// Determines if a node with the given attributes should be included in this configuration.
pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool { pub fn in_cfg(&self, attrs: &[ast::Attribute]) -> bool {
attrs.iter().all(|attr| { attrs.iter().all(|attr| {
if !is_cfg(attr) { if !is_cfg(attr) {
return true; return true;
@ -350,3 +349,17 @@ impl<'a> MutVisitor for StripUnconfigured<'a> {
fn is_cfg(attr: &ast::Attribute) -> bool { fn is_cfg(attr: &ast::Attribute) -> bool {
attr.check_name(sym::cfg) attr.check_name(sym::cfg)
} }
/// Process the potential `cfg` attributes on a module.
/// Also determine if the module should be included in this configuration.
pub fn process_configure_mod(
sess: &ParseSess,
cfg_mods: bool,
attrs: &[ast::Attribute],
) -> (bool, Vec<ast::Attribute>) {
// Don't perform gated feature checking.
let mut strip_unconfigured = StripUnconfigured { sess, features: None };
let mut attrs = attrs.to_owned();
strip_unconfigured.process_cfg_attrs(&mut attrs);
(!cfg_mods || strip_unconfigured.in_cfg(&attrs), attrs)
}

View file

@ -3,13 +3,13 @@ use crate::proc_macro::{collect_derives, MarkAttrs};
use crate::hygiene::{ExpnId, SyntaxContext, ExpnData, ExpnKind}; use crate::hygiene::{ExpnId, SyntaxContext, ExpnData, ExpnKind};
use crate::mbe::macro_rules::annotate_err_with_kind; use crate::mbe::macro_rules::annotate_err_with_kind;
use crate::placeholders::{placeholder, PlaceholderExpander}; use crate::placeholders::{placeholder, PlaceholderExpander};
use crate::config::StripUnconfigured;
use crate::configure;
use syntax::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path}; use syntax::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path};
use syntax::ast::{MacStmtStyle, StmtKind, ItemKind}; use syntax::ast::{MacStmtStyle, StmtKind, ItemKind};
use syntax::attr::{self, HasAttrs}; use syntax::attr::{self, HasAttrs};
use syntax::source_map::respan; use syntax::source_map::respan;
use syntax::configure;
use syntax::config::StripUnconfigured;
use syntax::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err}; use syntax::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
use syntax::mut_visit::*; use syntax::mut_visit::*;
use syntax::parse::DirectoryOwnership; use syntax::parse::DirectoryOwnership;

View file

@ -33,6 +33,35 @@ pub use mbe::macro_rules::compile_declarative_macro;
pub mod base; pub mod base;
pub mod build; pub mod build;
pub mod expand; pub mod expand;
#[macro_use] pub mod config;
pub mod proc_macro; pub mod proc_macro;
crate mod mbe; crate mod mbe;
// HACK(Centril, #64197): These shouldn't really be here.
// Rather, they should be with their respective modules which are defined in other crates.
// However, since for now constructing a `ParseSess` sorta requires `config` from this crate,
// these tests will need to live here in the iterim.
#[cfg(test)]
mod tests;
#[cfg(test)]
mod parse {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod lexer {
#[cfg(test)]
mod tests;
}
}
#[cfg(test)]
mod tokenstream {
#[cfg(test)]
mod tests;
}
#[cfg(test)]
mod mut_visit {
#[cfg(test)]
mod tests;
}

View file

@ -1,10 +1,9 @@
use super::*;
use crate::ast::{self, Ident};
use crate::tests::{string_to_crate, matches_codepattern}; use crate::tests::{string_to_crate, matches_codepattern};
use crate::print::pprust;
use crate::mut_visit; use syntax::ast::{self, Ident};
use crate::with_default_globals; use syntax::print::pprust;
use syntax::mut_visit::{self, MutVisitor};
use syntax::with_default_globals;
// This version doesn't care about getting comments or doc-strings in. // This version doesn't care about getting comments or doc-strings in.
fn fake_print_crate(s: &mut pprust::State<'_>, fn fake_print_crate(s: &mut pprust::State<'_>,

View file

@ -1,10 +1,13 @@
use super::*; use crate::config::process_configure_mod;
use crate::symbol::Symbol; use rustc_data_structures::sync::Lrc;
use crate::source_map::{SourceMap, FilePathMapping}; use syntax::token::{self, Token, TokenKind};
use crate::token; use syntax::sess::ParseSess;
use crate::util::comments::is_doc_comment; use syntax::source_map::{SourceMap, FilePathMapping};
use crate::with_default_globals; use syntax::util::comments::is_doc_comment;
use syntax::with_default_globals;
use syntax::parse::lexer::StringReader;
use syntax_pos::symbol::Symbol;
use errors::{Handler, emitter::EmitterWriter}; use errors::{Handler, emitter::EmitterWriter};
use std::io; use std::io;
@ -21,7 +24,11 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess {
None, None,
false, false,
); );
ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm) ParseSess::with_span_handler(
Handler::with_emitter(true, None, Box::new(emitter)),
sm,
process_configure_mod,
)
} }
// Creates a string reader for the given string. // Creates a string reader for the given string.

View file

@ -1,21 +1,27 @@
use super::*; use crate::config::process_configure_mod;
use crate::ast::{self, Name, PatKind};
use crate::attr::first_attr_value_str_by_name;
use crate::sess::ParseSess;
use crate::parse::{PResult, new_parser_from_source_str};
use crate::token::Token;
use crate::print::pprust::item_to_string;
use crate::ptr::P;
use crate::source_map::FilePathMapping;
use crate::symbol::{kw, sym};
use crate::tests::{matches_codepattern, string_to_stream, with_error_checking_parse}; use crate::tests::{matches_codepattern, string_to_stream, with_error_checking_parse};
use crate::tokenstream::{DelimSpan, TokenTree, TokenStream};
use crate::with_default_globals; use syntax::ast::{self, Name, PatKind};
use syntax_pos::{Span, BytePos, Pos}; use syntax::attr::first_attr_value_str_by_name;
use syntax::sess::ParseSess;
use syntax::token::{self, Token};
use syntax::print::pprust::item_to_string;
use syntax::ptr::P;
use syntax::source_map::FilePathMapping;
use syntax::symbol::{kw, sym};
use syntax::tokenstream::{DelimSpan, TokenTree, TokenStream};
use syntax::visit;
use syntax::with_default_globals;
use syntax::parse::new_parser_from_source_str;
use syntax_pos::{Span, BytePos, Pos, FileName};
use errors::PResult;
use std::path::PathBuf; use std::path::PathBuf;
fn sess() -> ParseSess {
ParseSess::new(FilePathMapping::empty(), process_configure_mod)
}
/// Parses an item. /// Parses an item.
/// ///
/// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and `Err` /// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and `Err`
@ -32,18 +38,12 @@ fn sp(a: u32, b: u32) -> Span {
/// Parses a string, return an expression. /// Parses a string, return an expression.
fn string_to_expr(source_str : String) -> P<ast::Expr> { fn string_to_expr(source_str : String) -> P<ast::Expr> {
let ps = ParseSess::new(FilePathMapping::empty()); with_error_checking_parse(source_str, &sess(), |p| p.parse_expr())
with_error_checking_parse(source_str, &ps, |p| {
p.parse_expr()
})
} }
/// Parses a string, returns an item. /// Parses a string, returns an item.
fn string_to_item(source_str : String) -> Option<P<ast::Item>> { fn string_to_item(source_str : String) -> Option<P<ast::Item>> {
let ps = ParseSess::new(FilePathMapping::empty()); with_error_checking_parse(source_str, &sess(), |p| p.parse_item())
with_error_checking_parse(source_str, &ps, |p| {
p.parse_item()
})
} }
#[should_panic] #[should_panic]
@ -169,20 +169,20 @@ fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
struct PatIdentVisitor { struct PatIdentVisitor {
spans: Vec<Span> spans: Vec<Span>
} }
impl<'a> crate::visit::Visitor<'a> for PatIdentVisitor { impl<'a> visit::Visitor<'a> for PatIdentVisitor {
fn visit_pat(&mut self, p: &'a ast::Pat) { fn visit_pat(&mut self, p: &'a ast::Pat) {
match p.kind { match p.kind {
PatKind::Ident(_ , ref ident, _) => { PatKind::Ident(_ , ref ident, _) => {
self.spans.push(ident.span.clone()); self.spans.push(ident.span.clone());
} }
_ => { _ => {
crate::visit::walk_pat(self, p); visit::walk_pat(self, p);
} }
} }
} }
} }
let mut v = PatIdentVisitor { spans: Vec::new() }; let mut v = PatIdentVisitor { spans: Vec::new() };
crate::visit::walk_item(&mut v, &item); visit::walk_item(&mut v, &item);
return v.spans; return v.spans;
} }
@ -233,7 +233,7 @@ let mut fflags: c_int = wb();
#[test] fn crlf_doc_comments() { #[test] fn crlf_doc_comments() {
with_default_globals(|| { with_default_globals(|| {
let sess = ParseSess::new(FilePathMapping::empty()); let sess = sess();
let name_1 = FileName::Custom("crlf_source_1".to_string()); let name_1 = FileName::Custom("crlf_source_1".to_string());
let source = "/// doc comment\r\nfn foo() {}".to_string(); let source = "/// doc comment\r\nfn foo() {}".to_string();
@ -268,7 +268,7 @@ fn ttdelim_span() {
} }
with_default_globals(|| { with_default_globals(|| {
let sess = ParseSess::new(FilePathMapping::empty()); let sess = sess();
let expr = parse_expr_from_source_str(PathBuf::from("foo").into(), let expr = parse_expr_from_source_str(PathBuf::from("foo").into(),
"foo!( fn main() { body } )".to_string(), &sess).unwrap(); "foo!( fn main() { body } )".to_string(), &sess).unwrap();
@ -292,11 +292,10 @@ fn ttdelim_span() {
#[test] #[test]
fn out_of_line_mod() { fn out_of_line_mod() {
with_default_globals(|| { with_default_globals(|| {
let sess = ParseSess::new(FilePathMapping::empty());
let item = parse_item_from_source_str( let item = parse_item_from_source_str(
PathBuf::from("foo").into(), PathBuf::from("foo").into(),
"mod foo { struct S; mod this_does_not_exist; }".to_owned(), "mod foo { struct S; mod this_does_not_exist; }".to_owned(),
&sess, &sess(),
).unwrap().unwrap(); ).unwrap().unwrap();
if let ast::ItemKind::Mod(ref m) = item.kind { if let ast::ItemKind::Mod(ref m) = item.kind {

View file

@ -1,16 +1,15 @@
use crate::ast; use crate::config::process_configure_mod;
use crate::parse::source_file_to_stream; use syntax::ast;
use crate::parse::new_parser_from_source_str; use syntax::tokenstream::TokenStream;
use crate::parse::parser::Parser; use syntax::sess::ParseSess;
use crate::sess::ParseSess; use syntax::source_map::{SourceMap, FilePathMapping};
use crate::source_map::{SourceMap, FilePathMapping}; use syntax::with_default_globals;
use crate::tokenstream::TokenStream; use syntax::parse::{source_file_to_stream, new_parser_from_source_str, parser::Parser};
use crate::with_default_globals; use syntax_pos::{BytePos, Span, MultiSpan};
use errors::emitter::EmitterWriter; use errors::emitter::EmitterWriter;
use errors::{PResult, Handler}; use errors::{PResult, Handler};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use syntax_pos::{BytePos, Span, MultiSpan};
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
@ -35,7 +34,7 @@ crate fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F)
/// Maps a string to tts, using a made-up filename. /// Maps a string to tts, using a made-up filename.
crate fn string_to_stream(source_str: String) -> TokenStream { crate fn string_to_stream(source_str: String) -> TokenStream {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
source_file_to_stream( source_file_to_stream(
&ps, &ps,
ps.source_map().new_source_file(PathBuf::from("bogofile").into(), ps.source_map().new_source_file(PathBuf::from("bogofile").into(),
@ -45,7 +44,7 @@ crate fn string_to_stream(source_str: String) -> TokenStream {
/// Parses a string, returns a crate. /// Parses a string, returns a crate.
crate fn string_to_crate(source_str : String) -> ast::Crate { crate fn string_to_crate(source_str : String) -> ast::Crate {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
with_error_checking_parse(source_str, &ps, |p| { with_error_checking_parse(source_str, &ps, |p| {
p.parse_crate_mod() p.parse_crate_mod()
}) })

View file

@ -1,9 +1,11 @@
use super::*;
use crate::ast::Name;
use crate::with_default_globals;
use crate::tests::string_to_stream; use crate::tests::string_to_stream;
use syntax::ast::Name;
use syntax::token;
use syntax::tokenstream::{TokenStream, TokenStreamBuilder, TokenTree};
use syntax::with_default_globals;
use syntax_pos::{Span, BytePos}; use syntax_pos::{Span, BytePos};
use smallvec::smallvec;
fn string_to_ts(string: &str) -> TokenStream { fn string_to_ts(string: &str) -> TokenStream {
string_to_stream(string.to_owned()) string_to_stream(string.to_owned())

View file

@ -6,6 +6,7 @@
#![feature(rustc_private)] #![feature(rustc_private)]
extern crate syntax; extern crate syntax;
extern crate syntax_expand;
extern crate rustc_errors; extern crate rustc_errors;
use rustc_errors::PResult; use rustc_errors::PResult;
@ -14,13 +15,13 @@ use syntax::attr::*;
use syntax::ast; use syntax::ast;
use syntax::sess::ParseSess; use syntax::sess::ParseSess;
use syntax::source_map::{FilePathMapping, FileName}; use syntax::source_map::{FilePathMapping, FileName};
use syntax::parse; use syntax::ptr::P;
use syntax::print::pprust;
use syntax::parse::parser::attr::*;
use syntax::parse::new_parser_from_source_str; use syntax::parse::new_parser_from_source_str;
use syntax::parse::parser::Parser; use syntax::parse::parser::Parser;
use syntax::token; use syntax::token;
use syntax::ptr::P; use syntax_expand::config::process_configure_mod;
use syntax::parse::parser::attr::*;
use syntax::print::pprust;
use std::fmt; use std::fmt;
// Copied out of syntax::util::parser_testing // Copied out of syntax::util::parser_testing
@ -72,8 +73,12 @@ fn str_compare<T, F: Fn(&T) -> String>(e: &str, expected: &[T], actual: &[T], f:
} }
} }
fn sess() -> ParseSess {
ParseSess::new(FilePathMapping::empty(), process_configure_mod)
}
fn check_expr_attrs(es: &str, expected: &[&str]) { fn check_expr_attrs(es: &str, expected: &[&str]) {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = sess();
let e = expr(es, &ps).expect("parse error"); let e = expr(es, &ps).expect("parse error");
let actual = &e.attrs; let actual = &e.attrs;
str_compare(es, str_compare(es,
@ -83,7 +88,7 @@ fn check_expr_attrs(es: &str, expected: &[&str]) {
} }
fn check_stmt_attrs(es: &str, expected: &[&str]) { fn check_stmt_attrs(es: &str, expected: &[&str]) {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = sess();
let e = stmt(es, &ps).expect("parse error"); let e = stmt(es, &ps).expect("parse error");
let actual = e.kind.attrs(); let actual = e.kind.attrs();
str_compare(es, str_compare(es,
@ -93,7 +98,7 @@ fn check_stmt_attrs(es: &str, expected: &[&str]) {
} }
fn reject_expr_parse(es: &str) { fn reject_expr_parse(es: &str) {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = sess();
match expr(es, &ps) { match expr(es, &ps) {
Ok(_) => panic!("parser did not reject `{}`", es), Ok(_) => panic!("parser did not reject `{}`", es),
Err(mut e) => e.cancel(), Err(mut e) => e.cancel(),
@ -101,7 +106,7 @@ fn reject_expr_parse(es: &str) {
} }
fn reject_stmt_parse(es: &str) { fn reject_stmt_parse(es: &str) {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = sess();
match stmt(es, &ps) { match stmt(es, &ps) {
Ok(_) => panic!("parser did not reject `{}`", es), Ok(_) => panic!("parser did not reject `{}`", es),
Err(mut e) => e.cancel(), Err(mut e) => e.cancel(),

View file

@ -5,11 +5,13 @@
#![feature(rustc_private)] #![feature(rustc_private)]
extern crate syntax; extern crate syntax;
extern crate syntax_expand;
use std::path::Path; use std::path::Path;
use syntax::sess::ParseSess; use syntax::sess::ParseSess;
use syntax::source_map::FilePathMapping; use syntax::source_map::FilePathMapping;
use syntax::parse; use syntax::parse::new_parser_from_file;
use syntax_expand::config::process_configure_mod;
#[path = "mod_dir_simple/test.rs"] #[path = "mod_dir_simple/test.rs"]
mod gravy; mod gravy;
@ -21,10 +23,10 @@ pub fn main() {
} }
fn parse() { fn parse() {
let parse_session = ParseSess::new(FilePathMapping::empty()); let parse_session = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
let path = Path::new(file!()); let path = Path::new(file!());
let path = path.canonicalize().unwrap(); let path = path.canonicalize().unwrap();
let mut parser = parse::new_parser_from_file(&parse_session, &path); let mut parser = new_parser_from_file(&parse_session, &path);
let _ = parser.parse_crate_mod(); let _ = parser.parse_crate_mod();
} }

View file

@ -21,6 +21,7 @@
extern crate rustc_data_structures; extern crate rustc_data_structures;
extern crate syntax; extern crate syntax;
extern crate syntax_expand;
use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::thin_vec::ThinVec;
use syntax::ast::*; use syntax::ast::*;
@ -28,14 +29,15 @@ use syntax::sess::ParseSess;
use syntax::source_map::{Spanned, DUMMY_SP, FileName}; use syntax::source_map::{Spanned, DUMMY_SP, FileName};
use syntax::source_map::FilePathMapping; use syntax::source_map::FilePathMapping;
use syntax::mut_visit::{self, MutVisitor, visit_clobber}; use syntax::mut_visit::{self, MutVisitor, visit_clobber};
use syntax::parse; use syntax::parse::new_parser_from_source_str;
use syntax::print::pprust; use syntax::print::pprust;
use syntax::ptr::P; use syntax::ptr::P;
use syntax_expand::config::process_configure_mod;
fn parse_expr(ps: &ParseSess, src: &str) -> Option<P<Expr>> { fn parse_expr(ps: &ParseSess, src: &str) -> Option<P<Expr>> {
let src_as_string = src.to_string(); let src_as_string = src.to_string();
let mut p = parse::new_parser_from_source_str( let mut p = new_parser_from_source_str(
ps, ps,
FileName::Custom(src_as_string.clone()), FileName::Custom(src_as_string.clone()),
src_as_string, src_as_string,
@ -202,7 +204,7 @@ fn main() {
} }
fn run() { fn run() {
let ps = ParseSess::new(FilePathMapping::empty()); let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
iter_exprs(2, &mut |mut e| { iter_exprs(2, &mut |mut e| {
// If the pretty printer is correct, then `parse(print(e))` should be identical to `e`, // If the pretty printer is correct, then `parse(print(e))` should be identical to `e`,