Writing Custom rules for cppcheck

7.9k views Asked by At

I am using cppcheck for static analysis. To accelerate review process I want to set up cppcheck to look for some custom rules, for example to check if geter functions defined as a const.

If anyone has experience in writing custom rules for cppcheck please can you provide some example to write custom rules?.

P.S I have done some research to find a tool which will allow me to write custom rules and make review process faster. I have find these links about this topic

What open source C++ static analysis tools are available?

C++ static code analysis tool on Windows

A free tool to check C/C++ source code against a set of coding standards?

1

There are 1 answers

3
Daniel Marjamäki On BEST ANSWER

I am a Cppcheck developer.

You can perhaps use the --rule and --rule-file options to add such rules. Maybe you can use a regular expression such as:

\sget[A-Za-z]+\(\)\s+{\s+return

It depends on your code base.

If you can write a regular expression then this is the most direct and simple way to create a custom rule.

For more information, read the "Writing rules" articles here: http://sourceforge.net/projects/cppcheck/files/Articles/

But maybe you want to write more sophisticated rules that search for such getter methods by using the Cppcheck SymbolDatabase, tokenlist and syntax tree. You can't use --rule and --rule-file then. You have these choices then:

  • Use --dump and write your own custom scripts that read the output data (xml).
  • Write C++ code and compile it into Cppcheck. This is relatively straightforward imho but requires that you compile Cppcheck yourself.