]> git.lizzy.rs Git - rust.git/commitdiff
rustc_driver: Allow running the compiler with a FileLoader
authorAdolfo Ochagavía <aochagavia92@gmail.com>
Tue, 26 Apr 2016 12:11:03 +0000 (14:11 +0200)
committerAdolfo Ochagavía <aochagavia92@gmail.com>
Wed, 27 Apr 2016 08:51:55 +0000 (10:51 +0200)
src/librustc/session/mod.rs
src/librustc_driver/lib.rs

index 9c8d71908286e2021f7b0242d1e176280c3dd01d..5b78e4de18b576dc669fdb25ff0b1f820a76b539 100644 (file)
@@ -408,6 +408,19 @@ pub fn build_session(sopts: config::Options,
                      registry: diagnostics::registry::Registry,
                      cstore: Rc<for<'a> CrateStore<'a>>)
                      -> Session {
+    build_session_with_codemap(sopts,
+                              local_crate_source_file,
+                              registry,
+                              cstore,
+                              Rc::new(codemap::CodeMap::new()))
+}
+
+pub fn build_session_with_codemap(sopts: config::Options,
+                                  local_crate_source_file: Option<PathBuf>,
+                                  registry: diagnostics::registry::Registry,
+                                  cstore: Rc<for<'a> CrateStore<'a>>,
+                                  codemap: Rc<codemap::CodeMap>)
+                                  -> Session {
     // 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
     // later via the source code.
@@ -419,7 +432,6 @@ pub fn build_session(sopts: config::Options,
         .unwrap_or(true);
     let treat_err_as_bug = sopts.treat_err_as_bug;
 
-    let codemap = Rc::new(codemap::CodeMap::new());
     let emitter: Box<Emitter> = match sopts.error_format {
         config::ErrorOutputType::HumanReadable(color_config) => {
             Box::new(EmitterWriter::stderr(color_config, Some(registry), codemap.clone()))
index 0e100f48ac38d64488defa10f3e3d7775c0e49ff..2f31fb0f4f1f9568eb78840f1862e7895c045551 100644 (file)
@@ -66,7 +66,7 @@
 use rustc_resolve as resolve;
 use rustc_save_analysis as save;
 use rustc_trans::back::link;
-use rustc::session::{config, Session, build_session, CompileResult};
+use rustc::session::{self, config, Session, build_session, CompileResult};
 use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
 use rustc::session::config::{get_unstable_features_setting, nightly_options};
 use rustc::middle::cstore::CrateStore;
 
 use rustc::session::early_error;
 
-use syntax::ast;
-use syntax::parse::{self, PResult};
-use syntax::errors;
+use syntax::{ast, errors, diagnostics};
+use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
 use syntax::errors::emitter::Emitter;
-use syntax::diagnostics;
-use syntax::parse::token;
 use syntax::feature_gate::{GatedCfg, UnstableFeatures};
+use syntax::parse::{self, PResult, token};
 
 #[cfg(test)]
 pub mod test;
@@ -148,11 +146,20 @@ pub fn run(args: Vec<String>) -> isize {
     0
 }
 
-// Parse args and run the compiler. This is the primary entry point for rustc.
-// See comments on CompilerCalls below for details about the callbacks argument.
 pub fn run_compiler<'a>(args: &[String],
                         callbacks: &mut CompilerCalls<'a>)
                         -> (CompileResult, Option<Session>) {
+    run_compiler_with_file_loader(args, callbacks, box RealFileLoader)
+}
+
+// Parse args and run the compiler. This is the primary entry point for rustc.
+// See comments on CompilerCalls below for details about the callbacks argument.
+// The FileLoader provides a way to load files from sources other than the file system.
+pub fn run_compiler_with_file_loader<'a, L>(args: &[String],
+                                            callbacks: &mut CompilerCalls<'a>,
+                                            loader: Box<L>)
+                                            -> (CompileResult, Option<Session>)
+    where L: FileLoader + 'static {
     macro_rules! do_or_return {($expr: expr, $sess: expr) => {
         match $expr {
             Compilation::Stop => return (Ok(()), $sess),
@@ -189,7 +196,12 @@ macro_rules! do_or_return {($expr: expr, $sess: expr) => {
     };
 
     let cstore = Rc::new(CStore::new(token::get_ident_interner()));
-    let sess = build_session(sopts, input_file_path, descriptions, cstore.clone());
+    let codemap = Rc::new(CodeMap::with_file_loader(loader));
+    let sess = session::build_session_with_codemap(sopts,
+                                                   input_file_path,
+                                                   descriptions,
+                                                   cstore.clone(),
+                                                   codemap);
     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
     let mut cfg = config::build_configuration(&sess);
     target_features::add_configuration(&mut cfg, &sess);