1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-14 18:18:08 -05:00
qadapt/qadapt-macro/src/lib.rs

184 lines
5.9 KiB
Rust
Raw Normal View History

//! Helper macros to use with the QADAPT allocator system
//!
//! This crate is intended for managing the QADAPT allocator,
//! and is unusable on its own.
//!
// TODO: This causes issues, but I can't track down why
// #![deny(missing_docs)]
extern crate proc_macro;
use proc_macro::Delimiter;
2018-11-15 00:35:07 -05:00
use proc_macro::Group;
use proc_macro::Spacing;
use proc_macro::TokenStream;
use proc_macro::TokenTree;
use std::iter::FromIterator;
2018-11-15 00:35:07 -05:00
macro_rules! group {
2018-11-15 23:08:02 -05:00
($delim:expr, $ts:expr) => {{
let _tt: TokenTree = ::proc_macro::Group::new($delim, $ts).into();
_tt
}};
2018-11-15 00:35:07 -05:00
($delim:expr) => {
group!($delim, ::proc_macro::TokenStream::new())
};
}
2018-11-15 00:35:07 -05:00
macro_rules! ident {
2018-11-15 23:08:02 -05:00
($name:expr, $span:expr) => {{
let _tt: TokenTree = ::proc_macro::Ident::new($name, $span).into();
_tt
}};
2018-11-15 00:35:07 -05:00
($name:expr) => {
ident!($name, ::proc_macro::Span::call_site())
};
}
2018-11-15 00:35:07 -05:00
macro_rules! punct {
2018-11-15 23:08:02 -05:00
($ch:expr, $spacing:expr) => {{
let _tt: TokenTree = ::proc_macro::Punct::new($ch, $spacing).into();
_tt
}};
}
2018-11-15 00:35:07 -05:00
macro_rules! token_stream {
($($tt:expr,)*) => {
{
let _v: Vec<::proc_macro::TokenTree> = vec![$($tt),*];
let _ts: TokenStream = ::proc_macro::TokenStream::from_iter(_v.into_iter());
_ts
}
2018-11-15 00:35:07 -05:00
};
($($tt:expr),*) => {
{
let _v: Vec<::proc_macro::TokenTree> = vec![$($tt),*];
let _ts: TokenStream = ::proc_macro::TokenStream::from_iter(_v.into_iter());
_ts
}
};
}
2018-11-18 21:29:32 -05:00
/// Generate the body of a function that is protected from making allocations.
/// The code is conditionally compiled so that all QADAPT-related bits
/// will be removed for release/bench builds, making the proc_macro safe
/// to leave on in production.
2018-11-15 23:08:02 -05:00
#[rustfmt::skip]
2018-11-18 21:29:32 -05:00
fn protected_body(fn_body: Group) -> TokenTree {
group!(Delimiter::Brace, token_stream!(
2018-11-15 00:35:07 -05:00
group!(Delimiter::Brace, token_stream!(
punct!(':', Spacing::Joint),
punct!(':', Spacing::Alone),
ident!("qadapt"),
punct!(':', Spacing::Joint),
punct!(':', Spacing::Alone),
2018-11-18 21:29:32 -05:00
ident!("enter_protected"),
2018-11-15 00:35:07 -05:00
group!(Delimiter::Parenthesis)
2018-11-18 21:29:32 -05:00
)),
2018-11-15 00:35:07 -05:00
ident!("let"),
ident!("__ret__"),
punct!('=', Spacing::Alone),
fn_body.into(),
punct!(';', Spacing::Alone),
2018-11-15 23:08:02 -05:00
punct!('#', Spacing::Alone),
// When `return` statements are involved, this code can get marked as
// unreachable because of early exit
group!(Delimiter::Bracket, token_stream!(
ident!("allow"),
group!(Delimiter::Parenthesis, token_stream!(
ident!("unreachable_code")
))
)),
group!(Delimiter::Brace, token_stream!(
2018-11-18 21:29:32 -05:00
group!(Delimiter::Brace, token_stream!(
punct!(':', Spacing::Joint),
punct!(':', Spacing::Alone),
ident!("qadapt"),
punct!(':', Spacing::Joint),
punct!(':', Spacing::Alone),
ident!("exit_protected"),
group!(Delimiter::Parenthesis)
)),
2018-11-15 23:08:02 -05:00
ident!("__ret__")
))
2018-11-15 00:35:07 -05:00
))
}
2018-11-15 23:08:02 -05:00
/// Walk through a TokenStream (typically from a Group Brace) and prepend calls
/// to `return` with an exit guard.
fn escape_return(ts: TokenStream) -> TokenStream {
let mut protected: Vec<TokenTree> = Vec::new();
2018-11-17 10:32:58 -05:00
let mut in_closure: bool = false;
2018-11-15 23:08:02 -05:00
let mut tt_iter = ts.into_iter();
while let Some(tt) = tt_iter.next() {
2018-12-06 22:12:45 -05:00
let tokens = match tt {
2018-11-17 10:32:58 -05:00
TokenTree::Group(ref g) if g.delimiter() == Delimiter::Brace && !in_closure => {
2018-11-15 23:08:02 -05:00
vec![group!(Delimiter::Brace, escape_return(g.stream()))]
}
2018-11-17 10:32:58 -05:00
TokenTree::Ident(ref i) if i.to_string() == "return" && !in_closure => vec![
2018-11-18 21:29:32 -05:00
group!(
Delimiter::Brace,
token_stream!(
punct!(':', Spacing::Joint),
punct!(':', Spacing::Alone),
ident!("qadapt"),
punct!(':', Spacing::Joint),
punct!(':', Spacing::Alone),
ident!("exit_protected"),
group!(Delimiter::Parenthesis)
)
),
2018-11-15 23:08:02 -05:00
tt.clone(),
],
2018-11-17 10:32:58 -05:00
TokenTree::Punct(ref p) if p.as_char() == '|' => {
in_closure = true;
vec![tt.clone()]
}
TokenTree::Punct(ref p) if p.as_char() == ';' => {
in_closure = false;
vec![tt.clone()]
}
2018-11-15 23:08:02 -05:00
t => vec![t],
};
protected.extend(tokens.into_iter());
2018-11-15 20:16:49 -05:00
}
2018-11-15 23:08:02 -05:00
TokenStream::from_iter(protected.into_iter())
2018-11-15 20:16:49 -05:00
}
/// Set up the QADAPT allocator to trigger a panic if any allocations happen during
/// calls to this function.
///
2018-11-17 11:04:37 -05:00
/// QADAPT will only track allocations in the current function call;
/// if (for example) this function receives the results of an allocation in a
2018-11-17 11:04:37 -05:00
/// separate thread, or defers allocations via closure/Future, those results
/// will not trigger an error.
#[proc_macro_attribute]
pub fn allocate_panic(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut protected_fn: Vec<TokenTree> = Vec::new();
let mut item_iter = item.into_iter();
2018-11-15 00:35:07 -05:00
// First, get the function body we're replicating
let mut fn_body = None;
while let Some(tt) = item_iter.next() {
match tt {
TokenTree::Group(ref g) if g.delimiter() == Delimiter::Brace => {
2018-11-15 23:08:02 -05:00
fn_body = Some(Group::new(Delimiter::Brace, escape_return(g.stream())));
2018-11-15 00:35:07 -05:00
break;
}
tt => {
protected_fn.push(tt.clone());
}
}
}
2018-11-15 00:35:07 -05:00
protected_fn.push(protected_body(fn_body.as_ref().unwrap().clone()));
while let Some(tt) = item_iter.next() {
protected_fn.push(tt)
}
TokenStream::from_iter(protected_fn.into_iter())
}