diff options
| author | bors <bors@rust-lang.org> | 2018-07-28 23:10:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-07-28 23:10:10 +0000 |
| commit | 6323d9a45bdf0ac2a9319a6a558537e0a7e6abd1 (patch) | |
| tree | a5564f46570f8ecb53639467095b4ff2a92b6688 /src/libsyntax | |
| parent | dab71516f1f4f6a63e32dffeb2625a12e5113485 (diff) | |
| parent | 71276c6abc5b7e0889db1f9e69e02eb5d2596f39 (diff) | |
| download | rust-6323d9a45bdf0ac2a9319a6a558537e0a7e6abd1.tar.gz rust-6323d9a45bdf0ac2a9319a6a558537e0a7e6abd1.zip | |
Auto merge of #52355 - pietroalbini:zfeature, r=eddyb
Add the -Zcrate-attr=foo unstable rustc option This PR adds a new unstable option to `rustc`: `-Zcrate-attr=foo`. The option can be used to inject crate-level attributes from the CLI, and it's meant to be used by tools like Crater that needs to add their own attributes to a crate without changing the source code. The exact reason I need this is to implement "edition runs" in Crater: we need to add the preview feature flag to every crate, and editing the crates' source code on the fly might produce unexpected results, while a compiler flag is more reliable. cc https://github.com/rust-lang-nursery/crater/issues/282 @Mark-Simulacrum
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/attr/mod.rs | 34 |
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 +} |
