Skip to content

Commit

Permalink
Rollup merge of #64243 - petrochenkov:cmdattr, r=alexcrichton
Browse files Browse the repository at this point in the history
Move injection of attributes from command line to `libsyntax_ext`

Just a tiny bit of code generation that wasn't moved into `libsyntax_ext` in #62771.
  • Loading branch information
Centril authored Sep 7, 2019
2 parents 69121f5 + 3dde650 commit 3d4cb50
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
4 changes: 3 additions & 1 deletion src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ pub fn register_plugins<'a>(
crate_name: &str,
) -> Result<(ast::Crate, PluginInfo)> {
krate = time(sess, "attributes injection", || {
syntax::attr::inject(krate, &sess.parse_sess, &sess.opts.debugging_opts.crate_attr)
syntax_ext::cmdline_attrs::inject(
krate, &sess.parse_sess, &sess.opts.debugging_opts.crate_attr
)
});

let (krate, features) = syntax::config::features(
Expand Down
59 changes: 13 additions & 46 deletions src/libsyntax/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::mut_visit::visit_clobber;
use crate::source_map::{BytePos, Spanned, DUMMY_SP};
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use crate::parse::parser::Parser;
use crate::parse::{self, ParseSess, PResult};
use crate::parse::{ParseSess, PResult};
use crate::parse::token::{self, Token};
use crate::ptr::P;
use crate::symbol::{sym, Symbol};
Expand All @@ -25,7 +25,7 @@ use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
use crate::GLOBALS;

use log::debug;
use syntax_pos::{FileName, Span};
use syntax_pos::Span;

use std::iter;
use std::ops::DerefMut;
Expand Down Expand Up @@ -381,28 +381,25 @@ crate fn mk_attr_id() -> AttrId {
AttrId(id)
}

/// Returns an inner attribute with the given value and span.
pub fn mk_attr_inner(item: MetaItem) -> Attribute {
pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute {
Attribute {
id: mk_attr_id(),
style: ast::AttrStyle::Inner,
path: item.path,
tokens: item.node.tokens(item.span),
style,
path,
tokens,
is_sugared_doc: false,
span: item.span,
span,
}
}

/// Returns an inner attribute with the given value and span.
pub fn mk_attr_inner(item: MetaItem) -> Attribute {
mk_attr(AttrStyle::Inner, item.path, item.node.tokens(item.span), item.span)
}

/// Returns an outer attribute with the given value and span.
pub fn mk_attr_outer(item: MetaItem) -> Attribute {
Attribute {
id: mk_attr_id(),
style: ast::AttrStyle::Outer,
path: item.path,
tokens: item.node.tokens(item.span),
is_sugared_doc: false,
span: item.span,
}
mk_attr(AttrStyle::Outer, item.path, item.node.tokens(item.span), item.span)
}

pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
Expand Down Expand Up @@ -716,33 +713,3 @@ derive_has_attrs! {
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm,
ast::Field, ast::FieldPat, ast::Variant, ast::Param
}

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::cli_crate_attr_source_code(&raw_attr),
raw_attr.clone(),
);

let start_span = parser.token.span;
let (path, tokens) = panictry!(parser.parse_meta_item_unrestricted());
let end_span = parser.token.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
}
2 changes: 1 addition & 1 deletion src/libsyntax/parse/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'a> Parser<'a> {
/// PATH
/// PATH `=` TOKEN_TREE
/// The delimiters or `=` are still put into the resulting token stream.
crate fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
pub fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
let meta = match self.token.kind {
token::Interpolated(ref nt) => match **nt {
Nonterminal::NtMeta(ref meta) => Some(meta.clone()),
Expand Down
30 changes: 30 additions & 0 deletions src/libsyntax_ext/cmdline_attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Attributes injected into the crate root from command line using `-Z crate-attr`.

use syntax::ast::{self, AttrStyle};
use syntax::attr::mk_attr;
use syntax::panictry;
use syntax::parse::{self, token, ParseSess};
use syntax_pos::FileName;

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::cli_crate_attr_source_code(&raw_attr),
raw_attr.clone(),
);

let start_span = parser.token.span;
let (path, tokens) = panictry!(parser.parse_meta_item_unrestricted());
let end_span = parser.token.span;
if parser.token != token::Eof {
parse_sess.span_diagnostic
.span_err(start_span.to(end_span), "invalid crate attribute");
continue;
}

krate.attrs.push(mk_attr(AttrStyle::Inner, path, tokens, start_span.to(end_span)));
}

krate
}
1 change: 1 addition & 0 deletions src/libsyntax_ext/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod source_util;
mod test;
mod trace_macros;

pub mod cmdline_attrs;
pub mod plugin_macro_defs;
pub mod proc_macro_harness;
pub mod standard_library_imports;
Expand Down

0 comments on commit 3d4cb50

Please sign in to comment.