Call proc macro and modify it's output

Is it possible to call another proc macro from a dependency crate and modify it's output in my own macro?

For example, wasmtime_component_macro exports a bindgen macro, can I modify it's output like this?

pub fn my_bindgen(stream: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let mut stream = wasmtime_component_macro::bindgen(stream);

    // Modify the stream

    // Return the modified stream

This results in a compile error: expected function, found macro wasmtime_component_macro::bindgen

The original bindgen proc macro is declared as a public function that takes and returns a TokenStream:

pub fn bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    bindgen::expand(&parse_macro_input!(input as bindgen::Config))

So is it possible to call it with a token stream and get the token stream result?

Invoking the macro with ! and the stream argument like this: let stream = wasmtime_component_macro::bindgen!(stream); doesn't work.

I don't think we can call functions from proc-macro lib. Not yet...

In my library, I split the code from proc-macro to a normal rust crate, which can be called from proc-macro library.

1 Like

I was thinking that too, but the macro I want to modify comes from someone else's crate. I guess I could create a feature request.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.