1

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

4
  • I never understood why nobody indents #ifdef and stuff like they would normal ifs in code. This is made worse by the fact that IDEs like to automatically totally unindent lines that begin with #. Commented Mar 1, 2012 at 3:30
  • @SethCarnegie I've read that older pre-processors didn't allow whitespace before the #, so that's why the old habit of not indenting it comes from. Commented Mar 1, 2012 at 3:38
  • Some people do ident #ifdef and I find it the best way to keep them readable. Commented Mar 1, 2012 at 3:43
  • agree with you @SethCarnegie, it is hard to read codes which are not indented. Commented Mar 1, 2012 at 4:19

4 Answers 4

2

Reading nested preprocessor macros can be made easier by indenting. Most preprocessors allow:


#ifdef foo
    #define bar (1)
#endif

But the following form should be universally allowed:


#ifdef foo
#    define bar (1)
#endif

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 -D

One 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 #warning if 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank everyone for your suggestions! I found that "#error" is the easiest way, just put it in each code branch and the compiler will report an error
1

You could ident it as

#ifdef CASE1                       
#  ifdef CASE1_1
#    define VALUE X
#  else
#    define VALUE Y
#  endif
#else
#  define VALUE Z
#endif

The compiler won't complain and the same identation style is currently used in qglobal.h from Qt which has to support a quite a number of compilers on many platforms.

Comments

0

Some IDEs will highlight the "good parts" or grey out the "bad parts"; Visual Studio does this (only if it understands the #defines, though!)

And of course, you can run the preprocessor over your source and look at the output -- for example, with the -E switch to g++.

Comments

0

Don't trust Visual Studio. It grays out code that executes. Lots of times, especially when you mix preprocessor macros in the project settings with those defined in your header files.

Simple approach?

#ifdef CASE1                       
#ifdef CASE1_1
#define VALUE X
const int branch = 1;
#else
#define VALUE Y
const int branch = 2;
#endif
#else
#define VALUE Z
const int branch = 3;
#endif

At the end, you have a variable whose value will tell you which branch got compiled in. Not very elegant, but it does do what you asked and I'm guessing you need this for debugging purposes anyway :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.