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!