Skip to content

syntax: modernise attribute handling in syntax::attr. #7902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::vec;
use syntax::ast;
use syntax::ast_map::{path, path_mod, path_name};
use syntax::attr;
use syntax::attr::{AttrMetaMethods};
use syntax::print::pprust;
use syntax::parse::token;

Expand Down Expand Up @@ -502,7 +503,7 @@ pub fn build_link_meta(sess: Session,
struct ProvidedMetas {
name: Option<@str>,
vers: Option<@str>,
cmh_items: ~[@ast::meta_item]
cmh_items: ~[@ast::MetaItem]
}

fn provided_link_metas(sess: Session, c: &ast::crate) ->
Expand All @@ -513,18 +514,10 @@ pub fn build_link_meta(sess: Session,
let linkage_metas = attr::find_linkage_metas(c.node.attrs);
attr::require_unique_names(sess.diagnostic(), linkage_metas);
for linkage_metas.iter().advance |meta| {
match attr::get_meta_item_value_str(*meta) {
Some(value) => {
let item_name : &str = attr::get_meta_item_name(*meta);
match item_name {
// Changing attr would avoid the need for the copy
// here
"name" => name = Some(value),
"vers" => vers = Some(value),
_ => cmh_items.push(*meta)
}
},
None => cmh_items.push(*meta)
match meta.name_str_pair() {
Some((n, value)) if "name" == n => name = Some(value),
Some((n, value)) if "vers" == n => vers = Some(value),
_ => cmh_items.push(*meta)
}
}

Expand All @@ -537,7 +530,7 @@ pub fn build_link_meta(sess: Session,

// This calculates CMH as defined above
fn crate_meta_extras_hash(symbol_hasher: &mut hash::State,
cmh_items: ~[@ast::meta_item],
cmh_items: ~[@ast::MetaItem],
dep_hashes: ~[@str]) -> @str {
fn len_and_str(s: &str) -> ~str {
fmt!("%u_%s", s.len(), s)
Expand All @@ -549,16 +542,16 @@ pub fn build_link_meta(sess: Session,

let cmh_items = attr::sort_meta_items(cmh_items);

fn hash(symbol_hasher: &mut hash::State, m: &@ast::meta_item) {
fn hash(symbol_hasher: &mut hash::State, m: &@ast::MetaItem) {
match m.node {
ast::meta_name_value(key, value) => {
ast::MetaNameValue(key, value) => {
write_string(symbol_hasher, len_and_str(key));
write_string(symbol_hasher, len_and_str_lit(value));
}
ast::meta_word(name) => {
ast::MetaWord(name) => {
write_string(symbol_hasher, len_and_str(name));
}
ast::meta_list(name, ref mis) => {
ast::MetaList(name, ref mis) => {
write_string(symbol_hasher, len_and_str(name));
for mis.iter().advance |m_| {
hash(symbol_hasher, m_);
Expand Down
43 changes: 17 additions & 26 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use extra::getopts;
use syntax::ast;
use syntax::abi;
use syntax::attr;
use syntax::attr::{AttrMetaMethods};
use syntax::codemap;
use syntax::diagnostic;
use syntax::parse;
Expand Down Expand Up @@ -95,12 +96,9 @@ pub fn default_configuration(sess: Session, argv0: @str, input: &input) ->
mk(@"build_input", source_name(input))];
}

pub fn append_configuration(cfg: ast::crate_cfg, name: @str)
-> ast::crate_cfg {
if attr::contains_name(cfg, name) {
cfg
} else {
vec::append_one(cfg, attr::mk_word_item(name))
pub fn append_configuration(cfg: &mut ast::crate_cfg, name: @str) {
if !cfg.iter().any(|mi| mi.name() == name) {
cfg.push(attr::mk_word_item(name))
}
}

Expand All @@ -109,18 +107,11 @@ pub fn build_configuration(sess: Session, argv0: @str, input: &input) ->
// Combine the configuration requested by the session (command line) with
// some default and generated configuration items
let default_cfg = default_configuration(sess, argv0, input);
let user_cfg = sess.opts.cfg.clone();
let mut user_cfg = sess.opts.cfg.clone();
// If the user wants a test runner, then add the test cfg
let user_cfg = if sess.opts.test {
append_configuration(user_cfg, @"test")
} else {
user_cfg
};

if sess.opts.test { append_configuration(&mut user_cfg, @"test") }
// If the user requested GC, then add the GC cfg
let user_cfg = append_configuration(
user_cfg,
if sess.opts.gc { @"gc" } else { @"nogc" });
append_configuration(&mut user_cfg, if sess.opts.gc { @"gc" } else { @"nogc" });
return vec::append(user_cfg, default_cfg);
}

Expand All @@ -130,7 +121,7 @@ fn parse_cfgspecs(cfgspecs: ~[~str],
do cfgspecs.consume_iter().transform |s| {
let sess = parse::new_parse_sess(Some(demitter));
parse::parse_meta_from_source_str(@"cfgspec", s.to_managed(), ~[], sess)
}.collect()
}.collect::<ast::crate_cfg>()
}

pub enum input {
Expand Down Expand Up @@ -215,6 +206,7 @@ pub fn compile_rest(sess: Session,
crate = time(time_passes, ~"configuration 2", ||
front::config::strip_unconfigured_items(crate));


crate = time(time_passes, ~"maybe building test harness", ||
front::test::modify_for_testing(sess, crate));
}
Expand Down Expand Up @@ -870,7 +862,7 @@ pub struct OutputFilenames {
pub fn build_output_filenames(input: &input,
odir: &Option<Path>,
ofile: &Option<Path>,
attrs: &[ast::attribute],
attrs: &[ast::Attribute],
sess: Session)
-> @OutputFilenames {
let obj_path;
Expand Down Expand Up @@ -912,12 +904,10 @@ pub fn build_output_filenames(input: &input,
let linkage_metas = attr::find_linkage_metas(attrs);
if !linkage_metas.is_empty() {
// But if a linkage meta is present, that overrides
let maybe_matches = attr::find_meta_items_by_name(linkage_metas, "name");
if !maybe_matches.is_empty() {
match attr::get_meta_item_value_str(maybe_matches[0]) {
Some(s) => stem = s,
_ => ()
}
let maybe_name = linkage_metas.iter().find_(|m| "name" == m.name());
match maybe_name.chain(|m| m.value_str()) {
Some(s) => stem = s,
_ => ()
}
// If the name is missing, we just default to the filename
// version
Expand Down Expand Up @@ -1011,7 +1001,8 @@ mod test {
@"rustc", matches, diagnostic::emit);
let sess = build_session(sessopts, diagnostic::emit);
let cfg = build_configuration(sess, @"whatever", &str_input(@""));
let test_items = attr::find_meta_items_by_name(cfg, "test");
assert_eq!(test_items.len(), 1u);
let mut test_items = cfg.iter().filter(|m| "test" == m.name());
assert!(test_items.next().is_some());
assert!(test_items.next().is_none());
}
}
15 changes: 4 additions & 11 deletions src/librustc/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub fn building_library(req_crate_type: crate_type,
match syntax::attr::first_attr_value_str_by_name(
crate.node.attrs,
"crate_type") {
Some(s) if "lib" == s => true,
Some(s) => "lib" == s,
_ => false
}
}
Expand All @@ -395,18 +395,11 @@ mod test {
use driver::session::{unknown_crate};

use syntax::ast;
use syntax::attr;
use syntax::codemap;

fn make_crate_type_attr(t: @str) -> ast::attribute {
codemap::respan(codemap::dummy_sp(), ast::attribute_ {
style: ast::attr_outer,
value: @codemap::respan(codemap::dummy_sp(),
ast::meta_name_value(
@"crate_type",
codemap::respan(codemap::dummy_sp(),
ast::lit_str(t)))),
is_sugared_doc: false
})
fn make_crate_type_attr(t: @str) -> ast::Attribute {
attr::mk_attr(attr::mk_name_value_item_str(@"crate_type", t))
}

fn make_crate(with_bin: bool, with_lib: bool) -> @ast::crate {
Expand Down
31 changes: 3 additions & 28 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use std::option;
use syntax::{ast, fold, attr};

type in_cfg_pred = @fn(attrs: &[ast::attribute]) -> bool;
type in_cfg_pred = @fn(attrs: &[ast::Attribute]) -> bool;

struct Context {
in_cfg: in_cfg_pred
Expand Down Expand Up @@ -175,31 +175,6 @@ fn trait_method_in_cfg(cx: @Context, meth: &ast::trait_method) -> bool {

// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(cfg: &[@ast::meta_item], attrs: &[ast::attribute]) -> bool {
metas_in_cfg(cfg, attr::attr_metas(attrs))
}

pub fn metas_in_cfg(cfg: &[@ast::meta_item],
metas: &[@ast::meta_item]) -> bool {
// The "cfg" attributes on the item
let cfg_metas = attr::find_meta_items_by_name(metas, "cfg");

// Pull the inner meta_items from the #[cfg(meta_item, ...)] attributes,
// so we can match against them. This is the list of configurations for
// which the item is valid
let cfg_metas = cfg_metas.consume_iter()
.filter_map(|i| attr::get_meta_item_list(i))
.collect::<~[~[@ast::meta_item]]>();

if cfg_metas.iter().all(|c| c.is_empty()) { return true; }

cfg_metas.iter().any(|cfg_meta| {
cfg_meta.iter().all(|cfg_mi| {
match cfg_mi.node {
ast::meta_list(s, ref it) if "not" == s
=> it.iter().all(|mi| !attr::contains(cfg, *mi)),
_ => attr::contains(cfg, *cfg_mi)
}
})
})
fn in_cfg(cfg: &[@ast::MetaItem], attrs: &[ast::Attribute]) -> bool {
attr::test_cfg(cfg, attrs.iter().transform(|x| *x))
}
16 changes: 5 additions & 11 deletions src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ pub fn maybe_inject_libstd_ref(sess: Session, crate: @ast::crate)
}

fn use_std(crate: &ast::crate) -> bool {
!attr::attrs_contains_name(crate.node.attrs, "no_std")
!attr::contains_name(crate.node.attrs, "no_std")
}
fn no_prelude(attrs: &[ast::attribute]) -> bool {
attr::attrs_contains_name(attrs, "no_implicit_prelude")
fn no_prelude(attrs: &[ast::Attribute]) -> bool {
attr::contains_name(attrs, "no_implicit_prelude")
}

fn inject_libstd_ref(sess: Session, crate: &ast::crate) -> @ast::crate {
Expand All @@ -48,14 +48,8 @@ fn inject_libstd_ref(sess: Session, crate: &ast::crate) -> @ast::crate {
node: ast::view_item_extern_mod(
sess.ident_of("std"), ~[], n1),
attrs: ~[
spanned(ast::attribute_ {
style: ast::attr_inner,
value: @spanned(ast::meta_name_value(
@"vers",
spanned(ast::lit_str(STD_VERSION.to_managed()))
)),
is_sugared_doc: false
})
attr::mk_attr(
attr::mk_name_value_item_str(@"vers", STD_VERSION.to_managed()))
],
vis: ast::private,
span: dummy_sp()
Expand Down
Loading