This commit is contained in:
sinkuu 2017-10-18 14:06:38 +09:00
parent b4ea47d23e
commit 9221bd9c97
2 changed files with 24 additions and 5 deletions

View file

@ -1,13 +1,15 @@
#![feature(plugin_registrar, rustc_private)]
#![feature(plugin_registrar, rustc_private, quote)]
extern crate rustc_plugin;
extern crate syntax;
use syntax::codemap::Span;
use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
use syntax::ext::build::AstBuilder; // trait for expr_usize
use rustc_plugin::Registry;
use syntax::ast::MetaItem;
use syntax::codemap::Span;
use syntax::ext::base::{Annotatable, ExtCtxt, MacEager, MacResult, SyntaxExtension};
use syntax::ext::build::AstBuilder; // trait for expr_usize
use syntax::symbol::Symbol;
use syntax::tokenstream::TokenTree;
fn expand_macro(cx: &mut ExtCtxt, sp: Span, _: &[TokenTree]) -> Box<MacResult + 'static> {
let e = cx.expr_usize(sp, 42);
@ -15,7 +17,23 @@ fn expand_macro(cx: &mut ExtCtxt, sp: Span, _: &[TokenTree]) -> Box<MacResult +
MacEager::expr(cx.expr_mut_addr_of(sp, e))
}
fn expand_attr_macro(cx: &mut ExtCtxt, _: Span, _: &MetaItem, annotated: Annotatable) -> Vec<Annotatable> {
vec![
Annotatable::Item(
quote_item!(
cx,
#[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
).unwrap()
),
annotated,
]
}
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_macro("mini_macro", expand_macro);
reg.register_syntax_extension(
Symbol::intern("mini_macro_attr"),
SyntaxExtension::MultiModifier(Box::new(expand_attr_macro)),
);
}

View file

@ -2,6 +2,7 @@
#![plugin(clippy_mini_macro_test)]
#[deny(warnings)]
#[mini_macro_attr]
fn main() {
let _ = mini_macro!();
}