ned14 / pcpp

A C99 preprocessor written in pure Python
Other
215 stars 39 forks source link

handle define inside ifdefs? #81

Closed drcicero closed 1 year ago

drcicero commented 1 year ago

Hi! I'm using pcpp 1.30,

can pcpp read defines inside ifdefs and apply them to the following defines?

I tried pccp on the following file:

#ifdef A
  #define B 1
  #ifdef B
    #define C 1
  #endif
#endif

with the following arguments and result:

$ pcpp --compress --passthru-defines --passthru-unknown-exprs --passthru-unfound-includes --passthru-comments --line-directive -- test.h
#ifdef A
  #define B 1
  #ifdef B
    #define C 1
  #endif
#endif

However I think it should be possible to get the following simpler output instead:

#ifdef A
  #define B 1
    #define C 1
#endif

Where #ifdef B can be removed, because we know B is obviously defined.

Of course in a real file, nobody would define a variable in one line and test for it in the next, but it can happen with includes and over longer distances, or through redefinition of a define with a different name. So I think this is a simple test case for the more complex scenarios.

ned14 commented 1 year ago

If you additionally passed in -U B to pcpp, I believe it would elide logic based on the value of B.

If you want what you want, you can customise PreprocessorHooks and have it do anything you want.