Search code examples
c++ccppcheck

CppCheck rule not show #define, comment


I'm create custom rule for cppCheck. When I create rule like not allow #define in project. But --rule=.+ not show #define in my test project. Is there any option or flag for cppcheck to show #define in my code ?

my rule file:

<?xml version="1.0"?>
<rule version="1">
       <pattern>#define</pattern>
</rule>

my example:

#define TEST 1

int main() {
    int a = TEST;
    return 0;
}

Solution

  • The document writing-rules-2.pdf at https://sourceforge.net/projects/cppcheck/files/Articles/ states: "The Cppcheck data is preprocessed. There are no comments, #define, #include, etc."

    As an alternative you can create a XML dump using cppcheck --dump yourfile.c and post-process the dump e.g. by a Python script. The #define TEST 1 line of your example is then part of the rawtokens and directivelist section. Several Cppcheck addons do it this way (e.g. Python MISRA-C addon). See "addons" directory of Cppcheck at GitHub. Within this directory there is also a Python helper script cppcheckdata.py which reads a dump file and creates object lists for tokens, functions, etc.