]> git.lizzy.rs Git - rust.git/commitdiff
Convert cfg syntax to new system
authorSteven Fackler <sfackler@gmail.com>
Thu, 25 Sep 2014 03:22:57 +0000 (20:22 -0700)
committerSteven Fackler <sfackler@gmail.com>
Sun, 28 Sep 2014 05:59:26 +0000 (22:59 -0700)
This removes the ability to use `foo(bar)` style cfgs. Switch them to
`foo_bar` or `foo="bar"` instead.

[breaking-change]

13 files changed:
src/librustc/driver/driver.rs
src/libsyntax/attr.rs
src/libsyntax/config.rs
src/libsyntax/ext/cfg.rs
src/libsyntax/ext/cfg_attr.rs
src/test/compile-fail/asm-in-bad-modifier.rs
src/test/compile-fail/asm-misplaced-option.rs
src/test/compile-fail/asm-out-assign-imm.rs
src/test/compile-fail/asm-out-no-modifier.rs
src/test/compile-fail/asm-out-read-uninit.rs
src/test/compile-fail/test-cfg.rs
src/test/pretty/raw-str-nonexpr.rs
src/test/run-pass/syntax-extension-cfg.rs

index 4ff9133c8a534e9748c2beabee67dec375118f63..ed0aeb952a1f053a5e5ebd674ff37d7657cf74c4 100644 (file)
@@ -226,7 +226,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
     // baz! should not use this definition unless foo is enabled.
 
     krate = time(time_passes, "configuration 1", krate, |krate|
-                 syntax::config::strip_unconfigured_items(krate));
+                 syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
 
     let mut addl_plugins = Some(addl_plugins);
     let Plugins { macros, registrars }
@@ -307,7 +307,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
 
     // strip again, in case expansion added anything with a #[cfg].
     krate = time(time_passes, "configuration 2", krate, |krate|
-                 syntax::config::strip_unconfigured_items(krate));
+                 syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
 
     krate = time(time_passes, "maybe building test harness", krate, |krate|
                  syntax::test::modify_for_testing(&sess.parse_sess,
index ace1e1245c7180cedae6c690a253bd5668056c13..efc75de7142b86965f17fdf462fba0c6715bdbae 100644 (file)
@@ -307,6 +307,32 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
     }
 }
 
+/// Tests if a cfg-pattern matches the cfg set
+pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P<MetaItem>], cfg: &ast::MetaItem) -> bool {
+    match cfg.node {
+        ast::MetaList(ref pred, ref mis) if pred.get() == "any" =>
+            mis.iter().any(|mi| cfg_matches(diagnostic, cfgs, &**mi)),
+        ast::MetaList(ref pred, ref mis) if pred.get() == "all" =>
+            mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi)),
+        ast::MetaList(ref pred, ref mis) if pred.get() == "not" => {
+            // NOTE: turn on after snapshot
+            /*
+            if mis.len() != 1 {
+                diagnostic.span_warn(cfg.span, "the use of multiple cfgs in the same `not` \
+                                                statement is deprecated. Change `not(a, b)` to \
+                                                `not(all(a, b))`.");
+            }
+            */
+            !mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi))
+        }
+        ast::MetaList(ref pred, _) => {
+            diagnostic.span_err(cfg.span, format!("invalid predicate `{}`", pred).as_slice());
+            false
+        },
+        ast::MetaWord(_) | ast::MetaNameValue(..) => contains(cfgs, cfg),
+    }
+}
+
 /// Tests if any `cfg(...)` meta items in `metas` match `cfg`. e.g.
 ///
 /// test_cfg(`[foo="a", bar]`, `[cfg(foo), cfg(bar)]`) == true
index 3b250de87016569d5360995cb9261a0dbb72c163..5b17f6f004a081ffcb64b8b1bd935ebe6eb9f2bf 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use attr::AttrMetaMethods;
+use diagnostic::SpanHandler;
 use fold::Folder;
 use {ast, fold, attr};
 use codemap::Spanned;
@@ -21,9 +23,9 @@ struct Context<'a> {
 
 // Support conditional compilation by transforming the AST, stripping out
 // any items that do not belong in the current configuration
