rust/src/librustc_builtin_macros/cmdline_attrs.rs

31 lines
1022 B
Rust
Raw Normal View History

//! Attributes injected into the crate root from command line using `-Z crate-attr`.
use rustc_expand::panictry;
use syntax::ast::{self, AttrItem, AttrStyle};
use syntax::attr::mk_attr;
2019-10-14 03:08:26 -05:00
use syntax::sess::ParseSess;
2019-12-22 16:42:04 -06:00
use syntax::token;
use syntax_pos::FileName;
pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -> ast::Crate {
for raw_attr in attrs {
let mut parser = rustc_parse::new_parser_from_source_str(
parse_sess,
FileName::cli_crate_attr_source_code(&raw_attr),
raw_attr.clone(),
);
let start_span = parser.token.span;
let AttrItem { path, args } = panictry!(parser.parse_attr_item());
let end_span = parser.token.span;
if parser.token != token::Eof {
2019-12-22 16:42:04 -06:00
parse_sess.span_diagnostic.span_err(start_span.to(end_span), "invalid crate attribute");
continue;
}
krate.attrs.push(mk_attr(AttrStyle::Inner, path, args, start_span.to(end_span)));
}
krate
}