summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2014-05-20 00:07:24 -0700
committerSteven Fackler <sfackler@gmail.com>2014-05-24 16:08:36 -0700
commitc305473d3c60d5b4590ef8c715468f718a7aad8f (patch)
treeb7b35aded387256555005b60a2faa16d9be3ad1f /src/libsyntax
parent6304a27b80f3923a8ffc009418c302aa8b06fb93 (diff)
downloadrust-c305473d3c60d5b4590ef8c715468f718a7aad8f.tar.gz
rust-c305473d3c60d5b4590ef8c715468f718a7aad8f.zip
Add AttrId to Attribute_
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs4
-rw-r--r--src/libsyntax/attr.rs36
-rw-r--r--src/libsyntax/ext/build.rs6
-rw-r--r--src/libsyntax/ext/deriving/clone.rs3
-rw-r--r--src/libsyntax/ext/deriving/cmp/eq.rs3
-rw-r--r--src/libsyntax/ext/deriving/cmp/ord.rs3
-rw-r--r--src/libsyntax/ext/deriving/cmp/totaleq.rs5
-rw-r--r--src/libsyntax/ext/deriving/cmp/totalord.rs3
-rw-r--r--src/libsyntax/ext/deriving/default.rs3
-rw-r--r--src/libsyntax/ext/deriving/generic/mod.rs2
-rw-r--r--src/libsyntax/ext/deriving/hash.rs3
-rw-r--r--src/libsyntax/ext/deriving/primitive.rs3
-rw-r--r--src/libsyntax/ext/deriving/zero.rs3
-rw-r--r--src/libsyntax/ext/expand.rs2
-rw-r--r--src/libsyntax/ext/format.rs7
-rw-r--r--src/libsyntax/fold.rs1
-rw-r--r--src/libsyntax/parse/attr.rs8
17 files changed, 74 insertions, 21 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index d4c01746098..e77d1faf05d 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -1024,9 +1024,13 @@ pub enum AttrStyle {
     AttrInner,
 }
 
