2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
cppcheckdata
|
|
|
|
|
|
|
|
This is a Python module that helps you access Cppcheck dump data.
|
|
|
|
|
|
|
|
License: No restrictions, use this as you need.
|
|
|
|
"""
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2015-08-18 10:59:57 +02:00
|
|
|
import xml.etree.ElementTree as ET
|
2015-12-07 19:54:41 +01:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
|
|
class Directive:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Directive class. Contains information about each preprocessor directive in the source code.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
str The directive line, with all C or C++ comments removed
|
|
|
|
file Name of (possibly included) file where directive is defined
|
|
|
|
linenr Line number in (possibly included) file where directive is defined
|
|
|
|
|
|
|
|
To iterate through all directives use such code:
|
|
|
|
@code
|
|
|
|
data = cppcheckdata.parsedump(...)
|
|
|
|
for cfg in data.configurations:
|
|
|
|
for directive in cfg.directives:
|
|
|
|
print(directive.str)
|
|
|
|
@endcode
|
|
|
|
"""
|
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
str = None
|
|
|
|
file = None
|
|
|
|
linenr = None
|
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.str = element.get('str')
|
|
|
|
self.file = element.get('file')
|
|
|
|
self.linenr = element.get('linenr')
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2016-07-25 13:47:26 +02:00
|
|
|
class Token:
|
|
|
|
"""
|
|
|
|
Token class. Contains information about each token in the source code.
|
|
|
|
|
|
|
|
The CppcheckData.tokenlist is a list of Token items
|
|
|
|
|
|
|
|
C++ class: http://cppcheck.net/devinfo/doxyoutput/classToken.html
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
str Token string
|
|
|
|
next Next token in tokenlist. For last token, next is None.
|
|
|
|
previous Previous token in tokenlist. For first token, previous is None.
|
|
|
|
link Linked token in tokenlist. Each '(', '[' and '{' are linked to the
|
|
|
|
corresponding '}', ']' and ')'. For templates, the '<' is linked to
|
|
|
|
the corresponding '>'.
|
|
|
|
scope Scope information for this token. See the Scope class.
|
|
|
|
isName Is this token a symbol name
|
|
|
|
isNumber Is this token a number, for example 123, 12.34
|
|
|
|
isInt Is this token a int value such as 1234
|
|
|
|
isFloat Is this token a int value such as 12.34
|
|
|
|
isString Is this token a string literal such as "hello"
|
|
|
|
strlen string length for string literal
|
|
|
|
isChar Is this token a char literal such as 'x'
|
|
|
|
isOp Is this token a operator
|
|
|
|
isArithmeticalOp Is this token a arithmetic operator
|
|
|
|
isAssignmentOp Is this token a assignment operator
|
|
|
|
isComparisonOp Is this token a comparison operator
|
2016-07-25 18:03:44 +02:00
|
|
|
isLogicalOp Is this token a logical operator: && ||
|
2016-07-25 13:47:26 +02:00
|
|
|
varId varId for token, each variable has a unique non-zero id
|
|
|
|
variable Variable information for this token. See the Variable class.
|
|
|
|
function If this token points at a function call, this attribute has the Function
|
|
|
|
information. See the Function class.
|
|
|
|
values Possible values of token
|
|
|
|
typeScope type scope (token->type()->classScope)
|
|
|
|
astParent ast parent
|
|
|
|
astOperand1 ast operand1
|
|
|
|
astOperand2 ast operand2
|
|
|
|
file file name
|
|
|
|
linenr line number
|
|
|
|
|
|
|
|
To iterate through all tokens use such code:
|
|
|
|
@code
|
|
|
|
data = cppcheckdata.parsedump(...)
|
|
|
|
for cfg in data.configurations:
|
|
|
|
code = ''
|
|
|
|
for token in cfg.tokenlist:
|
|
|
|
code = code + token.str + ' '
|
|
|
|
print(code)
|
|
|
|
@endcode
|
|
|
|
"""
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
Id = None
|
|
|
|
str = None
|
|
|
|
next = None
|
|
|
|
previous = None
|
2015-06-21 13:26:32 +02:00
|
|
|
linkId = None
|
|
|
|
link = None
|
2014-07-21 16:10:04 +02:00
|
|
|
scopeId = None
|
|
|
|
scope = None
|
2015-06-21 13:26:32 +02:00
|
|
|
isName = False
|
|
|
|
isNumber = False
|
|
|
|
isInt = False
|
|
|
|
isFloat = False
|
|
|
|
isString = False
|
2014-07-21 16:10:04 +02:00
|
|
|
strlen = None
|
2015-06-21 13:26:32 +02:00
|
|
|
isChar = False
|
|
|
|
isOp = False
|
|
|
|
isArithmeticalOp = False
|
|
|
|
isAssignmentOp = False
|
|
|
|
isComparisonOp = False
|
|
|
|
isLogicalOp = False
|
2014-07-21 16:10:04 +02:00
|
|
|
varId = None
|
|
|
|
variableId = None
|
|
|
|
variable = None
|
|
|
|
functionId = None
|
|
|
|
function = None
|
|
|
|
valuesId = None
|
|
|
|
values = None
|
2015-08-21 10:55:19 +02:00
|
|
|
|
2015-08-28 18:07:12 +02:00
|
|
|
typeScopeId = None
|
|
|
|
typeScope = None
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
astParentId = None
|
|
|
|
astParent = None
|
|
|
|
astOperand1Id = None
|
|
|
|
astOperand1 = None
|
|
|
|
astOperand2Id = None
|
|
|
|
astOperand2 = None
|
2015-06-21 13:26:32 +02:00
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
file = None
|
|
|
|
linenr = None
|
2014-07-16 15:26:59 +02:00
|
|
|
|
|
|
|
def __init__(self, element):
|
2014-07-21 16:10:04 +02:00
|
|
|
self.Id = element.get('id')
|
|
|
|
self.str = element.get('str')
|
|
|
|
self.next = None
|
|
|
|
self.previous = None
|
|
|
|
self.scopeId = element.get('scope')
|
|
|
|
self.scope = None
|
|
|
|
type = element.get('type')
|
2014-07-21 12:44:53 +02:00
|
|
|
if type == 'name':
|
|
|
|
self.isName = True
|
|
|
|
elif type == 'number':
|
|
|
|
self.isNumber = True
|
|
|
|
if element.get('isInt'):
|
|
|
|
self.isInt = True
|
|
|
|
elif element.get('isFloat'):
|
|
|
|
self.isFloat = True
|
|
|
|
elif type == 'string':
|
|
|
|
self.isString = True
|
|
|
|
self.strlen = int(element.get('strlen'))
|
|
|
|
elif type == 'char':
|
|
|
|
self.isChar = True
|
|
|
|
elif type == 'op':
|
|
|
|
self.isOp = True
|
|
|
|
if element.get('isArithmeticalOp'):
|
|
|
|
self.isArithmeticalOp = True
|
|
|
|
elif element.get('isAssignmentOp'):
|
|
|
|
self.isAssignmentOp = True
|
|
|
|
elif element.get('isComparisonOp'):
|
|
|
|
self.isComparisonOp = True
|
|
|
|
elif element.get('isLogicalOp'):
|
|
|
|
self.isLogicalOp = True
|
2014-07-21 16:10:04 +02:00
|
|
|
self.linkId = element.get('link')
|
|
|
|
self.link = None
|
|
|
|
self.varId = element.get('varId')
|
|
|
|
self.variableId = element.get('variable')
|
|
|
|
self.variable = None
|
|
|
|
self.functionId = element.get('function')
|
|
|
|
self.function = None
|
|
|
|
self.valuesId = element.get('values')
|
|
|
|
self.values = None
|
2015-08-28 18:07:12 +02:00
|
|
|
self.typeScopeId = element.get('type-scope')
|
|
|
|
self.typeScope = None
|
2014-07-21 16:10:04 +02:00
|
|
|
self.astParentId = element.get('astParent')
|
|
|
|
self.astParent = None
|
2014-07-16 15:26:59 +02:00
|
|
|
self.astOperand1Id = element.get('astOperand1')
|
2014-07-21 16:10:04 +02:00
|
|
|
self.astOperand1 = None
|
2014-07-16 15:26:59 +02:00
|
|
|
self.astOperand2Id = element.get('astOperand2')
|
2014-07-21 16:10:04 +02:00
|
|
|
self.astOperand2 = None
|
|
|
|
self.file = element.get('file')
|
|
|
|
self.linenr = element.get('linenr')
|
2014-07-16 15:26:59 +02:00
|
|
|
|
|
|
|
def setId(self, IdMap):
|
2014-07-21 16:10:04 +02:00
|
|
|
self.scope = IdMap[self.scopeId]
|
|
|
|
self.link = IdMap[self.linkId]
|
|
|
|
self.variable = IdMap[self.variableId]
|
|
|
|
self.function = IdMap[self.functionId]
|
|
|
|
self.values = IdMap[self.valuesId]
|
2015-08-28 18:07:12 +02:00
|
|
|
self.typeScope = IdMap[self.typeScopeId]
|
2014-07-21 16:10:04 +02:00
|
|
|
self.astParent = IdMap[self.astParentId]
|
2014-07-16 15:26:59 +02:00
|
|
|
self.astOperand1 = IdMap[self.astOperand1Id]
|
|
|
|
self.astOperand2 = IdMap[self.astOperand2Id]
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
def getValue(self, v):
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Get value if it exists
|
|
|
|
Returns None if it doesn't exist
|
|
|
|
"""
|
|
|
|
|
2014-07-18 18:10:52 +02:00
|
|
|
if not self.values:
|
|
|
|
return None
|
|
|
|
for value in self.values:
|
|
|
|
if value.intvalue == v:
|
|
|
|
return value
|
|
|
|
return None
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
class Scope:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Scope. Information about global scope, function scopes, class scopes, inner scopes, etc.
|
|
|
|
C++ class: http://cppcheck.net/devinfo/doxyoutput/classScope.html
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
classStart The { Token for this scope
|
|
|
|
classEnd The } Token for this scope
|
|
|
|
className Name of this scope.
|
|
|
|
For a function scope, this is the function name;
|
|
|
|
For a class scope, this is the class name.
|
|
|
|
type Type of scope: Global, Function, Class, If, While
|
|
|
|
"""
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
Id = None
|
2014-07-16 15:26:59 +02:00
|
|
|
classStartId = None
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
classStart = None
|
|
|
|
classEndId = None
|
|
|
|
classEnd = None
|
|
|
|
className = None
|
|
|
|
type = None
|
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.Id = element.get('id')
|
|
|
|
self.className = element.get('className')
|
2014-07-16 15:26:59 +02:00
|
|
|
self.classStartId = element.get('classStart')
|
2014-07-21 16:10:04 +02:00
|
|
|
self.classStart = None
|
|
|
|
self.classEndId = element.get('classEnd')
|
|
|
|
self.classEnd = None
|
2016-09-07 17:38:27 +02:00
|
|
|
self.nestedInId = element.get('nestedIn')
|
2014-07-21 16:10:04 +02:00
|
|
|
self.nestedIn = None
|
|
|
|
self.type = element.get('type')
|
2014-07-16 15:26:59 +02:00
|
|
|
|
|
|
|
def setId(self, IdMap):
|
|
|
|
self.classStart = IdMap[self.classStartId]
|
|
|
|
self.classEnd = IdMap[self.classEndId]
|
|
|
|
self.nestedIn = IdMap[self.nestedInId]
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
class Function:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Information about a function
|
|
|
|
C++ class:
|
|
|
|
http://cppcheck.net/devinfo/doxyoutput/classFunction.html
|
|
|
|
"""
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
Id = None
|
|
|
|
argument = None
|
2014-07-16 15:26:59 +02:00
|
|
|
argumentId = None
|
2015-07-28 14:34:37 +02:00
|
|
|
tokenDef = None
|
|
|
|
tokenDefId = None
|
2015-07-28 14:18:58 +02:00
|
|
|
name = None
|
2014-07-21 16:10:04 +02:00
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.Id = element.get('id')
|
2015-07-28 14:34:37 +02:00
|
|
|
self.tokenDefId = element.get('tokenDef')
|
2015-07-28 14:18:58 +02:00
|
|
|
self.name = element.get('name')
|
2014-07-21 16:10:04 +02:00
|
|
|
self.argument = {}
|
2014-07-16 15:26:59 +02:00
|
|
|
self.argumentId = {}
|
|
|
|
for arg in element:
|
|
|
|
self.argumentId[arg.get('nr')] = arg.get('id')
|
2014-07-21 16:10:04 +02:00
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
def setId(self, IdMap):
|
2014-12-22 16:03:43 +01:00
|
|
|
for argnr, argid in self.argumentId.items():
|
2014-07-16 15:26:59 +02:00
|
|
|
self.argument[argnr] = IdMap[argid]
|
2015-07-28 14:34:37 +02:00
|
|
|
self.tokenDef = IdMap[self.tokenDefId]
|
2014-07-21 16:10:04 +02:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
class Variable:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Information about a variable
|
|
|
|
C++ class:
|
|
|
|
http://cppcheck.net/devinfo/doxyoutput/classVariable.html
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
nameToken name token in variable declaration
|
|
|
|
typeStartToken start token of variable declaration
|
|
|
|
typeEndToken end token of variable declaration
|
|
|
|
isArgument Is this variable a function argument?
|
|
|
|
isArray Is this variable an array?
|
|
|
|
isClass Is this variable a class or struct?
|
|
|
|
isLocal Is this variable a local variable?
|
|
|
|
isPointer Is this variable a pointer
|
|
|
|
isReference Is this variable a reference
|
|
|
|
isStatic Is this variable static?
|
|
|
|
"""
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
Id = None
|
|
|
|
nameTokenId = None
|
|
|
|
nameToken = None
|
2014-07-16 15:26:59 +02:00
|
|
|
typeStartTokenId = None
|
2014-07-21 16:10:04 +02:00
|
|
|
typeStartToken = None
|
|
|
|
typeEndTokenId = None
|
|
|
|
typeEndToken = None
|
2015-06-21 09:54:07 +02:00
|
|
|
isArgument = False
|
|
|
|
isArray = False
|
|
|
|
isClass = False
|
|
|
|
isLocal = False
|
|
|
|
isPointer = False
|
|
|
|
isReference = False
|
|
|
|
isStatic = False
|
2014-07-18 17:29:39 +02:00
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
def __init__(self, element):
|
2014-07-21 16:10:04 +02:00
|
|
|
self.Id = element.get('id')
|
|
|
|
self.nameTokenId = element.get('nameToken')
|
|
|
|
self.nameToken = None
|
2014-07-16 15:26:59 +02:00
|
|
|
self.typeStartTokenId = element.get('typeStartToken')
|
2014-07-21 16:10:04 +02:00
|
|
|
self.typeStartToken = None
|
|
|
|
self.typeEndTokenId = element.get('typeEndToken')
|
|
|
|
self.typeEndToken = None
|
2015-06-21 09:54:07 +02:00
|
|
|
self.isArgument = element.get('isArgument') == 'true'
|
|
|
|
self.isArray = element.get('isArray') == 'true'
|
|
|
|
self.isClass = element.get('isClass') == 'true'
|
|
|
|
self.isLocal = element.get('isLocal') == 'true'
|
|
|
|
self.isPointer = element.get('isPointer') == 'true'
|
|
|
|
self.isReference = element.get('isReference') == 'true'
|
|
|
|
self.isStatic = element.get('isStatic') == 'true'
|
2014-07-16 15:26:59 +02:00
|
|
|
|
|
|
|
def setId(self, IdMap):
|
|
|
|
self.nameToken = IdMap[self.nameTokenId]
|
|
|
|
self.typeStartToken = IdMap[self.typeStartTokenId]
|
|
|
|
self.typeEndToken = IdMap[self.typeEndTokenId]
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
class ValueFlow:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
ValueFlow::Value class
|
|
|
|
Each possible value has a ValueFlow::Value item.
|
|
|
|
Each ValueFlow::Value either has a intvalue or tokvalue
|
|
|
|
C++ class:
|
|
|
|
http://cppcheck.net/devinfo/doxyoutput/classValueFlow_1_1Value.html
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
values Possible values
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
Id = None
|
|
|
|
values = None
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
class Value:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Value class
|
2016-07-25 18:03:44 +02:00
|
|
|
|
2016-07-25 13:47:26 +02:00
|
|
|
Attributes:
|
|
|
|
intvalue integer value
|
|
|
|
tokvalue token value
|
|
|
|
condition condition where this Value comes from
|
|
|
|
"""
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
intvalue = None
|
2015-06-20 21:58:23 +02:00
|
|
|
tokvalue = None
|
2014-07-16 15:26:59 +02:00
|
|
|
condition = None
|
2014-07-21 16:10:04 +02:00
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
def __init__(self, element):
|
2015-06-20 21:58:23 +02:00
|
|
|
self.intvalue = element.get('intvalue')
|
|
|
|
if self.intvalue:
|
|
|
|
self.intvalue = int(self.intvalue)
|
|
|
|
self.tokvalue = element.get('tokvalue')
|
2014-07-18 17:29:39 +02:00
|
|
|
self.condition = element.get('condition-line')
|
|
|
|
if self.condition:
|
|
|
|
self.condition = int(self.condition)
|
2014-07-16 15:26:59 +02:00
|
|
|
|
|
|
|
def __init__(self, element):
|
2014-07-21 16:10:04 +02:00
|
|
|
self.Id = element.get('id')
|
2014-07-16 15:26:59 +02:00
|
|
|
self.values = []
|
|
|
|
for value in element:
|
|
|
|
self.values.append(ValueFlow.Value(value))
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Configuration:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Configuration class
|
|
|
|
This class contains the directives, tokens, scopes, functions,
|
|
|
|
variables and value flows for one configuration.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name Name of the configuration, "" for default
|
|
|
|
directives List of Directive items
|
|
|
|
tokenlist List of Token items
|
|
|
|
scopes List of Scope items
|
|
|
|
functions List of Function items
|
|
|
|
variables List of Variable items
|
2016-07-25 18:03:44 +02:00
|
|
|
valueflow List of ValueFlow values
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
name = ''
|
2015-12-07 19:54:41 +01:00
|
|
|
directives = []
|
2014-07-16 15:26:59 +02:00
|
|
|
tokenlist = []
|
2014-07-21 16:10:04 +02:00
|
|
|
scopes = []
|
2014-07-16 15:26:59 +02:00
|
|
|
functions = []
|
|
|
|
variables = []
|
2014-07-18 17:29:39 +02:00
|
|
|
valueflow = []
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
def __init__(self, confignode):
|
|
|
|
self.name = confignode.get('cfg')
|
2015-12-07 19:54:41 +01:00
|
|
|
self.directives = []
|
2014-07-16 15:26:59 +02:00
|
|
|
self.tokenlist = []
|
2014-07-21 16:10:04 +02:00
|
|
|
self.scopes = []
|
2015-08-19 12:16:13 +02:00
|
|
|
self.functions = []
|
2014-07-16 15:26:59 +02:00
|
|
|
self.variables = []
|
|
|
|
self.valueflow = []
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
for element in confignode:
|
2015-12-07 19:54:41 +01:00
|
|
|
if element.tag == 'directivelist':
|
|
|
|
for directive in element:
|
|
|
|
self.directives.append(Directive(directive))
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
if element.tag == 'tokenlist':
|
|
|
|
for token in element:
|
|
|
|
self.tokenlist.append(Token(token))
|
|
|
|
|
|
|
|
# set next/previous..
|
|
|
|
prev = None
|
|
|
|
for token in self.tokenlist:
|
|
|
|
token.previous = prev
|
|
|
|
if prev:
|
|
|
|
prev.next = token
|
2014-07-17 07:39:19 +02:00
|
|
|
prev = token
|
2014-07-16 15:26:59 +02:00
|
|
|
if element.tag == 'scopes':
|
|
|
|
for scope in element:
|
|
|
|
self.scopes.append(Scope(scope))
|
|
|
|
for functionList in scope:
|
|
|
|
if functionList.tag == 'functionList':
|
|
|
|
for function in functionList:
|
|
|
|
self.functions.append(Function(function))
|
|
|
|
if element.tag == 'variables':
|
|
|
|
for variable in element:
|
|
|
|
self.variables.append(Variable(variable))
|
|
|
|
if element.tag == 'valueflow':
|
|
|
|
for values in element:
|
|
|
|
self.valueflow.append(ValueFlow(values))
|
|
|
|
|
|
|
|
IdMap = {}
|
|
|
|
IdMap[None] = None
|
2014-07-21 16:10:04 +02:00
|
|
|
IdMap['0'] = None
|
2014-07-16 15:26:59 +02:00
|
|
|
for token in self.tokenlist:
|
|
|
|
IdMap[token.Id] = token
|
|
|
|
for scope in self.scopes:
|
|
|
|
IdMap[scope.Id] = scope
|
|
|
|
for function in self.functions:
|
|
|
|
IdMap[function.Id] = function
|
|
|
|
for variable in self.variables:
|
|
|
|
IdMap[variable.Id] = variable
|
|
|
|
for values in self.valueflow:
|
2014-07-18 17:29:39 +02:00
|
|
|
IdMap[values.Id] = values.values
|
2014-07-16 15:26:59 +02:00
|
|
|
|
|
|
|
for token in self.tokenlist:
|
|
|
|
token.setId(IdMap)
|
|
|
|
for scope in self.scopes:
|
|
|
|
scope.setId(IdMap)
|
|
|
|
for function in self.functions:
|
|
|
|
function.setId(IdMap)
|
|
|
|
for variable in self.variables:
|
|
|
|
variable.setId(IdMap)
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CppcheckData:
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Class that makes cppcheck dump data available
|
|
|
|
Contains a list of Configuration instances
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
configurations List of Configurations
|
|
|
|
|
|
|
|
To iterate through all configurations use such code:
|
|
|
|
@code
|
|
|
|
data = cppcheckdata.parsedump(...)
|
|
|
|
for cfg in data.configurations:
|
|
|
|
print('cfg: ' + cfg.name)
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
To iterate through all tokens in each configuration use such code:
|
|
|
|
@code
|
|
|
|
data = cppcheckdata.parsedump(...)
|
|
|
|
for cfg in data.configurations:
|
|
|
|
print('cfg: ' + cfg.name)
|
|
|
|
code = ''
|
|
|
|
for token in cfg.tokenlist:
|
|
|
|
code = code + token.str + ' '
|
|
|
|
print(' ' + code)
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
To iterate through all scopes (functions, types, etc) use such code:
|
|
|
|
@code
|
|
|
|
data = cppcheckdata.parsedump(...)
|
|
|
|
for cfg in data.configurations:
|
|
|
|
print('cfg: ' + cfg.name)
|
|
|
|
for scope in cfg.scopes:
|
|
|
|
print(' type:' + scope.type + ' name:' + scope.className)
|
|
|
|
@endcode
|
|
|
|
"""
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
configurations = []
|
|
|
|
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.configurations = []
|
|
|
|
|
|
|
|
data = ET.parse(filename)
|
|
|
|
# root is 'dumps' node, each config has its own 'dump' subnode.
|
|
|
|
for cfgnode in data.getroot():
|
|
|
|
self.configurations.append(Configuration(cfgnode))
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
def parsedump(filename):
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
parse a cppcheck dump file
|
|
|
|
"""
|
2014-07-16 15:26:59 +02:00
|
|
|
return CppcheckData(filename)
|
2014-07-20 15:24:40 +02:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
|
2014-07-20 15:24:40 +02:00
|
|
|
def astIsFloat(token):
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Check if type of ast node is float/double
|
|
|
|
"""
|
|
|
|
|
2014-07-20 15:24:40 +02:00
|
|
|
if not token:
|
|
|
|
return False
|
|
|
|
if token.str == '.':
|
|
|
|
return astIsFloat(token.astOperand2)
|
|
|
|
if '+-*/%'.find(token.str) == 0:
|
|
|
|
if True == astIsFloat(token.astOperand1):
|
|
|
|
return True
|
|
|
|
return astIsFloat(token.astOperand2)
|
|
|
|
if not token.variable:
|
|
|
|
# float literal?
|
|
|
|
if token.str[0].isdigit():
|
|
|
|
for c in token.str:
|
2014-07-21 16:10:04 +02:00
|
|
|
if c == 'f' or c == '.' or c == 'E':
|
2014-07-20 15:24:40 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
typeToken = token.variable.typeStartToken
|
|
|
|
endToken = token.variable.typeEndToken
|
|
|
|
while typeToken != endToken:
|
|
|
|
if typeToken.str == 'float' or typeToken.str == 'double':
|
|
|
|
return True
|
|
|
|
typeToken = typeToken.next
|
|
|
|
if typeToken.str == 'float' or typeToken.str == 'double':
|
|
|
|
return True
|
|
|
|
return False
|
2015-12-07 19:54:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CppCheckFormatter(argparse.HelpFormatter):
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
Properly formats multiline argument helps
|
|
|
|
"""
|
2015-12-07 19:54:41 +01:00
|
|
|
def _split_lines(self, text, width):
|
|
|
|
# this is the RawTextHelpFormatter._split_lines
|
|
|
|
if text.startswith('R|'):
|
2016-07-23 13:44:05 +02:00
|
|
|
return text[2:].splitlines()
|
2015-12-07 19:54:41 +01:00
|
|
|
return argparse.HelpFormatter._split_lines(self, text, width)
|
|
|
|
|
|
|
|
def ArgumentParser():
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
2015-12-07 19:54:41 +01:00
|
|
|
Returns an argparse argument parser with an already-added
|
|
|
|
argument definition for -t/--template
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
2015-12-07 19:54:41 +01:00
|
|
|
parser = argparse.ArgumentParser(formatter_class=CppCheckFormatter)
|
|
|
|
parser.add_argument('-t', '--template', metavar='<text>',
|
2016-07-23 13:44:05 +02:00
|
|
|
default='{callstack}: ({severity}) {message}',
|
|
|
|
help="R|Format the error messages. E.g.\n"
|
|
|
|
"'{file}:{line},{severity},{id},{message}' or\n"
|
|
|
|
"'{file}({line}):({severity}) {message}' or\n"
|
|
|
|
"'{callstack} {message}'\n"
|
|
|
|
"Pre-defined templates: gcc, vs, edit")
|
2015-12-07 19:54:41 +01:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def reportError(template, callstack=[], severity='', message='', id=''):
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
2015-12-07 19:54:41 +01:00
|
|
|
Format an error message according to the template.
|
2016-07-23 13:44:05 +02:00
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
:param template: format string, or 'gcc', 'vs' or 'edit'.
|
|
|
|
:param callstack: e.g. [['file1.cpp',10],['file2.h','20'], ... ]
|
|
|
|
:param severity: e.g. 'error', 'warning' ...
|
|
|
|
:param id: message ID.
|
|
|
|
:param message: message text.
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
2015-12-07 19:54:41 +01:00
|
|
|
# expand predefined templates
|
|
|
|
if template == 'gcc':
|
|
|
|
template = '{file}:{line}: {severity}: {message}'
|
|
|
|
elif template == 'vs':
|
|
|
|
template = '{file}({line}): {severity}: {message}'
|
|
|
|
elif template == 'edit':
|
|
|
|
template = '{file} +{line}: {severity}: {message}'
|
|
|
|
# compute 'callstack}, {file} and {line} replacements
|
2016-07-23 13:44:05 +02:00
|
|
|
stack = ' -> '.join(['[' + f + ':' + str(l) + ']' for (f, l) in callstack])
|
2015-12-07 19:54:41 +01:00
|
|
|
file = callstack[-1][0]
|
|
|
|
line = str(callstack[-1][1])
|
|
|
|
# format message
|
|
|
|
return template.format(callstack=stack, file=file, line=line,
|
2016-07-23 13:44:05 +02:00
|
|
|
severity=severity, message=message, id=id)
|