I'm trying to find the best way to extract a list of evaluated defines from source code.
I found a few questions about how macros are evaluated at compile time, but I'm specifically looking for extracting the result for each #define to dump to a file.
Here is some sample code the question:
#define VALUE1 0x01
#define VALUE2 0x10
#define VALUE3 VALUE1 | VALUE2
int main(void) {
printf("VALUE01: 0x%02X\n", VALUE1);
printf("VALUE02: 0x%02X\n", VALUE2);
printf("VALUE03: 0x%02X\n", VALUE3); //prints evaluated VALUE3
}
Technically, the sample code could achieve what I'm looking for, but is cumbersome and would require scripting to generate and maintain. Ideally, there would already be a compiler flag that I overlooked that could do this since it already has to evaluate the macros, but there wasn't one that I could find. Are there any other compiler tools that could be used to achieve this?
This answered question was the closest answer I found and provides me with the ability to dump all defines, but it dumps the un-evaluated form as seen in source code:
$ gcc -dM -E test.c | grep "VAL"
#define VALUE1 0x01
#define VALUE2 0x10
#define VALUE3 VALUE1 | VALUE2
.... (and all the other preprocessor defines)