When the code is burden with a lot of #if, #ifdef. Like the case below
#ifdef CASE1
#ifdef CASE1_1
#define VALUE X
#else
#define VALUE Y
#endif
#else
#define VALUE Z
#endif
Is there a way to quickly determine which branch the code will get compiled. Any suggestions are appreciated. Thanks and Best Regards
Reading nested preprocessor macros can be made easier by indenting. Most preprocessors allow:
But the following form should be universally allowed:
While that helps follow the nest, it doesn’t guarantee that you’ll be able to spot which path is active, particularly if you are doing definitions by passing options to your compiler via
-DOne solution is to run the preprocessor over your source files and inspect the output. You can either do that by running the preprocessor executable manually (eg.
cpp), or by telling your compiler to stop after running the preprocessor (eg.gcc -E)That is guaranteed to show you what values have been substituted for your preprocessor macros. But the output from the preprocessor can be hard to follow.
If you’re just trying to trace a small section of code (as in your example) you can do it manually by liberally inserting
#error(or#warningif your preprocessor supports it), and looking at the errors dumped from your compiler. This is typically the simplest option as you don’t have to make changes to your build system to get the visibility that you’re after.Another option, depending on the complexity of your preprocessor macros, is to run another preprocessor tool over your source files that will generate friendlier output than the preprocessor. I’ve had success with filepp in the past.