From 71276c6abc5b7e0889db1f9e69e02eb5d2596f39 Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 18 Jul 2018 20:34:08 +0200 Subject: [PATCH] Add the -Zcrate-attr=foo nightly rustc flag to inject crate attributes --- src/librustc/ich/impls_syntax.rs | 1 + src/librustc/session/config.rs | 2 ++ src/librustc_driver/driver.rs | 6 +++++- src/libsyntax/attr/mod.rs | 34 +++++++++++++++++++++++++++++-- src/libsyntax_pos/lib.rs | 5 +++++ src/test/run-pass/z-crate-attr.rs | 21 +++++++++++++++++++ 6 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/test/run-pass/z-crate-attr.rs diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index c9ac6cdedbbc6..c106966fb70be 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -423,6 +423,7 @@ impl_stable_hash_for!(enum ::syntax_pos::FileName { Anon, MacroExpansion, ProcMacroSourceCode, + CliCrateAttr, CfgSpec, Custom(s) }); diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 4490b2f3fa91a..6ba2a1bac1157 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1369,6 +1369,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "don't run LLVM in parallel (while keeping codegen-units and ThinLTO)"), no_leak_check: bool = (false, parse_bool, [UNTRACKED], "disables the 'leak check' for subtyping; unsound, but useful for tests"), + crate_attr: Vec = (Vec::new(), parse_string_push, [TRACKED], + "inject the given attribute in the crate"), } pub fn default_lib_output() -> CrateType { diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 91392ab013d6c..b04cffe8da501 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -798,7 +798,7 @@ where pub fn phase_2_configure_and_expand_inner<'a, F>( sess: &'a Session, cstore: &'a CStore, - krate: ast::Crate, + mut krate: ast::Crate, registry: Option, crate_name: &str, addl_plugins: Option>, @@ -810,6 +810,10 @@ pub fn phase_2_configure_and_expand_inner<'a, F>( where F: FnOnce(&ast::Crate) -> CompileResult, { + krate = time(sess, "attributes injection", || { + syntax::attr::inject(krate, &sess.parse_sess, &sess.opts.debugging_opts.crate_attr) + }); + let (mut krate, features) = syntax::config::features( krate, &sess.parse_sess, diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index d746ac3c5771b..137b94230a3cd 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 +} diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index cc09a944e4ccc..a4d0bcd5c7662 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -100,6 +100,8 @@ pub enum FileName { ProcMacroSourceCode, /// Strings provided as --cfg [cfgspec] stored in a crate_cfg CfgSpec, + /// Strings provided as crate attributes in the CLI + CliCrateAttr, /// Custom sources for explicit parser calls from plugins and drivers Custom(String), } @@ -115,6 +117,7 @@ impl std::fmt::Display for FileName { Anon => write!(fmt, ""), ProcMacroSourceCode => write!(fmt, ""), CfgSpec => write!(fmt, "cfgspec"), + CliCrateAttr => write!(fmt, ""), Custom(ref s) => write!(fmt, "<{}>", s), } } @@ -137,6 +140,7 @@ impl FileName { MacroExpansion | ProcMacroSourceCode | CfgSpec | + CliCrateAttr | Custom(_) | QuoteExpansion => false, } @@ -150,6 +154,7 @@ impl FileName { MacroExpansion | ProcMacroSourceCode | CfgSpec | + CliCrateAttr | Custom(_) | QuoteExpansion => false, Macros(_) => true, diff --git a/src/test/run-pass/z-crate-attr.rs b/src/test/run-pass/z-crate-attr.rs new file mode 100644 index 0000000000000..3df0985a2a38b --- /dev/null +++ b/src/test/run-pass/z-crate-attr.rs @@ -0,0 +1,21 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This test checks if an unstable feature is enabled with the -Zcrate-attr=feature(foo) flag. If +// the exact feature used here is causing problems feel free to replace it with another +// perma-unstable feature. + +// compile-flags: -Zcrate-attr=feature(abi_unadjusted) + +#![allow(dead_code)] + +extern "unadjusted" fn foo() {} + +fn main() {}