From 4a25327b57a1a98d057a4b5d99326a43a9460a53 Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Thu, 30 Jun 2011 20:58:43 +0200 Subject: [PATCH 1/8] Manual: Use tag for files and directories --- man/manual.docbook | 7 ++++--- man/writing-rules-1.docbook | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index f0f3ef59c..ac5b93e75 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -106,11 +106,12 @@ Checking path/file2.cpp... cppcheck src/a src/b - All files under "src/a" and "src/b" are then checked. + 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: + specify files/paths to ignore. With this command no files in + src/c are checked: cppcheck -isrc/c src diff --git a/man/writing-rules-1.docbook b/man/writing-rules-1.docbook index 620acf1e5..45dabd74a 100644 --- a/man/writing-rules-1.docbook +++ b/man/writing-rules-1.docbook @@ -85,7 +85,7 @@ free(p); } - Save that code as dealloc.cpp and then use + Save that code as dealloc.cpp and then use cppcheck --rule=".+" dealloc.cpp: $ ./cppcheck --rule=".+" dealloc.cpp @@ -134,7 +134,7 @@ Checking dealloc.cpp... </message> </rule> - If you save that xml data in dealloc.rule you + If you save that xml data in dealloc.rule you can test this rule: $ cppcheck --rule-file=dealloc.rule dealloc.cpp From e69c26a0cedc8b539604b050363e47ad7ae974ba Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Fri, 1 Jul 2011 17:06:56 +0200 Subject: [PATCH 2/8] Manual: Use and tags --- man/manual.docbook | 52 ++++++++++++++++++++----------------- man/writing-rules-1.docbook | 4 +-- man/writing-rules-2.docbook | 28 +++++++++++--------- man/writing-rules-3.docbook | 2 +- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index ac5b93e75..ce9da73dd 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -109,8 +109,8 @@ Checking path/file2.cpp... 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 + 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 @@ -175,16 +175,17 @@ Checking path/file2.cpp...
Enable messages - By default only error messages are shown. - Through the --enable command more checks can be - enabled. + By default only error messages + are shown. Through the --enable + command more checks can be enabled.
Stylistic issues - With --enable=style you enable most - warning, style and - performance messages. + With --enable=style you + enable most warning, + style and + performance messages. Here is a simple code example: @@ -198,8 +199,8 @@ Checking path/file2.cpp... } 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: + by default. To enable the stylistic messages, use the + --enable=style command: cppcheck --enable=style file3.c @@ -224,7 +225,7 @@ Checking path/file2.cpp... Enable all checks To enable all checks your can use the - --enable=all flag: + --enable=all flag: cppcheck --enable=all path
@@ -257,7 +258,7 @@ Checking path/file2.cpp... behaviour. But if you want to manually limit the checking you can do so with - -D. + -D. Beware that only the macros, which are given here and the macros defined in source files and known header files are considered. That @@ -276,7 +277,8 @@ Checking path/file2.cpp... Cppcheck can generate the output in XML format. - Use the --xml flag when you execute cppcheck: + Use the --xml flag when you + execute cppcheck: cppcheck --xml file1.cpp @@ -341,8 +343,8 @@ Checking path/file2.cpp... If you want to reformat the output so it looks different you can use templates. - To get Visual Studio compatible output you can use "--template - vs": + To get Visual Studio compatible output you can use + --template vs: cppcheck --template vs gui/test.cpp @@ -352,7 +354,8 @@ Checking path/file2.cpp... gui/test.cpp(31): error: Memory leak: b gui/test.cpp(16): error: Mismatching allocation and deallocation: k - To get gcc compatible output you can use "--template gcc": + To get gcc compatible output you can use + --template gcc: cppcheck --template gcc gui/test.cpp @@ -380,16 +383,17 @@ gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocati Suppressions If you want to filter out certain errors you can suppress these. - The --suppress= command line option is used to specify - suppressions on the command line. The format is one of: + The --suppress= command line option + is used to specify suppressions on the command line. + The format 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 + 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). @@ -425,7 +429,7 @@ uninitvar
User-defined allocation/deallocation functions - Cppcheck understands many common allocation and + Cppcheck understands many common allocation and deallocation functions. But not all. Here is example code that might leak memory or resources: @@ -459,11 +463,11 @@ void DestroyFred(void *p) When Cppcheck see this it understands that CreateFred will return allocated memory and that DestroyFred will deallocate memory. - Now, execute Cppcheck this way: + Now, execute cppcheck this way: cppcheck --append=fred.cpp fred1.cpp - The output from cppcheck is: + The output from cppcheck is: Checking fred1.cpp... [fred1.cpp:5]: (error) Memory leak: f @@ -487,7 +491,7 @@ void DestroyFred(void *p) code. To enable the exception safety checking you can use - --enable: + --enable: cppcheck --enable=exceptNew --enable=exceptRealloc fred.cpp diff --git a/man/writing-rules-1.docbook b/man/writing-rules-1.docbook index 45dabd74a..0de22f83d 100644 --- a/man/writing-rules-1.docbook +++ b/man/writing-rules-1.docbook @@ -30,7 +30,7 @@ Data representation of the source code The data used by the rules are not the raw source code. - Cppcheck will read the source code and process it + Cppcheck will read the source code and process it before the rules are used. Cppcheck is designed to find bugs and dangerous code. Stylistic @@ -86,7 +86,7 @@ } Save that code as dealloc.cpp and then use - cppcheck --rule=".+" dealloc.cpp: + cppcheck --rule=".+" dealloc.cpp: $ ./cppcheck --rule=".+" dealloc.cpp Checking dealloc.cpp... diff --git a/man/writing-rules-2.docbook b/man/writing-rules-2.docbook index 4b4295c4e..27f97c380 100644 --- a/man/writing-rules-2.docbook +++ b/man/writing-rules-2.docbook @@ -33,23 +33,25 @@ There are two ways to look at the data representation at runtime. - Using --rule=.+ is one way. All tokens are written on a line: + Using --rule=.+ is one way. + All tokens are written on a line: int a ; int b ; - Using --debug is another way. The tokens are line separated in the - same way as the original code: + Using --debug is another way. + The tokens are line separated in the same way as the original code: 1: int a@1 ; 2: int b@2 ; - In the --debug output there are "@1" and "@2" shown. These are the + In the --debug output there are + "@1" and "@2" shown. These are the variable ids (Cppcheck gives each variable a unique id). You can ignore these if you only plan to write rules with regular expressions, you can't use variable ids with regular expressions. - In general, I will use the --rule=.+ output in - this article because it is more compact. + In general, I will use the --rule=.+ + output in this article because it is more compact.
@@ -130,7 +132,8 @@ s8 x; array[x + 2] = 0; } - The --debug output for that is: + The --debug output for that + is: 1: void f ( ) 2: { @@ -154,7 +157,8 @@ s8 x; free(b); } - The --debug output for that is: + The --debug output for that + is: 1: void f ( ) 2: { @@ -196,7 +200,7 @@ s8 x; f2(); } - The --debug output: + The --debug output: 1: void f ( int x@1 ) 2: { @@ -253,7 +257,7 @@ s8 x; } The x=f1() is broken out. The - --debug output: + --debug output: 1: void f ( ) 2: { @@ -274,7 +278,7 @@ s8 x; } The x=f1() is broken out twice. The - --debug output: + --debug output: 1: void f ( ) 2: { @@ -331,7 +335,7 @@ s8 x; if (x != 0); } - The --debug output is: + The --debug output is: 1: void f ( ) 2: { diff --git a/man/writing-rules-3.docbook b/man/writing-rules-3.docbook index 102937ffe..38869c504 100644 --- a/man/writing-rules-3.docbook +++ b/man/writing-rules-3.docbook @@ -18,7 +18,7 @@ Introduction The goal for this article is to introduce how - Cppcheck rules are written with C++. With C++ it is + Cppcheck rules are written with C++. With C++ it is possible to write more complex rules than is possible with regular expressions.
From d46deb528b2e9e903a6621bc641703e56fc29aed Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Fri, 1 Jul 2011 17:13:51 +0200 Subject: [PATCH 3/8] Manual: Don't wrap with --- man/cppcheck-design.docbook | 10 ++++++---- man/manual.docbook | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/man/cppcheck-design.docbook b/man/cppcheck-design.docbook index f34dc0e12..3435b6244 100644 --- a/man/cppcheck-design.docbook +++ b/man/cppcheck-design.docbook @@ -94,7 +94,7 @@ analysis can be needed to avoid false warnings. Here is an example that logically is the same as the previous example: - void f1(char *s) + void f1(char *s) { s[20] = 0; } @@ -105,7 +105,9 @@ void f2() if (x + y == 2) { f1(a); } -}Cppcheck will report this message: +} + + Cppcheck will report this message: Array 'a[10]' index 20 out of bounds @@ -116,7 +118,7 @@ void f2() prove that "x+y==2" can be true when the function is called from "f2". No error message is reported for this code: - void f1(char *s) + void f1(char *s) { if (x + y == 2) { s[20] = 0; @@ -127,7 +129,7 @@ void f2() { char a[10]; f1(a); -} +}
diff --git a/man/manual.docbook b/man/manual.docbook index ce9da73dd..4798edcdb 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -206,9 +206,9 @@ Checking path/file2.cpp... The output from Cppcheck is now: - Checking file3.c... + 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 +[file3.c:3]: (style) The scope of the variable i can be reduced
From 533426d3c22dc4ca447aba9a16e7cdf570055b62 Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Fri, 1 Jul 2011 19:49:16 +0200 Subject: [PATCH 4/8] Manual: Use tags for menu items --- man/manual.docbook | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 4798edcdb..5dd33e95c 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -570,7 +570,7 @@ htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.<
Check source code - Use the Check menu. + Use the Check menu.
@@ -579,7 +579,7 @@ htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.< The results are shown in a list. You can show/hide certain types of messages through the - View menu. + View menu. Results can be saved to an XML file that can later be opened. See Save results to file and Open @@ -590,10 +590,11 @@ htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.< Settings The language can be changed at any time by using the - Language menu. + Language menu. More settings are available in - Edit>Preferences. + EditPreferences + .
From 364900c5c01bc18d54575538ba3c1fbfbb2bf568 Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Sat, 2 Jul 2011 14:04:13 +0200 Subject: [PATCH 5/8] Manual: Miscellaneous tag changes --- man/manual.docbook | 28 +++++++++++++++------------- man/writing-rules-1.docbook | 14 ++++++++------ man/writing-rules-2.docbook | 2 +- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 5dd33e95c..6d14a173c 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -434,13 +434,13 @@ uninitvar Here is example code that might leak memory or resources: - void foo(int x) + void foo(int x) { void *f = CreateFred(); if (x == 1) return; DestroyFred(f); -} +} If you analyse that with Cppcheck it won't find any leaks: @@ -460,8 +460,9 @@ void DestroyFred(void *p) free(p); } - When Cppcheck see this it understands that CreateFred will return - allocated memory and that DestroyFred will deallocate memory. + When Cppcheck see this it understands that CreateFred() + will return allocated memory and that DestroyFred() + will deallocate memory. Now, execute cppcheck this way: @@ -487,7 +488,7 @@ void DestroyFred(void *p) { } - By default cppcheck will not detect any problems in that + By default Cppcheck will not detect any problems in that code. To enable the exception safety checking you can use @@ -499,8 +500,8 @@ void DestroyFred(void *p) [fred.cpp:3]: (style) Upon exception there is memory leak: a - If an exception occurs when b is allocated, - a will leak. + If an exception occurs when b is allocated, + a will leak. Here is another example: @@ -528,17 +529,18 @@ int a(int sz) You can convert the XML output from cppcheck into a HTML report. You'll need Python and the pygments module - (http://pygments.org/) for this to work. In the Cppcheck source - tree there is a folder "htmlreport" that contains a script that transforms - a Cppcheck XML file into HTML output. + (http://pygments.org/) for this to + work. In the Cppcheck source tree there is a folder + htmlreport that contains a script + that transforms a Cppcheck XML file into HTML output. This command generates the help screen: - htmlreport/cppcheck-htmlreport -h + htmlreport/cppcheck-htmlreport -h The output screen says: - Usage: cppcheck-htmlreport [options] + Usage: cppcheck-htmlreport [options] Options: -h, --help show this help message and exit @@ -547,7 +549,7 @@ Options: --report-dir=REPORT_DIR The directory where the html report content is written. --source-dir=SOURCE_DIR - Base directory where source code files can be found. + Base directory where source code files can be found. An example usage: diff --git a/man/writing-rules-1.docbook b/man/writing-rules-1.docbook index 0de22f83d..3168b1195 100644 --- a/man/writing-rules-1.docbook +++ b/man/writing-rules-1.docbook @@ -39,7 +39,8 @@ you write rules. Between each token in the code there is always a space. For instance - the raw code "1+f()" is processed into "1 + f ( )". + the raw code "1+f()" is processed into "1 + f ( )" + . The code is simplified in many ways.
@@ -62,10 +63,10 @@
Step 1 - Creating the regular expression - Cppcheck uses the PCRE library to handle - regular expressions. PCRE stands for "Perl Compatible - Regular Expressions". The homepage for PCRE is - http://www.pcre.org. + Cppcheck uses the PCRE library to handle regular expressions. + PCRE stands for "Perl Compatible Regular Expressions". + The homepage for PCRE is + http://www.pcre.org/. Let's create a regular expression that checks for code such as: @@ -74,7 +75,8 @@ free(p); For such code the condition is often redundant (on most - implementations it is valid to free a NULL pointer). + implementations it is valid to free a NULL pointer). + The regular expression must be written for the simplified code. To see what the simplified code looks like you can create a source file diff --git a/man/writing-rules-2.docbook b/man/writing-rules-2.docbook index 27f97c380..71e1714d3 100644 --- a/man/writing-rules-2.docbook +++ b/man/writing-rules-2.docbook @@ -256,7 +256,7 @@ s8 x; } } - The x=f1() is broken out. The + The x=f1() is broken out. The --debug output: 1: void f ( ) From a02d6b461968d3ff6cc9d5b20a5bf26ca1f27e88 Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Sat, 2 Jul 2011 14:50:01 +0200 Subject: [PATCH 6/8] Manual: Move "Writing Cppcheck rules" author to the beginning of the appendix --- man/manual.docbook | 10 ++++++++++ man/writing-rules-1.docbook | 16 +--------------- man/writing-rules-2.docbook | 16 +--------------- man/writing-rules-3.docbook | 14 +------------- 4 files changed, 13 insertions(+), 43 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 6d14a173c..30bb7f503 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -627,6 +627,16 @@ htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.< + + + Daniel + Marjamäki + + Cppcheck + + + 2010-2011 + Writing Cppcheck rules &writing-rules-1; &writing-rules-2; diff --git a/man/writing-rules-1.docbook b/man/writing-rules-1.docbook index 3168b1195..ec4d10111 100644 --- a/man/writing-rules-1.docbook +++ b/man/writing-rules-1.docbook @@ -1,20 +1,6 @@
- - Part 1 - Getting started - - - Daniel - - Marjamäki - - - Cppcheck - - - - 2010 - + Part 1 - Getting started
Introduction diff --git a/man/writing-rules-2.docbook b/man/writing-rules-2.docbook index 71e1714d3..23156266e 100644 --- a/man/writing-rules-2.docbook +++ b/man/writing-rules-2.docbook @@ -1,20 +1,6 @@
- - Part 2 - The Cppcheck data representation - - - Daniel - - Marjamäki - - - Cppcheck - - - - 2010 - + Part 2 - The Cppcheck data representation
Introduction diff --git a/man/writing-rules-3.docbook b/man/writing-rules-3.docbook index 38869c504..07078c3a0 100644 --- a/man/writing-rules-3.docbook +++ b/man/writing-rules-3.docbook @@ -1,18 +1,6 @@
- - Part 3 - Introduction to writing rules with C++ - - - DanielMarjamäki - - - Cppcheck - - - - 2011 - + Part 3 - Introduction to writing rules with C++
Introduction From 9d9d1063de1dd99b6bc531a0cda208c1bf580e3e Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Sat, 2 Jul 2011 15:45:10 +0200 Subject: [PATCH 7/8] * Manual: Miscellaneous tag changes --- man/manual.docbook | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 30bb7f503..5c772731a 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -294,7 +294,7 @@ Checking path/file2.cpp... - file + file filename. Both relative and absolute paths are possible @@ -302,7 +302,7 @@ Checking path/file2.cpp... - line + line a number @@ -310,7 +310,7 @@ Checking path/file2.cpp... - id + id id of error. These are always valid symbolnames. @@ -318,17 +318,18 @@ Checking path/file2.cpp... - severity + severity - either error or style. - warning and performance are - saved as style. + either error or + style. + warning and performance + are saved as style. - msg + msg the error message @@ -375,8 +376,6 @@ gui/test.cpp:16: error: Mismatching allocation and deallocation: kChecking gui/test.cpp... gui/test.cpp,31,error,memleak,Memory leak: b gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocation: k - - @@ -391,15 +390,15 @@ gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocati [error id]:[filename2] [error id] - The error id is the id that you want to suppress. + 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). + 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. + The filename may include the wildcard characters + * or ?, which match any sequence of + characters or any single character respectively. cppcheck --suppress=memleak:file1.cpp src/ @@ -415,8 +414,6 @@ uninitvar You can then use the suppressions file: cppcheck --suppressions suppressions.txt src/ - - From 8edf654d24c9d78204f9de6c5a53a5e1a620e20a Mon Sep 17 00:00:00 2001 From: Tim Gerundt Date: Sat, 2 Jul 2011 16:21:58 +0200 Subject: [PATCH 8/8] Manual: Add internal links --- man/manual.docbook | 14 +++++++------- man/writing-rules-1.docbook | 2 +- man/writing-rules-2.docbook | 2 +- man/writing-rules-3.docbook | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 5c772731a..1bdca18ac 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -250,7 +250,7 @@ Checking path/file2.cpp...
- + Preprocessor configurations By default Cppcheck will check all preprocessor configurations @@ -263,11 +263,11 @@ Checking path/file2.cpp... Beware that only the macros, which are given here and the macros defined in source files and known header files are considered. That excludes all the macros defined in some system header files, which are by - default not examined by cppcheck. + default not examined by Cppcheck. The usage: if you, for example, want to limit the checking so the - only configuration to check should be "DEBUG=1;__cplusplus" then something - like this can be used: + only configuration to check should be DEBUG=1;__cplusplus + then something like this can be used: cppcheck -DDEBUG=1 -D__cplusplus path @@ -617,9 +617,9 @@ htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.< recommended that this known functionality is redefined. But feel free to try it. - As you can read in chapter 3 in this manual the default is that - Cppcheck checks all configurations. So only provide preprocessor defines - if you want to limit the checking. + As you can read in chapter + 3 in this manual the default is that Cppcheck checks all configurations. + So only provide preprocessor defines if you want to limit the checking.
diff --git a/man/writing-rules-1.docbook b/man/writing-rules-1.docbook index ec4d10111..1dd1db043 100644 --- a/man/writing-rules-1.docbook +++ b/man/writing-rules-1.docbook @@ -1,5 +1,5 @@ -
+
Part 1 - Getting started
diff --git a/man/writing-rules-2.docbook b/man/writing-rules-2.docbook index 23156266e..5a246d6c0 100644 --- a/man/writing-rules-2.docbook +++ b/man/writing-rules-2.docbook @@ -1,5 +1,5 @@ -
+
Part 2 - The Cppcheck data representation
diff --git a/man/writing-rules-3.docbook b/man/writing-rules-3.docbook index 07078c3a0..cb5b56787 100644 --- a/man/writing-rules-3.docbook +++ b/man/writing-rules-3.docbook @@ -1,5 +1,5 @@ -
+
Part 3 - Introduction to writing rules with C++
@@ -83,9 +83,9 @@ void CheckOther::divisionByZeroError()
Condition before deallocation - In the first Writing rules article I described a - rule that looks for redundant conditions. Here is the regular expression - that was shown: + In the first Writing rules part + I described a rule that looks for redundant conditions. Here is the regular + expression that was shown: if \( p \) { free \( p \) ; }