Skip to content

Commit

Permalink
Validate syntax of --cfg command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Sep 5, 2018
1 parent b0a05c5 commit a6adeae
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 41 deletions.
39 changes: 25 additions & 14 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_target::spec::{Target, TargetTriple};
use lint;
use middle::cstore;

use syntax::ast::{self, IntTy, UintTy};
use syntax::ast::{self, IntTy, UintTy, MetaItemKind};
use syntax::source_map::{FileName, FilePathMapping};
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
use syntax::parse::token;
Expand Down Expand Up @@ -1735,22 +1735,33 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
let mut parser =
parse::new_parser_from_source_str(&sess, FileName::CfgSpec, s.to_string());

let meta_item = panictry!(parser.parse_meta_item());
macro_rules! error {($reason: expr) => {
early_error(ErrorOutputType::default(),
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
}}

if parser.token != token::Eof {
early_error(
ErrorOutputType::default(),
&format!("invalid --cfg argument: {}", s),
)
} else if meta_item.is_meta_item_list() {
let msg = format!(
"invalid predicate in --cfg command line argument: `{}`",
meta_item.ident
);
early_error(ErrorOutputType::default(), &msg)
match &mut parser.parse_meta_item() {
Ok(meta_item) if parser.token == token::Eof => {
if meta_item.ident.segments.len() != 1 {
error!("argument key must be an identifier");
}
match &meta_item.node {
MetaItemKind::List(..) => {
error!(r#"expected `key` or `key="value"`"#);
}
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
error!("argument value must be a string");
}
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
return (meta_item.name(), meta_item.value_str());
}
}
}
Ok(..) => {}
Err(err) => err.cancel(),
}

(meta_item.name(), meta_item.value_str())
error!(r#"expected `key` or `key="value"`"#);
})
.collect::<ast::CrateConfig>()
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/cfg-arg-invalid-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// compile-flags: --cfg a(b=c)
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
fn main() {}
3 changes: 3 additions & 0 deletions src/test/ui/cfg-arg-invalid-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// compile-flags: --cfg a{b}
// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`)
fn main() {}
3 changes: 3 additions & 0 deletions src/test/ui/cfg-arg-invalid-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// compile-flags: --cfg a::b
// error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)
fn main() {}
3 changes: 3 additions & 0 deletions src/test/ui/cfg-arg-invalid-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// compile-flags: --cfg a(b)
// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`)
fn main() {}
3 changes: 3 additions & 0 deletions src/test/ui/cfg-arg-invalid-5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// compile-flags: --cfg a=10
// error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)
fn main() {}
13 changes: 0 additions & 13 deletions src/test/ui/cfg-arg-invalid.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/cfg-empty-codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// compile-flags: --cfg ""

// error-pattern: expected identifier, found
// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`)

pub fn main() {
}
13 changes: 0 additions & 13 deletions src/test/ui/issues/issue-31495.rs

This file was deleted.

0 comments on commit a6adeae

Please sign in to comment.