抑制 Cppcheck 中自动绑定事件处理程序从未使用过的警告

Suppress never-used warnings for auto-bound event handlers in Cppcheck

我使用 Cppcheck 1.70 检查 C++-Builder 项目。我收到很多这样的样式警告

[source\DbgRecMain.cpp:452]: (style) The function 'FormResize' is never used.

这些函数是 使用的事件处理程序,但不是明确地从 C++ 代码中使用:它们在加载相应的表单或数据模块后由 VCL 运行时绑定。自然地,Cppcheck 不检查 DFM 文件,这就是它无法检测事件和那里定义的处理程序之间的引用的原因。

我想到的一些选项

我如何专门禁止这些关于明显未使用的事件处理程序的警告?

CppCheck documentation中有一章是关于压制warnings/errors的。第 6.2 章尤其对您有用,因为您将能够根据需要抑制有关各个事件处理程序的警告:

Chapter 6. Suppressions

If you want to filter out certain errors you can suppress these.

6.1. Suppressing a certain error type

You can suppress certain types of errors. The format for such a suppression is one of:

[error id]:[filename]:[line]
[error id]:[filename2]
[error id]

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. This may be * to suppress all warnings (for a specified file or files).

The filename may include the wildcard characters * or ?, which match any sequence of characters or any single character respectively. It is recommended that you use "/" as path separator on all operating systems.

6.1.1. Command line suppression

The --suppress= command line option is used to specify suppressions on the command line. Example:

cppcheck --suppress=memleak:src/file1.cpp src/

6.1.2. Listing suppressions in a file

You can create a suppressions file. Example:

// suppress memleak and exceptNew errors in the file src/file1.cpp
memleak:src/file1.cpp
exceptNew:src/file1.cpp

// suppress all uninitvar errors in all files
uninitvar

Note that you may add empty lines and comments in the suppressions file. You can use the suppressions file like this:

cppcheck --suppressions-list=suppressions.txt src/

6.2. Inline suppressions

Suppressions can also be added directly in the code by adding comments that contain special keywords. Before adding such comments, consider that the code readability is sacrificed a little.

This code will normally generate an error message:

void f() {
    char arr[5];
    arr[10] = 0;
}

The output is:

# cppcheck test.c
Checking test.c...
[test.c:3]: (error) Array ’arr[5]’ index 10 out of bounds

To suppress the error message, a comment can be added:

void f() {
    char arr[5];

    // cppcheck-suppress arrayIndexOutOfBounds
    arr[10] = 0;
}

Now the --inline-suppr flag can be used to suppress the warning. No error is reported when invoking cppcheck this way:

cppcheck --inline-suppr test.c

另请参阅以下问题了解更多详情:

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

Can I include cppcheck suppression within a function header?