Cppcheck 1.49
Table of Contents
1. Introduction............................................................................................................................................1
2. Getting started .......................................................................................................................................2
2.1. First test.......................................................................................................................................2
2.2. Checking all files in a folder .......................................................................................................2
2.3. Excluding a file or folder from checking ....................................................................................2
2.4. Severities .....................................................................................................................................3
2.5. Enable messages .........................................................................................................................3
2.5.1. Stylistic issues ................................................................................................................3
2.5.2. Unused functions ............................................................................................................4
2.5.3. Enable all checks ............................................................................................................4
2.6. Saving results in file ....................................................................................................................4
2.7. Multithreaded checking...............................................................................................................5
3. Preprocessor configurations..................................................................................................................6
4. XML output............................................................................................................................................7
5. Reformatting the output........................................................................................................................8
6. Suppressions...........................................................................................................................................9
7. Leaks .....................................................................................................................................................10
7.1. User-defined allocation/deallocation functions.........................................................................10
8. Exception safety ...................................................................................................................................12
9. HTML report .......................................................................................................................................13
10. Graphical user interface....................................................................................................................14
10.1. Introduction.............................................................................................................................14
10.2. Check source code ..................................................................................................................14
10.3. Inspecting results.....................................................................................................................14
10.4. Settings....................................................................................................................................14
10.5. Project files..............................................................................................................................14
iii
Chapter 1. Introduction
Cppcheck is an analysis tool for C/C++ code. Unlike C/C++ compilers and many other analysis tools, it
doesn’t detect syntax errors. Cppcheck only detects the types of bugs that the compilers normally fail to
detect. The goal is no false positives.
Supported code and platforms:
• You can check non-standard code that includes various compiler extensions, inline assembly code, etc.
• Cppcheck should be compilable by any C++ compiler that handles the latest C++ standard.
• Cppcheck should work on any platform that has sufficient CPU and memory.
Accuracy
Please understand that there are limits of Cppcheck. Cppcheck is rarely wrong about reported errors. But
there are many bugs that it doesn’t detect.
You will find more bugs in your software by testing your software carefully, than by using Cppcheck.
You will find more bugs in your software by instrumenting your software, than by using Cppcheck. But
Cppcheck can still detect some of the bugs that you miss when testing and instrumenting your software.
1
Chapter 2. Getting started
2.1. First test
Here is a simple code
int main()
{
char a[10];
a[10] = 0;
return 0;
}
If you save that into file1.c and execute:
cppcheck file1.c
The output from cppcheck will then be:
Checking file1.c...
[file1.c:4]: (error) Array ’a[10]’ index 10 out of bounds
2.2. Checking all files in a folder
Normally a program has many source files. And you want to check them all. Cppcheck can check all
source files in a directory:
cppcheck path
If "path" is a folder then cppcheck will check all source files in this folder.
Checking path/file1.cpp...
1/2 files checked 50% done
Checking path/file2.cpp...
2/2 files checked 100% done
2.3. Excluding a file or folder from checking
To exclude a file or folder, there are two options.
2
Chapter 2. Getting started
The first option is to only provide the paths and files you want to check.
cppcheck src/a src/b
All files under "src/a" and "src/b" are then checked.
The second option is to use -i, with it you specify files/paths to ignore. With this command no files in
"src/c" are checked:
cppcheck -isrc/c src
2.4. Severities
The possible severities for messages are:
error
used when bugs are found
warning
suggestions about defensive programming to prevent bugs
style
stylistic issues related to code cleanup (unused functions, redundant code, constness, and such)
performance
suggestions for making the code faster
information
Informational messages that might be interesting. Ignore these messages unless you really agree.
* The performance messages are based on ’common knowledge’. It is not certain that fixing performance messages will make any
measurable difference in speed. Fixing performance messages generally doesn’t make your code more readable.
2.5. Enable messages
By default only error messages are shown. Through the --enable command more checks can be
enabled.
3
2.5.1. Stylistic issues
With --enable=style you enable most warning, style and performance messages.
Chapter 2. Getting started
Here is a simple code example:
void f(int x)
{
int i;
if (x == 0)
{
i = 0;
}
}
There are no bugs in that code so Cppcheck won’t report anything by default. To enable the stylistic
messages, use the --enable=style command:
cppcheck --enable=style file3.c
The output from Cppcheck is now:
Checking file3.c...
[file3.c:3]: (style) Variable ’i’ is assigned a value that is never used
[file3.c:3]: (style) The scope of the variable i can be reduced
2.5.2. Unused functions
This check will try to find unused functions. It is best to use this when the whole program is checked, so
that all usages is seen by cppcheck.
cppcheck --enable=unusedFunction path
2.5.3. Enable all checks
To enable all checks your can use the --enable=all flag:
cppcheck --enable=all path
4
2.6. Saving results in file
Chapter 2. Getting started
Many times you will want to save the results in a file. You can use the normal shell redirection for piping
error output to a file.
cppcheck file1.c 2> err.txt
2.7. Multithreaded checking
To use 4 threads to check the files in a folder:
cppcheck -j 4 path
5