VicaYang
VicaYang

Reputation: 554

Is there any way to search the macro name by value?

Thanks to @selbie, a more clear question is

I've got some magic number or string I need to reference in code. There's a good chance one of the platform header files has already defined this value as an existing macro. And if so, how would I discover the macro with another name, so I don't end up duplicating it was another name?


We know that macro can be computed (or replaced in fact?) in compile time. So I want to know if there any way to search macro name by its value?

Here is a example. When I parse the USN record, I find that FileReferenceNumber of the root of driver is always 1407374883553285, so I would like to check whether it is defined in XXX.h previously, then I don't need to define another one.

By the way, if we can search macro, how about constexpr?

Upvotes: 1

Views: 152

Answers (1)

rici
rici

Reputation: 241931

Gcc and clang will print out a list of #defines if you invoke them with the options -E -dM. (If you don't use -E, -dM does something else.)

Unfortunately, macros and arithmetic expressions in the macro replacement texts are not expanded / evaluated, so you'll only be able to find the value if you know it's textual representation. Still, it's a first step.

That won't work for enum member values and constexprs. I don't think there is any way to search for those which doesn't involve using some C parsing library to build a symbol table. Such libraries exist, but they're not necessarily well-documented, stable, or easy to use.

Upvotes: 3

Related Questions