Wolf
Wolf

Reputation: 10238

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

I use Cppcheck 1.70 for checking C++-Builder projects. I get a lot of style warnings like this

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

These functions are event handlers that are used, but not explicitly from within the C++ code: they are bound by the VCL runtime after loading the corresponding form or data module. Naturally, Cppcheck does not check the DFM files, that's why it cannot detect the references between events and handlers defined there.

Some options that come to my mind

How can I specifically suppress these warnings about apparently unused event handlers?

Upvotes: 4

Views: 7201

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596683

There is a chapter in the CppCheck documentation about suppressing warnings/errors. Chapter 6.2 in particular will be useful to you, as you will be able to suppress warnings about the individual event handlers as needed:

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

Also see the following questions for more details:

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

Can I include cppcheck suppression within a function header?

Upvotes: 7

Related Questions