diff --git a/man/writing-rules.docbook b/man/writing-rules.docbook index 1112dc067..fc504d4bb 100644 --- a/man/writing-rules.docbook +++ b/man/writing-rules.docbook @@ -120,32 +120,79 @@ Simple rules can be defined through regular expressions. - A rule consist of: +
+ Creating regular expression - - - a pattern to search for. - + Let's create a regular expression that checks for: - - an error message that is reported when pattern is found - - + if (p) + free(p); - Here is a simple example: + The condition is often redundant, it is valid to free a NULL + pointer. - <?xml version="1.0"?> + It is important to write the regular expression so it matches the + simplified code. Create a source file that has the bad code: + + void f() { + 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. + + ##file dealloc.cpp +1: void f ( ) { +2: if ( p ) { free ( p ) ; } +3: } + + I save that in a file test.txt. + + Now we can use grep to develop a regular + expression. + + grep "if [(] p [)] { free [(] p [)] ; }" test.txt + + Feel free to improve the pattern. +
+ +
+ Create rule file + + A rule consist of: + + + + a pattern to search for. + + + + an optional error message that is reported when pattern is + found + + + + Here is a simple example: + + <?xml version="1.0"?> <rule version="1"> - <pattern>/ 0</pattern> + <pattern>if [(] p [)] { free [(] p [)] ; }</pattern> <message> - <id>divbyzero</id> - <severity>error</severity> - <summary>Division by zero</summary> + <id>redundantCondition</id> + <severity>style</severity> + <summary>Redundant condition. It is valid to free a NULL pointer.</summary> </message> </rule> - + The message, id, + severity and summary elements are + optional. If they are not written default values are used. - + Now you can test this rule. Use the cppcheck + --rule-file=dealloc.rule test.cpp command. +