lagman said:
Sorry.. I'll clarify.. Not all preprocessor directives, just
conditionally compiled code (i.e. #ifdef/#ifndef)
Do you want it to remove only the #ifdef and #ifndef statements? Do
you also want #if's removed? Do you want it to also remove the
corresponding #else, #elif, and #endif statements? Do you want it to
also remove all the code between each #ifdef/#ifndef direction and the
matching #endif directive? Those are all pretty trivial options.
They'll produce code that might not even compile, and if it does,
might not do what the original code did. But if that's what you want,
it's quite feasible.
I think it's most likely that you want it to remove all of the
conditional compilation directives, and the branches that would not
have been selected, while keeping the branches that would have been
selected. On some systems, preprocessing is performed by a completely
separate program; even when that's not the case, many compilers
provide an option (often -E) that has precisely that effect. However,
you've indicated that the conditional compilation directives are the
only ones you want removed, and that makes it much more complicated.
This is not a common need; I wouldn't know where to look for an
existing utility that performs such filtration. If you want to write
one yourself, it will get a bit complicated. The fundamental problem
is that determining which branch of a #if is used requires
implementation of macro expansion, which in turn requires implementing
#defines. In particular, it requires knowing which identifiers have
been pre-#defined by the compiler. It also requires implementation of
a large subset of C's expression syntax. Determining which branch is
chosen by #ifdef/#ifndef is simpler, because it only requires keeping
track of which macros have been #defined, without worrying about what
their definitions are. However, this also implies that #Include has to
be implemented, since the relevant #define statements might have
occurred inside of files that should have been #included. In other
words, such a program would have to implement most of the
preprocessing directives in order to decide which branch to keep, but
it must produce output that doesn't actually reflect the application
of those directives.
Given that standard headers might use non-standard extensions to C,
might #define identifiers with names reserved to the implementation,
and might not even be implemented as files in accessible locations, I
don't think that there's any way to write a portable program that
performs such a task in general. However, If you can restrict the
inputs to code which doesn't refer to any such identifiers, and
doesn't rely upon any impIementation-specific exetensions, I think it
would be feasible, but it won't be trivial.