From 68f2c47c5cd50dd32c03ac4370c292ba5982696c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 5 Dec 2010 13:19:30 +0100 Subject: [PATCH] Writing rules: Minor updates. Trying to make it easier. --- man/writing-rules.docbook | 60 +++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/man/writing-rules.docbook b/man/writing-rules.docbook index fc504d4bb..deb2ba782 100644 --- a/man/writing-rules.docbook +++ b/man/writing-rules.docbook @@ -118,7 +118,8 @@
Regular expressions - Simple rules can be defined through regular expressions. + Simple rules can be defined through regular expressions. Cppcheck + uses PCRE to handle regular expressions.
Creating regular expression @@ -128,35 +129,42 @@ if (p) free(p); - The condition is often redundant, it is valid to free a NULL - pointer. + The condition is often redundant, on most implementations it is + valid to free a NULL pointer. - It is important to write the regular expression so it matches the - simplified code. Create a source file that has the bad code: + The regular expression must match the simplified code. Create a + source file that has the bad code: void f() { - if (p) free(p); + if (p) + free(p); } - I intentionally put the whole pattern on a single line. The - simplified code is written on a single line of code. - - To see the simplified code I use cppcheck --debug - file.cpp. + To see the simplified code use cppcheck --debug + dealloc.cpp. ##file dealloc.cpp 1: void f ( ) { -2: if ( p ) { free ( p ) ; } -3: } +2: if ( p ) { +3: free ( p ) ; } +4: } - I save that in a file test.txt. + In the --debug output there are line feeds and + line numbers. But the newlines and line numbers are only there to make + the output easier to read. The real simplified code is written on a + single line: - Now we can use grep to develop a regular - expression. + void f ( ) { if ( p ) { free ( p ) ; } } - grep "if [(] p [)] { free [(] p [)] ; }" test.txt + Now we can use cppcheck --rule to develop a + regular expression. - Feel free to improve the pattern. + $ cppcheck --rule="if \( p \) { free \( p \) ; }" dealloc.cpp +Checking dealloc.cpp... +[dealloc.cpp:2]: (style) found 'if ( p ) { free ( p ) ; }' + + Feel free to improve the pattern. Above, the pointer name must be + "p" to get a match.
@@ -170,8 +178,8 @@ - an optional error message that is reported when pattern is - found + an error message that is reported when pattern is found - this + is optional, if none is given a default message is written. @@ -179,7 +187,7 @@ <?xml version="1.0"?> <rule version="1"> - <pattern>if [(] p [)] { free [(] p [)] ; }</pattern> + <pattern>if \( p \) { free \( p \) ; }</pattern> <message> <id>redundantCondition</id> <severity>style</severity> @@ -189,10 +197,14 @@ The message, id, severity and summary elements are - optional. If they are not written default values are used. + optional. But highly recommended. - Now you can test this rule. Use the cppcheck - --rule-file=dealloc.rule test.cpp command. + If you save that xml data in dealloc.rule you + can test this rule: + + $ cppcheck --rule-file=dealloc.rule dealloc.cpp +Checking dealloc.cpp... +[dealloc.cpp:2]: (style) Redundant condition. It is valid to free a NULL pointer.