1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#![warn(missing_docs)]

//! Provides the `#[trace]` macro. See the `callback-trace` crate for
//! more details.

extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn, parse_quote, Block, Expr, Token, punctuated::Punctuated};
use syn::parse::Parser;

/// The `#[trace]` macro. Needs to be applied to function definitions,
/// and will invoke callbacks managed by the the `callback-trace`
/// before and after the functions body.
///
/// # Example
/// ```notest
/// use call_trace::trace;
///
/// #[trace]
/// fn foo() {
///     // ...
/// }
/// ```
#[proc_macro_attribute]
pub fn trace(attr: TokenStream, input: TokenStream) -> TokenStream {
    assert!(attr.is_empty());
    let input = parse_macro_input!(input as ItemFn);

    // Hand the output tokens back to the compiler
    TokenStream::from(quote! {
        #[::call_trace::trace_with(::call_trace::on_trace)]
        #input
    })
}

/// The `#[trace_with]` macro. Needs to be applied to function definitions,
/// and will call a user-provided expression with the function body wrapped
/// in a closure, and a `CallContext` parameter.
///
/// The attribute accepts additional expression arguments that will
/// be inserted after the context paramter.
///
/// # Example
/// ```notest
/// use call_trace::{trace_with, CallContext};
///
/// impl MyType {
///     #[trace_with(self.trace())]
///     fn foo(&mut self) {
///         // ...
///     }
///
///     fn trace<T, F: FnOnce() -> T>(&mut self) -> impl FnOnce(F, CallContext) -> T {
///         |f, _ctx| {
///             f()
///         }
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn trace_with(attr: TokenStream, input: TokenStream) -> TokenStream {
    let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
    let mut with_args: Vec<_> = parser.parse(attr).unwrap().into_iter().collect();
    let with = with_args.remove(0);

    let input = parse_macro_input!(input as ItemFn);
    let fn_name = input.ident.clone();

    // Hand the output tokens back to the compiler
    TokenStream::from(quote! {
        #[::call_trace::inject_with(#with, ::call_trace::CallContext {
            file: file!(),
            line: line!(),
            fn_name: stringify!(#fn_name),
        } #(,#with_args)*)]
        #input
    })
}

/// The `#[inject_with]` macro. Needs to be applied to function definitions,
/// and will call a user-provided expression with the function body wrapped
/// in a closure.
///
/// The attribute accepts additional expression arguments that will
/// be passed to the user-provided function as extra arguments.
///
/// # Example
/// ```notest
/// use call_trace::inject_with;
///
/// impl MyType {
///     #[inject_with(self.trace())]
///     fn foo(&mut self) {
///         // ...
///     }
///
///     fn trace<T, F: FnOnce() -> T>(&mut self) -> impl FnOnce(F) -> T {
///         |f| {
///             f()
///         }
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn inject_with(attr: TokenStream, input: TokenStream) -> TokenStream {
    let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
    let mut with_args: Vec<_> = parser.parse(attr).unwrap().into_iter().collect();
    let with = with_args.remove(0);

    let mut input = parse_macro_input!(input as ItemFn);

    let inner_block = input.block;
    let trace_wrapper: Block = parse_quote! {
        {
            #with(move || #inner_block #(,#with_args)*)
        }
    };
    input.block = Box::new(trace_wrapper);

    // Hand the output tokens back to the compiler
    TokenStream::from(quote! { #input })

}