I want to expand a certain attribute crate to see what the macro crate, I tried using cargo expand first, but I found it expand all macros, make it difficult to read.
Then I wondered if I can do this: since attribute macros are defined as a function, can I call it directly with parameters, and print the output token stream? I tried, and the complier could not even find the function.
So, can I do this? If not, is there any convenience way to expand only specified macros?
The easiest way I can think of to expand only one specific macro is to create a minimal example in a toy crate and then run cargo expand on that.
Doing that will prevent the other macros from being expanded.
That is the result I would expect, yes. The way macros are defined, as well as how they hook into the compiler, is different from how "standard" rust code works.
Otherwise, an easier way would be to cargo vendor to get a local copy of the crate featuring the proc-macro, and to use a Cargo patch or something to shadow the remote one with the one from that local copy, so that you can then insert println! statements or similar things to get that one level of expansion.
Also note that some proc-macro authors (such as myself) offer an optional Cargo feature to do this already for you, and I even once offered that the output be filtered by a pattern given in an env var:
I’d run into the same issue in the past but just published the quick hack dont-expand I’d come up with to get around the issue. I only published it with the std macros but it would be easy to add more common crate macros to it.
I have no clue how easy it would be to do, but I'm noticing a demand for something akin to common lisp's macroexpand-1 form, which expands the form it is invoked on exactly once i.e. if the expansion contains macro calls, those are not expanded.
What cargo expand does is more akin to common lisp's macroexpand-all form, which essentially runs macroexpand-1 as a fixed-point algorithm until there are no macros left to expand.