Skip to main content

cortex_m_macros/
lib.rs

1//! Internal implementation details of `cortex-m`.
2//!
3//! Do not use this crate directly.
4
5extern crate proc_macro;
6
7use proc_macro::TokenStream;
8use quote::quote;
9use syn::{Item, Meta, parse_macro_input, parse_quote};
10
11#[proc_macro_attribute]
12pub fn asm_cfg(attr: TokenStream, item: TokenStream) -> TokenStream {
13    let cfg_expr = parse_macro_input!(attr as Meta);
14    let wrapped_item = parse_macro_input!(item as Item);
15
16    let new_item = match wrapped_item {
17        Item::Fn(f) => asm_cfg_wrap_fn(cfg_expr, f),
18        // TODO(wt): we should probably support modules as well
19        // Item::Mod(m) => asm_wrapper_wrap_mod(cfg_expr, m),
20        _ => unimplemented!(),
21    };
22
23    quote! {
24        #new_item
25    }
26    .into()
27}
28
29fn asm_cfg_wrap_fn(cfg_expr: Meta, mut f: syn::ItemFn) -> Item {
30    let old_block = f.block;
31    f.block = parse_quote! {
32        {
33            #[cfg(#cfg_expr)]
34            #old_block
35
36            #[cfg(not(#cfg_expr))]
37            unimplemented!()
38        }
39    };
40    parse_quote! {
41        #[allow(unused)]
42        #f
43    }
44}
45
46#[cfg(test)]
47mod tests {
48
49    #[test]
50    fn test_asm_cfg() {
51        macrotest::expand("proc_macro_tests/*.rs");
52    }
53}