diff options
| author | Steven Fackler <sfackler@gmail.com> | 2014-09-24 20:22:57 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2014-09-27 22:59:26 -0700 |
| commit | 9519abecfb727d71bd8209ffd94816b2cb87180f (patch) | |
| tree | c287f36d9735cff0510c4aa8d0b33468714deb33 /src/libsyntax | |
| parent | dcdbdc10036b444ef39c329e9440d4acc6975fda (diff) | |
| download | rust-9519abecfb727d71bd8209ffd94816b2cb87180f.tar.gz rust-9519abecfb727d71bd8209ffd94816b2cb87180f.zip | |
Convert cfg syntax to new system
This removes the ability to use `foo(bar)` style cfgs. Switch them to `foo_bar` or `foo="bar"` instead. [breaking-change]
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/attr.rs | 26 | ||||
| -rw-r--r-- | src/libsyntax/config.rs | 37 | ||||
| -rw-r--r-- | src/libsyntax/ext/cfg.rs | 19 | ||||
| -rw-r--r-- | src/libsyntax/ext/cfg_attr.rs | 25 |
4 files changed, 72 insertions, 35 deletions
diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index ace1e1245c7..efc75de7142 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -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 diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 3b250de8701..5b17f6f004a 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -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 } diff --git a/src/libsyntax/ext/cfg.rs b/src/libsyntax/ext/cfg.rs index 79cb47fee7b..342e7e6d52e 100644 --- a/src/libsyntax/ext/cfg.rs +++ b/src/libsyntax/ext/cfg.rs @@ -22,7 +22,6 @@ use ext::build::AstBuilder; 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)) } diff --git a/src/libsyntax/ext/cfg_attr.rs b/src/libsyntax/ext/cfg_attr.rs index ad02b50f248..a85f12edb22 100644 --- a/src/libsyntax/ext/cfg_attr.rs +++ b/src/libsyntax/ext/cfg_attr.rs @@ -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), - } -} |
