Home > Software design >  Parsing "extern "C"" block with syn in a proc-macro
Parsing "extern "C"" block with syn in a proc-macro

Time:06-03

I need to do some macro magic on top of the linked library functions for work (can't tell what exactly, NDA and all). I'm trying to parse the block into ItemForeignMod, since it's the only struct that I could see fit. I'm doing it like this:

#[proc_macro_attribute]
pub fn bound_block(
    _attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let item = parse_macro_input!(item as ItemForeignMod);
    ...
}

But it panics with:

error: expected `(`, found `<function_name>`
    --> <project>/src/bindings/<bound_library>.rs:4491:12
     |
4491 |     pub fn <function_name>(
     |            ^^^^^^^^^^^^^^^ expected `(`

Example of one of the extern blocks (generated by rust-bindgen):

#[<library>_macros::bound_block]
extern "C" {
    pub fn <function>() -> <ReturnCode>;
}

I'm still relatively new to proc macros, I have a project where I heavily utilize them, but it's not that serious. I was also trying to look through syn source code to see where I might've messed up, but can't seem to spot anything. My knowledge is definitely lacking in this department.

CodePudding user response:

Was a mistake in an inner function, but compiler made me confused about the origin of the error. All good now

  • Related