1

A cross-platform C/C++ program will usually use lots of macros to test whether some features exist in the platform where it is built:

...
#if defined(HAVE_EPOLL)
    handle_connection_with_epoll()
#else
    handle_connection_without_epoll()
#endif // HAVE EPOLL
...

Normally one will then use ./configure to generate a config.h header file, in which the HAVE_EPOLL will be defined if there is epoll interfaces (i.e. Linux) and we will go with the handle_connection_with_epoll() code, otherwise HAVE_EPOLL will not be defined and we will got with the handle_connection_without_epoll() code.

Reading these kind of code is painful, especially when there are nested conditional macro branch:

...
#if defined(HAVE_EPOLL)
#if defined(HAVE_FCNTL)
    handle_connection_with_epoll_and_fcntl()
#else
    handle_connection_with_epoll()
#endif // HAVE_FCNTL
#else
#if defined(HAVE_FCNTL)
    handle_connection_without_epoll()
#else
...
#endif // HAVE_FCNTL
#endif // HAVE EPOLL
...

So, is there any tool which can be used to remove those dead macro branch after ./configure so that the code read more clearly?

For example, if HAVE_EPOLL and HAVE_FCNTL are defined after ./configure, the snippet above will become

handle_connection_with_epoll_and_fcntl()

which is much clearer!

jww
  • 97,681
  • 90
  • 411
  • 885
walkerlala
  • 1,599
  • 1
  • 19
  • 32
  • Possible duplicate of [Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?](https://stackoverflow.com/q/525283/608639) and [Recursively remove preprocessor macros](https://stackoverflow.com/q/43833971/608639) – jww Aug 20 '18 at 05:16
  • 2
    An understandable wish, but generally speaking, a bad idea. Because you would no longer be looking at the actual source of the software. Any questions you might ask, bug reports you might file, would be referencing not the original source but the result of _your_ configure run... – DevSolar Aug 20 '18 at 05:18
  • As DevSolar stated there are some downsides to automatically removing code. I've even seen the IDE colorizers/pretty printers get it wrong when compared to what the C preprocessor would do. I assume you accept the risk and just want options. You also have code folding like described at [Code folding in Eclipse](https://stackoverflow.com/q/13505413/608639) and [Preprocessor-aware code navigation in IDE for C project](https://stackoverflow.com/q/38635017/608639). – jww Aug 20 '18 at 05:23
  • And there is also this brute force method which runs the preprocessor on the source, but the code is usually uglier not prettier: [Can gcc output C code after preprocessing?](https://stackoverflow.com/q/4900870/608639) – jww Aug 20 '18 at 05:29

0 Answers0