Writing Custom rules for cppcheck
Asked Answered
A

1

9

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?

Abfarad answered 20/6, 2015 at 13:59 Comment(4)
Hi, TM_. Requests for offline resources and requests for tool suggestions are off-topic for stackoverflow.Flageolet
Hi @DrewDormann actually my question about writing custom rules for cppcheck. If even just mentioning also off-topic I can remove this part from my question.Abfarad
I bet that would help. I re-read each sentence that you typed and asked myself "Is this a question?" I couldn't find a question anywhere.Flageolet
@DrewDormann thanks for willing help. my question about writing custom rules for cppcheck . Edited my question.Abfarad
B
14

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.
Bondman answered 20/6, 2015 at 15:6 Comment(3)
Hi @Daniel Marjamäki Thanks for response. So as written in document you provided rule is XML file, is there a way to use this custom created rule file with cppcheck GUI ? Where can I set up new rule in GUI?Abfarad
Currently you can't use rules in the GUI. I think it would be relatively easy to improve the GUI, but nobody has wanted to do it.Allo
Ok thanks I will try to use with windows cmd terminal.Abfarad

© 2022 - 2024 — McMap. All rights reserved.