2009-11-11 20:45:26 +01:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
|
2011-10-08 13:45:53 +02:00
|
|
|
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
|
2009-11-11 20:45:26 +01:00
|
|
|
<book>
|
|
|
|
<bookinfo>
|
2013-06-02 15:40:51 +02:00
|
|
|
<title>Cppcheck 1.61 dev</title>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<date>2013-07-14</date>
|
2009-11-11 20:45:26 +01:00
|
|
|
</bookinfo>
|
|
|
|
|
2009-11-13 18:41:27 +01:00
|
|
|
<chapter>
|
|
|
|
<title>Introduction</title>
|
|
|
|
|
2009-11-14 15:17:06 +01:00
|
|
|
<para>Cppcheck is an analysis tool for C/C++ code. Unlike C/C++ compilers
|
2009-11-16 18:01:47 +01:00
|
|
|
and many other analysis tools, it doesn't detect syntax errors. Cppcheck
|
2009-11-14 15:17:06 +01:00
|
|
|
only detects the types of bugs that the compilers normally fail to detect.
|
2009-11-15 16:50:54 +01:00
|
|
|
The goal is no false positives.</para>
|
2009-11-13 18:41:27 +01:00
|
|
|
|
2009-12-04 20:00:01 +01:00
|
|
|
<para>Supported code and platforms:</para>
|
2009-11-13 18:41:27 +01:00
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>You can check non-standard code that includes various compiler
|
|
|
|
extensions, inline assembly code, etc.</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
2009-12-04 20:00:01 +01:00
|
|
|
<para>Cppcheck should be compilable by any C++ compiler that handles
|
|
|
|
the latest C++ standard.</para>
|
2009-11-13 18:41:27 +01:00
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
2010-12-13 15:13:50 +01:00
|
|
|
<para>Cppcheck should work on any platform that has sufficient CPU and
|
2009-12-04 20:00:01 +01:00
|
|
|
memory.</para>
|
2009-11-13 18:41:27 +01:00
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2009-12-04 20:00:01 +01:00
|
|
|
|
|
|
|
<para>Accuracy</para>
|
|
|
|
|
|
|
|
<para>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.</para>
|
|
|
|
|
|
|
|
<para>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.</para>
|
2009-11-13 18:41:27 +01:00
|
|
|
</chapter>
|
|
|
|
|
2009-11-11 20:45:26 +01:00
|
|
|
<chapter>
|
|
|
|
<title>Getting started</title>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>First test</title>
|
|
|
|
|
|
|
|
<para>Here is a simple code</para>
|
|
|
|
|
|
|
|
<programlisting>int main()
|
|
|
|
{
|
|
|
|
char a[10];
|
|
|
|
a[10] = 0;
|
|
|
|
return 0;
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>If you save that into <filename>file1.c</filename> and
|
|
|
|
execute:</para>
|
|
|
|
|
|
|
|
<programlisting>cppcheck file1.c</programlisting>
|
|
|
|
|
|
|
|
<para>The output from cppcheck will then be:</para>
|
|
|
|
|
|
|
|
<programlisting>Checking file1.c...
|
2010-05-08 10:24:40 +02:00
|
|
|
[file1.c:4]: (error) Array 'a[10]' index 10 out of bounds</programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Checking all files in a folder</title>
|
|
|
|
|
2010-12-13 15:13:50 +01:00
|
|
|
<para>Normally a program has many source files. And you want to check
|
|
|
|
them all. Cppcheck can check all source files in a directory:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
|
|
|
<programlisting>cppcheck path</programlisting>
|
|
|
|
|
2010-12-13 15:13:50 +01:00
|
|
|
<para>If "path" is a folder then cppcheck will check all source files in
|
2009-11-11 20:45:26 +01:00
|
|
|
this folder.</para>
|
|
|
|
|
|
|
|
<programlisting>Checking path/file1.cpp...
|
|
|
|
1/2 files checked 50% done
|
|
|
|
Checking path/file2.cpp...
|
|
|
|
2/2 files checked 100% done</programlisting>
|
|
|
|
</section>
|
|
|
|
|
2010-08-12 21:51:20 +02:00
|
|
|
<section>
|
|
|
|
<title>Excluding a file or folder from checking</title>
|
|
|
|
|
2011-02-06 12:04:54 +01:00
|
|
|
<para>To exclude a file or folder, there are two options.</para>
|
2010-08-12 21:51:20 +02:00
|
|
|
|
2011-02-06 12:04:54 +01:00
|
|
|
<para>The first option is to only provide the paths and files you want
|
|
|
|
to check.</para>
|
2010-08-12 21:51:20 +02:00
|
|
|
|
|
|
|
<programlisting>cppcheck src/a src/b</programlisting>
|
|
|
|
|
2011-06-30 20:58:43 +02:00
|
|
|
<para>All files under <filename class="directory">src/a</filename> and
|
|
|
|
<filename class="directory">src/b</filename> are then checked.</para>
|
2010-08-12 21:51:20 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>The second option is to use <parameter
|
|
|
|
class="command">-i</parameter>, with it you specify files/paths to
|
|
|
|
ignore. With this command no files in <filename
|
|
|
|
class="directory">src/c</filename> are checked:</para>
|
2011-02-06 12:04:54 +01:00
|
|
|
|
|
|
|
<programlisting>cppcheck -isrc/c src</programlisting>
|
2010-08-12 21:51:20 +02:00
|
|
|
</section>
|
|
|
|
|
2009-11-11 20:45:26 +01:00
|
|
|
<section>
|
2010-12-03 20:00:59 +01:00
|
|
|
<title>Severities</title>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2010-12-03 20:00:59 +01:00
|
|
|
<para>The possible severities for messages are:</para>
|
2010-10-17 18:06:02 +02:00
|
|
|
|
|
|
|
<variablelist>
|
2010-12-03 20:00:59 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term>error</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>used when bugs are found</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2010-10-17 18:06:02 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term>warning</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>suggestions about defensive programming to prevent
|
|
|
|
bugs</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>style</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>stylistic issues related to code cleanup (unused functions,
|
|
|
|
redundant code, constness, and such)</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>performance</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>Suggestions for making the code faster. These suggestions
|
|
|
|
are only based on common knowledge. It is not certain you'll get
|
|
|
|
any measurable difference in speed by fixing these
|
|
|
|
messages.</para>
|
2011-09-03 14:27:05 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>portability</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-01-11 18:35:37 +01:00
|
|
|
<para>portability warnings. 64-bit portability. code might work
|
|
|
|
different on different compilers. etc.</para>
|
2010-10-17 18:06:02 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2011-02-06 12:04:54 +01:00
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>information</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-01-11 18:35:37 +01:00
|
|
|
<para>Informational messages about checking problems.</para>
|
2011-02-06 12:04:54 +01:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2010-10-17 18:06:02 +02:00
|
|
|
</variablelist>
|
2010-12-03 20:00:59 +01:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Enable messages</title>
|
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>By default only <parameter class="command">error</parameter>
|
|
|
|
messages are shown. Through the <parameter
|
|
|
|
class="command">--enable</parameter> command more checks can be
|
|
|
|
enabled.</para>
|
2010-12-03 20:00:59 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<programlisting># enable warning messages
|
|
|
|
cppcheck --enable=warning file.c
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# enable performance messages
|
|
|
|
cppcheck --enable=performance file.c
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# enable information messages
|
|
|
|
cppcheck --enable=information file.c
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# For historical reasons, --enable=style enables warning, performance,
|
|
|
|
# portability and style messages. These are all reported as "style" when
|
|
|
|
# using the old xml format.
|
|
|
|
cppcheck --enable=style file.c
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# enable warning and information messages
|
|
|
|
cppcheck --enable=warning,information file.c
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# enable unusedFunction checking. This is not enabled by --enable=style
|
|
|
|
# because it doesn't work well on libraries.
|
|
|
|
cppcheck --enable=unusedFunction file.c
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# enable all messages
|
|
|
|
cppcheck --enable=all</programlisting>
|
2013-01-12 11:16:57 +01:00
|
|
|
|
2013-06-16 11:57:02 +02:00
|
|
|
<para>Please note that <literal>--enable=unusedFunction</literal> should
|
|
|
|
only be used when the whole program is scanned. And therefore
|
|
|
|
<literal>--enable=all</literal> should also only be used when the whole
|
|
|
|
program is scanned. The reason is that the unusedFunction checking will
|
|
|
|
warn if a function is not called. There will be noise if function calls
|
|
|
|
are not seen.</para>
|
|
|
|
|
2013-01-12 11:16:57 +01:00
|
|
|
<section>
|
|
|
|
<title>Inconclusive checks</title>
|
|
|
|
|
|
|
|
<para>By default Cppcheck only writes error messages if it is certain.
|
|
|
|
With <parameter class="command">--inconclusive</parameter> error
|
2013-06-09 10:34:45 +02:00
|
|
|
messages will also be written when the analysis is
|
|
|
|
inconclusive.</para>
|
2013-01-12 11:16:57 +01:00
|
|
|
|
|
|
|
<programlisting>cppcheck --inconclusive path</programlisting>
|
|
|
|
|
|
|
|
<para>This can of course cause false warnings, it might be reported
|
|
|
|
that there are bugs even though there are not. Only use this command
|
|
|
|
if false warnings are acceptable.</para>
|
|
|
|
</section>
|
2009-11-11 20:45:26 +01:00
|
|
|
</section>
|
2009-11-14 15:17:06 +01:00
|
|
|
|
2009-12-04 20:00:01 +01:00
|
|
|
<section>
|
2010-12-03 20:00:59 +01:00
|
|
|
<title>Saving results in file</title>
|
2009-12-04 20:00:01 +01:00
|
|
|
|
2010-12-03 20:00:59 +01:00
|
|
|
<para>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.</para>
|
2009-12-04 20:00:01 +01:00
|
|
|
|
2010-12-03 20:00:59 +01:00
|
|
|
<programlisting>cppcheck file1.c 2> err.txt</programlisting>
|
2009-12-04 20:00:01 +01:00
|
|
|
</section>
|
|
|
|
|
2009-11-14 15:17:06 +01:00
|
|
|
<section>
|
|
|
|
<title>Multithreaded checking</title>
|
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>The option -j is used to specify the number of threads you want to
|
|
|
|
use. For example, to use 4 threads to check the files in a
|
|
|
|
folder:</para>
|
2009-11-14 15:17:06 +01:00
|
|
|
|
|
|
|
<programlisting>cppcheck -j 4 path</programlisting>
|
|
|
|
</section>
|
2009-11-11 20:45:26 +01:00
|
|
|
</chapter>
|
|
|
|
|
2011-07-02 16:21:58 +02:00
|
|
|
<chapter id="preprocessor-configurations">
|
2010-05-16 08:48:03 +02:00
|
|
|
<title>Preprocessor configurations</title>
|
|
|
|
|
|
|
|
<para>By default Cppcheck will check all preprocessor configurations
|
2013-06-09 10:34:45 +02:00
|
|
|
(except those that have #error in them).</para>
|
2010-05-16 08:48:03 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>You can use -D to change this. When you use -D, cppcheck will by
|
|
|
|
default only check the given configuration and nothing else. This is how
|
|
|
|
compilers work. But you can use <literal>--force</literal> or
|
|
|
|
<literal><literal>--max-configs</literal></literal> to override the number
|
|
|
|
of configurations.</para>
|
2010-05-16 08:48:03 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<programlisting># check all configurations
|
|
|
|
cppcheck file.c
|
2010-07-10 08:12:23 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# only check the configuration A
|
|
|
|
cppcheck -DA file.c
|
2010-05-16 08:48:03 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
# check all configurations when macro A is defined
|
|
|
|
cppcheck -DA --force file.c</programlisting>
|
|
|
|
|
|
|
|
<para>Another useful flag might be -U. It undefines a symbol. Example
|
|
|
|
usage:</para>
|
|
|
|
|
|
|
|
<programlisting>cppcheck -UX file.c</programlisting>
|
|
|
|
|
|
|
|
<para>That will mean that X is not defined. Cppcheck will not check what
|
|
|
|
happens when X is defined.</para>
|
2010-05-16 08:48:03 +02:00
|
|
|
</chapter>
|
|
|
|
|
2009-11-11 20:45:26 +01:00
|
|
|
<chapter>
|
|
|
|
<title>XML output</title>
|
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<para>Cppcheck can generate the output in <literal>XML</literal> format.
|
|
|
|
There is an old <literal>XML</literal> format (version 1) and a new
|
|
|
|
<literal>XML</literal> format (version 2). Please use the new version if
|
|
|
|
you can.</para>
|
|
|
|
|
|
|
|
<para>The old version is kept for backwards compatibility only. It will
|
|
|
|
not be changed. But it will likely be removed someday. Use
|
|
|
|
<parameter>--xml</parameter> to enable this format.</para>
|
|
|
|
|
|
|
|
<para>The new version fixes a few problems with the old format. The new
|
|
|
|
format will probably be updated in future versions of cppcheck with new
|
|
|
|
attributes and elements. A sample command to check a file and output
|
|
|
|
errors in the new <literal>XML</literal> format:</para>
|
|
|
|
|
|
|
|
<para><programlisting>cppcheck --xml-version=2 file1.cpp</programlisting>Here
|
|
|
|
is a sample version 2 report:</para>
|
|
|
|
|
|
|
|
<programlisting><?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<results version="2">
|
|
|
|
<cppcheck version="1.53">
|
|
|
|
<errors>
|
|
|
|
<error id="someError" severity="error" msg="short error text"
|
|
|
|
verbose="long error text" inconclusive="true">
|
|
|
|
<location file="file.c" line="1"/>
|
|
|
|
</error>
|
|
|
|
</errors>
|
|
|
|
</results></programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<section>
|
|
|
|
<title>The <error> element</title>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<para>Each error is reported in a <literal><error></literal>
|
|
|
|
element. Attributes:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag class="attribute">id</sgmltag></term>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<listitem>
|
|
|
|
<para>id of error. These are always valid symbolnames.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag class="attribute">severity</sgmltag></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>either: <literal>error</literal>,
|
|
|
|
<literal>warning</literal>, <literal>style</literal>,
|
|
|
|
<literal>performance</literal>, <literal>portability</literal> or
|
|
|
|
<literal>information</literal></para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag class="attribute">msg</sgmltag></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>the error message in short format</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag>verbose</sgmltag></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>the error message in long format.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag>inconclusive</sgmltag></term>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<listitem>
|
|
|
|
<para>This attribute is only used when the message is
|
|
|
|
inconclusive.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</section>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<section>
|
|
|
|
<title>The <location> element</title>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<para>All locations related to an error is listed with
|
|
|
|
<literal><location></literal> elements. The primary location is
|
|
|
|
listed first.</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<para>Attributes:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag class="attribute">file</sgmltag></term>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<listitem>
|
|
|
|
<para>filename. Both relative and absolute paths are
|
|
|
|
possible</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag class="attribute">line</sgmltag></term>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<listitem>
|
|
|
|
<para>a number</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><sgmltag>msg</sgmltag></term>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2012-04-08 11:56:53 +02:00
|
|
|
<listitem>
|
|
|
|
<para>this attribute doesn't exist yet. But in the future we may
|
2013-06-09 10:34:45 +02:00
|
|
|
add a short message for each location.</para>
|
2012-04-08 11:56:53 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</section>
|
2009-11-11 20:45:26 +01:00
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<chapter>
|
|
|
|
<title>Reformatting the output</title>
|
|
|
|
|
|
|
|
<para>If you want to reformat the output so it looks different you can use
|
|
|
|
templates.</para>
|
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>To get Visual Studio compatible output you can use <parameter
|
|
|
|
class="command">--template=vs</parameter>:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2011-11-10 23:00:15 +01:00
|
|
|
<programlisting>cppcheck --template=vs gui/test.cpp</programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
|
|
|
<para>This output will look like this:</para>
|
|
|
|
|
|
|
|
<programlisting>Checking gui/test.cpp...
|
|
|
|
gui/test.cpp(31): error: Memory leak: b
|
|
|
|
gui/test.cpp(16): error: Mismatching allocation and deallocation: k</programlisting>
|
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>To get gcc compatible output you can use <parameter
|
|
|
|
class="command">--template=gcc</parameter>:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2011-11-10 23:00:15 +01:00
|
|
|
<programlisting>cppcheck --template=gcc gui/test.cpp</programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
|
|
|
<para>The output will look like this:</para>
|
|
|
|
|
|
|
|
<programlisting>Checking gui/test.cpp...
|
|
|
|
gui/test.cpp:31: error: Memory leak: b
|
|
|
|
gui/test.cpp:16: error: Mismatching allocation and deallocation: k</programlisting>
|
|
|
|
|
|
|
|
<para>You can write your own pattern (for example a comma-separated
|
|
|
|
format):</para>
|
|
|
|
|
2011-11-10 23:00:15 +01:00
|
|
|
<programlisting>cppcheck --template="{file},{line},{severity},{id},{message}" gui/test.cpp</programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
|
|
|
<para>The output will look like this:</para>
|
|
|
|
|
|
|
|
<programlisting>Checking gui/test.cpp...
|
|
|
|
gui/test.cpp,31,error,memleak,Memory leak: b
|
|
|
|
gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocation: k</programlisting>
|
2013-03-02 15:16:18 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>The following format specifiers are supported:</para>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>callstack</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-07-15 08:44:00 +02:00
|
|
|
callstack - if available
|
2013-07-14 16:41:20 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>file</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-07-15 08:44:00 +02:00
|
|
|
filename
|
2013-07-14 16:41:20 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>id</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-07-15 08:44:00 +02:00
|
|
|
message id
|
2013-07-14 16:41:20 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>line</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-07-15 08:44:00 +02:00
|
|
|
line number
|
2013-07-14 16:41:20 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>message</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-07-15 08:44:00 +02:00
|
|
|
verbose message text
|
2013-07-14 16:41:20 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>severity</term>
|
|
|
|
|
|
|
|
<listitem>
|
2013-07-15 08:44:00 +02:00
|
|
|
severity
|
2013-07-14 16:41:20 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2013-07-08 06:44:25 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>The escape sequences \b (backspace), \n (newline), \r (formfeed) and
|
|
|
|
\t (horizontal tab) are supported.</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<chapter>
|
|
|
|
<title>Suppressions</title>
|
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<para>If you want to filter out certain errors you can suppress
|
|
|
|
these.</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<section>
|
|
|
|
<title>Suppressing a certain error type</title>
|
|
|
|
|
|
|
|
<para>You can suppress certain types of errors. The format for such a
|
|
|
|
suppression is one of:</para>
|
|
|
|
|
|
|
|
<programlisting>[error id]:[filename]:[line]
|
2009-11-11 20:45:26 +01:00
|
|
|
[error id]:[filename2]
|
|
|
|
[error id]</programlisting>
|
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<para>The <replaceable>error id</replaceable> is the id that you want to
|
|
|
|
suppress. The easiest way to get it is to use the <parameter
|
|
|
|
class="command">--xml</parameter> command line flag. Copy and paste the
|
|
|
|
<replaceable>id</replaceable> string from the XML output. This may be
|
|
|
|
<literal>*</literal> to suppress all warnings (for a specified file or
|
|
|
|
files).</para>
|
|
|
|
|
|
|
|
<para>The <replaceable>filename</replaceable> may include the wildcard
|
|
|
|
characters <literal>*</literal> or <literal>?</literal>, which match any
|
|
|
|
sequence of characters or any single character respectively. It is
|
|
|
|
recommended that you use "/" as path separator on all operating
|
|
|
|
systems.</para>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Command line suppression</title>
|
|
|
|
|
|
|
|
<para>The <parameter class="command">--suppress=</parameter> command
|
|
|
|
line option is used to specify suppressions on the command line.
|
|
|
|
Example:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<programlisting>cppcheck --suppress=memleak:src/file1.cpp src/</programlisting>
|
|
|
|
</section>
|
2011-02-08 12:13:50 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<section>
|
|
|
|
<title>Listing suppressions in a file</title>
|
2011-02-23 08:59:30 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<para>You can create a suppressions file. Example:</para>
|
2011-02-23 08:59:30 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<programlisting>// suppress memleak and exceptNew errors in the file src/file1.cpp
|
|
|
|
memleak:src/file1.cpp
|
|
|
|
exceptNew:src/file1.cpp
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
// suppress all uninitvar errors in all files
|
2009-11-11 20:45:26 +01:00
|
|
|
uninitvar</programlisting>
|
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<para>Note that you may add empty lines and comments in the
|
|
|
|
suppressions file.</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2011-07-21 08:34:57 +02:00
|
|
|
<para>You can use the suppressions file like this:</para>
|
|
|
|
|
|
|
|
<programlisting>cppcheck --suppressions suppressions.txt src/</programlisting>
|
|
|
|
</section>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Inline suppressions</title>
|
|
|
|
|
|
|
|
<para>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.</para>
|
|
|
|
|
|
|
|
<para>This code will normally generate an error message:</para>
|
|
|
|
|
|
|
|
<programlisting>void f() {
|
|
|
|
char arr[5];
|
|
|
|
arr[10] = 0;
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>The output is:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck test.c
|
|
|
|
Checking test.c...
|
|
|
|
[test.c:3]: (error) Array 'arr[5]' index 10 out of bounds</programlisting>
|
|
|
|
|
|
|
|
<para>To suppress the error message, a comment can be added:</para>
|
|
|
|
|
|
|
|
<programlisting>void f() {
|
|
|
|
char arr[5];
|
|
|
|
|
|
|
|
// cppcheck-suppress arrayIndexOutOfBounds
|
|
|
|
arr[10] = 0;
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>Now the --inline-suppr flag can be used to suppress the warning.
|
|
|
|
No error is reported when invoking cppcheck this way:</para>
|
|
|
|
|
|
|
|
<programlisting>cppcheck --inline-suppr test.c</programlisting>
|
|
|
|
</section>
|
2009-11-11 20:45:26 +01:00
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<chapter>
|
2013-07-14 16:41:20 +02:00
|
|
|
<title>Rules</title>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>You can define custom rules using regular expressions.</para>
|
|
|
|
|
|
|
|
<para>These rules can not perform sophisticated analysis of the code. But
|
|
|
|
they give you an easy way to check for various simple patterns in the
|
|
|
|
code.</para>
|
|
|
|
|
|
|
|
<para>To get started writing rules, see the related articles here:</para>
|
|
|
|
|
|
|
|
<para><uri>http://sourceforge.net/projects/cppcheck/files/Articles/</uri></para>
|
|
|
|
|
|
|
|
<para>The file format for rules is:</para>
|
|
|
|
|
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<rule>
|
|
|
|
<tokenlist>LIST</tokenlist>
|
|
|
|
<pattern>PATTERN</pattern>
|
|
|
|
<message>
|
|
|
|
<id>ID</id>
|
|
|
|
<severity>SEVERITY</severity>
|
|
|
|
<summary>SUMMARY</summary>
|
|
|
|
</message>
|
|
|
|
</rule></programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
|
|
|
<section>
|
2013-07-14 16:41:20 +02:00
|
|
|
<title><tokenlist></title>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>The <literal><tokenlist></literal> element is optional. With
|
|
|
|
this element you can control what tokens are checked. The
|
|
|
|
<literal>LIST</literal> can be either <literal>define</literal>,
|
|
|
|
<literal>raw</literal>, <literal>normal</literal> or
|
|
|
|
<literal>simple</literal>.</para>
|
2009-11-14 11:01:09 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>define</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>used to check #define preprocessor statements.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>raw</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>used to check the preprocessor output.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>normal</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>used to check the <literal>normal</literal> token list.
|
|
|
|
There are some simplifications.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>simple</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>used to check the simple token list. All simplifications are
|
|
|
|
used. Most Cppcheck checks use the simple token list.</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
|
|
|
|
<para>If there is no <tokenlist> element then
|
|
|
|
<literal>simple</literal> is used automatically.</para>
|
|
|
|
</section>
|
2009-11-14 11:01:09 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<section>
|
|
|
|
<title><pattern></title>
|
|
|
|
|
|
|
|
<para>The <literal>PATTERN</literal> is the
|
|
|
|
<literal>PCRE</literal>-compatible regular expression that will be
|
|
|
|
executed.</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title><id></title>
|
|
|
|
|
|
|
|
<para>The ID specify the user-defined message id.</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title><severity></title>
|
|
|
|
|
|
|
|
<para>The <literal>SEVERITY</literal> must be one of the
|
|
|
|
<literal>Cppcheck</literal> severities: <literal>information</literal>,
|
|
|
|
<literal>performance</literal>, <literal>portability</literal>,
|
|
|
|
<literal>style</literal>, <literal>warning</literal>, or
|
|
|
|
<literal>error</literal>.</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title><summary></title>
|
|
|
|
|
|
|
|
<para>Optional. The summary for the message. If no summary is given, the
|
|
|
|
matching tokens is written.</para>
|
|
|
|
</section>
|
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<chapter>
|
|
|
|
<title>Library configuration</title>
|
|
|
|
|
|
|
|
<para><literal>Cppcheck</literal> has internal knowledge about how
|
|
|
|
standard C/C++ functions work. There is no internal knowledge about how
|
2013-07-15 21:58:29 +02:00
|
|
|
all libraries and environments work, and there can't be.
|
|
|
|
<literal>Cppcheck</literal> can be told how libraries and environments
|
|
|
|
work by using configuration files.</para>
|
2013-07-14 16:41:20 +02:00
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<para>The idea is that users will be able to download library
|
|
|
|
configuration files for all popular libraries and environments
|
|
|
|
here:</para>
|
2013-07-14 16:41:20 +02:00
|
|
|
|
|
|
|
<para><uri>http://cppcheck.sourceforge.net/archive</uri></para>
|
|
|
|
|
|
|
|
<para>Ideally, all you need to do is choose and download the configuration
|
|
|
|
files you need.</para>
|
|
|
|
|
|
|
|
<para>The archive is not complete however. If you can't find the
|
|
|
|
configuration file you need in the archive, you can wait - maybe somebody
|
|
|
|
else will write it and share it. Or you can write your own configuration
|
|
|
|
file (and then it's possible to share your configuration file with
|
|
|
|
others).</para>
|
|
|
|
|
|
|
|
<para>A minimal configuration file looks like this:</para>
|
|
|
|
|
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
</def></programlisting>
|
|
|
|
|
|
|
|
<section>
|
2013-07-15 08:44:00 +02:00
|
|
|
<title><alloc> and <dealloc></title>
|
2013-07-14 16:41:20 +02:00
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<para>Allocation and deallocation is defined <literal>using
|
|
|
|
<alloc> and <dealloc>. These are used inside inside
|
|
|
|
<literal><memory></literal> or
|
|
|
|
<literal><resource>.</literal></literal></para>
|
2013-07-14 16:41:20 +02:00
|
|
|
|
|
|
|
<para>Here is example code:</para>
|
|
|
|
|
|
|
|
<programlisting>void ok()
|
|
|
|
{
|
|
|
|
char *p = alloc_something();
|
|
|
|
free_something(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void leak()
|
2009-11-14 11:01:09 +01:00
|
|
|
{
|
2013-07-14 16:41:20 +02:00
|
|
|
char *p = alloc_something();
|
2011-07-02 14:04:13 +02:00
|
|
|
}</programlisting>
|
2009-11-14 11:01:09 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>Cppcheck doesn't normally report any errors for that:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<programlisting># cppcheck test.c
|
|
|
|
Checking test.c...</programlisting>
|
2009-11-14 11:01:09 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>Example configuration:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<memory>
|
|
|
|
<alloc>alloc_something</alloc>
|
2013-07-15 08:44:00 +02:00
|
|
|
<dealloc>free_something</dealloc>
|
2013-07-14 16:41:20 +02:00
|
|
|
</memory>
|
|
|
|
</def></programlisting>
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<para>That tells Cppcheck that <literal>alloc_something</literal>
|
|
|
|
allocates memory and that the matching deallocation function is
|
|
|
|
<literal>free_something</literal>.</para>
|
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>Output from <literal>Cppcheck</literal>:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck --library=something.cfg test.c
|
|
|
|
Checking test.c...
|
|
|
|
[test.c:10]: (error) Memory leak: p</programlisting>
|
2013-07-15 08:44:00 +02:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title><ignore> and <use></title>
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<para>The <literal><ignore></literal> and
|
|
|
|
<literal><use></literal> tells Cppcheck how functions uses
|
|
|
|
allocated memory. Example code:</para>
|
2013-07-14 16:41:20 +02:00
|
|
|
|
|
|
|
<programlisting>void f()
|
2009-11-11 20:45:26 +01:00
|
|
|
{
|
2013-07-14 16:41:20 +02:00
|
|
|
char *p = alloc_something();
|
|
|
|
do_something(p);
|
|
|
|
*p = 0;
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>If you want that the <literal>do_something</literal> function call
|
|
|
|
is ignored, use <ignore>:</para>
|
|
|
|
|
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<memory>
|
|
|
|
<alloc>alloc_something</alloc>
|
2013-07-15 08:44:00 +02:00
|
|
|
<dealloc>free_something</dealloc>
|
2013-07-14 16:41:20 +02:00
|
|
|
</memory>
|
|
|
|
<ignore>do_something</ignore>
|
|
|
|
</def></programlisting>
|
|
|
|
|
|
|
|
<para>Running <literal>Cppcheck</literal> now:</para>
|
|
|
|
|
|
|
|
<para><programlisting># cppcheck --library=something.cfg test.c
|
|
|
|
Checking test.c...
|
|
|
|
[test.c:10]: (error) Memory leak: p</programlisting>If the
|
|
|
|
<literal>do_something</literal> takes the allocated memory and
|
|
|
|
deallocates it later, then use <literal><use></literal>
|
|
|
|
instead:</para>
|
|
|
|
|
|
|
|
<para><programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<memory>
|
|
|
|
<alloc>alloc_something</alloc>
|
2013-07-15 08:44:00 +02:00
|
|
|
<dealloc>free_something</dealloc>
|
2013-07-14 16:41:20 +02:00
|
|
|
<use>do_something</use>
|
|
|
|
</memory>
|
|
|
|
</def></programlisting>Running Cppcheck now:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck --library=something.cfg test.c
|
|
|
|
Checking test.c...</programlisting>
|
2013-07-15 08:44:00 +02:00
|
|
|
|
|
|
|
<para>Cppcheck will often assume that functions "use" allocated memory.
|
|
|
|
By using <ignore> you can make Cppcheck detect more errors. By
|
|
|
|
using <use>, no extra errors are detected but Cppcheck will not
|
|
|
|
need to assume.</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
2013-07-15 21:58:29 +02:00
|
|
|
<title>allocate but not initialize</title>
|
2013-07-15 08:44:00 +02:00
|
|
|
|
|
|
|
<para>Some allocation function initialize the data, others don't. Here
|
|
|
|
is a example code:</para>
|
|
|
|
|
|
|
|
<programlisting>void f()
|
|
|
|
{
|
|
|
|
char *p = alloc_something();
|
|
|
|
char c = *p;
|
|
|
|
free_something();
|
|
|
|
}</programlisting>
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<para>No error is reported:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck --library=something.cfg test.c
|
|
|
|
Checking test.c...</programlisting>
|
|
|
|
|
2013-07-15 08:44:00 +02:00
|
|
|
<para>Here is a configuration that tells cppcheck that alloc_something
|
|
|
|
doesn't initialize the data:</para>
|
|
|
|
|
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<memory>
|
|
|
|
<alloc init="false">alloc_something</alloc>
|
|
|
|
<dealloc>free_something</dealloc>
|
|
|
|
</memory>
|
|
|
|
</def></programlisting>
|
|
|
|
|
|
|
|
<para>Now you will get this error message:</para>
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<programlisting># cppcheck --library=something.cfg test.c
|
2013-07-15 08:44:00 +02:00
|
|
|
Checking test.c...
|
|
|
|
[test.c:4]: (error) Memory is allocated but not initialized: p</programlisting>
|
2013-07-14 16:41:20 +02:00
|
|
|
</section>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
<section>
|
|
|
|
<title>function arguments: null pointers</title>
|
|
|
|
|
|
|
|
<para>You can define if a function parameter can be NULL or if it must
|
|
|
|
be non-NULL.</para>
|
|
|
|
|
|
|
|
<para>Example code:</para>
|
|
|
|
|
|
|
|
<programlisting>void do_something(char *p);
|
|
|
|
|
|
|
|
void f()
|
|
|
|
{
|
|
|
|
do_something(NULL);
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>Normally no error is reported for that code.</para>
|
|
|
|
|
|
|
|
<para>But if the do_something() parameter should be non-NULL you can use
|
|
|
|
this configuration:</para>
|
|
|
|
|
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<function name="do_something">
|
|
|
|
<arg nr="1">
|
|
|
|
<not-null/>
|
|
|
|
</arg>
|
|
|
|
</function>
|
|
|
|
</def></programlisting>
|
|
|
|
|
|
|
|
<para>Now the output from cppcheck is:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck --library=something.cfg test1.c
|
|
|
|
Checking test1.c...
|
|
|
|
[test1.c:5]: (error) Null pointer dereference</programlisting>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Function arguments: uninitialized data</title>
|
|
|
|
|
|
|
|
<para>Here is example code:</para>
|
|
|
|
|
|
|
|
<programlisting>void do_something(char *p);
|
|
|
|
|
|
|
|
void f()
|
|
|
|
{
|
|
|
|
char str[10];
|
|
|
|
do_something(str);
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>Normally <literal>Cppcheck</literal> doesn't report any error
|
|
|
|
message for that. However if the parameter must be initialized there is
|
|
|
|
a problem. Here is a configuration that says that the parameter must be
|
|
|
|
initialized:</para>
|
|
|
|
|
|
|
|
<para><programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<function name="do_something">
|
|
|
|
<arg nr="1">
|
|
|
|
<not-uninit/>
|
|
|
|
</arg>
|
|
|
|
</function>
|
|
|
|
</def></programlisting>Now the cppcheck output is:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck --library=something.cfg test1.c
|
|
|
|
Checking test1.c...
|
|
|
|
[test1.c:6]: (error) Uninitialized variable: str</programlisting>
|
|
|
|
</section>
|
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<section>
|
|
|
|
<title>no return</title>
|
|
|
|
|
|
|
|
<para>You can define if a function is "noreturn" or not. Example
|
|
|
|
code:</para>
|
|
|
|
|
|
|
|
<programlisting>int f(int x)
|
2009-11-11 20:45:26 +01:00
|
|
|
{
|
2013-07-14 16:41:20 +02:00
|
|
|
int a;
|
|
|
|
if (x == 3)
|
|
|
|
a = 1;
|
|
|
|
else
|
|
|
|
do_something();
|
|
|
|
return a; // a is uninitialized if do_something() is called and it returns
|
2009-11-11 20:45:26 +01:00
|
|
|
}</programlisting>
|
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>The output is:</para>
|
|
|
|
|
|
|
|
<programlisting># cppcheck test.c
|
|
|
|
Checking test.c...</programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>To tell Cppcheck that <literal>do_something</literal> is not a
|
|
|
|
<literal>noreturn</literal> function, use such configuration:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<programlisting><?xml version="1.0"?>
|
|
|
|
<def>
|
|
|
|
<function name="do_something">
|
|
|
|
<noreturn>false</noreturn>
|
|
|
|
</function>
|
|
|
|
</def></programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<para>Now Cppcheck will be able to detect the error:</para>
|
2009-11-11 20:45:26 +01:00
|
|
|
|
2013-07-14 16:41:20 +02:00
|
|
|
<programlisting>cppcheck --library=something.cfg test.c
|
|
|
|
Checking test.c...
|
|
|
|
[test.c:8]: (error) Uninitialized variable: a</programlisting>
|
2009-11-11 20:45:26 +01:00
|
|
|
</section>
|
|
|
|
</chapter>
|
|
|
|
|
2009-12-25 18:56:14 +01:00
|
|
|
<chapter>
|
2010-12-13 15:13:50 +01:00
|
|
|
<title>HTML report</title>
|
2009-12-25 18:56:14 +01:00
|
|
|
|
2010-12-13 15:13:50 +01:00
|
|
|
<para>You can convert the XML output from cppcheck into a HTML report.
|
2013-06-09 10:34:45 +02:00
|
|
|
You'll need Python and the pygments module (<ulink
|
|
|
|
url="http://pygments.org/">http://pygments.org/</ulink>) for this to work.
|
|
|
|
In the Cppcheck source tree there is a folder <filename
|
|
|
|
class="directory">htmlreport</filename> that contains a script that
|
|
|
|
transforms a Cppcheck XML file into HTML output.</para>
|
2009-12-25 18:56:14 +01:00
|
|
|
|
|
|
|
<para>This command generates the help screen:</para>
|
|
|
|
|
2011-07-02 14:04:13 +02:00
|
|
|
<programlisting>htmlreport/cppcheck-htmlreport -h</programlisting>
|
2009-12-25 18:56:14 +01:00
|
|
|
|
|
|
|
<para>The output screen says:</para>
|
|
|
|
|
2011-07-02 14:04:13 +02:00
|
|
|
<programlisting>Usage: cppcheck-htmlreport [options]
|
2009-12-25 18:56:14 +01:00
|
|
|
|
|
|
|
Options:
|
2012-09-22 20:41:13 +02:00
|
|
|
-h, --help show this help message and exit
|
|
|
|
--file=FILE The cppcheck xml output file to read defects from.
|
|
|
|
Default is reading from stdin.
|
|
|
|
--report-dir=REPORT_DIR
|
2009-12-25 18:56:14 +01:00
|
|
|
The directory where the html report content is written.
|
|
|
|
--source-dir=SOURCE_DIR
|
2011-07-02 14:04:13 +02:00
|
|
|
Base directory where source code files can be found.</programlisting>
|
2009-12-25 18:56:14 +01:00
|
|
|
|
|
|
|
<para>An example usage:</para>
|
|
|
|
|
|
|
|
<programlisting>./cppcheck gui/test.cpp --xml 2> err.xml
|
|
|
|
htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.</programlisting>
|
2010-07-13 16:23:02 +02:00
|
|
|
</chapter>
|
2009-12-25 18:56:14 +01:00
|
|
|
|
2010-07-13 16:23:02 +02:00
|
|
|
<chapter>
|
|
|
|
<title>Graphical user interface</title>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Introduction</title>
|
|
|
|
|
|
|
|
<para>A Cppcheck GUI is available.</para>
|
|
|
|
|
|
|
|
<para>The main screen is shown immediately when the GUI is
|
|
|
|
started.</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Check source code</title>
|
|
|
|
|
2011-07-01 19:49:16 +02:00
|
|
|
<para>Use the <guimenu>Check</guimenu> menu.</para>
|
2010-07-13 16:23:02 +02:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Inspecting results</title>
|
|
|
|
|
|
|
|
<para>The results are shown in a list.</para>
|
|
|
|
|
|
|
|
<para>You can show/hide certain types of messages through the
|
2011-07-01 19:49:16 +02:00
|
|
|
<guimenu>View</guimenu> menu.</para>
|
2010-07-13 16:23:02 +02:00
|
|
|
|
2010-12-13 15:13:50 +01:00
|
|
|
<para>Results can be saved to an XML file that can later be opened. See
|
2010-07-13 16:23:02 +02:00
|
|
|
<literal>Save results to file</literal> and <literal>Open
|
|
|
|
XML</literal>.</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Settings</title>
|
|
|
|
|
|
|
|
<para>The language can be changed at any time by using the
|
2011-07-01 19:49:16 +02:00
|
|
|
<guimenu>Language</guimenu> menu.</para>
|
2010-07-13 16:23:02 +02:00
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>More settings are available in <menuchoice>
|
|
|
|
<guimenu>Edit</guimenu>
|
|
|
|
|
|
|
|
<guimenuitem>Preferences</guimenuitem>
|
|
|
|
</menuchoice>.</para>
|
2010-07-13 16:23:02 +02:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Project files</title>
|
|
|
|
|
|
|
|
<para>The project files are used to store project specific settings.
|
|
|
|
These settings are:</para>
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>include folders</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>preprocessor defines</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
|
2013-06-09 10:34:45 +02:00
|
|
|
<para>As you can read in <link
|
|
|
|
linkend="preprocessor-configurations">chapter 3</link> in this manual
|
|
|
|
the default is that Cppcheck checks all configurations. So only provide
|
|
|
|
preprocessor defines if you want to limit the checking.</para>
|
2010-07-13 16:23:02 +02:00
|
|
|
</section>
|
2009-12-25 18:56:14 +01:00
|
|
|
</chapter>
|
2009-11-11 20:45:26 +01:00
|
|
|
</book>
|