How to use cppcheck's inline suppression filter option for C++ code?
Asked Answered
N

4

37

I would like to use Cppcheck for static code analysis of my C++ code. I learned that I can suppress some kind of warnings with --inline-suppr command. However, I can't find what "suppressed_error_id" I should put in the comment:

// cppcheck-suppress "suppressed_error_id"
Nicety answered 2/6, 2010 at 9:17 Comment(3)
It seems you cannot put quotes around the error id: e.g. it should be // cppcheck-suppress noExplicitConstructorOxysalt
+1 for mentioning to call cppcheck with command line argument --inline-suppr to activate cppcheck-suppress. This should not be forgotten!Flinch
@SemjonMössinger Yes I was using the the Cppcheck GUI and it needs a checkbox enabled in the PreferencesAcromegaly
U
20

According to the cppcheck help:

The error id is the id that you want to suppress. The easiest way to get it is to use the --xml command line flag. Copy and paste the id string from the xml output.

So run cppcheck against some code that contains the error with the --xml flag, and then look in the generated XML file to find its name.

Uncommon answered 2/6, 2010 at 9:25 Comment(7)
Hey, thanks for quick answer, however the tip you gave works for --suppression option put in a separate file. I tried to achieve the same thing with comments just in my code, without luck so far with the use of these ids. Perhaps, the ids are OK and the problem lies elsewhere.Nicety
@btz what makes you think cppcheck suports putting command line switches in code comments?Uncommon
I run ./cppcheck --help and saw --inline-suppr option: --inline-suppr Enable inline suppressions. Use them by placing one or more comments in the form: // cppcheck-suppress memleak on the lines before the warning to suppress. Tried to use it in my code (running cppcheck with --inline-suppr) with the ids taken form xml output, but it does not work for me.Nicety
@btz I hadn't notice that - just tested it, and you are right - it doesn't work.Uncommon
Hello, just tested it with the latest cppcheck version 1.44 and it works like a charm. Things you have to remember when using cppcheck-suppress: 1. put "//cppcheck-suppress <id>" in a new line before the line in which error to be suppressed occurrs. 2. provide the correct, full paths to cppcheck (I used wrong one with /home/mycode/other_dir/../test.cpp) 3. Enjoy warning/problems free cppcheck reports :-) Thanks a lot Neil.Nicety
BTW, I had problems with multi-line statements. For example: // cppcheck-suppress errorname cout << a <newline> << b <newline> << line with cppcheck problem; Would still result in the error, but this would not: // cppcheck-suppress errorname cout << a << b << line with cppcheck problem;Terrellterrena
I was stuck until I added the argument to cppcheck --inline-suppr EnablePerbunan
M
29

You can change the output template to display the error id from the command line, which is quite neat.

For a Visual Studio format output with error id displayed, add this to your command line:

--template "{file}({line}): {severity} ({id}): {message}"

This will produce output something like this:

s:\src\jpeg.cpp(123): error (bufferAccessOutOfBounds): Buffer access out-of-bounds: abRY

Which you can then suppress by adding the line:

// cppcheck-suppress bufferAccessOutOfBounds

To the previous line in the source file.

Misvalue answered 7/7, 2011 at 15:7 Comment(2)
You can use the same template to get a list of possible error id's: cppcheck --errorlist --template='{file}:{line},{severity},{id},{message}'Virgenvirgie
Note that the "// cppcheck-suppress id" comment line should start from the first character of the line. It won't work if it is indented.Pinchhit
U
20

According to the cppcheck help:

The error id is the id that you want to suppress. The easiest way to get it is to use the --xml command line flag. Copy and paste the id string from the xml output.

So run cppcheck against some code that contains the error with the --xml flag, and then look in the generated XML file to find its name.

Uncommon answered 2/6, 2010 at 9:25 Comment(7)
Hey, thanks for quick answer, however the tip you gave works for --suppression option put in a separate file. I tried to achieve the same thing with comments just in my code, without luck so far with the use of these ids. Perhaps, the ids are OK and the problem lies elsewhere.Nicety
@btz what makes you think cppcheck suports putting command line switches in code comments?Uncommon
I run ./cppcheck --help and saw --inline-suppr option: --inline-suppr Enable inline suppressions. Use them by placing one or more comments in the form: // cppcheck-suppress memleak on the lines before the warning to suppress. Tried to use it in my code (running cppcheck with --inline-suppr) with the ids taken form xml output, but it does not work for me.Nicety
@btz I hadn't notice that - just tested it, and you are right - it doesn't work.Uncommon
Hello, just tested it with the latest cppcheck version 1.44 and it works like a charm. Things you have to remember when using cppcheck-suppress: 1. put "//cppcheck-suppress <id>" in a new line before the line in which error to be suppressed occurrs. 2. provide the correct, full paths to cppcheck (I used wrong one with /home/mycode/other_dir/../test.cpp) 3. Enjoy warning/problems free cppcheck reports :-) Thanks a lot Neil.Nicety
BTW, I had problems with multi-line statements. For example: // cppcheck-suppress errorname cout << a <newline> << b <newline> << line with cppcheck problem; Would still result in the error, but this would not: // cppcheck-suppress errorname cout << a << b << line with cppcheck problem;Terrellterrena
I was stuck until I added the argument to cppcheck --inline-suppr EnablePerbunan
M
3

According to the cppcheck man page, you can use the --template option to change the default output to include the id, e.g.

cppcheck /the/src/file --template='{file}:{line},{severity},{id},{message}'
Macrogamete answered 4/10, 2014 at 14:55 Comment(0)
H
1

If you're using the GUI, you can right click on the message that you want to suppress to pop up a menu. Select "Copy message id". Paste the message id into your code in place of "suppressed_error_id".

Habitforming answered 4/10, 2016 at 16:58 Comment(2)
Hi, can you describe in more detail what GUI you are referring to?Nicety
@Blaise, Sara is referring to the cppcheck GUI interface.Parallelize

© 2022 - 2024 — McMap. All rights reserved.