2019-09-06 20:41:54 +00:00
|
|
|
//! Attributes injected into the crate root from command line using `-Z crate-attr`.
|
|
|
|
|
2023-06-21 11:01:53 +00:00
|
|
|
use crate::errors;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::attr::mk_attr;
|
|
|
|
use rustc_ast::token;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::{self as ast, AttrItem, AttrStyle};
|
2020-01-11 14:03:15 +00:00
|
|
|
use rustc_session::parse::ParseSess;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::FileName;
|
2019-09-06 20:41:54 +00:00
|
|
|
|
2023-02-18 12:23:57 +00:00
|
|
|
pub fn inject(krate: &mut ast::Crate, parse_sess: &ParseSess, attrs: &[String]) {
|
2019-09-06 20:41:54 +00:00
|
|
|
for raw_attr in attrs {
|
2019-10-15 20:48:13 +00:00
|
|
|
let mut parser = rustc_parse::new_parser_from_source_str(
|
2019-09-06 20:41:54 +00:00
|
|
|
parse_sess,
|
|
|
|
FileName::cli_crate_attr_source_code(&raw_attr),
|
|
|
|
raw_attr.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let start_span = parser.token.span;
|
2020-09-26 23:33:42 +00:00
|
|
|
let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item(false) {
|
2020-03-17 07:59:56 +00:00
|
|
|
Ok(ai) => ai,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2019-09-06 20:41:54 +00:00
|
|
|
let end_span = parser.token.span;
|
|
|
|
if parser.token != token::Eof {
|
2023-06-21 11:01:53 +00:00
|
|
|
parse_sess
|
|
|
|
.span_diagnostic
|
|
|
|
.emit_err(errors::InvalidCrateAttr { span: start_span.to(end_span) });
|
2019-09-06 20:41:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:29:40 +00:00
|
|
|
krate.attrs.push(mk_attr(
|
|
|
|
&parse_sess.attr_id_generator,
|
|
|
|
AttrStyle::Inner,
|
|
|
|
path,
|
|
|
|
args,
|
|
|
|
start_span.to(end_span),
|
|
|
|
));
|
2019-09-06 20:41:54 +00:00
|
|
|
}
|
|
|
|
}
|