1
Fork 0

syntax: modernise attribute handling in syntax::attr.

This does a number of things, but especially dramatically reduce the
number of allocations performed for operations involving attributes/
meta items:

- Converts ast::meta_item & ast::attribute and other associated enums
  to CamelCase.
- Converts several standalone functions in syntax::attr into methods,
  defined on two traits AttrMetaMethods & AttributeMethods. The former
  is common to both MetaItem and Attribute since the latter is a thin
  wrapper around the former.
- Deletes functions that are unnecessary due to iterators.
- Converts other standalone functions to use iterators and the generic
  AttrMetaMethods rather than allocating a lot of new vectors (e.g. the
  old code would have to allocate a new vector to use functions that
  operated on &[meta_item] on &[attribute].)
- Moves the core algorithm of the #[cfg] matching to syntax::attr,
  similar to find_inline_attr and find_linkage_metas.

This doesn't have much of an effect on the speed of #[cfg] stripping,
despite hugely reducing the number of allocations performed; presumably
most of the time is spent in the ast folder rather than doing attribute
checks.

Also fixes the Eq instance of MetaItem_ to correctly ignore spaces, so
that `rustc --cfg 'foo(bar)'` now works.
This commit is contained in:
Huon Wilson 2013-07-19 21:51:37 +10:00
parent 32586faa6a
commit cc760a647a
60 changed files with 738 additions and 821 deletions

View file

@ -14,6 +14,7 @@ use ast::{illegal_ctxt};
use ast;
use ast_util::{new_rename, new_mark, resolve};
use attr;
use attr::AttrMetaMethods;
use codemap;
use codemap::{span, ExpnInfo, NameAndSpan};
use ext::base::*;
@ -126,7 +127,7 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv,
// the item into a new set of items.
let new_items = do vec::flat_map(module_.items) |item| {
do item.attrs.rev_iter().fold(~[*item]) |items, attr| {
let mname = attr::get_attr_name(attr);
let mname = attr.name();
match (*extsbox).find(&intern(mname)) {
Some(@SE(ItemDecorator(dec_fn))) => {
@ -196,8 +197,8 @@ pub fn expand_item(extsbox: @mut SyntaxEnv,
}
// does this attribute list contain "macro_escape" ?
pub fn contains_macro_escape (attrs: &[ast::attribute]) -> bool {
attrs.iter().any(|attr| "macro_escape" == attr::get_attr_name(attr))
pub fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool {
attr::contains_name(attrs, "macro_escape")
}
// Support for item-position macro invocations, exactly the same
@ -793,7 +794,7 @@ pub fn new_ident_resolver() ->
mod test {
use super::*;
use ast;
use ast::{attribute_, attr_outer, meta_word, empty_ctxt};
use ast::{Attribute_, AttrOuter, MetaWord, empty_ctxt};
use codemap;
use codemap::spanned;
use parse;
@ -883,14 +884,14 @@ mod test {
assert_eq!(contains_macro_escape (attrs2),false);
}
// make a "meta_word" outer attribute with the given name
fn make_dummy_attr(s: @str) -> ast::attribute {
// make a MetaWord outer attribute with the given name
fn make_dummy_attr(s: @str) -> ast::Attribute {
spanned {
span:codemap::dummy_sp(),
node: attribute_ {
style: attr_outer,
node: Attribute_ {
style: AttrOuter,
value: @spanned {
node: meta_word(s),
node: MetaWord(s),
span: codemap::dummy_sp(),
},
is_sugared_doc: false,