Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Custom rules for cppcheck

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?

like image 927
T M Avatar asked Jun 20 '15 13:06

T M


1 Answers

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.
like image 105
Daniel Marjamäki Avatar answered Sep 20 '22 07:09

Daniel Marjamäki