From 851f89d15fdf31f333ebb409069ff6de89e30b0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 18 Jul 2014 19:17:32 +0200 Subject: [PATCH] manual: add chapter about writing cppcheck extensions with python --- man/manual.docbook | 265 +++++++++++++++++++++++++++------------------ 1 file changed, 158 insertions(+), 107 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 649334615..b2fe8fb0c 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -594,113 +594,6 @@ Checking test.c... - - Rules - - You can define custom rules using regular expressions. - - These rules can not perform sophisticated analysis of the code. But - they give you an easy way to check for various simple patterns in the - code. - - To get started writing rules, see the related articles here: - - http://sourceforge.net/projects/cppcheck/files/Articles/ - - The file format for rules is: - - <?xml version="1.0"?> -<rule> - <tokenlist>LIST</tokenlist> - <pattern>PATTERN</pattern> - <message> - <id>ID</id> - <severity>SEVERITY</severity> - <summary>SUMMARY</summary> - </message> -</rule> - -
- <tokenlist> - - The <tokenlist> element is optional. With - this element you can control what tokens are checked. The - LIST can be either define, - raw, normal or - simple. - - - - define - - - used to check #define preprocessor statements. - - - - - raw - - - used to check the preprocessor output. - - - - - normal - - - used to check the normal token list. - There are some simplifications. - - - - - simple - - - used to check the simple token list. All simplifications are - used. Most Cppcheck checks use the simple token list. - - - - - If there is no <tokenlist> element then - simple is used automatically. -
- -
- <pattern> - - The PATTERN is the - PCRE-compatible regular expression that will be - executed. -
- -
- <id> - - The ID specify the user-defined message id. -
- -
- <severity> - - The SEVERITY must be one of the - Cppcheck severities: information, - performance, portability, - style, warning, or - error. -
- -
- <summary> - - Optional. The summary for the message. If no summary is given, the - matching tokens is written. -
-
- Library configuration @@ -997,6 +890,164 @@ Checking noreturn.c... + + Rules + + You can define custom rules using regular expressions. + + These rules can not perform sophisticated analysis of the code. But + they give you an easy way to check for various simple patterns in the + code. + + To get started writing rules, see the related articles here: + + http://sourceforge.net/projects/cppcheck/files/Articles/ + + The file format for rules is: + + <?xml version="1.0"?> +<rule> + <tokenlist>LIST</tokenlist> + <pattern>PATTERN</pattern> + <message> + <id>ID</id> + <severity>SEVERITY</severity> + <summary>SUMMARY</summary> + </message> +</rule> + +
+ <tokenlist> + + The <tokenlist> element is optional. With + this element you can control what tokens are checked. The + LIST can be either define, + raw, normal or + simple. + + + + define + + + used to check #define preprocessor statements. + + + + + raw + + + used to check the preprocessor output. + + + + + normal + + + used to check the normal token list. + There are some simplifications. + + + + + simple + + + used to check the simple token list. All simplifications are + used. Most Cppcheck checks use the simple token list. + + + + + If there is no <tokenlist> element then + simple is used automatically. +
+ +
+ <pattern> + + The PATTERN is the + PCRE-compatible regular expression that will be + executed. +
+ +
+ <id> + + The ID specify the user-defined message id. +
+ +
+ <severity> + + The SEVERITY must be one of the + Cppcheck severities: information, + performance, portability, + style, warning, or + error. +
+ +
+ <summary> + + Optional. The summary for the message. If no summary is given, the + matching tokens is written. +
+
+ + + Cppcheck extensions with Python + + Using dump files it is possible to write Cppcheck extensions with + for instance Python. + + The cppcheckdata.py module + (http://github.com/danmar/cppcheck/blob/master/tools/cppcheckdata.py) + allows you to load such dump file. It contains + Token/Variable/ValueFlow.Value/Scope + classes that are similar to the C++ classes in + Cppcheck-core. The doxygen information for the + C++ classes should be somewhat useful for Python + developers also. + +
+ Simple checker: Division by zero + + Here is a simple checker: + + import cppcheckdata + +data = cppcheckdata.parsedump('1.c.dump') + +for token in data.tokenlist: + if token.str == '/' or token.str == '%': + # Get denominator (2nd operand) + den = token.astOperand2 + + # Can denominator be zero? + if den.getValue(0): + print '[' + token.file + ':' + str(token.linenr) + '] Division by zero' + + Example usage: + + cppcheck --dump 1.c +python divzero.py +
+ +
+ Licensing + + The dump file is just a xml file, so it is an open interface + without restrictions. You can use it in any way you need. + + The cppcheckdata.py is also free to use. No + matter if your project is open source or closed source. Use it for any + purpose. +
+
+ HTML report