Search code examples
cparsinggccc-preprocessorpreprocessor-directive

GCC Preprocessing: Leave undefined Conditionals without assuming zero-values


I'm using GCC to preprocess some C-files.

Examplesource:

#define A 1
#define B 0

#if A && B > 0
  bla()
#else
  #if C == 0
    foo()
  #else
    foo2()
  #endif
  foo3()
#endif

I'm running this from a batchfile which leaves Comments (-CC) and does not work through macros (-fdirectives-only):

gcc -E -CC -fdirectives-only -Wundef infile.c > outfile.c

It gives me the following output for the above example because it assumes C=0 automatically:

foo()
foo3()

I'd want the following output (NO assumptions taken by GCC):

  #if C == 0
    foo()
  #else
    foo2()
  #endif
  foo3()

Is there ANY way to do that? I know that's just how the GCC SHOULD work, but maybe by doing some manual work, it could be done - do you have any idea?

I know, the alternative Coan would make this possible - but I'm not allowed to use it :-(

Thanks in advance!


SORRY GUYS, I MEANT TO #define A & B. EDITED MY SOURCE


Solution

  • No, there is no way to do this without some third-party application. Neither GCC nor clang include this functionality, which is why third-party tools exist.