From 71276c6abc5b7e0889db1f9e69e02eb5d2596f39 Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 18 Jul 2018 20:34:08 +0200 Subject: Add the -Zcrate-attr=foo nightly rustc flag to inject crate attributes --- src/libsyntax/attr/mod.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index d746ac3c577..137b94230a3 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -22,11 +22,11 @@ pub use self::ReprAttr::*; pub use self::StabilityLevel::*; use ast; -use ast::{AttrId, Attribute, Name, Ident, Path, PathSegment}; +use ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment}; use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind}; use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam}; use codemap::{BytePos, Spanned, respan, dummy_spanned}; -use syntax_pos::Span; +use syntax_pos::{FileName, Span}; use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use parse::parser::Parser; use parse::{self, ParseSess, PResult}; @@ -821,3 +821,33 @@ derive_has_attrs! { Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm, ast::Field, ast::FieldPat, ast::Variant_ } + +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::CliCrateAttr, + raw_attr.clone(), + ); + + let start_span = parser.span; + let (path, tokens) = panictry!(parser.parse_path_and_tokens()); + let end_span = parser.span; + if parser.token != token::Eof { + parse_sess.span_diagnostic + .span_err(start_span.to(end_span), "invalid crate attribute"); + continue; + } + + krate.attrs.push(Attribute { + id: mk_attr_id(), + style: AttrStyle::Inner, + path, + tokens, + is_sugared_doc: false, + span: start_span.to(end_span), + }); + } + + krate +} -- cgit 1.4.1-3-g733a5