Port more stuff to mark used attributes
This commit is contained in:
parent
50181add04
commit
e0648093d8
8 changed files with 63 additions and 53 deletions
|
@ -176,7 +176,7 @@ pub fn get_static_methods_if_impl(cstore: &cstore::CStore,
|
||||||
|
|
||||||
pub fn get_item_attrs(cstore: &cstore::CStore,
|
pub fn get_item_attrs(cstore: &cstore::CStore,
|
||||||
def_id: ast::DefId,
|
def_id: ast::DefId,
|
||||||
f: |Vec<@ast::MetaItem> |) {
|
f: |Vec<ast::Attribute> |) {
|
||||||
let cdata = cstore.get_crate_data(def_id.krate);
|
let cdata = cstore.get_crate_data(def_id.krate);
|
||||||
decoder::get_item_attrs(&*cdata, def_id.node, f)
|
decoder::get_item_attrs(&*cdata, def_id.node, f)
|
||||||
}
|
}
|
||||||
|
|
|
@ -953,20 +953,14 @@ pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
|
||||||
|
|
||||||
pub fn get_item_attrs(cdata: Cmd,
|
pub fn get_item_attrs(cdata: Cmd,
|
||||||
orig_node_id: ast::NodeId,
|
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;
|
// 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
|
// we assume that someone passing in a tuple struct ctor is actually wanting to
|
||||||
// look at the definition
|
// look at the definition
|
||||||
let node_id = get_tuple_struct_definition_if_ctor(cdata, orig_node_id);
|
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 node_id = node_id.map(|x| x.node).unwrap_or(orig_node_id);
|
||||||
let item = lookup_item(node_id, cdata.data());
|
let item = lookup_item(node_id, cdata.data());
|
||||||
reader::tagged_docs(item, tag_attributes, |attributes| {
|
f(get_attributes(item));
|
||||||
reader::tagged_docs(attributes, tag_attribute, |attribute| {
|
|
||||||
f(get_meta_items(attribute));
|
|
||||||
true
|
|
||||||
});
|
|
||||||
true
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {
|
fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {
|
||||||
|
|
|
@ -64,7 +64,7 @@ use collections::SmallIntMap;
|
||||||
use syntax::abi;
|
use syntax::abi;
|
||||||
use syntax::ast_map;
|
use syntax::ast_map;
|
||||||
use syntax::ast_util::IdVisitingOperation;
|
use syntax::ast_util::IdVisitingOperation;
|
||||||
use syntax::attr::{AttrMetaMethods, AttributeMethods};
|
use syntax::attr::AttrMetaMethods;
|
||||||
use syntax::attr;
|
use syntax::attr;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::parse::token::InternedString;
|
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]) {
|
fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) {
|
||||||
for attr in attrs.iter() {
|
for attr in attrs.iter() {
|
||||||
if !attr::is_used(attr) {
|
if !attr::is_used(attr) {
|
||||||
cx.span_lint(UnusedAttribute, attr.span,
|
cx.span_lint(UnusedAttribute, attr.span, "unused attribute");
|
||||||
format!("unused attribute {}", attr.name()).as_slice());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1654,9 +1653,7 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
|
||||||
let stability = if ast_util::is_local(id) {
|
let stability = if ast_util::is_local(id) {
|
||||||
// this crate
|
// this crate
|
||||||
let s = cx.tcx.map.with_attrs(id.node, |attrs| {
|
let s = cx.tcx.map.with_attrs(id.node, |attrs| {
|
||||||
attrs.map(|a| {
|
attrs.map(|a| attr::find_stability(a.as_slice()))
|
||||||
attr::find_stability(a.iter().map(|a| a.meta()))
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
match s {
|
match s {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
|
@ -1672,9 +1669,9 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
|
||||||
let mut s = None;
|
let mut s = None;
|
||||||
// run through all the attributes and take the first
|
// run through all the attributes and take the first
|
||||||
// stability one.
|
// 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() {
|
if s.is_none() {
|
||||||
s = attr::find_stability(meta_items.move_iter())
|
s = attr::find_stability(attrs.as_slice())
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
s
|
s
|
||||||
|
|
|
@ -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);
|
let f = decl_rust_fn(ccx, fn_ty, name);
|
||||||
|
|
||||||
csearch::get_item_attrs(&ccx.sess().cstore, did, |meta_items| {
|
csearch::get_item_attrs(&ccx.sess().cstore, did, |attrs| {
|
||||||
set_llvm_fn_attrs(meta_items.iter().map(|&x| {
|
set_llvm_fn_attrs(attrs.as_slice(), f)
|
||||||
attr::mk_attr_outer(attr::mk_attr_id(), x)
|
|
||||||
}).collect::<Vec<_>>().as_slice(), f)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ccx.externs.borrow_mut().insert(name.to_strbuf(), f);
|
ccx.externs.borrow_mut().insert(name.to_strbuf(), f);
|
||||||
|
|
|
@ -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
|
// (This should really be an iterator, but that would require csearch and
|
||||||
// decoder to use iterators instead of higher-order functions.)
|
// 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) {
|
if is_local(did) {
|
||||||
let item = tcx.map.expect_item(did.node);
|
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 {
|
} else {
|
||||||
|
info!("getting foreign attrs");
|
||||||
let mut cont = true;
|
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 {
|
if cont {
|
||||||
cont = meta_items.iter().advance(|ptrptr| f(*ptrptr));
|
cont = attrs.iter().advance(|attr| f(attr));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
info!("done");
|
||||||
cont
|
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 {
|
pub fn has_attr(tcx: &ctxt, did: DefId, attr: &str) -> bool {
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
each_attr(tcx, did, |item| {
|
each_attr(tcx, did, |item| {
|
||||||
if item.name().equiv(&attr) {
|
if item.check_name(attr) {
|
||||||
found = true;
|
found = true;
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1091,8 +1091,8 @@ impl<'a> fmt::Show for Item<'a> {
|
||||||
shortty(self.item), self.item.name.get_ref().as_slice()));
|
shortty(self.item), self.item.name.get_ref().as_slice()));
|
||||||
|
|
||||||
// Write stability attributes
|
// Write stability attributes
|
||||||
match attr::find_stability(self.item.attrs.iter()) {
|
match attr::find_stability_generic(self.item.attrs.iter()) {
|
||||||
Some(ref stability) => {
|
Some((ref stability, _)) => {
|
||||||
try!(write!(fmt,
|
try!(write!(fmt,
|
||||||
"<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
|
"<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
|
||||||
lvl = stability.level.to_str(),
|
lvl = stability.level.to_str(),
|
||||||
|
|
|
@ -238,14 +238,14 @@ pub fn contains_name<AM: AttrMetaMethods>(metas: &[AM], name: &str) -> bool {
|
||||||
debug!("attr::contains_name (name={})", name);
|
debug!("attr::contains_name (name={})", name);
|
||||||
metas.iter().any(|item| {
|
metas.iter().any(|item| {
|
||||||
debug!(" testing: {}", item.name());
|
debug!(" testing: {}", item.name());
|
||||||
item.name().equiv(&name)
|
item.check_name(name)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
|
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
|
||||||
-> Option<InternedString> {
|
-> Option<InternedString> {
|
||||||
attrs.iter()
|
attrs.iter()
|
||||||
.find(|at| at.name().equiv(&name))
|
.find(|at| at.check_name(name))
|
||||||
.and_then(|at| at.value_str())
|
.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> {
|
-> Option<InternedString> {
|
||||||
items.iter()
|
items.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.find(|mi| mi.name().equiv(&name))
|
.find(|mi| mi.check_name(name))
|
||||||
.and_then(|i| i.value_str())
|
.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> {
|
pub fn find_linkage_metas(attrs: &[Attribute]) -> Vec<@MetaItem> {
|
||||||
let mut result = Vec::new();
|
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 {
|
match attr.meta().node {
|
||||||
MetaList(_, ref items) => result.push_all(items.as_slice()),
|
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]
|
// FIXME (#2809)---validate the usage of #[inline] and #[inline]
|
||||||
attrs.iter().fold(InlineNone, |ia,attr| {
|
attrs.iter().fold(InlineNone, |ia,attr| {
|
||||||
match attr.node.value.node {
|
match attr.node.value.node {
|
||||||
MetaWord(ref n) if n.equiv(&("inline")) => InlineHint,
|
MetaWord(ref n) if n.equiv(&("inline")) => {
|
||||||
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
|
InlineHint
|
||||||
}
|
}
|
||||||
}
|
MetaList(ref n, ref items) if n.equiv(&("inline")) => {
|
||||||
_ => ia
|
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.
|
// this doesn't work.
|
||||||
let some_cfg_matches = metas.any(|mi| {
|
let some_cfg_matches = metas.any(|mi| {
|
||||||
debug!("testing name: {}", mi.name());
|
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");
|
debug!("is cfg");
|
||||||
no_cfgs = false;
|
no_cfgs = false;
|
||||||
// only #[cfg(...)] ones are understood.
|
// only #[cfg(...)] ones are understood.
|
||||||
|
@ -399,11 +403,13 @@ pub enum StabilityLevel {
|
||||||
Locked
|
Locked
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find the first stability attribute. `None` if none exists.
|
pub fn find_stability_generic<'a,
|
||||||
pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It)
|
AM: AttrMetaMethods,
|
||||||
-> Option<Stability> {
|
I: Iterator<&'a AM>>
|
||||||
for m in metas {
|
(mut attrs: I)
|
||||||
let level = match m.name().get() {
|
-> Option<(Stability, &'a AM)> {
|
||||||
|
for attr in attrs {
|
||||||
|
let level = match attr.name().get() {
|
||||||
"deprecated" => Deprecated,
|
"deprecated" => Deprecated,
|
||||||
"experimental" => Experimental,
|
"experimental" => Experimental,
|
||||||
"unstable" => Unstable,
|
"unstable" => Unstable,
|
||||||
|
@ -413,14 +419,22 @@ pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It)
|
||||||
_ => continue // not a stability level
|
_ => continue // not a stability level
|
||||||
};
|
};
|
||||||
|
|
||||||
return Some(Stability {
|
return Some((Stability {
|
||||||
level: level,
|
level: level,
|
||||||
text: m.value_str()
|
text: attr.value_str()
|
||||||
});
|
}, attr));
|
||||||
}
|
}
|
||||||
None
|
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]) {
|
pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) {
|
||||||
let mut set = HashSet::new();
|
let mut set = HashSet::new();
|
||||||
for meta in metas.iter() {
|
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
|
* present (before fields, if any) with that type; reprensentation
|
||||||
* optimizations which would remove it will not be done.
|
* 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 {
|
-> ReprAttr {
|
||||||
let mut acc = acc;
|
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")) => {
|
ast::MetaList(ref s, ref items) if s.equiv(&("repr")) => {
|
||||||
|
mark_used(attr);
|
||||||
for item in items.iter() {
|
for item in items.iter() {
|
||||||
match item.node {
|
match item.node {
|
||||||
ast::MetaWord(ref word) => {
|
ast::MetaWord(ref word) => {
|
||||||
|
|
|
@ -265,6 +265,8 @@ pub fn expand_item(it: @ast::Item, fld: &mut MacroExpander)
|
||||||
|
|
||||||
match fld.extsbox.find(&intern(mname.get())) {
|
match fld.extsbox.find(&intern(mname.get())) {
|
||||||
Some(&ItemDecorator(dec_fn)) => {
|
Some(&ItemDecorator(dec_fn)) => {
|
||||||
|
attr::mark_used(attr);
|
||||||
|
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: attr.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
|
@ -336,6 +338,7 @@ fn expand_item_modifiers(mut it: @ast::Item, fld: &mut MacroExpander)
|
||||||
|
|
||||||
match fld.extsbox.find(&intern(mname.get())) {
|
match fld.extsbox.find(&intern(mname.get())) {
|
||||||
Some(&ItemModifier(dec_fn)) => {
|
Some(&ItemModifier(dec_fn)) => {
|
||||||
|
attr::mark_used(attr);
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: attr.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue