[cfe-dev] Interact with the preprocessor

Paolo Bolzoni bolzoni at cs.unipr.it
Mon Feb 9 09:25:28 PST 2009


dear cfe-list,

we need a way to verify the source code has some proprieties that
are lost after preprocessing, but impossible to detect directly from
source code without a full-fledged preprocessor.

For example, the `High-Integrity C++ Coding Standard Manual' has a rule
(14.10) that says that include directives can't contains path specifiers.

The code:

#define FILE "path/mylib.h"
#include FILE

does violate the rule, but I can't verify it automatically from the source
without a preprocessor because I need to know the meaning of FILE
(it might be defined in an other included file) and I can't verify it
afterwards because both the #define and the #include disappear.


Second example: it is always a bad idea (and it actually makes the code
ill formed) redefine a keyword. But preprocessors do not warn against it.

The code:

#define private public
class A { ... };

violates the rule, and we'd like to be able to intercept the action
of defining the macro in order to warn against this abuse of the
preprocessor.  Again, the problem is undetectable in the preprocessed code.


Final example (again about registering the macro names): all inclusion
guards should be different inside the same project. Otherwise the inclusion
of one file will prevent to include another file.  In this case we need
to insert our code in the preprocessor each time a #define is found in
order to see if the macro has already been defined elsewhere.

Is the clang preprocessor easy to interact/interface with?  As far we could
understand, the answer should be negative because clang's preprocessor
seems to be tightly integrated with the subsequent parsing phases, but
we could be wrong (actually, we _hope_ to be wrong), so I asked here.

Thanks for any insight.
pb



More information about the cfe-dev mailing list