Skip to content

Commit 9733f4c

Browse files
nbdd0121BennoLossin
authored andcommitted
rust: macros: convert #[export] to use syn
This eliminates the custom `function_name` helper. Reviewed-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Gary Guo <gary@garyguo.net>
1 parent ee1c3ae commit 9733f4c

3 files changed

Lines changed: 12 additions & 35 deletions

File tree

rust/macros/export.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,23 @@
33
use proc_macro2::TokenStream;
44
use quote::quote;
55

6-
use crate::helpers::function_name;
7-
86
/// Please see [`crate::export`] for documentation.
9-
pub(crate) fn export(_attr: TokenStream, ts: TokenStream) -> TokenStream {
10-
let Some(name) = function_name(ts.clone()) else {
11-
return "::core::compile_error!(\"The #[export] attribute must be used on a function.\");"
12-
.parse::<TokenStream>()
13-
.unwrap();
14-
};
7+
pub(crate) fn export(f: syn::ItemFn) -> TokenStream {
8+
let name = &f.sig.ident;
159

16-
// This verifies that the function has the same signature as the declaration generated by
17-
// bindgen. It makes use of the fact that all branches of an if/else must have the same type.
18-
let signature_check = quote!(
10+
quote! {
11+
// This verifies that the function has the same signature as the declaration generated by
12+
// bindgen. It makes use of the fact that all branches of an if/else must have the same
13+
// type.
1914
const _: () = {
2015
if true {
2116
::kernel::bindings::#name
2217
} else {
2318
#name
2419
};
2520
};
26-
);
27-
28-
let no_mangle = quote!(#[no_mangle]);
2921

30-
TokenStream::from_iter([signature_check, no_mangle, ts])
22+
#[no_mangle]
23+
#f
24+
}
3125
}

rust/macros/helpers.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use proc_macro2::{
44
token_stream,
5-
Ident,
65
TokenStream,
76
TokenTree, //
87
};
@@ -50,23 +49,6 @@ impl AsciiLitStr {
5049
}
5150
}
5251

53-
/// Given a function declaration, finds the name of the function.
54-
pub(crate) fn function_name(input: TokenStream) -> Option<Ident> {
55-
let mut input = input.into_iter();
56-
while let Some(token) = input.next() {
57-
match token {
58-
TokenTree::Ident(i) if i == "fn" => {
59-
if let Some(TokenTree::Ident(i)) = input.next() {
60-
return Some(i);
61-
}
62-
return None;
63-
}
64-
_ => continue,
65-
}
66-
}
67-
None
68-
}
69-
7052
pub(crate) fn file() -> String {
7153
#[cfg(not(CONFIG_RUSTC_HAS_SPAN_FILE))]
7254
{

rust/macros/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ pub fn vtable(attr: TokenStream, input: TokenStream) -> TokenStream {
234234
/// This macro is *not* the same as the C macros `EXPORT_SYMBOL_*`. All Rust symbols are currently
235235
/// automatically exported with `EXPORT_SYMBOL_GPL`.
236236
#[proc_macro_attribute]
237-
pub fn export(attr: TokenStream, ts: TokenStream) -> TokenStream {
238-
export::export(attr.into(), ts.into()).into()
237+
pub fn export(attr: TokenStream, input: TokenStream) -> TokenStream {
238+
parse_macro_input!(attr as syn::parse::Nothing);
239+
export::export(parse_macro_input!(input)).into()
239240
}
240241

241242
/// Like [`core::format_args!`], but automatically wraps arguments in [`kernel::fmt::Adapter`].

0 commit comments

Comments
 (0)