Skip to content

Commit

Permalink
Move argfile expansion into run_compiler
Browse files Browse the repository at this point in the history
This will make @path work with miri and other non-standard entrypoints.

Also since this simplifies librustc_driver::args, move it into a simple source file. Also
remove the tests since they're doing nothing more than checking `str::lines` has the right
behaviour.
  • Loading branch information
jsgf committed Aug 20, 2019
1 parent d2219c2 commit d949774
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 239 deletions.
53 changes: 53 additions & 0 deletions src/librustc_driver/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::error;
use std::fmt;
use std::fs;
use std::io;
use std::str;
use std::sync::atomic::{AtomicBool, Ordering};

static USED_ARGSFILE_FEATURE: AtomicBool = AtomicBool::new(false);

pub fn used_unstable_argsfile() -> bool {
USED_ARGSFILE_FEATURE.load(Ordering::Relaxed)
}

pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
if arg.starts_with("@") {
let path = &arg[1..];
let file = match fs::read_to_string(path) {
Ok(file) => {
USED_ARGSFILE_FEATURE.store(true, Ordering::Relaxed);
file
}
Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
return Err(Error::Utf8Error(Some(path.to_string())));
}
Err(err) => return Err(Error::IOError(path.to_string(), err)),
};
Ok(file.lines().map(ToString::to_string).collect())
} else {
Ok(vec![arg])
}
}

#[derive(Debug)]
pub enum Error {
Utf8Error(Option<String>),
IOError(String, io::Error),
}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
}
}
}

impl error::Error for Error {
fn description(&self) -> &'static str {
"argument error"
}
}
84 changes: 0 additions & 84 deletions src/librustc_driver/args/mod.rs

This file was deleted.

145 changes: 0 additions & 145 deletions src/librustc_driver/args/tests.rs

This file was deleted.

20 changes: 14 additions & 6 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,22 @@ impl Callbacks for TimePassesCallbacks {
// 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(
args: &[String],
at_args: &[String],
callbacks: &mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>
) -> interface::Result<()> {
let mut args = Vec::new();
for arg in at_args {
match args::arg_expand(arg.clone()) {
Ok(arg) => args.extend(arg),
Err(err) => early_error(ErrorOutputType::default(),
&format!("Failed to load argument file: {}", err)),
}
}
let diagnostic_output = emitter.map(|emitter| DiagnosticOutput::Raw(emitter))
.unwrap_or(DiagnosticOutput::Default);
let matches = match handle_options(args) {
let matches = match handle_options(&args) {
Some(matches) => matches,
None => return Ok(()),
};
Expand Down Expand Up @@ -1199,10 +1207,10 @@ pub fn main() {
init_rustc_env_logger();
let mut callbacks = TimePassesCallbacks::default();
let result = report_ices_to_stderr_if_any(|| {
let args = args::ArgsIter::new().enumerate()
.map(|(i, arg)| arg.unwrap_or_else(|err| {
early_error(ErrorOutputType::default(),
&format!("Argument {} is not valid: {}", i, err))
let args = env::args_os().enumerate()
.map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
early_error(ErrorOutputType::default(),
&format!("Argument {} is not valid Unicode: {:?}", i, arg))
}))
.collect::<Vec<_>>();
run_compiler(&args, &mut callbacks, None, None)
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/commandline-argfile-badutf8.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Check to see if we can get parameters from an @argsfile file
//
// build-fail
// normalize-stderr-test: "Argument \d+" -> "Argument $$N"
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-badutf8.args

#[cfg(not(cmdline_set))]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/commandline-argfile-badutf8.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: Argument $N is not valid: Utf8 error in $DIR/commandline-argfile-badutf8.args
error: Failed to load argument file: Utf8 error in $DIR/commandline-argfile-badutf8.args

2 changes: 1 addition & 1 deletion src/test/ui/commandline-argfile-missing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Check to see if we can get parameters from an @argsfile file
//
// ignore-tidy-linelength
// build-fail
// normalize-stderr-test: "Argument \d+" -> "Argument $$N"
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-missing.args
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/commandline-argfile-missing.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: Argument $N is not valid: IO Error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
error: Failed to load argument file: IO Error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)

0 comments on commit d949774

Please sign in to comment.