diff --git a/man/writing-rules.docbook b/man/writing-rules.docbook index fdd4be503..901c70a8b 100644 --- a/man/writing-rules.docbook +++ b/man/writing-rules.docbook @@ -8,7 +8,7 @@ Daniel - Marjamäki + Marjamaki Cppcheck @@ -21,34 +21,11 @@
Introduction - This is a short guide for developers who want to write Cppcheck - rules. + This is a short and simple guide that describes how rules are + written for Cppcheck. - There are two ways to write rules. - - - - Regular expressions - - - Simple rules can be created by using regular expressions. No - compilation is required. - - - - - C++ - - - Advanced rules must be created with C++. These rules must be - compiled and linked statically with Cppcheck. - - - - - It is a good first step to use regular expressions. It is easier. - You'll get results quicker. Therefore this guide will focus on regular - expressions. + The patterns are defined with regular expressions. It is required + that you know how regular expressions work.
@@ -59,105 +36,73 @@ before the rules are used. Cppcheck is designed to find bugs and dangerous code. Stylistic - information such as indentation, comments, etc are filtered out at an + information (such as indentation, comments, etc) are filtered out at an early state. You don't need to worry about such stylistic information when 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 code is simplified in many ways. For example: - - - - The templates are instantiated - - - - The typedefs are handled - - - - There is no "else if". These are converted into "else { - if.." - - - - The bodies of "if", "else", "while", "do" and "for" are always - enclosed in "{" and "}" - - - - A declaration of multiple variables is split up into multiple - variable declarations. "int a,b;" => "int a; int b;" - - - - There is no sizeof - - - - NULL is replaced with 0 - - - - Static value flow analysis is made. Known values are inserted - into the code. - - - - .. and many more - - - - The simplifications are made in the Cppcheck - Tokenizer. For more information see: - http://cppcheck.sourceforge.net/doxyoutput/classTokenizer.html + The code is simplified in many ways.
- Regular expressions + Creating a simple rule - Simple rules can be defined through regular expressions. + When creating a rule there are two steps: - 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. + + + Create the regular expression + + + + Create a XML based rule file + +
- Creating the regular expression + Step 1 - Creating the regular expression - Let's create a regular expression that checks for: + 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: if (p) free(p); - For such code the condition is often redundant, on most - implementations it is valid to free a NULL pointer. + For such code the condition is often redundant (on most + 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 - with some code: + with the code: void f() { if (p) free(p); } - Save that code as dealloc.cpp and use + Save that code as dealloc.cpp and then use cppcheck --rule=".+" dealloc.cpp: $ ./cppcheck --rule=".+" dealloc.cpp Checking dealloc.cpp... [dealloc.cpp:1]: (style) found ' void f ( ) { if ( p ) { free ( p ) ; } }' + The regular expression .+ matches everything + and the matching text is shown on the screen. + From that output we can see that the simplified code is: void f ( ) { if ( p ) { free ( p ) ; } } - Now that we know how the simplified code looks for a simple test - case, we can create a regular expression: + Now that we know how the simplified code looks. We can create a + regular expression that matches it properly: $ cppcheck --rule="if \( p \) { free \( p \) ; }" dealloc.cpp Checking dealloc.cpp... @@ -165,9 +110,9 @@ Checking dealloc.cpp...
- Create rule file + Step 2 - Create rule file - A rule consist of: + A rule file is a simple XML file that contains: