Auto merge of #32169 - mitaa:anon-tip, r=nrc

Allow custom filenames for anonymous inputs

This came out of #29253 but doesn't fix it.

I thought it might be worth merging on its own nonetheless.
This commit is contained in:
bors 2016-03-14 14:44:55 -07:00
commit 6d215fe04c
8 changed files with 32 additions and 19 deletions

View File

@ -172,8 +172,12 @@ pub enum PrintRequest {
pub enum Input {
/// Load source from file
File(PathBuf),
/// The string is the source
Str(String)
Str {
/// String that is shown in place of a filename
name: String,
/// Anonymous source string
input: String,
},
}
impl Input {
@ -181,7 +185,7 @@ impl Input {
match *self {
Input::File(ref ifile) => ifile.file_stem().unwrap()
.to_str().unwrap().to_string(),
Input::Str(_) => "rust_out".to_string(),
Input::Str { .. } => "rust_out".to_string(),
}
}
}

View File

@ -231,7 +231,6 @@ pub fn compile_input(sess: &Session,
Ok(())
}
/// The name used for source code that doesn't originate in a file
/// (e.g. source from stdin or a string)
pub fn anon_src() -> String {
@ -242,7 +241,7 @@ pub fn source_name(input: &Input) -> String {
match *input {
// FIXME (#9639): This needs to handle non-utf8 paths
Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
Input::Str(_) => anon_src(),
Input::Str { ref name, .. } => name.clone(),
}
}
@ -434,9 +433,9 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
Input::File(ref file) => {
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
}
Input::Str(ref src) => {
parse::parse_crate_from_source_str(anon_src().to_string(),
src.to_string(),
Input::Str { ref input, ref name } => {
parse::parse_crate_from_source_str(name.clone(),
input.clone(),
cfg.clone(),
&sess.parse_sess)
}

View File

@ -223,7 +223,8 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
if ifile == "-" {
let mut src = String::new();
io::stdin().read_to_string(&mut src).unwrap();
Some((Input::Str(src), None))
Some((Input::Str { name: driver::anon_src(), input: src },
None))
} else {
Some((Input::File(PathBuf::from(ifile)),
Some(PathBuf::from(ifile))))
@ -511,7 +512,7 @@ impl RustcDefaultCalls {
.unwrap();
println!("{}", String::from_utf8(v).unwrap());
}
&Input::Str(_) => {
&Input::Str { .. } => {
early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
}
}
@ -994,9 +995,9 @@ fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<as
Input::File(ref ifile) => {
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
}
Input::Str(ref src) => {
parse::parse_crate_attrs_from_source_str(driver::anon_src().to_string(),
src.to_string(),
Input::Str { ref name, ref input } => {
parse::parse_crate_attrs_from_source_str(name.clone(),
input.clone(),
Vec::new(),
&sess.parse_sess)
}

View File

@ -113,7 +113,10 @@ fn test_env<F>(source_string: &str,
Rc::new(CodeMap::new()), cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let krate_config = Vec::new();
let input = config::Input::Str(source_string.to_string());
let input = config::Input::Str {
name: driver::anon_src(),
input: source_string.to_string(),
};
let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
.expect("phase 2 aborted");

View File

@ -205,7 +205,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
current_dir().unwrap().join(path)
}
},
Input::Str(_) => PathBuf::new() // FIXME: this is wrong
Input::Str { ref name, .. } => PathBuf::from(name.clone()),
};
Crate {

View File

@ -180,7 +180,10 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
// the test harness wants its own `main` & top level functions, so
// never wrap the test in `fn main() { ... }`
let test = maketest(test, Some(cratename), as_test_harness, opts);
let input = config::Input::Str(test.to_string());
let input = config::Input::Str {
name: driver::anon_src(),
input: test.to_owned(),
};
let mut outputs = HashMap::new();
outputs.insert(OutputType::Exe, None);

View File

@ -216,7 +216,10 @@ fn build_exec_options(sysroot: PathBuf) -> Options {
/// for crates used in the given input.
fn compile_program(input: &str, sysroot: PathBuf)
-> Option<(llvm::ModuleRef, Vec<PathBuf>)> {
let input = Input::Str(input.to_string());
let input = Input::Str {
name: driver::anon_src(),
input: input.to_string(),
};
let thread = Builder::new().name("compile_program".to_string());
let handle = thread.spawn(move || {

View File

@ -18,7 +18,7 @@ extern crate syntax;
use rustc::session::{build_session, Session};
use rustc::session::config::{basic_options, build_configuration, Input, OutputType};
use rustc_driver::driver::{compile_input, CompileController};
use rustc_driver::driver::{compile_input, CompileController, anon_src};
use rustc_metadata::cstore::CStore;
use syntax::diagnostics::registry::Registry;
use syntax::parse::token;
@ -67,7 +67,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
compile_input(&sess, &cstore,
cfg,
&Input::Str(code),
&Input::Str { name: anon_src(), input: code },
&None,
&Some(output),
None,