2018-11-24 07:34:13 -06:00
|
|
|
// force-host
|
2018-03-10 20:16:26 -06:00
|
|
|
// no-prefer-dynamic
|
|
|
|
|
2019-05-21 17:09:58 -05:00
|
|
|
// Proc macros commonly used by tests.
|
|
|
|
// `panic`/`print` -> `panic_bang`/`print_bang` to avoid conflicts with standard macros.
|
|
|
|
|
2018-03-10 20:16:26 -06:00
|
|
|
#![crate_type = "proc-macro"]
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
2021-02-22 15:07:36 -06:00
|
|
|
use proc_macro::{TokenStream, TokenTree};
|
2018-03-10 20:16:26 -06:00
|
|
|
|
2019-05-21 17:09:58 -05:00
|
|
|
// Macro that return empty token stream.
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn empty(_: TokenStream) -> TokenStream {
|
|
|
|
TokenStream::new()
|
|
|
|
}
|
|
|
|
|
2018-03-10 20:16:26 -06:00
|
|
|
#[proc_macro_attribute]
|
2019-05-21 17:09:58 -05:00
|
|
|
pub fn empty_attr(_: TokenStream, _: TokenStream) -> TokenStream {
|
|
|
|
TokenStream::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(Empty, attributes(empty_helper))]
|
|
|
|
pub fn empty_derive(_: TokenStream) -> TokenStream {
|
|
|
|
TokenStream::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macro that panics.
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn panic_bang(_: TokenStream) -> TokenStream {
|
|
|
|
panic!("panic-bang");
|
2018-03-10 20:16:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
2019-05-21 17:09:58 -05:00
|
|
|
pub fn panic_attr(_: TokenStream, _: TokenStream) -> TokenStream {
|
|
|
|
panic!("panic-attr");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(Panic, attributes(panic_helper))]
|
|
|
|
pub fn panic_derive(_: TokenStream) -> TokenStream {
|
|
|
|
panic!("panic-derive");
|
2018-03-10 20:16:26 -06:00
|
|
|
}
|
|
|
|
|
2019-05-21 17:09:58 -05:00
|
|
|
// Macros that return the input stream.
|
|
|
|
|
2018-03-10 20:16:26 -06:00
|
|
|
#[proc_macro]
|
2019-05-21 17:09:58 -05:00
|
|
|
pub fn identity(input: TokenStream) -> TokenStream {
|
2018-03-10 20:16:26 -06:00
|
|
|
input
|
|
|
|
}
|
2019-05-21 17:09:58 -05:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn identity_attr(_: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(Identity, attributes(identity_helper))]
|
|
|
|
pub fn identity_derive(input: TokenStream) -> TokenStream {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macros that iterate and re-collect the input stream.
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn recollect(input: TokenStream) -> TokenStream {
|
|
|
|
input.into_iter().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn recollect_attr(_: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
input.into_iter().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(Recollect, attributes(recollect_helper))]
|
|
|
|
pub fn recollect_derive(input: TokenStream) -> TokenStream {
|
|
|
|
input.into_iter().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macros that print their input in the original and re-collected forms (if they differ).
|
|
|
|
|
|
|
|
fn print_helper(input: TokenStream, kind: &str) -> TokenStream {
|
2021-02-22 15:07:36 -06:00
|
|
|
print_helper_ext(input, kind, true)
|
|
|
|
}
|
|
|
|
|
2021-03-20 23:17:19 -05:00
|
|
|
fn deep_recollect(input: TokenStream) -> TokenStream {
|
|
|
|
input.into_iter().map(|tree| {
|
|
|
|
match tree {
|
|
|
|
TokenTree::Group(group) => {
|
|
|
|
let inner = deep_recollect(group.stream());
|
|
|
|
let mut new_group = TokenTree::Group(
|
|
|
|
proc_macro::Group::new(group.delimiter(), inner)
|
|
|
|
);
|
|
|
|
new_group.set_span(group.span());
|
|
|
|
new_group
|
|
|
|
}
|
|
|
|
_ => tree,
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
2021-02-22 15:07:36 -06:00
|
|
|
fn print_helper_ext(input: TokenStream, kind: &str, debug: bool) -> TokenStream {
|
2019-05-21 17:09:58 -05:00
|
|
|
let input_display = format!("{}", input);
|
|
|
|
let input_debug = format!("{:#?}", input);
|
2021-03-20 23:17:19 -05:00
|
|
|
let recollected = input.clone().into_iter().collect();
|
2019-05-21 17:09:58 -05:00
|
|
|
let recollected_display = format!("{}", recollected);
|
|
|
|
let recollected_debug = format!("{:#?}", recollected);
|
2021-03-20 23:17:19 -05:00
|
|
|
|
|
|
|
let deep_recollected = deep_recollect(input);
|
|
|
|
let deep_recollected_display = format!("{}", deep_recollected);
|
|
|
|
let deep_recollected_debug = format!("{:#?}", deep_recollected);
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-21 17:09:58 -05:00
|
|
|
println!("PRINT-{} INPUT (DISPLAY): {}", kind, input_display);
|
|
|
|
if recollected_display != input_display {
|
|
|
|
println!("PRINT-{} RE-COLLECTED (DISPLAY): {}", kind, recollected_display);
|
|
|
|
}
|
2021-03-20 23:17:19 -05:00
|
|
|
|
|
|
|
if deep_recollected_display != recollected_display {
|
|
|
|
println!("PRINT-{} DEEP-RE-COLLECTED (DISPLAY): {}", kind, deep_recollected_display);
|
|
|
|
}
|
|
|
|
|
2021-02-22 15:07:36 -06:00
|
|
|
if debug {
|
|
|
|
println!("PRINT-{} INPUT (DEBUG): {}", kind, input_debug);
|
|
|
|
if recollected_debug != input_debug {
|
|
|
|
println!("PRINT-{} RE-COLLECTED (DEBUG): {}", kind, recollected_debug);
|
|
|
|
}
|
2021-03-20 23:17:19 -05:00
|
|
|
if deep_recollected_debug != recollected_debug {
|
|
|
|
println!("PRINT-{} DEEP-RE-COLLETED (DEBUG): {}", kind, deep_recollected_debug);
|
|
|
|
}
|
2019-05-21 17:09:58 -05:00
|
|
|
}
|
|
|
|
recollected
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn print_bang(input: TokenStream) -> TokenStream {
|
|
|
|
print_helper(input, "BANG")
|
|
|
|
}
|
|
|
|
|
2020-06-20 19:59:04 -05:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn print_bang_consume(input: TokenStream) -> TokenStream {
|
|
|
|
print_helper(input, "BANG");
|
|
|
|
TokenStream::new()
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:09:58 -05:00
|
|
|
#[proc_macro_attribute]
|
2021-02-22 15:07:36 -06:00
|
|
|
pub fn print_attr(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let debug = match &args.into_iter().collect::<Vec<_>>()[..] {
|
|
|
|
[TokenTree::Ident(ident)] if ident.to_string() == "nodebug" => false,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
print_helper_ext(input, "ATTR", debug)
|
2019-05-21 17:09:58 -05:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:52:51 -05:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn print_attr_args(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
print_helper(args, "ATTR_ARGS");
|
|
|
|
input
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:51:44 -06:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn print_target_and_args(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
print_helper(args, "ATTR_ARGS");
|
|
|
|
print_helper(input.clone(), "ATTR");
|
|
|
|
input
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn print_target_and_args_consume(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
print_helper(args, "ATTR_ARGS");
|
|
|
|
print_helper(input.clone(), "ATTR");
|
|
|
|
TokenStream::new()
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:09:58 -05:00
|
|
|
#[proc_macro_derive(Print, attributes(print_helper))]
|
|
|
|
pub fn print_derive(input: TokenStream) -> TokenStream {
|
2020-05-30 16:04:57 -05:00
|
|
|
print_helper(input, "DERIVE");
|
|
|
|
TokenStream::new()
|
2019-05-21 17:09:58 -05:00
|
|
|
}
|