]>
Commit | Line | Data |
---|---|---|
e1599b0c XL |
1 | //! Attributes injected into the crate root from command line using `-Z crate-attr`. |
2 | ||
74b04a01 XL |
3 | use rustc_ast::attr::mk_attr; |
4 | use rustc_ast::token; | |
3dfed10e | 5 | use rustc_ast::{self as ast, AttrItem, AttrStyle}; |
74b04a01 | 6 | use rustc_session::parse::ParseSess; |
dfeec247 | 7 | use rustc_span::FileName; |
e1599b0c | 8 | |
353b0b11 | 9 | pub fn inject(krate: &mut ast::Crate, parse_sess: &ParseSess, attrs: &[String]) { |
e1599b0c | 10 | for raw_attr in attrs { |
60c5eb7d | 11 | let mut parser = rustc_parse::new_parser_from_source_str( |
e1599b0c XL |
12 | parse_sess, |
13 | FileName::cli_crate_attr_source_code(&raw_attr), | |
14 | raw_attr.clone(), | |
15 | ); | |
16 | ||
17 | let start_span = parser.token.span; | |
29967ef6 | 18 | let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item(false) { |
ba9703b0 XL |
19 | Ok(ai) => ai, |
20 | Err(mut err) => { | |
21 | err.emit(); | |
22 | continue; | |
23 | } | |
24 | }; | |
e1599b0c XL |
25 | let end_span = parser.token.span; |
26 | if parser.token != token::Eof { | |
dfeec247 | 27 | parse_sess.span_diagnostic.span_err(start_span.to(end_span), "invalid crate attribute"); |
e1599b0c XL |
28 | continue; |
29 | } | |
30 | ||
f2b60f7d FG |
31 | krate.attrs.push(mk_attr( |
32 | &parse_sess.attr_id_generator, | |
33 | AttrStyle::Inner, | |
34 | path, | |
35 | args, | |
36 | start_span.to(end_span), | |
37 | )); | |
e1599b0c | 38 | } |
e1599b0c | 39 | } |