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

不想你离开。 提交于 2019-11-29 11:02:20

问题


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"

回答1:


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.




回答2:


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.




回答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}'



回答4:


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".



来源:https://stackoverflow.com/questions/2956331/how-to-use-cppchecks-inline-suppression-filter-option-for-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!