]> git.lizzy.rs Git - rust.git/commitdiff
Validate syntax of `--cfg` command line arguments
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 5 Sep 2018 22:15:46 +0000 (01:15 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 5 Sep 2018 23:04:04 +0000 (02:04 +0300)
src/librustc/session/config.rs
src/test/ui/cfg-arg-invalid-1.rs [new file with mode: 0644]
src/test/ui/cfg-arg-invalid-2.rs [new file with mode: 0644]
src/test/ui/cfg-arg-invalid-3.rs [new file with mode: 0644]
src/test/ui/cfg-arg-invalid-4.rs [new file with mode: 0644]
src/test/ui/cfg-arg-invalid-5.rs [new file with mode: 0644]
src/test/ui/cfg-arg-invalid.rs [deleted file]
src/test/ui/cfg-empty-codemap.rs
src/test/ui/issues/issue-31495.rs [deleted file]

index ee3fabc58d53f6edc6e6b150a1b652727991587b..73c9729feee32864d18ea9a227558fbf4a67fa03 100644 (file)
@@ -21,7 +21,7 @@
 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;
@@ -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>()
 }
diff --git a/src/test/ui/cfg-arg-invalid-1.rs b/src/test/ui/cfg-arg-invalid-1.rs
new file mode 100644 (file)
index 0000000..36dd78d
--- /dev/null
@@ -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() {}
diff --git a/src/test/ui/cfg-arg-invalid-2.rs b/src/test/ui/cfg-arg-invalid-2.rs
new file mode 100644 (file)
index 0000000..48d656a
--- /dev/null
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a{b}
+// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-3.rs b/src/test/ui/cfg-arg-invalid-3.rs
new file mode 100644 (file)
index 0000000..96ac782
--- /dev/null
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a::b
+// error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-4.rs b/src/test/ui/cfg-arg-invalid-4.rs
new file mode 100644 (file)
index 0000000..e7dfa17
--- /dev/null
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a(b)
+// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-5.rs b/src/test/ui/cfg-arg-invalid-5.rs
new file mode 100644 (file)
index 0000000..a939f45
--- /dev/null
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a=10
+// error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid.rs b/src/test/ui/cfg-arg-invalid.rs
deleted file mode 100644 (file)
index 4046303..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2016 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.
-
-// compile-flags: --cfg a{b}
-// error-pattern: invalid --cfg argument: a{b}
-fn main() {}
index f06d22d985f49f176f5c88da18d14aab87e7873c..5cf8135ca6bc6795d333c5c9abde6250e0933fc9 100644 (file)
@@ -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() {
 }
diff --git a/src/test/ui/issues/issue-31495.rs b/src/test/ui/issues/issue-31495.rs
deleted file mode 100644 (file)
index 794b8bb..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2015 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.
-
-// compile-flags: --cfg foo(bar)
-// error-pattern: invalid predicate in --cfg command line argument: `foo`
-fn main() {}