about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2018-07-18 20:34:08 +0200
committerPietro Albini <pietro@pietroalbini.org>2018-07-27 19:51:21 +0200
commit71276c6abc5b7e0889db1f9e69e02eb5d2596f39 (patch)
tree190d317f72be167b50eae66cc4563f50695a1628 /src/libsyntax
parentb18b9edf006c10f4e08794d31425001401e27a09 (diff)
downloadrust-71276c6abc5b7e0889db1f9e69e02eb5d2596f39.tar.gz
rust-71276c6abc5b7e0889db1f9e69e02eb5d2596f39.zip
Add the -Zcrate-attr=foo nightly rustc flag to inject crate attributes
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/attr/mod.rs34
1 files changed, 32 insertions, 2 deletions
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
+}