如何对 C++ 代码使用 cppcheck 的内联抑制过滤器选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2956331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:36:34  来源:igfitidea点击:

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

c++static-analysiserror-suppressioncppcheck

提问by Blaise

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-supprcommand. However, I can't find what "suppressed_error_id" I should put in the comment:

我想使用 Cppcheck 对我的 C++ 代码进行静态代码分析。我了解到我可以使用--inline-suppr命令抑制某种警告。但是,我找不到应该在评论中输入什么“suppressed_error_id”:

// cppcheck-suppress "suppressed_error_id"

采纳答案by Andy Krouwel

According to the cppcheck help:

根据 cppcheck 帮助:

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.

错误 id 是您要抑制的 id。获得它的最简单方法是使用 --xml 命令行标志。从 xml 输出中复制并粘贴 id 字符串。

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

因此,对一些包含带有--xml标志的错误的代码运行 cppcheck ,然后在生成的 XML 文件中查找它的名称。

回答by Andy Krouwel

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

您可以更改输出模板以从命令行显示错误 id,这非常简洁。

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

对于显示错误 ID 的 Visual Studio 格式输出,请将其添加到命令行:

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

到源文件中的上一行。

回答by mabraham

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

根据cppcheck 手册页,您可以使用该--template选项更改默认输出以包含 id,例如

cppcheck /the/src/file --template='{file}:{line},{severity},{id},{message}'

回答by Sara Wolfie

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

如果您使用的是 GUI,则可以右键单击要取消显示的消息以弹出菜单。选择“复制消息 ID”。将消息 ID 粘贴到您的代码中以代替“suppressed_error_id”。