Ericsson / CodeCompass

CodeCompass is a software comprehension tool for large scale software written in C/C++ and Java
https://codecompass.net
GNU General Public License v3.0
516 stars 101 forks source link

Macro expansion error in C/C++ for _Pragma #731

Open mcserep opened 5 months ago

mcserep commented 5 months ago

PPMacroCallback::MacroExpands deliberately omits expanding #pragma statements and other preprocessor directives:
https://github.com/Ericsson/CodeCompass/blob/d2b0ee675d01456658c7dd710661562022fc0398/plugins/cpp/parser/src/ppmacrocallback.cpp#L73-L77

However, pragmas can also be defined with the _Pragma operator, see reference:
https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html

C99 introduced the _Pragma operator. This feature addresses a major problem with ‘#pragma’: being a directive, it cannot be produced as the result of macro expansion. _Pragma is an operator, much like sizeof or defined, and can be embedded in a macro.

Consider the following MWE, which will produce multiple errors, as the pragmas will be seemingly re-preprocessed.

#define CF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#define CF_ASSUME_NONNULL_END   _Pragma("clang assume_nonnull end")

CF_ASSUME_NONNULL_BEGIN  // error: already inside '#pragma clang assume_nonnull'
int bar = 42;
CF_ASSUME_NONNULL_END    // error: not currently inside '#pragma clang assume_nonnull'