The default way to skip undefined preprocessor branches with unifdef?

I am using complex C code that includes many compilation options. This makes the code very difficult to read. I would like to create a copy of the code that reflects how it is actually compiled. I've gotten pretty good results using the "unifdef" utility that I didn't know about until recently. However, I am puzzled as to why this is so difficult to call, and I wonder if I am missing something.

Consider the following example:

#ifdef A
  printf("A\n");
#endif
#ifdef B
  printf("B\n");
#endif

      

If you call unifdef with "unifdef -DA junk.c", you get:

  printf("A\n");
#ifdef B
  printf("B\n");
#endif

      

Since you didn't tell unifdef that B was undefined, it didn't take it out.

I would like the utility to behave so that when I say unifdef -DA I get instead:

  printf("A\n");

      

This will match what the C preprocessor does: any undefined branches are omitted.

To get this behavior with unifdef, it seems to me that one should use "unifdef -DA -UB junk.c", explicitly indicating that B is undefined. Though, I may have missed an easier way to call it.

I wrote a python script to generate a long list of required -D and -U flags from the Makefile code I use (usually 80 per routine). And the results are excellent. But I'm wondering if such a script is actually necessary.

It's also possible that another utility (sunifdef? Coan?) Has my desired behavior built in already; if so, please indicate it.

+3


source to share


3 answers


The program coan

does what you want it to do with the flag -m

:

$ coan source -DA -m test.c 
  printf("A\n");

      



From the man page:

-m, --implicit
    Assume that any symbol that is not --define-ed is implicitly
    --undef-ed.

      

+7


source


If I understand your question correctly, you want to see the code after the preprocessor has stepped over it, right? Then why don't you let the preprocess work on it and see what result it produces? Just start the compilation call using the same arguments you used when compiling, but add an argument to it -E

that means "do nothing but preprocess".



$ cat preproc.c 
#ifdef A
  printf("A\n");
#endif
#ifdef B
  printf("B\n");
#endif

$ gcc -E preproc.c 
# 1 "preproc.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "preproc.c"

$ gcc -E -DA preproc.c 
# 1 "preproc.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "preproc.c"

  printf("A\n");

$ gcc -E -DB preproc.c
# 1 "preproc.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "preproc.c"




  printf("B\n");

$

      

+1


source


You can use the unifdefall utility that comes with unifdef. See http://dotat.at/prog/unifdef

+1


source







All Articles