8

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?

Community
  • 1
  • 1
T M
  • 2,905
  • 1
  • 24
  • 48
  • Hi, TM_. Requests for offline resources and requests for tool suggestions are off-topic for stackoverflow. – Drew Dormann Jun 20 '15 at 14:04
  • 1
    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. – T M Jun 20 '15 at 14:17
  • 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. – Drew Dormann Jun 20 '15 at 14:24
  • @DrewDormann thanks for willing help. my question about writing custom rules for cppcheck . Edited my question. – T M Jun 20 '15 at 14:42

1 Answers1

9

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.
Daniel Marjamäki
  • 2,629
  • 11
  • 13
  • 1
    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? – T M Jun 20 '15 at 17:20
  • 1
    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. – Daniel Marjamäki Jun 22 '15 at 05:24
  • 1
    Ok thanks I will try to use with windows cmd terminal. – T M Jun 22 '15 at 06:42