From 49a9b59490298870be1880ca72772012666333dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 21 Jun 2015 15:18:24 +0200 Subject: [PATCH] manual: rewrote the chapter about cppcheck extensions/addons --- man/manual.docbook | 190 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 155 insertions(+), 35 deletions(-) diff --git a/man/manual.docbook b/man/manual.docbook index 22e91d681..9415badcc 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -5,7 +5,7 @@ Cppcheck 1.70 dev - 2015-01-03 + 2015-06-20 @@ -366,8 +366,8 @@ cppcheck -DA --force file.c cwe - CWE ID for message. This attribute is only used - when the CWE ID for the message is known. + CWE ID for message. This attribute is only used when the CWE + ID for the message is known. @@ -1192,7 +1192,8 @@ Checking unusedvar.cpp... To get started writing rules, see the related articles here: - http://sourceforge.net/projects/cppcheck/files/Articles/ + http://sourceforge.net/projects/cppcheck/files/Articles/ The file format for rules is: @@ -1294,53 +1295,172 @@ Checking unusedvar.cpp... - Cppcheck extensions with Python + Cppcheck addons - Using dump files it is possible to write Cppcheck extensions with - for instance Python. + Cppcheck addons are implemented as standalone scripts or programs. + With Cppcheck addons, you can for instance: - 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. + + + add extra custom checkers that use sophisticated analysis + + + + visualize your code + + + + etc + +
- Simple checker: Division by zero + Using Cppcheck addons - Here is a simple checker: + Currently there are two steps to use an addon: - import cppcheckdata + + + Run Cppcheck to generate dump files + -data = cppcheckdata.parsedump('1.c.dump') + + Run the addon on the dump files + + -for token in data.tokenlist: - if token.str == '/' or token.str == '%': - # Get denominator (2nd operand) - den = token.astOperand2 + The --dump flag is used to generate dump files. + To generate a dump file for every source file in the foo/ folder: - # Can denominator be zero? - if den.getValue(0): - print '[' + token.file + ':' + str(token.linenr) + '] Division by zero' + cppcheck --dump foo/ - Example usage: + To run a addon script on all dump files in the foo/ folder: - cppcheck --dump 1.c -python divzero.py + python addon.py foo/*.dump + +
+ Where to find some Cppcheck addons + + There are a few addons that can be downloaded. + + + + Addons provided by the Cppcheck project, currently just a + simple threadsafety checker: + http://github.com/danmar/cppcheck/blob/master/addons + + + + ublinter, a project that wants to "lint" for "undefined + behaviour": http://github.com/danmar/ublinter + + + + We would be happy to add a link to your addon here (no matter if + it's commercial or free). +
- Licensing + Writing Cppcheck addons - 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. + Cppcheck generates dump files in XML format that contains: - The cppcheckdata.py is also free to use. No - matter if your project is open source or closed source. Use it for any - purpose. + + + Token list + + + + Syntax trees + + + + Symbol database (functions, classes, variables, all scopes, + ..) + + + + Known values (value flow analysis) + + + + Cppcheck can't execute addons directly. There is no direct + interface. This means there are not much restrictions: + + + + You can use any licensing you want for your addons + + + + You can use an arbitrary script/programming language to write + addons + + + + The user interface and output is defined by you + + + + You can use addons for other use cases than generating + warnings + + + + For your convenience, Cppcheck provides cppcheckdata.py that you + can use to access Cppcheck data from Python. Using this is + optional. + +
+ Example 1 - print all tokens + + Script: + + import sys +import cppcheckdata + +def printtokens(data): + for token in data.tokenlist: + print(token.str) + +for arg in sys.argv[1:]: + printtokens(cppcheckdata.parse(arg)) +
+ +
+ Example 2 - List all functions + + Script: + + import sys +import cppcheckdata + +def printfunctions(data): + for scope in data.scopes: + if scope.type == 'Function': + print(scope.className) + +for arg in sys.argv[1:]: + printfunctions(cppcheckdata.parse(arg)) +
+ +
+ Example 3 - List all classes + + Script: + + import sys +import cppcheckdata + +def printclasses(data): + for scope in data.scopes: + if scope.type == 'Class': + print(scope.className) + +for arg in sys.argv[1:]: + printfunctions(cppcheckdata.parse(arg)) +