Port more stuff to mark used attributes

This commit is contained in:
Steven Fackler 2014-05-21 00:05:45 -07:00
parent 50181add04
commit e0648093d8
8 changed files with 63 additions and 53 deletions

View file

@ -176,7 +176,7 @@ pub fn get_static_methods_if_impl(cstore: &cstore::CStore,
pub fn get_item_attrs(cstore: &cstore::CStore,
def_id: ast::DefId,
f: |Vec<@ast::MetaItem> |) {
f: |Vec<ast::Attribute> |) {
let cdata = cstore.get_crate_data(def_id.krate);
decoder::get_item_attrs(&*cdata, def_id.node, f)
}

View file

@ -953,20 +953,14 @@ pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
pub fn get_item_attrs(cdata: Cmd,
orig_node_id: ast::NodeId,
f: |Vec<@ast::MetaItem> |) {
f: |Vec<ast::Attribute>|) {
// The attributes for a tuple struct are attached to the definition, not the ctor;
// we assume that someone passing in a tuple struct ctor is actually wanting to
// look at the definition
let node_id = get_tuple_struct_definition_if_ctor(cdata, orig_node_id);
let node_id = node_id.map(|x| x.node).unwrap_or(orig_node_id);
let item = lookup_item(node_id, cdata.data());
reader::tagged_docs(item, tag_attributes, |attributes| {
reader::tagged_docs(attributes, tag_attribute, |attribute| {
f(get_meta_items(attribute));
true
});
true
});
f(get_attributes(item));
}
fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {

View file

@ -64,7 +64,7 @@ use collections::SmallIntMap;
use syntax::abi;
use syntax::ast_map;
use syntax::ast_util::IdVisitingOperation;
use syntax::attr::{AttrMetaMethods, AttributeMethods};
use syntax::attr::AttrMetaMethods;
use syntax::attr;
use syntax::codemap::Span;
use syntax::parse::token::InternedString;
@ -1148,8 +1148,7 @@ fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) {
fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) {
for attr in attrs.iter() {
if !attr::is_used(attr) {
cx.span_lint(UnusedAttribute, attr.span,
format!("unused attribute {}", attr.name()).as_slice());
cx.span_lint(UnusedAttribute, attr.span, "unused attribute");
}
}
}
@ -1654,9 +1653,7 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
let stability = if ast_util::is_local(id) {
// this crate
let s = cx.tcx.map.with_attrs(id.node, |attrs| {
attrs.map(|a| {
attr::find_stability(a.iter().map(|a| a.meta()))
})
attrs.map(|a| attr::find_stability(a.as_slice()))
});
match s {
Some(s) => s,
@ -1672,9 +1669,9 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
let mut s = None;
// run through all the attributes and take the first
// stability one.
csearch::get_item_attrs(&cx.tcx.sess.cstore, id, |meta_items| {
csearch::get_item_attrs(&cx.tcx.sess.cstore, id, |attrs| {
if s.is_none() {
s = attr::find_stability(meta_items.move_iter())
s = attr::find_stability(attrs.as_slice())
}
});
s

View file

@ -227,10 +227,8 @@ fn get_extern_rust_fn(ccx: &CrateContext, fn_ty: ty::t, name: &str, did: ast::De
let f = decl_rust_fn(ccx, fn_ty, name);
csearch::get_item_attrs(&ccx.sess().cstore, did, |meta_items| {
set_llvm_fn_attrs(meta_items.iter().map(|&x| {
attr::mk_attr_outer(attr::mk_attr_id(), x)
}).collect::<Vec<_>>().as_slice(), f)
csearch::get_item_attrs(&ccx.sess().cstore, did, |attrs| {
set_llvm_fn_attrs(attrs.as_slice(), f)
});
ccx.externs.borrow_mut().insert(name.to_strbuf(), f);

View file

@ -3889,20 +3889,22 @@ pub fn lookup_trait_def(cx: &ctxt, did: ast::DefId) -> Rc<ty::TraitDef> {
}
}
/// Iterate over meta_items of a definition.
/// Iterate over attributes of a definition.
// (This should really be an iterator, but that would require csearch and
// decoder to use iterators instead of higher-order functions.)
pub fn each_attr(tcx: &ctxt, did: DefId, f: |@ast::MetaItem| -> bool) -> bool {
pub fn each_attr(tcx: &ctxt, did: DefId, f: |&ast::Attribute| -> bool) -> bool {
if is_local(did) {
let item = tcx.map.expect_item(did.node);
item.attrs.iter().advance(|attr| f(attr.node.value))
item.attrs.iter().advance(|attr| f(attr))
} else {
info!("getting foreign attrs");
let mut cont = true;
csearch::get_item_attrs(&tcx.sess.cstore, did, |meta_items| {
csearch::get_item_attrs(&tcx.sess.cstore, did, |attrs| {
if cont {
cont = meta_items.iter().advance(|ptrptr| f(*ptrptr));
cont = attrs.iter().advance(|attr| f(attr));
}
});
info!("done");
cont
}
}
@ -3911,7 +3913,7 @@ pub fn each_attr(tcx: &ctxt, did: DefId, f: |@ast::MetaItem| -> bool) -> bool {
pub fn has_attr(tcx: &ctxt, did: DefId, attr: &str) -> bool {
let mut found = false;
each_attr(tcx, did, |item| {
if item.name().equiv(&attr) {
if item.check_name(attr) {
found = true;
false
} else {

View file

@ -1091,8 +1091,8 @@ impl<'a> fmt::Show for Item<'a> {
shortty(self.item), self.item.name.get_ref().as_slice()));
// Write stability attributes
match attr::find_stability(self.item.attrs.iter()) {
Some(ref stability) => {
match attr::find_stability_generic(self.item.attrs.iter()) {
Some((ref stability, _)) => {
try!(write!(fmt,
"<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
lvl = stability.level.to_str(),

View file

@ -238,14 +238,14 @@ pub fn contains_name<AM: AttrMetaMethods>(metas: &[AM], name: &str) -> bool {
debug!("attr::contains_name (name={})", name);
metas.iter().any(|item| {
debug!(" testing: {}", item.name());
item.name().equiv(&name)
item.check_name(name)
})
}
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
-> Option<InternedString> {
attrs.iter()
.find(|at| at.name().equiv(&name))
.find(|at| at.check_name(name))
.and_then(|at| at.value_str())
}
@ -253,7 +253,7 @@ pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str)
-> Option<InternedString> {
items.iter()
.rev()
.find(|mi| mi.name().equiv(&name))
.find(|mi| mi.check_name(name))
.and_then(|i| i.value_str())
}
@ -289,7 +289,7 @@ pub fn sort_meta_items(items: &[@MetaItem]) -> Vec<@MetaItem> {
*/
pub fn find_linkage_metas(attrs: &[Attribute]) -> Vec<@MetaItem> {
let mut result = Vec::new();
for attr in attrs.iter().filter(|at| at.name().equiv(&("link"))) {
for attr in attrs.iter().filter(|at| at.check_name("link")) {
match attr.meta().node {
MetaList(_, ref items) => result.push_all(items.as_slice()),
_ => ()
@ -318,17 +318,21 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
// FIXME (#2809)---validate the usage of #[inline] and #[inline]
attrs.iter().fold(InlineNone, |ia,attr| {
match attr.node.value.node {
MetaWord(ref n) if n.equiv(&("inline")) => InlineHint,
MetaList(ref n, ref items) if n.equiv(&("inline")) => {
if contains_name(items.as_slice(), "always") {
InlineAlways
} else if contains_name(items.as_slice(), "never") {
InlineNever
} else {
MetaWord(ref n) if n.equiv(&("inline")) => {
mark_used(attr);
InlineHint
}
}
_ => ia
MetaList(ref n, ref items) if n.equiv(&("inline")) => {
mark_used(attr);
if contains_name(items.as_slice(), "always") {
InlineAlways
} else if contains_name(items.as_slice(), "never") {
InlineNever
} else {
InlineHint
}
}
_ => ia
}
})
}
@ -348,7 +352,7 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>>
// this doesn't work.
let some_cfg_matches = metas.any(|mi| {
debug!("testing name: {}", mi.name());
if mi.name().equiv(&("cfg")) { // it is a #[cfg()] attribute
if mi.check_name("cfg") { // it is a #[cfg()] attribute
debug!("is cfg");
no_cfgs = false;
// only #[cfg(...)] ones are understood.
@ -399,11 +403,13 @@ pub enum StabilityLevel {
Locked
}
/// Find the first stability attribute. `None` if none exists.
pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It)
-> Option<Stability> {
for m in metas {
let level = match m.name().get() {
pub fn find_stability_generic<'a,
AM: AttrMetaMethods,
I: Iterator<&'a AM>>
(mut attrs: I)
-> Option<(Stability, &'a AM)> {
for attr in attrs {
let level = match attr.name().get() {
"deprecated" => Deprecated,
"experimental" => Experimental,
"unstable" => Unstable,
@ -413,14 +419,22 @@ pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It)
_ => continue // not a stability level
};
return Some(Stability {
return Some((Stability {
level: level,
text: m.value_str()
});
text: attr.value_str()
}, attr));
}
None
}
/// Find the first stability attribute. `None` if none exists.
pub fn find_stability(attrs: &[Attribute]) -> Option<Stability> {
find_stability_generic(attrs.iter()).map(|(s, attr)| {
mark_used(attr);
s
})
}
pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) {
let mut set = HashSet::new();
for meta in metas.iter() {
@ -447,11 +461,13 @@ pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) {
* present (before fields, if any) with that type; reprensentation
* optimizations which would remove it will not be done.
*/
pub fn find_repr_attr(diagnostic: &SpanHandler, attr: @ast::MetaItem, acc: ReprAttr)
pub fn find_repr_attr(diagnostic: &SpanHandler, attr: &Attribute, acc: ReprAttr)
-> ReprAttr {
let mut acc = acc;
match attr.node {
info!("{}", ::print::pprust::attribute_to_str(attr));
match attr.node.value.node {
ast::MetaList(ref s, ref items) if s.equiv(&("repr")) => {
mark_used(attr);
for item in items.iter() {
match item.node {
ast::MetaWord(ref word) => {

View file

@ -265,6 +265,8 @@ pub fn expand_item(it: @ast::Item, fld: &mut MacroExpander)
match fld.extsbox.find(&intern(mname.get())) {
Some(&ItemDecorator(dec_fn)) => {
attr::mark_used(attr);
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
@ -336,6 +338,7 @@ fn expand_item_modifiers(mut it: @ast::Item, fld: &mut MacroExpander)
match fld.extsbox.find(&intern(mname.get())) {
Some(&ItemModifier(dec_fn)) => {
attr::mark_used(attr);
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {