0

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;
}
BulletRain
  • 92
  • 9
  • 1
    `not show #define` - please create an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) - a minimal reproducible example, including steps on how did you created the custom rule? How did you add the custom rule? On what code did you test? Please including a smallest possible source code of your "test project" where the `#define` doesn't show. – KamilCuk Oct 16 '19 at 08:52
  • Thanks you for your comment. I have edit my question – BulletRain Oct 16 '19 at 09:11
  • Why on earth would you not allow `#define`? How do you plan to write header guards without it? – Lundin Oct 16 '19 at 13:02

1 Answers1

0

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.

Mathias Schmid
  • 431
  • 4
  • 7