From 80b2c0594b557d43f6476e9b5790328cc7418d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 4 Dec 2010 20:01:55 +0100 Subject: [PATCH] Writing rules: Target this guide for beginners. Skip C++ and only describe how rules are created with regular expressions. --- man/writing-rules.docbook | 180 ++++++++++++-------------------------- 1 file changed, 55 insertions(+), 125 deletions(-) diff --git a/man/writing-rules.docbook b/man/writing-rules.docbook index 49e31b04f..1112dc067 100644 --- a/man/writing-rules.docbook +++ b/man/writing-rules.docbook @@ -21,8 +21,8 @@
Introduction - This is supposed to be a manual for developers who want to write - Cppcheck rules. + This is a short guide for developers who want to write Cppcheck + rules. There are two ways to write rules. @@ -46,130 +46,73 @@ - The data used by the rules are not the raw source code. Cppcheck - will read the source code and process it before the rules are used. + 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.
Data representation of the source code - There are two types of data you can use: symbol database and token - list. + The data used by the rules are not the raw source code. + Cppcheck will read the source code and process it + before the rules are used. -
- Token lists + Cppcheck is designed to find bugs and dangerous code. Stylistic + 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. - The code is stored in token lists (simple double-linked - lists). + Between each token in the code there is always a space. For instance + the raw code "1+f()" is processed into "1 + f ( )". - The token lists are designed for rule matching. All redundant - information is removed. A number of transformations are made - automatically on the token lists to simplify writing rules. + The code is simplified in many ways. For example: - The class Tokenizer create the token lists and - perform all simplifications. + + + The templates are instantiated + - The class Token is used for every token in the - token list. The Token class also contain - functionality for matching tokens. + + The typedefs are handled + -
- Normal token list + + There is no "else if". These are converted into "else { + if.." + - The first token list that is created has many basic - simplifications. For example: + + The bodies of "if", "else", "while", "do" and "for" are always + enclosed in "{" and "}" + - - - There are no templates. Templates have been - instantiated. - + + A declaration of multiple variables is split up into multiple + variable declarations. "int a,b;" => "int a; int b;" + - - There is no "else if". These are converted into "else { if - .." - + + There is no sizeof + - - The bodies of "if", "else", "while", "do" and "for" are - always enclosed in "{" and "}". - + + NULL is replaced with 0 + - - A declaration of multiple variables is split up into - multiple variable declarations. "int a,b;" => "int a; int - b;" - + + Static value flow analysis is made. Known values are inserted + into the code. + - - All variables have unique ID numbers - - -
+ + .. and many more + +
-
- Simplified token list - - The second token list that is created has all simplifications - the normal token list has and then many more simplifications. For - example: - - - - There is no sizeof - - - - There are no templates. - - - - Control flow transformations. - - - - NULL is replaced with 0. - - - - Static value flow analysis is made. Known values are - inserted into the code. - - - - variable initialization is replaced with assignment - - - - The simple token list is written if you use - --debug. For example, use cppcheck --debug - test1.cpp and check this code: - - void f1() { - int a = 1; - f2(a++); -} - - The result is: - - ##file test1.cpp -1: void f1 ( ) { -2: ; ; -3: f2 ( 1 ) ; -4: } - - -
- -
- Reference - - To learn more about the token lists, the doxygen information for - the Tokenizer is recommended. - - http://cppcheck.sourceforge.net/doxyoutput/classTokenizer.html -
-
+ The simplifications are made in the Cppcheck + Tokenizer. For more information see: + http://cppcheck.sourceforge.net/doxyoutput/classTokenizer.html
@@ -189,10 +132,10 @@ - Here is an example: + Here is a simple example: <?xml version="1.0"?> -<rule data="simple"> +<rule version="1"> <pattern>/ 0</pattern> <message> <id>divbyzero</id> @@ -201,21 +144,8 @@ </message> </rule> - It is recommended that you use the simple token - list whenever you can. If you need some information that is removed in it - then try the normal token list. + - When you write the patterns remember that; - - - - tokens are always separated by spaces. "1+2" is not - possible. - - - - there is no indentation, spaces, comments, line breaks. - - +