Skip to content

Commit 115c6c8

Browse files
committed
Auto merge of rust-lang#33217 - aochagavia:fileloader, r=nrc
rustc_driver: Allow running the compiler with a FileLoader cc @nrc. I chose to implement this in such a way that it doesn't break anything. Please let me know if you want me to change anything.
2 parents 02acf09 + 6c50c88 commit 115c6c8

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/librustc/session/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ pub fn build_session(sopts: config::Options,
408408
registry: diagnostics::registry::Registry,
409409
cstore: Rc<for<'a> CrateStore<'a>>)
410410
-> Session {
411+
build_session_with_codemap(sopts,
412+
local_crate_source_file,
413+
registry,
414+
cstore,
415+
Rc::new(codemap::CodeMap::new()))
416+
}
417+
418+
pub fn build_session_with_codemap(sopts: config::Options,
419+
local_crate_source_file: Option<PathBuf>,
420+
registry: diagnostics::registry::Registry,
421+
cstore: Rc<for<'a> CrateStore<'a>>,
422+
codemap: Rc<codemap::CodeMap>)
423+
-> Session {
411424
// FIXME: This is not general enough to make the warning lint completely override
412425
// normal diagnostic warnings, since the warning lint can also be denied and changed
413426
// later via the source code.
@@ -419,7 +432,6 @@ pub fn build_session(sopts: config::Options,
419432
.unwrap_or(true);
420433
let treat_err_as_bug = sopts.treat_err_as_bug;
421434

422-
let codemap = Rc::new(codemap::CodeMap::new());
423435
let emitter: Box<Emitter> = match sopts.error_format {
424436
config::ErrorOutputType::HumanReadable(color_config) => {
425437
Box::new(EmitterWriter::stderr(color_config, Some(registry), codemap.clone()))

src/librustc_driver/lib.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use pretty::{PpMode, UserIdentifiedItem};
6666
use rustc_resolve as resolve;
6767
use rustc_save_analysis as save;
6868
use rustc_trans::back::link;
69-
use rustc::session::{config, Session, build_session, CompileResult};
69+
use rustc::session::{self, config, Session, build_session, CompileResult};
7070
use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
7171
use rustc::session::config::{get_unstable_features_setting, nightly_options};
7272
use rustc::middle::cstore::CrateStore;
@@ -91,13 +91,11 @@ use std::thread;
9191

9292
use rustc::session::early_error;
9393

94-
use syntax::ast;
95-
use syntax::parse::{self, PResult};
96-
use syntax::errors;
94+
use syntax::{ast, errors, diagnostics};
95+
use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
9796
use syntax::errors::emitter::Emitter;
98-
use syntax::diagnostics;
99-
use syntax::parse::token;
10097
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
98+
use syntax::parse::{self, PResult, token};
10199

102100
#[cfg(test)]
103101
pub mod test;
@@ -148,11 +146,20 @@ pub fn run(args: Vec<String>) -> isize {
148146
0
149147
}
150148

151-
// Parse args and run the compiler. This is the primary entry point for rustc.
152-
// See comments on CompilerCalls below for details about the callbacks argument.
153149
pub fn run_compiler<'a>(args: &[String],
154150
callbacks: &mut CompilerCalls<'a>)
155151
-> (CompileResult, Option<Session>) {
152+
run_compiler_with_file_loader(args, callbacks, box RealFileLoader)
153+
}
154+
155+
// Parse args and run the compiler. This is the primary entry point for rustc.
156+
// See comments on CompilerCalls below for details about the callbacks argument.
157+
// The FileLoader provides a way to load files from sources other than the file system.
158+
pub fn run_compiler_with_file_loader<'a, L>(args: &[String],
159+
callbacks: &mut CompilerCalls<'a>,
160+
loader: Box<L>)
161+
-> (CompileResult, Option<Session>)
162+
where L: FileLoader + 'static {
156163
macro_rules! do_or_return {($expr: expr, $sess: expr) => {
157164
match $expr {
158165
Compilation::Stop => return (Ok(()), $sess),
@@ -189,7 +196,12 @@ pub fn run_compiler<'a>(args: &[String],
189196
};
190197

191198
let cstore = Rc::new(CStore::new(token::get_ident_interner()));
192-
let sess = build_session(sopts, input_file_path, descriptions, cstore.clone());
199+
let codemap = Rc::new(CodeMap::with_file_loader(loader));
200+
let sess = session::build_session_with_codemap(sopts,
201+
input_file_path,
202+
descriptions,
203+
cstore.clone(),
204+
codemap);
193205
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
194206
let mut cfg = config::build_configuration(&sess);
195207
target_features::add_configuration(&mut cfg, &sess);

0 commit comments

Comments
 (0)