+#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
+pub struct AttrId(pub uint);
+
 // doc-comments are promoted to attributes that have is_sugared_doc = true
 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
 pub struct Attribute_ {
+    pub id: AttrId,
     pub style: AttrStyle,
     pub value: @MetaItem,
     pub is_sugared_doc: bool,
diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs
index 77c335b8936..83ac2c08efb 100644
--- a/src/libsyntax/attr.rs
+++ b/src/libsyntax/attr.rs
@@ -11,7 +11,7 @@
 // Functions dealing with attributes and meta items
 
 use ast;
-use ast::{Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
+use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
 use codemap::{Span, Spanned, spanned, dummy_spanned};
 use codemap::BytePos;
 use diagnostic::SpanHandler;
@@ -22,6 +22,18 @@ use crateid::CrateId;
 
 use collections::HashSet;
 
+local_data_key!(used_attrs: HashSet<AttrId>)
+
+pub fn mark_used(attr: &Attribute) {
+    let mut used = used_attrs.replace(None).unwrap_or_else(|| HashSet::new());
+    used.insert(attr.node.id);
+    used_attrs.replace(Some(used));
+}
+
+pub fn is_used(attr: &Attribute) -> bool {
+    used_attrs.get().map_or(false, |used| used.contains(&attr.node.id))
+}
+
 pub trait AttrMetaMethods {
     // This could be changed to `fn check_name(&self, name: InternedString) ->
     // bool` which would facilitate a side table recording which
@@ -127,9 +139,9 @@ impl AttributeMethods for Attribute {
                 token::intern_and_get_ident(strip_doc_comment_decoration(
                         comment.get()).as_slice()));
             if self.node.style == ast::AttrOuter {
-                mk_attr_outer(meta)
+                mk_attr_outer(self.node.id, meta)
             } else {
-                mk_attr_inner(meta)
+                mk_attr_inner(self.node.id, meta)
             }
         } else {
             *self
@@ -158,9 +170,18 @@ pub fn mk_word_item(name: InternedString) -> @MetaItem {
     @dummy_spanned(MetaWord(name))
 }
 
+local_data_key!(next_attr_id: uint)
+
+pub fn mk_attr_id() -> AttrId {
+    let id = next_attr_id.replace(None).unwrap_or(0);
+    next_attr_id.replace(Some(id + 1));
+    AttrId(id)
+}
+
 /// Returns an inner attribute with the given value.
-pub fn mk_attr_inner(item: @MetaItem) -> Attribute {
+pub fn mk_attr_inner(id: AttrId, item: @MetaItem) -> Attribute {
     dummy_spanned(Attribute_ {
+        id: id,
         style: ast::AttrInner,
         value: item,
         is_sugared_doc: false,
@@ -168,19 +189,22 @@ pub fn mk_attr_inner(item: @MetaItem) -> Attribute {
 }
 
 /// Returns an outer attribute with the given value.
-pub fn mk_attr_outer(item: @MetaItem) -> Attribute {
+pub fn mk_attr_outer(id: AttrId, item: @MetaItem) -> Attribute {
     dummy_spanned(Attribute_ {
+        id: id,
         style: ast::AttrOuter,
         value: item,
         is_sugared_doc: false,
     })
 }
 
-pub fn mk_sugared_doc_attr(text: InternedString, lo: BytePos, hi: BytePos)
+pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos,
+                           hi: BytePos)
                            -> Attribute {
     let style = doc_comment_style(text.get());
     let lit = spanned(lo, hi, ast::LitStr(text, ast::CookedStr));
     let attr = Attribute_ {
+        id: id,
         style: style,
         value: @spanned(lo, hi, MetaNameValue(InternedString::new("doc"),
                                               lit)),
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index 3c7415ae0e9..44c177d19ca 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -231,7 +231,7 @@ pub trait AstBuilder {
                     generics: Generics) -> @ast::Item;
     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> @ast::Item;
 
-    fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute;
+    fn attribute(&self, id: AttrId, sp: Span, mi: @ast::MetaItem) -> ast::Attribute;
 
     fn meta_word(&self, sp: Span, w: InternedString) -> @ast::MetaItem;
     fn meta_list(&self,
@@ -925,8 +925,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
         self.item_ty_poly(span, name, ty, ast_util::empty_generics())
     }
 
-    fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute {
+    fn attribute(&self, id: ast::AttrId, sp: Span, mi: @ast::MetaItem)
+                 -> ast::Attribute {
         respan(sp, ast::Attribute_ {
+            id: id,
             style: ast::AttrOuter,
             value: mi,
             is_sugared_doc: false,
diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs
index 89c94891b33..73bfe9c27b6 100644
--- a/src/libsyntax/ext/deriving/clone.rs
+++ b/src/libsyntax/ext/deriving/clone.rs
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use attr;
 use ast::{MetaItem, Item, Expr};
 use codemap::Span;
 use ext::base::ExtCtxt;
@@ -21,7 +22,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
                              item: @Item,
                              push: |@Item|) {
     let inline = cx.meta_word(span, InternedString::new("inline"));
-    let attrs = vec!(cx.attribute(span, inline));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs
index 92b3788c247..31d6fcb9d6f 100644
--- a/src/libsyntax/ext/deriving/cmp/eq.rs
+++ b/src/libsyntax/ext/deriving/cmp/eq.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 use ast::{MetaItem, Item, Expr};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -34,7 +35,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
     macro_rules! md (
         ($name:expr, $f:ident) => { {
             let inline = cx.meta_word(span, InternedString::new("inline"));
-            let attrs = vec!(cx.attribute(span, inline));
+            let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
             MethodDef {
                 name: $name,
                 generics: LifetimeBounds::empty(),
diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs
index dd2f90cfa5f..3d79de4feb1 100644
--- a/src/libsyntax/ext/deriving/cmp/ord.rs
+++ b/src/libsyntax/ext/deriving/cmp/ord.rs
@@ -10,6 +10,7 @@
 
 use ast;
 use ast::{MetaItem, Item, Expr};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -24,7 +25,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
     macro_rules! md (
         ($name:expr, $op:expr, $equal:expr) => { {
             let inline = cx.meta_word(span, InternedString::new("inline"));
-            let attrs = vec!(cx.attribute(span, inline));
+            let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
             MethodDef {
                 name: $name,
                 generics: LifetimeBounds::empty(),
diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs
index b76caccffec..42bbf8e415a 100644
--- a/src/libsyntax/ext/deriving/cmp/totaleq.rs
+++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 use ast::{MetaItem, Item, Expr};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -37,8 +38,8 @@ pub fn expand_deriving_totaleq(cx: &mut ExtCtxt,
     let inline = cx.meta_word(span, InternedString::new("inline"));
     let hidden = cx.meta_word(span, InternedString::new("hidden"));
     let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
-    let attrs = vec!(cx.attribute(span, inline),
-                     cx.attribute(span, doc));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline),
+                     cx.attribute(attr::mk_attr_id(), span, doc));
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs
index 3ca4f9e2862..6413bdab344 100644
--- a/src/libsyntax/ext/deriving/cmp/totalord.rs
+++ b/src/libsyntax/ext/deriving/cmp/totalord.rs
@@ -10,6 +10,7 @@
 
 use ast;
 use ast::{MetaItem, Item, Expr};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -24,7 +25,7 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt,
                                 item: @Item,
                                 push: |@Item|) {
     let inline = cx.meta_word(span, InternedString::new("inline"));
-    let attrs = vec!(cx.attribute(span, inline));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs
index c225906ed2b..a0499a2cc1e 100644
--- a/src/libsyntax/ext/deriving/default.rs
+++ b/src/libsyntax/ext/deriving/default.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 use ast::{MetaItem, Item, Expr};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -21,7 +22,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
                             item: @Item,
                             push: |@Item|) {
     let inline = cx.meta_word(span, InternedString::new("inline"));
-    let attrs = vec!(cx.attribute(span, inline));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs
index 0875daddc0f..28595fecd89 100644
--- a/src/libsyntax/ext/deriving/generic/mod.rs
+++ b/src/libsyntax/ext/deriving/generic/mod.rs
@@ -182,6 +182,7 @@ use std::cell::RefCell;
 use ast;
 use ast::{P, EnumDef, Expr, Ident, Generics, StructDef};
 use ast_util;
+use attr;
 use attr::AttrMetaMethods;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -427,6 +428,7 @@ impl<'a> TraitDef<'a> {
                         self_ty_params.into_vec()), None);
 
         let attr = cx.attribute(
+            attr::mk_attr_id(),
             self.span,
             cx.meta_word(self.span,
                          InternedString::new("automatically_derived")));
diff --git a/src/libsyntax/ext/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs
index 3e6b8d522d4..8b368968f49 100644
--- a/src/libsyntax/ext/deriving/hash.rs
+++ b/src/libsyntax/ext/deriving/hash.rs
@@ -10,6 +10,7 @@
 
 use ast;
 use ast::{MetaItem, Item, Expr, MutMutable};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -37,7 +38,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
          Path::new(vec!("std", "hash", "sip", "SipState")))
     };
     let inline = cx.meta_word(span, InternedString::new("inline"));
-    let attrs = vec!(cx.attribute(span, inline));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
     let hash_trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/deriving/primitive.rs b/src/libsyntax/ext/deriving/primitive.rs
index 5066a395b41..e3621b51c4d 100644
--- a/src/libsyntax/ext/deriving/primitive.rs
+++ b/src/libsyntax/ext/deriving/primitive.rs
@@ -10,6 +10,7 @@
 
 use ast::{MetaItem, Item, Expr};
 use ast;
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -22,7 +23,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
                                       item: @Item,
                                       push: |@Item|) {
     let inline = cx.meta_word(span, InternedString::new("inline"));
-    let attrs = vec!(cx.attribute(span, inline));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/deriving/zero.rs b/src/libsyntax/ext/deriving/zero.rs
index 449851dd3ea..c60cdab9099 100644
--- a/src/libsyntax/ext/deriving/zero.rs
+++ b/src/libsyntax/ext/deriving/zero.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 use ast::{MetaItem, Item, Expr};
+use attr;
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -21,7 +22,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt,
                             item: @Item,
                             push: |@Item|) {
     let inline = cx.meta_word(span, InternedString::new("inline"));
-    let attrs = vec!(cx.attribute(span, inline));
+    let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 989d0a463c3..83118df5e65 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -972,6 +972,7 @@ mod test {
     use super::*;
     use ast;
     use ast::{Attribute_, AttrOuter, MetaWord};
+    use attr;
     use codemap;
     use codemap::Spanned;
     use ext::base::{CrateLoader, MacroCrate};
@@ -1103,6 +1104,7 @@ mod test {
         Spanned {
             span:codemap::DUMMY_SP,
             node: Attribute_ {
+                id: attr::mk_attr_id(),
                 style: AttrOuter,
                 value: @Spanned {
                     node: MetaWord(token::intern_and_get_ident(s)),
diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs
index ad4b798cfe5..c3b3c4eed57 100644
--- a/src/libsyntax/ext/format.rs
+++ b/src/libsyntax/ext/format.rs
@@ -10,6 +10,7 @@
 
 use ast;
 use ast::P;
+use attr;
 use codemap::{Span, respan};
 use ext::base::*;
 use ext::base;
@@ -382,7 +383,8 @@ impl<'a, 'b> Context<'a, 'b> {
                           .meta_word(self.fmtsp,
                                      InternedString::new(
                                          "address_insignificant"));
-        let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
+        let unnamed = self.ecx.attribute(attr::mk_attr_id(), self.fmtsp,
+                                         unnamed);
 
         // Do not warn format string as dead code
         let dead_code = self.ecx.meta_word(self.fmtsp,
@@ -390,7 +392,8 @@ impl<'a, 'b> Context<'a, 'b> {
         let allow_dead_code = self.ecx.meta_list(self.fmtsp,
                                                  InternedString::new("allow"),
                                                  vec!(dead_code));
-        let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code);
+        let allow_dead_code = self.ecx.attribute(attr::mk_attr_id(), self.fmtsp,
+                                                 allow_dead_code);
         return vec!(unnamed, allow_dead_code);
     }
 
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 9813e12de01..ae5cf550bb9 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -360,6 +360,7 @@ fn fold_attribute_<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
     Spanned {
         span: fld.new_span(at.span),
         node: ast::Attribute_ {
+            id: at.node.id,
             style: at.node.style,
             value: fold_meta_item_(at.node.value, fld),
             is_sugared_doc: at.node.is_sugared_doc
diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs
index 89d1b8f9342..e86dcb3d311 100644
--- a/src/libsyntax/parse/attr.rs
+++ b/src/libsyntax/parse/attr.rs
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use attr;
 use ast;
 use codemap::{spanned, Spanned, mk_sp, Span};
 use parse::common::*; //resolve bug?
@@ -39,6 +40,7 @@ impl<'a> ParserAttr for Parser<'a> {
               }
               token::DOC_COMMENT(s) => {
                 let attr = ::attr::mk_sugared_doc_attr(
+                    attr::mk_attr_id(),
                     self.id_to_interned_str(s),
                     self.span.lo,
                     self.span.hi
@@ -101,6 +103,7 @@ impl<'a> ParserAttr for Parser<'a> {
         return Spanned {
             span: span,
             node: ast::Attribute_ {
+                id: attr::mk_attr_id(),
                 style: style,
                 value: value,
                 is_sugared_doc: false
@@ -132,7 +135,10 @@ impl<'a> ParserAttr for Parser<'a> {
                     // we need to get the position of this token before we bump.
                     let Span { lo, hi, .. } = self.span;
                     self.bump();
-                    ::attr::mk_sugared_doc_attr(self.id_to_interned_str(s), lo, hi)
+                    ::attr::mk_sugared_doc_attr(attr::mk_attr_id(),
+                                                self.id_to_interned_str(s),
+                                                lo,
+                                                hi)
                 }
                 _ => {
                     break;