externcfn_attr/
lib.rs

1#![allow(unused)]
2
3extern crate proc_macro;
4
5use proc_macro::{TokenStream};
6use quote::quote;
7use syn::{parse_macro_input, Attribute, ItemFn, Visibility, Signature};
8
9#[proc_macro_attribute]
10pub fn externcfnattr(_attr: TokenStream, item: TokenStream) -> TokenStream {
11    // Parse the input tokens into a syntax tree
12    let input = parse_macro_input!(item as ItemFn);
13
14    // Decompose the function into its components
15    let ItemFn { attrs, vis, sig, block } = input;
16    let Signature { constness, unsafety, ident, inputs, output, .. } = sig;
17
18    // Create the new function with the desired modifications
19    let expanded = quote! {
20        #(#attrs)*
21        #[no_mangle]
22        #vis #constness #unsafety extern "C" fn #ident(#inputs) #output
23        #block
24    };
25
26    // Return the generated tokens back into the compiler
27    TokenStream::from(expanded)
28}