3

Suppose we have and original source file which includes code blocks inside #ifdef conditionals and another file which has been manually "preprocessed", so some of the code blocks have been included an others have been removed. How can I see only modifications that don't come from an #ifdef?

As an example, the original file could be:

#ifdef X
  blabla1
#else
  blabla2
#endif

and the modified file is:

blabla2
new line

I only want to detect that new line has been added.

Is there a tool or a short way to do this? If I knew in advance if X was defined or not, I could strip the irrelevant code with unifdef, but in this case I don't know it and once there are many variables, trying all possible combinations is infeasible.

I think a way to do it would be through a diff to identify the differences and then check if the differences are inside an ifdef, but this doesn't seem straightforward to implement.

jinawee
  • 492
  • 5
  • 16
  • You can pre-process the file(s) and then compare the pre-processed output. – Paul Ogilvie Apr 20 '18 at 13:04
  • Note that your manually pre-processed, modified file _must_ have assumed certain defines, otherwise it cannot achieve its resulting C file. – Paul Ogilvie Apr 20 '18 at 13:06
  • @PaulOgilvie Correct, I just don't know them. – jinawee Apr 20 '18 at 13:13
  • See [Is there a preprocessor which eliminates `#ifdef` blocks based on values defined/undefined?](https://stackoverflow.com/questions/525283) for tools (Son of Ifdef, Coan) that can do the 'manual preprocessing' automatically. – Jonathan Leffler Apr 20 '18 at 15:18

1 Answers1

0

You can try using gcc -E which expands all preprocessor input. So if you wanted to try expanding some files which have #ifdef X and #ifdef Y you can use: gcc -E -DX -DY [filename] | clang-format.

clang-format should also get rid of empty lines for you.

You can also try using unifdef instead of gcc -E and you can look up the man page for what options you need there.

Aneesh Durg
  • 464
  • 4
  • 14