-pub fn strip_unconfigured_items(krate: ast::Crate) -> ast::Crate {
+pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
     let config = krate.config.clone();
-    strip_items(krate, |attrs| in_cfg(config.as_slice(), attrs))
+    strip_items(krate, |attrs| in_cfg(diagnostic, config.as_slice(), attrs))
 }
 
 impl<'a> fold::Folder for Context<'a> {
@@ -249,7 +251,34 @@ fn impl_item_in_cfg(cx: &mut Context, impl_item: &ast::ImplItem) -> bool {
 
 // Determine if an item should be translated in the current crate
 // configuration based on the item's attributes
-fn in_cfg(cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
-    attr::test_cfg(cfg, attrs.iter())
+fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
+    let mut in_cfg = false;
+    let mut seen_cfg = false;
+    for attr in attrs.iter() {
+        let mis = match attr.node.value.node {
+            ast::MetaList(_, ref mis) if attr.check_name("cfg") => mis,
+            _ => continue
+        };
+
+        // NOTE: turn on after snapshot
+        /*
+        if mis.len() != 1 {
+            diagnostic.span_warn(attr.span, "The use of multiple cfgs in the top level of \
+                                             `#[cfg(..)]` is deprecated. Change `#[cfg(a, b)]` to \
+                                             `#[cfg(all(a, b))]`.");
+        }
+
+        if seen_cfg {
+            diagnostic.span_warn(attr.span, "The semantics of multiple `#[cfg(..)]` attributes on \
+                                             same item are changing from the union of the cfgs to \
+                                             the intersection of the cfgs. Change `#[cfg(a)] \
+                                             #[cfg(b)]` to `#[cfg(any(a, b))]`.");
+        }
+        */
+
+        seen_cfg = true;
+        in_cfg |= mis.iter().all(|mi| attr::cfg_matches(diagnostic, cfg, &**mi));
+    }
+    in_cfg | !seen_cfg
 }
 
index 79cb47fee7b45a4ef01c4c8da7261a064cfea505..342e7e6d52e847e18083f151f90fe8a15b3ccbaf 100644 (file)
@@ -22,7 +22,6 @@
 use attr;
 use attr::*;
 use parse::attr::ParserAttr;
-use parse::token::InternedString;
 use parse::token;
 
 
@@ -39,11 +38,17 @@ pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
         p.expect(&token::COMMA);
     }
 
-    // test_cfg searches for meta items looking like `cfg(foo, ...)`
-    let in_cfg = Some(cx.meta_list(sp, InternedString::new("cfg"), cfgs));
+    // NOTE: turn on after snapshot
+    /*
+    if cfgs.len() != 1 {
+        cx.span_warn(sp, "The use of multiple cfgs at the top level of `cfg!` \
+                          is deprecated. Change `cfg!(a, b)` to \
+                          `cfg!(all(a, b))`.");
+    }
+    */
+
+    let matches_cfg = cfgs.iter().all(|cfg| attr::cfg_matches(&cx.parse_sess.span_diagnostic,
+                                                              cx.cfg.as_slice(), &**cfg));
 
-    let matches_cfg = attr::test_cfg(cx.cfg().as_slice(),
-                                     in_cfg.iter());
-    let e = cx.expr_bool(sp, matches_cfg);
-    MacExpr::new(e)
+    MacExpr::new(cx.expr_bool(sp, matches_cfg))
 }
index ad02b50f248b4a6f3be98c98e58c6fbb6baf8783..a85f12edb228648c672be37ff972662a45d890b9 100644 (file)
@@ -25,33 +25,10 @@ pub fn expand(cx: &mut ExtCtxt, sp: Span, mi: &ast::MetaItem, it: P<ast::Item>)
     };
 
     let mut out = (*it).clone();
-    if cfg_matches(cx, &**cfg) {
+    if attr::cfg_matches(&cx.parse_sess.span_diagnostic, cx.cfg.as_slice(), &**cfg) {
         out.attrs.push(cx.attribute(attr.span, attr.clone()));
     }
 
     P(out)
 }
 
-fn cfg_matches(cx: &mut ExtCtxt, cfg: &ast::MetaItem) -> bool {
-    match cfg.node {
-        ast::MetaList(ref pred, ref mis) if pred.get() == "any" =>
-            mis.iter().any(|mi| cfg_matches(cx, &**mi)),
-        ast::MetaList(ref pred, ref mis) if pred.get() == "all" =>
-            mis.iter().all(|mi| cfg_matches(cx, &**mi)),
-        ast::MetaList(ref pred, ref mis) if pred.get() == "not" => {
-            if mis.len() != 1 {
-                cx.span_err(cfg.span, format!("expected 1 value, got {}",
-                                              mis.len()).as_slice());
-                return false;
-            }
-            !cfg_matches(cx, &*mis[0])
-        }
-        ast::MetaList(ref pred, _) => {
-            cx.span_err(cfg.span,
-                        format!("invalid predicate `{}`", pred).as_slice());
-            false
-        },
-        ast::MetaWord(_) | ast::MetaNameValue(..) =>
-            attr::contains(cx.cfg.as_slice(), cfg),
-    }
-}
index 82ecfd0ca72e2f75aba4f44522f5e5a8f618b638..d2216d95867beaf28844d6a1097ac31878cf18f8 100644 (file)
 
 fn foo(x: int) { println!("{}", x); }
 
