3

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

  • Adding some explicit references, but this have to be maintained by hand.
  • Suppressing all warnings of this kind, but this would hide really dead code.

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

Wolf
  • 9,679
  • 7
  • 62
  • 108

1 Answers1

5

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?

Community
  • 1
  • 1
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Yes, the RT*M part. The second part is to filter the DFM files and create a list of `On\w+ = \w+`. Probably it's easier to generate some nonsense code that "uses" these handlers instead of finding line numbers. – Wolf Sep 16 '15 at 08:07
  • You don't need the line numbers. Just put an appropriate `cppcheck-suppress` comment immediately above each event handler that CppCheck complains about. – Remy Lebeau Sep 16 '15 at 17:11
  • Ok, this seems the most pragmatic solution. I already use some inline suppressions. I also tried the pseudo-usage code approach which worked. But after having a broader look on the problem I tend to suppress the `unusedFunction` check for all Form files (via `--suppressions-list`), because I already keep them as simple and short as possible. – Wolf Sep 17 '15 at 08:36
  • I have to accept your answer, even if it did not help me with my original issue. **It helped understand** that I should have asked better: I tried to avoid manual work in response to connect/disconnect handlers in the IDE which means doing the same thing twice. (In cases like this, I'm unsatisfied by the solution and try to step back to understand my actual problem even better. And that led me to question my understanding of IDE-managed units e.g. Forms.) – Wolf Sep 20 '15 at 11:49