Skip to content

Commit

Permalink
Slightly improved rustc error messages for invalid -C arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
inrustwetrust committed Nov 15, 2014
1 parent bc0b612 commit 3391ddd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
35 changes: 27 additions & 8 deletions src/librustc/driver/config.rs
Expand Up @@ -268,8 +268,21 @@ macro_rules! cgoptions(

pub type CodegenSetter = fn(&mut CodegenOptions, v: Option<&str>) -> bool;
pub const CG_OPTIONS: &'static [(&'static str, CodegenSetter,
&'static str)] =
&[ $( (stringify!($opt), cgsetters::$opt, $desc) ),* ];
Option<&'static str>, &'static str)] =
&[ $( (stringify!($opt), cgsetters::$opt, cg_type_descs::$parse, $desc) ),* ];

#[allow(non_upper_case_globals)]
mod cg_type_descs {
pub const parse_bool: Option<&'static str> = None;
pub const parse_opt_bool: Option<&'static str> = None;
pub const parse_string: Option<&'static str> = Some("a string");
pub const parse_opt_string: Option<&'static str> = Some("a string");
pub const parse_list: Option<&'static str> = Some("a space-separated list of strings");
pub const parse_opt_list: Option<&'static str> = Some("a space-separated list of strings");
pub const parse_uint: Option<&'static str> = Some("a number");
pub const parse_passes: Option<&'static str> =
Some("a space-separated list of passes, or `all`");
}

mod cgsetters {
use super::{CodegenOptions, Passes, SomePasses, AllPasses};
Expand Down Expand Up @@ -421,19 +434,25 @@ pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions
let value = iter.next();
let option_to_lookup = key.replace("-", "_");
let mut found = false;
for &(candidate, setter, _) in CG_OPTIONS.iter() {
for &(candidate, setter, opt_type_desc, _) in CG_OPTIONS.iter() {
if option_to_lookup.as_slice() != candidate { continue }
if !setter(&mut cg, value) {
match value {
Some(..) => {
match (value, opt_type_desc) {
(Some(..), None) => {
early_error(format!("codegen option `{}` takes no \
value", key).as_slice())
}
None => {
(None, Some(type_desc)) => {
early_error(format!("codegen option `{0}` requires \
a value (-C {0}=<value>)",
key).as_slice())
{1} (-C {0}=<value>)",
key, type_desc).as_slice())
}
(Some(value), Some(type_desc)) => {
early_error(format!("incorrect value `{}` for codegen \
option `{}` - {} was expected",
value, key, type_desc).as_slice())
}
(None, None) => unreachable!()
}
}
found = true;
Expand Down
12 changes: 4 additions & 8 deletions src/librustc/driver/mod.rs
Expand Up @@ -299,14 +299,10 @@ fn describe_debug_flags() {

fn describe_codegen_flags() {
println!("\nAvailable codegen options:\n");
let mut cg = config::basic_codegen_options();
for &(name, parser, desc) in config::CG_OPTIONS.iter() {
// we invoke the parser function on `None` to see if this option needs
// an argument or not.
let (width, extra) = if parser(&mut cg, None) {
(25, "")
} else {
(21, "=val")
for &(name, _, opt_type_desc, desc) in config::CG_OPTIONS.iter() {
let (width, extra) = match opt_type_desc {
Some(..) => (21, "=val"),
None => (25, "")
};
println!(" -C {:>width$s}{} -- {}", name.replace("_", "-"),
extra, desc, width=width);
Expand Down
24 changes: 24 additions & 0 deletions src/test/run-make/codegen-options-parsing/Makefile
@@ -0,0 +1,24 @@
-include ../tools.mk

all:
#Option taking a number
$(RUSTC) -C codegen-units dummy.rs 2>&1 | \
grep 'codegen option `codegen-units` requires a number'
$(RUSTC) -C codegen-units= dummy.rs 2>&1 | \
grep 'incorrect value `` for codegen option `codegen-units` - a number was expected'
$(RUSTC) -C codegen-units=foo dummy.rs 2>&1 | \
grep 'incorrect value `foo` for codegen option `codegen-units` - a number was expected'
$(RUSTC) -C codegen-units=1 dummy.rs
#Option taking a string
$(RUSTC) -C extra-filename dummy.rs 2>&1 | \
grep 'codegen option `extra-filename` requires a string'
$(RUSTC) -C extra-filename= dummy.rs 2>&1
$(RUSTC) -C extra-filename=foo dummy.rs 2>&1
#Option taking no argument
$(RUSTC) -C lto= dummy.rs 2>&1 | \
grep 'codegen option `lto` takes no value'
$(RUSTC) -C lto=1 dummy.rs 2>&1 | \
grep 'codegen option `lto` takes no value'
$(RUSTC) -C lto=foo dummy.rs 2>&1 | \
grep 'codegen option `lto` takes no value'
$(RUSTC) -C lto dummy.rs
11 changes: 11 additions & 0 deletions src/test/run-make/codegen-options-parsing/dummy.rs
@@ -0,0 +1,11 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {}

0 comments on commit 3391ddd

Please sign in to comment.