2019-09-06 15:41:54 -05:00
|
|
|
//! Attributes injected into the crate root from command line using `-Z crate-attr`.
|
|
|
|
|
2019-08-17 17:10:56 -05:00
|
|
|
use syntax::ast::{self, AttrItem, AttrStyle};
|
2019-09-06 15:41:54 -05:00
|
|
|
use syntax::attr::mk_attr;
|
|
|
|
use syntax::panictry;
|
2019-10-14 03:08:26 -05:00
|
|
|
use syntax::parse::{self, token};
|
|
|
|
use syntax::sess::ParseSess;
|
2019-09-06 15:41:54 -05:00
|
|
|
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 = 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;
|
2019-08-17 17:10:56 -05:00
|
|
|
let AttrItem { path, tokens } = panictry!(parser.parse_attr_item());
|
2019-09-06 15:41:54 -05:00
|
|
|
let end_span = parser.token.span;
|
|
|
|
if parser.token != token::Eof {
|
|
|
|
parse_sess.span_diagnostic
|
|
|
|
.span_err(start_span.to(end_span), "invalid crate attribute");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
krate.attrs.push(mk_attr(AttrStyle::Inner, path, tokens, start_span.to(end_span)));
|
|
|
|
}
|
|
|
|
|
|
|
|
krate
|
|
|
|
}
|