-#[cfg(target_arch = "x86")]
-#[cfg(target_arch = "x86_64")]
-#[cfg(target_arch = "arm")]
-
+#[cfg(any(target_arch = "x86",
+          target_arch = "x86_64",
+          target_arch = "arm"))]
 pub fn main() {
     let x: int;
     let y: int;
@@ -27,5 +26,5 @@ pub fn main() {
     foo(y);
 }
 
-#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
+#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
 pub fn main() {}
index 8e93b91276f2ea20b5539e20552dafb7e8bebbbe..8006789d440c3437a2435ecc7ac6748753cf0799 100644 (file)
@@ -14,8 +14,8 @@
 
 #![allow(dead_code)]
 
-#[cfg(target_arch = "x86")]
-#[cfg(target_arch = "x86_64")]
+#[cfg(any(target_arch = "x86",
+          target_arch = "x86_64"))]
 pub fn main() {
     // assignment not dead
     let mut x: int = 0;
index 2319594cb97072b9cf76aa13115ef13ef31f215c..a35f72ab4dc7b83e29e6e063931ae269bc1bba47 100644 (file)
@@ -12,9 +12,9 @@
 
 fn foo(x: int) { println!("{}", x); }
 
-#[cfg(target_arch = "x86")]
-#[cfg(target_arch = "x86_64")]
-#[cfg(target_arch = "arm")]
+#[cfg(any(target_arch = "x86",
+          target_arch = "x86_64",
+          target_arch = "arm"))]
 pub fn main() {
     let x: int;
     x = 1; //~ NOTE prior assignment occurs here
@@ -25,5 +25,5 @@ pub fn main() {
     foo(x);
 }
 
-#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
+#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
 pub fn main() {}
index 5535c167e3cc291e121285c3363784907365c422..76d4c516c4e826d4647774a50ea1994f2fa0be00 100644 (file)
@@ -12,9 +12,9 @@
 
 fn foo(x: int) { println!("{}", x); }
 
-#[cfg(target_arch = "x86")]
-#[cfg(target_arch = "x86_64")]
-#[cfg(target_arch = "arm")]
+#[cfg(any(target_arch = "x86",
+          target_arch = "x86_64",
+          target_arch = "arm"))]
 pub fn main() {
     let x: int;
     unsafe {
@@ -23,5 +23,5 @@ pub fn main() {
     foo(x);
 }
 
-#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
+#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
 pub fn main() {}
index 1d807acefe6a359c9dfb0fc890b27eee6810782a..aa83a89fec0a983aa46f654252b25c8095a8ec85 100644 (file)
@@ -12,9 +12,9 @@
 
 fn foo(x: int) { println!("{}", x); }
 
-#[cfg(target_arch = "x86")]
-#[cfg(target_arch = "x86_64")]
-#[cfg(target_arch = "arm")]
+#[cfg(any(target_arch = "x86",
+          target_arch = "x86_64",
+          target_arch = "arm"))]
 pub fn main() {
     let x: int;
     unsafe {
@@ -23,5 +23,5 @@ pub fn main() {
     foo(x);
 }
 
-#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
+#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
 pub fn main() {}
index a8c528135135a56c8fc8406130ed43a2d1210378..8be1c35ae5ccad824d88205037cec345e50e84b2 100644 (file)
@@ -10,7 +10,7 @@
 
 // compile-flags: --cfg foo
 
-#[cfg(foo, bar)] // foo AND bar
+#[cfg(all(foo, bar))] // foo AND bar
 fn foo() {}
 
 fn main() {
index 08d599b1e6f7787f56dfdbada84ca1cb80b37ee7..965b1760f1ff16f7c4b1cd21464d14419b7a04db 100644 (file)
@@ -12,7 +12,7 @@
 
 #![feature(asm)]
 
-#[cfg = r#"just parse this"#]
+#[cfg(foo = r#"just parse this"#)]
 extern crate r##"blah"## as blah;
 
 fn main() { unsafe { asm!(r###"blah"###); } }
index d09a4d990fd9cc63bb3c582d73eb0621cbe475ba..ab6468b2a857f6931085c173523d700522de86d0 100644 (file)
@@ -8,27 +8,27 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// compile-flags: --cfg foo --cfg bar(baz) --cfg qux="foo"
+// compile-flags: --cfg foo --cfg qux="foo"
 
 pub fn main() {
     // check
     if ! cfg!(foo) { fail!() }
     if   cfg!(not(foo)) { fail!() }
 
-    if ! cfg!(bar(baz)) { fail!() }
-    if   cfg!(not(bar(baz))) { fail!() }
-
     if ! cfg!(qux="foo") { fail!() }
     if   cfg!(not(qux="foo")) { fail!() }
 
-    if ! cfg!(foo, bar(baz), qux="foo") { fail!() }
-    if   cfg!(not(foo, bar(baz), qux="foo")) { fail!() }
+    if ! cfg!(foo, qux="foo") { fail!() }
+    if   cfg!(not(foo, qux="foo")) { fail!() }
+    if   cfg!(all(not(foo, qux="foo"))) { fail!() }
 
     if cfg!(not_a_cfg) { fail!() }
-    if cfg!(not_a_cfg, foo, bar(baz), qux="foo") { fail!() }
+    if cfg!(not_a_cfg, foo, qux="foo") { fail!() }
+    if cfg!(all(not_a_cfg, foo, qux="foo")) { fail!() }
+    if ! cfg!(any(not_a_cfg, foo)) { fail!() }
 
     if ! cfg!(not(not_a_cfg)) { fail!() }
-    if ! cfg!(not(not_a_cfg), foo, bar(baz), qux="foo") { fail!() }
+    if ! cfg!(not(not_a_cfg), foo, qux="foo") { fail!() }
 
     if cfg!(trailing_comma, ) { fail!() }
 }