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-12-07 19:54:41 +01:00
|
|
|
import argparse
|
2019-06-24 19:28:52 +02:00
|
|
|
import json
|
2019-04-09 20:36:24 +02:00
|
|
|
import sys
|
2015-12-07 19:54:41 +01:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
from fnmatch import fnmatch
|
2015-12-07 19:54:41 +01:00
|
|
|
|
2020-08-29 07:44:13 +02:00
|
|
|
EXIT_CODE = 0
|
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
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
|
2019-08-18 12:19:05 +02:00
|
|
|
column = 0
|
2015-12-07 19:54:41 +01:00
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.str = element.get('str')
|
|
|
|
self.file = element.get('file')
|
2019-04-13 10:22:13 +02:00
|
|
|
self.linenr = int(element.get('linenr'))
|
2015-12-07 19:54:41 +01:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["str", "file", "linenr"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"Directive",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
2017-06-04 22:51:48 +02:00
|
|
|
|
2017-04-16 09:11:20 +02:00
|
|
|
class ValueType:
|
|
|
|
"""
|
|
|
|
ValueType class. Contains (promoted) type information for each node in the AST.
|
|
|
|
"""
|
|
|
|
|
|
|
|
type = None
|
|
|
|
sign = None
|
2018-05-02 20:56:03 +02:00
|
|
|
bits = 0
|
2017-04-16 09:11:20 +02:00
|
|
|
constness = 0
|
|
|
|
pointer = 0
|
|
|
|
typeScopeId = None
|
|
|
|
typeScope = None
|
|
|
|
originalTypeName = None
|
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.type = element.get('valueType-type')
|
|
|
|
self.sign = element.get('valueType-sign')
|
2018-05-02 20:56:03 +02:00
|
|
|
bits = element.get('valueType-bits')
|
|
|
|
if bits:
|
|
|
|
self.bits = int(bits)
|
2017-04-16 09:11:20 +02:00
|
|
|
self.typeScopeId = element.get('valueType-typeScope')
|
|
|
|
self.originalTypeName = element.get('valueType-originalTypeName')
|
|
|
|
constness = element.get('valueType-constness')
|
|
|
|
if constness:
|
|
|
|
self.constness = int(constness)
|
|
|
|
else:
|
|
|
|
self.constness = 0
|
|
|
|
pointer = element.get('valueType-pointer')
|
|
|
|
if pointer:
|
|
|
|
self.pointer = int(pointer)
|
|
|
|
else:
|
|
|
|
self.pointer = 0
|
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["type", "sign", "bits", "typeScopeId", "originalTypeName",
|
|
|
|
"constness", "constness", "pointer"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"ValueType",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-04-16 09:11:20 +02:00
|
|
|
def setId(self, IdMap):
|
|
|
|
self.typeScope = IdMap[self.typeScopeId]
|
|
|
|
|
2017-04-16 12:13:30 +02:00
|
|
|
def isIntegral(self):
|
2017-06-04 22:51:48 +02:00
|
|
|
return self.type in {'bool', 'char', 'short', 'int', 'long', 'long long'}
|
2017-04-16 12:13:30 +02:00
|
|
|
|
|
|
|
def isFloat(self):
|
2017-06-04 22:51:48 +02:00
|
|
|
return self.type in {'float', 'double', 'long double'}
|
|
|
|
|
2018-05-22 09:07:48 +02:00
|
|
|
def isEnum(self):
|
|
|
|
return self.typeScope and self.typeScope.type == "Enum"
|
|
|
|
|
|
|
|
|
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-12-09 22:31:47 +01:00
|
|
|
isUnsigned Is this token a unsigned type
|
|
|
|
isSigned Is this token a signed type
|
2019-08-15 14:24:39 +02:00
|
|
|
isExpandedMacro Is this token a expanded macro token
|
2020-09-06 11:02:22 +02:00
|
|
|
isSplittedVarDecl Is this token a splitted variable declaration. "int a,b; => int a; int b;"
|
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
|
2017-04-15 21:55:07 +02:00
|
|
|
valueType type information
|
2016-07-25 13:47:26 +02:00
|
|
|
typeScope type scope (token->type()->classScope)
|
|
|
|
astParent ast parent
|
|
|
|
astOperand1 ast operand1
|
|
|
|
astOperand2 ast operand2
|
|
|
|
file file name
|
|
|
|
linenr line number
|
2019-08-18 12:19:05 +02:00
|
|
|
column column
|
2016-07-25 13:47:26 +02:00
|
|
|
|
|
|
|
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
|
2016-12-09 22:31:47 +01:00
|
|
|
isUnsigned = False
|
|
|
|
isSigned = False
|
2019-08-15 14:24:39 +02:00
|
|
|
isExpandedMacro = False
|
2020-09-06 11:02:22 +02:00
|
|
|
isSplittedVarDecl = False
|
2014-07-21 16:10:04 +02:00
|
|
|
varId = None
|
|
|
|
variableId = None
|
|
|
|
variable = None
|
|
|
|
functionId = None
|
|
|
|
function = None
|
|
|
|
valuesId = None
|
|
|
|
values = None
|
2017-04-15 21:55:07 +02:00
|
|
|
valueType = 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
|
2019-08-18 12:19:05 +02:00
|
|
|
column = 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
|
2016-12-09 22:31:47 +01:00
|
|
|
if element.get('isUnsigned'):
|
|
|
|
self.isUnsigned = True
|
|
|
|
if element.get('isSigned'):
|
|
|
|
self.isSigned = True
|
2014-07-21 12:44:53 +02:00
|
|
|
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
|
2019-08-15 14:24:39 +02:00
|
|
|
if element.get('isExpandedMacro'):
|
|
|
|
self.isExpandedMacro = True
|
2020-09-06 11:02:22 +02:00
|
|
|
if element.get('isSplittedVarDecl'):
|
2020-09-06 11:33:37 +02:00
|
|
|
self.isSplittedVarDecl = True
|
2014-07-21 16:10:04 +02:00
|
|
|
self.linkId = element.get('link')
|
|
|
|
self.link = None
|
2019-04-13 10:22:13 +02:00
|
|
|
if element.get('varId'):
|
|
|
|
self.varId = int(element.get('varId'))
|
2014-07-21 16:10:04 +02:00
|
|
|
self.variableId = element.get('variable')
|
|
|
|
self.variable = None
|
|
|
|
self.functionId = element.get('function')
|
|
|
|
self.function = None
|
|
|
|
self.valuesId = element.get('values')
|
|
|
|
self.values = None
|
2017-04-16 09:11:20 +02:00
|
|
|
if element.get('valueType-type'):
|
|
|
|
self.valueType = ValueType(element)
|
|
|
|
else:
|
|
|
|
self.valueType = 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')
|
2019-04-13 10:22:13 +02:00
|
|
|
self.linenr = int(element.get('linenr'))
|
2019-08-18 12:19:05 +02:00
|
|
|
self.column = int(element.get('column'))
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
2019-12-15 19:46:54 +01:00
|
|
|
attrs = ["Id", "str", "scopeId", "isName", "isUnsigned", "isSigned",
|
|
|
|
"isNumber", "isInt", "isFloat", "isString", "strlen",
|
|
|
|
"isChar", "isOp", "isArithmeticalOp", "isComparisonOp",
|
2020-09-06 11:02:22 +02:00
|
|
|
"isLogicalOp", "isExpandedMacro", "isSplittedVarDecl",
|
|
|
|
"linkId", "varId", "variableId", "functionId", "valuesId",
|
|
|
|
"valueType", "typeScopeId", "astParentId", "astOperand1Id",
|
|
|
|
"file", "linenr", "column"]
|
2019-12-09 19:08:45 +01:00
|
|
|
return "{}({})".format(
|
|
|
|
"Token",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
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]
|
2017-04-16 09:11:20 +02:00
|
|
|
if self.valueType:
|
|
|
|
self.valueType.setId(IdMap)
|
2014-07-16 15:26:59 +02:00
|
|
|
|
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
|
2018-04-28 23:06:54 +02:00
|
|
|
bodyStart The { Token for this scope
|
|
|
|
bodyEnd The } Token for this scope
|
2016-07-25 13:47:26 +02:00
|
|
|
className Name of this scope.
|
|
|
|
For a function scope, this is the function name;
|
|
|
|
For a class scope, this is the class name.
|
2019-07-13 15:09:50 +02:00
|
|
|
function If this scope belongs at a function call, this attribute
|
|
|
|
has the Function information. See the Function class.
|
2016-07-25 13:47:26 +02:00
|
|
|
type Type of scope: Global, Function, Class, If, While
|
|
|
|
"""
|
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
Id = None
|
2018-04-28 23:06:54 +02:00
|
|
|
bodyStartId = None
|
|
|
|
bodyStart = None
|
|
|
|
bodyEndId = None
|
|
|
|
bodyEnd = None
|
2014-07-21 16:10:04 +02:00
|
|
|
className = None
|
2019-07-13 15:09:50 +02:00
|
|
|
functionId = None
|
|
|
|
function = None
|
2017-04-08 19:33:26 +02:00
|
|
|
nestedInId = None
|
|
|
|
nestedIn = None
|
2014-07-21 16:10:04 +02:00
|
|
|
type = None
|
2019-05-22 21:39:33 +02:00
|
|
|
isExecutable = None
|
2014-07-21 16:10:04 +02:00
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.Id = element.get('id')
|
|
|
|
self.className = element.get('className')
|
2019-07-13 15:09:50 +02:00
|
|
|
self.functionId = element.get('function')
|
|
|
|
self.function = None
|
2018-04-28 23:06:54 +02:00
|
|
|
self.bodyStartId = element.get('bodyStart')
|
|
|
|
self.bodyStart = None
|
|
|
|
self.bodyEndId = element.get('bodyEnd')
|
|
|
|
self.bodyEnd = 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')
|
2019-11-12 09:30:43 +01:00
|
|
|
self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do',
|
|
|
|
'Switch', 'Try', 'Catch', 'Unconditional', 'Lambda'))
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
2019-12-15 19:46:54 +01:00
|
|
|
attrs = ["Id", "className", "functionId", "bodyStartId", "bodyEndId",
|
2019-12-09 19:08:45 +01:00
|
|
|
"nestedInId", "nestedIn", "type", "isExecutable"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"Scope",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
2014-07-16 15:26:59 +02:00
|
|
|
def setId(self, IdMap):
|
2018-04-28 23:06:54 +02:00
|
|
|
self.bodyStart = IdMap[self.bodyStartId]
|
|
|
|
self.bodyEnd = IdMap[self.bodyEndId]
|
2014-07-16 15:26:59 +02:00
|
|
|
self.nestedIn = IdMap[self.nestedInId]
|
2019-07-13 15:09:50 +02:00
|
|
|
self.function = IdMap[self.functionId]
|
2014-07-16 15:26:59 +02:00
|
|
|
|
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
|
2019-07-13 15:09:50 +02:00
|
|
|
|
|
|
|
Attributes
|
|
|
|
argument Argument list
|
|
|
|
tokenDef Token in function definition
|
|
|
|
isVirtual Is this function is virtual
|
|
|
|
isImplicitlyVirtual Is this function is virtual this in the base classes
|
2019-10-26 08:32:46 +02:00
|
|
|
isStatic Is this function is static
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
|
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
|
2018-04-30 16:52:51 +02:00
|
|
|
type = None
|
2018-04-13 21:15:43 +02:00
|
|
|
isVirtual = None
|
|
|
|
isImplicitlyVirtual = None
|
2019-10-26 08:32:46 +02:00
|
|
|
isStatic = None
|
2020-04-16 12:14:29 +02:00
|
|
|
nestedIn = None
|
2014-07-21 16:10:04 +02:00
|
|
|
|
2020-04-16 12:14:29 +02:00
|
|
|
def __init__(self, element, nestedIn):
|
2014-07-21 16:10:04 +02:00
|
|
|
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')
|
2018-04-30 16:52:51 +02:00
|
|
|
self.type = element.get('type')
|
2018-04-13 21:15:43 +02:00
|
|
|
isVirtual = element.get('isVirtual')
|
|
|
|
self.isVirtual = (isVirtual and isVirtual == 'true')
|
|
|
|
isImplicitlyVirtual = element.get('isImplicitlyVirtual')
|
|
|
|
self.isImplicitlyVirtual = (isImplicitlyVirtual and isImplicitlyVirtual == 'true')
|
2019-10-26 08:32:46 +02:00
|
|
|
isStatic = element.get('isStatic')
|
|
|
|
self.isStatic = (isStatic and isStatic == 'true')
|
2020-04-16 12:14:29 +02:00
|
|
|
self.nestedIn = nestedIn
|
2018-04-13 21:15:43 +02:00
|
|
|
|
2014-07-21 16:10:04 +02:00
|
|
|
self.argument = {}
|
2014-07-16 15:26:59 +02:00
|
|
|
self.argumentId = {}
|
2014-07-21 16:10:04 +02:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["Id", "tokenDefId", "name", "type", "isVirtual",
|
2019-12-15 19:46:54 +01:00
|
|
|
"isImplicitlyVirtual", "isStatic", "argumentId"]
|
2019-12-09 19:08:45 +01:00
|
|
|
return "{}({})".format(
|
|
|
|
"Function",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
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:
|
2018-06-19 08:50:32 +02:00
|
|
|
nameToken Name token in variable declaration
|
|
|
|
typeStartToken Start token of variable declaration
|
|
|
|
typeEndToken End token of variable declaration
|
2018-06-17 14:46:59 +02:00
|
|
|
access Global/Local/Namespace/Public/Protected/Public/Throw/Argument
|
2018-06-19 08:50:32 +02:00
|
|
|
scope Variable scope
|
2016-07-25 13:47:26 +02:00
|
|
|
isArgument Is this variable a function argument?
|
|
|
|
isArray Is this variable an array?
|
|
|
|
isClass Is this variable a class or struct?
|
2018-03-14 11:55:01 +01:00
|
|
|
isConst Is this variable a const variable?
|
2019-05-26 18:46:00 +02:00
|
|
|
isGlobal Is this variable a global variable?
|
2017-04-17 07:45:27 +02:00
|
|
|
isExtern Is this variable an extern variable?
|
2016-07-25 13:47:26 +02:00
|
|
|
isLocal Is this variable a local variable?
|
|
|
|
isPointer Is this variable a pointer
|
|
|
|
isReference Is this variable a reference
|
|
|
|
isStatic Is this variable static?
|
2018-04-12 20:23:50 +02:00
|
|
|
constness Variable constness (same encoding as ValueType::constness)
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
|
|
|
|
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
|
2018-06-17 14:46:59 +02:00
|
|
|
access = None
|
2018-06-19 08:50:32 +02:00
|
|
|
scopeId = None
|
|
|
|
scope = None
|
2015-06-21 09:54:07 +02:00
|
|
|
isArgument = False
|
|
|
|
isArray = False
|
|
|
|
isClass = False
|
2018-03-14 11:55:01 +01:00
|
|
|
isConst = False
|
2017-04-17 07:45:27 +02:00
|
|
|
isExtern = False
|
2019-05-26 18:46:00 +02:00
|
|
|
isGlobal = False
|
2015-06-21 09:54:07 +02:00
|
|
|
isLocal = False
|
|
|
|
isPointer = False
|
|
|
|
isReference = False
|
|
|
|
isStatic = False
|
2018-04-12 20:23:50 +02:00
|
|
|
constness = 0
|
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
|
2018-06-17 14:46:59 +02:00
|
|
|
self.access = element.get('access')
|
2018-06-19 08:50:32 +02:00
|
|
|
self.scopeId = element.get('scope')
|
|
|
|
self.scope = 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'
|
2018-03-14 11:55:01 +01:00
|
|
|
self.isConst = element.get('isConst') == 'true'
|
2019-05-26 18:46:00 +02:00
|
|
|
self.isGlobal = element.get('access') == 'Global'
|
2017-04-17 07:45:27 +02:00
|
|
|
self.isExtern = element.get('isExtern') == 'true'
|
2015-06-21 09:54:07 +02:00
|
|
|
self.isLocal = element.get('isLocal') == 'true'
|
|
|
|
self.isPointer = element.get('isPointer') == 'true'
|
|
|
|
self.isReference = element.get('isReference') == 'true'
|
|
|
|
self.isStatic = element.get('isStatic') == 'true'
|
2018-04-12 20:23:50 +02:00
|
|
|
self.constness = element.get('constness')
|
|
|
|
if self.constness:
|
|
|
|
self.constness = int(self.constness)
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
2019-12-15 19:46:54 +01:00
|
|
|
attrs = ["Id", "nameTokenId", "typeStartTokenId", "typeEndTokenId",
|
|
|
|
"access", "scopeId", "isArgument", "isArray", "isClass",
|
|
|
|
"isConst", "isGlobal", "isExtern", "isLocal", "isPointer",
|
|
|
|
"isReference", "isStatic", "constness", ]
|
2019-12-09 19:08:45 +01:00
|
|
|
return "{}({})".format(
|
|
|
|
"Variable",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
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]
|
2018-06-19 08:50:32 +02:00
|
|
|
self.scope = IdMap[self.scopeId]
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
class Value:
|
|
|
|
"""
|
|
|
|
Value class
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
intvalue integer value
|
|
|
|
tokvalue token value
|
|
|
|
floatvalue float value
|
|
|
|
containerSize container size
|
|
|
|
condition condition where this Value comes from
|
|
|
|
valueKind 'known' or 'possible'
|
|
|
|
inconclusive Is value inconclusive?
|
|
|
|
"""
|
|
|
|
|
|
|
|
intvalue = None
|
|
|
|
tokvalue = None
|
|
|
|
floatvalue = None
|
|
|
|
containerSize = None
|
|
|
|
condition = None
|
|
|
|
valueKind = None
|
|
|
|
inconclusive = False
|
|
|
|
|
|
|
|
def isKnown(self):
|
|
|
|
return self.valueKind and self.valueKind == 'known'
|
|
|
|
|
|
|
|
def isPossible(self):
|
|
|
|
return self.valueKind and self.valueKind == 'possible'
|
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.intvalue = element.get('intvalue')
|
|
|
|
if self.intvalue:
|
|
|
|
self.intvalue = int(self.intvalue)
|
|
|
|
self.tokvalue = element.get('tokvalue')
|
|
|
|
self.floatvalue = element.get('floatvalue')
|
|
|
|
self.containerSize = element.get('container-size')
|
|
|
|
self.condition = element.get('condition-line')
|
|
|
|
if self.condition:
|
|
|
|
self.condition = int(self.condition)
|
|
|
|
if element.get('known'):
|
|
|
|
self.valueKind = 'known'
|
|
|
|
elif element.get('possible'):
|
|
|
|
self.valueKind = 'possible'
|
|
|
|
if element.get('inconclusive'):
|
|
|
|
self.inconclusive = True
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["intvalue", "tokvalue", "floatvalue", "containerSize",
|
|
|
|
"condition", "valueKind", "inconclusive"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"Value",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
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 = []
|
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["Id", "values"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"ValueFlow",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
2019-11-12 09:30:43 +01:00
|
|
|
|
2018-04-24 22:19:24 +02:00
|
|
|
class Suppression:
|
|
|
|
"""
|
|
|
|
Suppression class
|
|
|
|
This class contains a suppression entry to suppress a warning.
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
errorId The id string of the error to suppress, can be a wildcard
|
|
|
|
fileName The name of the file to suppress warnings for, can include wildcards
|
|
|
|
lineNumber The number of the line to suppress warnings from, can be 0 to represent any line
|
|
|
|
symbolName The name of the symbol to match warnings for, can include wildcards
|
|
|
|
"""
|
|
|
|
|
|
|
|
errorId = None
|
|
|
|
fileName = None
|
|
|
|
lineNumber = None
|
|
|
|
symbolName = None
|
|
|
|
|
|
|
|
def __init__(self, element):
|
|
|
|
self.errorId = element.get('errorId')
|
|
|
|
self.fileName = element.get('fileName')
|
|
|
|
self.lineNumber = element.get('lineNumber')
|
|
|
|
self.symbolName = element.get('symbolName')
|
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ['errorId' , "fileName", "lineNumber", "symbolName"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"Suppression",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
2018-06-04 21:50:21 +02:00
|
|
|
def isMatch(self, file, line, message, errorId):
|
2018-06-13 22:32:27 +02:00
|
|
|
if ((self.fileName is None or fnmatch(file, self.fileName))
|
2019-11-12 09:30:43 +01:00
|
|
|
and (self.lineNumber is None or line == self.lineNumber)
|
|
|
|
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
|
|
|
|
and fnmatch(errorId, self.errorId)):
|
2018-06-04 21:50:21 +02:00
|
|
|
return True
|
2018-04-24 22:19:24 +02:00
|
|
|
else:
|
2018-06-04 21:50:21 +02:00
|
|
|
return False
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2019-11-12 09:30:43 +01:00
|
|
|
|
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,
|
2018-04-24 22:19:24 +02:00
|
|
|
variables, value flows, and suppressions for one configuration.
|
2016-07-25 13:47:26 +02:00
|
|
|
|
|
|
|
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
|
2019-12-27 08:50:56 +01:00
|
|
|
standards List of Standards 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 = []
|
2020-01-31 23:38:42 +01:00
|
|
|
standards = None
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
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 = []
|
2020-01-31 23:38:42 +01:00
|
|
|
self.standards = Standards()
|
2019-12-27 08:50:56 +01:00
|
|
|
|
|
|
|
def set_tokens_links(self):
|
|
|
|
"""Set next/previous links between tokens."""
|
|
|
|
prev = None
|
|
|
|
for token in self.tokenlist:
|
|
|
|
token.previous = prev
|
|
|
|
if prev:
|
|
|
|
prev.next = token
|
|
|
|
prev = token
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
def set_id_map(self, arguments):
|
2017-10-07 22:44:35 +02:00
|
|
|
IdMap = {None: None, '0': None, '00000000': None, '0000000000000000': 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
|
2018-04-10 11:10:10 +02:00
|
|
|
for variable in arguments:
|
|
|
|
IdMap[variable.Id] = variable
|
2014-07-16 15:26:59 +02:00
|
|
|
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)
|
2018-04-10 11:10:10 +02:00
|
|
|
for variable in arguments:
|
|
|
|
variable.setId(IdMap)
|
2014-07-16 15:26:59 +02:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
def setIdMap(self, functions_arguments):
|
|
|
|
"""Set relationships between objects stored in this configuration.
|
|
|
|
:param functions_arguments: List of Variable objects which are function arguments
|
|
|
|
"""
|
|
|
|
self.set_tokens_links()
|
|
|
|
self.set_id_map(functions_arguments)
|
2019-12-09 19:08:45 +01:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2017-04-15 19:15:48 +02:00
|
|
|
class Platform:
|
|
|
|
"""
|
|
|
|
Platform class
|
|
|
|
This class contains type sizes
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name Name of the platform
|
|
|
|
char_bit CHAR_BIT value
|
|
|
|
short_bit SHORT_BIT value
|
|
|
|
int_bit INT_BIT value
|
|
|
|
long_bit LONG_BIT value
|
|
|
|
long_long_bit LONG_LONG_BIT value
|
|
|
|
pointer_bit POINTER_BIT value
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = ''
|
|
|
|
char_bit = 0
|
|
|
|
short_bit = 0
|
|
|
|
int_bit = 0
|
|
|
|
long_bit = 0
|
|
|
|
long_long_bit = 0
|
|
|
|
pointer_bit = 0
|
|
|
|
|
|
|
|
def __init__(self, platformnode):
|
|
|
|
self.name = platformnode.get('name')
|
|
|
|
self.char_bit = int(platformnode.get('char_bit'))
|
|
|
|
self.short_bit = int(platformnode.get('short_bit'))
|
|
|
|
self.int_bit = int(platformnode.get('int_bit'))
|
|
|
|
self.long_bit = int(platformnode.get('long_bit'))
|
|
|
|
self.long_long_bit = int(platformnode.get('long_long_bit'))
|
|
|
|
self.pointer_bit = int(platformnode.get('pointer_bit'))
|
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["name", "char_bit", "short_bit", "int_bit",
|
|
|
|
"long_bit", "long_long_bit", "pointer_bit"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"Platform",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
2019-11-12 09:30:43 +01:00
|
|
|
|
2019-06-14 12:06:57 +02:00
|
|
|
class Standards:
|
|
|
|
"""
|
|
|
|
Standards class
|
|
|
|
This class contains versions of standards that were used for the cppcheck
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
c C Standard used
|
|
|
|
cpp C++ Standard used
|
|
|
|
posix If Posix was used
|
|
|
|
"""
|
|
|
|
|
2020-01-31 23:38:42 +01:00
|
|
|
c = ""
|
|
|
|
cpp = ""
|
|
|
|
posix = False
|
|
|
|
|
|
|
|
def set_c(self, node):
|
|
|
|
self.c = node.get("version")
|
|
|
|
|
|
|
|
def set_cpp(self, node):
|
|
|
|
self.cpp = node.get("version")
|
|
|
|
|
|
|
|
def set_posix(self, node):
|
|
|
|
self.posix = node.get("posix") is not None
|
2019-11-12 09:30:43 +01:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
attrs = ["c", "cpp", "posix"]
|
|
|
|
return "{}({})".format(
|
|
|
|
"Standards",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
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:
|
2019-12-27 08:50:56 +01:00
|
|
|
filename Path to Cppcheck dump file
|
|
|
|
rawTokens List of rawToken elements
|
|
|
|
suppressions List of Suppressions
|
2016-07-25 13:47:26 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2017-04-09 10:11:54 +02:00
|
|
|
rawTokens = []
|
2017-04-15 19:15:48 +02:00
|
|
|
platform = None
|
2018-06-04 21:50:21 +02:00
|
|
|
suppressions = []
|
2015-12-14 09:37:26 +01:00
|
|
|
|
|
|
|
def __init__(self, filename):
|
2019-12-27 08:50:56 +01:00
|
|
|
"""
|
|
|
|
:param filename: Path to Cppcheck dump file
|
|
|
|
"""
|
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
files = [] # source files for elements occurred in this configuration
|
|
|
|
platform_done = False
|
|
|
|
rawtokens_done = False
|
|
|
|
suppressions_done = False
|
|
|
|
|
|
|
|
# Parse general configuration options from <dumps> node
|
|
|
|
# We intentionally don't clean node resources here because we
|
|
|
|
# want to serialize in memory only small part of the XML tree.
|
|
|
|
for event, node in ElementTree.iterparse(self.filename, events=('start', 'end')):
|
|
|
|
if platform_done and rawtokens_done and suppressions_done:
|
|
|
|
break
|
|
|
|
if node.tag == 'platform' and event == 'start':
|
|
|
|
self.platform = Platform(node)
|
|
|
|
platform_done = True
|
|
|
|
elif node.tag == 'rawtokens' and event == 'end':
|
|
|
|
for rawtokens_node in node:
|
|
|
|
if rawtokens_node.tag == 'file':
|
|
|
|
files.append(rawtokens_node.get('name'))
|
|
|
|
elif rawtokens_node.tag == 'tok':
|
|
|
|
tok = Token(rawtokens_node)
|
|
|
|
tok.file = files[int(rawtokens_node.get('fileIndex'))]
|
|
|
|
self.rawTokens.append(tok)
|
|
|
|
rawtokens_done = True
|
|
|
|
elif node.tag == 'suppressions' and event == 'end':
|
|
|
|
for suppressions_node in node:
|
|
|
|
self.suppressions.append(Suppression(suppressions_node))
|
|
|
|
suppressions_done = True
|
|
|
|
|
|
|
|
# Set links between rawTokens.
|
|
|
|
for i in range(len(self.rawTokens)-1):
|
|
|
|
self.rawTokens[i+1].previous = self.rawTokens[i]
|
|
|
|
self.rawTokens[i].next = self.rawTokens[i+1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def configurations(self):
|
|
|
|
"""
|
|
|
|
Return the list of all available Configuration objects.
|
|
|
|
"""
|
|
|
|
return list(self.iterconfigurations())
|
|
|
|
|
|
|
|
def iterconfigurations(self):
|
|
|
|
"""
|
|
|
|
Create and return iterator for the available Configuration objects.
|
|
|
|
The iterator loops over all Configurations in the dump file tree, in document order.
|
|
|
|
"""
|
|
|
|
cfg = None
|
|
|
|
cfg_arguments = [] # function arguments for Configuration node initialization
|
|
|
|
cfg_function = None
|
|
|
|
cfg_valueflow = None
|
|
|
|
|
|
|
|
# Scopes contains <varlist> with all occurred variables. Some of them
|
|
|
|
# appearaed in <variables> node for this configuration.
|
|
|
|
# Others are arguments of functions.
|
|
|
|
# They have similar tag <var> but doesn't contain any attributes. So we
|
|
|
|
# set set a special state when iterate <varlist> node to prevent
|
|
|
|
# overriding of cfg.variables list with empty values.
|
|
|
|
iter_varlist = False
|
|
|
|
|
|
|
|
# Use iterable objects to traverse XML tree for dump files incrementally.
|
|
|
|
# Iterative approach is required to avoid large memory consumption.
|
|
|
|
# Calling .clear() is necessary to let the element be garbage collected.
|
|
|
|
for event, node in ElementTree.iterparse(self.filename, events=('start', 'end')):
|
|
|
|
# Serialize new configuration node
|
|
|
|
if node.tag == 'dump':
|
|
|
|
if event == 'start':
|
|
|
|
cfg = Configuration(node.get('cfg'))
|
|
|
|
continue
|
|
|
|
elif event == 'end':
|
|
|
|
cfg.setIdMap(cfg_arguments)
|
|
|
|
yield cfg
|
|
|
|
cfg = None
|
|
|
|
cfg_arguments = []
|
|
|
|
|
2020-01-31 23:38:42 +01:00
|
|
|
# Parse standards
|
2019-12-27 08:50:56 +01:00
|
|
|
elif node.tag == "standards" and event == 'start':
|
|
|
|
continue
|
2020-01-31 23:38:42 +01:00
|
|
|
elif node.tag == 'c' and event == 'start':
|
|
|
|
cfg.standards.set_c(node)
|
|
|
|
elif node.tag == 'cpp' and event == 'start':
|
|
|
|
cfg.standards.set_cpp(node)
|
|
|
|
elif node.tag == 'posix' and event == 'start':
|
|
|
|
cfg.standards.set_posix(node)
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
# Parse directives list
|
|
|
|
elif node.tag == 'directive' and event == 'start':
|
|
|
|
cfg.directives.append(Directive(node))
|
2017-04-09 10:11:54 +02:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
# Parse tokens
|
|
|
|
elif node.tag == 'tokenlist' and event == 'start':
|
|
|
|
continue
|
|
|
|
elif node.tag == 'token' and event == 'start':
|
|
|
|
cfg.tokenlist.append(Token(node))
|
2017-04-15 19:15:48 +02:00
|
|
|
|
2019-12-27 08:50:56 +01:00
|
|
|
# Parse scopes
|
|
|
|
elif node.tag == 'scopes' and event == 'start':
|
|
|
|
continue
|
|
|
|
elif node.tag == 'scope' and event == 'start':
|
|
|
|
cfg.scopes.append(Scope(node))
|
|
|
|
elif node.tag == 'varlist':
|
|
|
|
if event == 'start':
|
|
|
|
iter_varlist = True
|
|
|
|
elif event == 'end':
|
|
|
|
iter_varlist = False
|
|
|
|
|
|
|
|
# Parse functions
|
|
|
|
elif node.tag == 'functionList' and event == 'start':
|
|
|
|
continue
|
|
|
|
elif node.tag == 'function':
|
|
|
|
if event == 'start':
|
2020-04-16 12:14:29 +02:00
|
|
|
cfg_function = Function(node, cfg.scopes[-1])
|
2019-12-27 08:50:56 +01:00
|
|
|
continue
|
|
|
|
elif event == 'end':
|
|
|
|
cfg.functions.append(cfg_function)
|
|
|
|
cfg_function = None
|
|
|
|
|
|
|
|
# Parse function arguments
|
|
|
|
elif node.tag == 'arg' and event == 'start':
|
|
|
|
arg_nr = int(node.get('nr'))
|
|
|
|
arg_variable_id = node.get('variable')
|
|
|
|
cfg_function.argumentId[arg_nr] = arg_variable_id
|
|
|
|
|
|
|
|
# Parse variables
|
|
|
|
elif node.tag == 'var' and event == 'start':
|
|
|
|
var = Variable(node)
|
|
|
|
if var.nameTokenId:
|
|
|
|
cfg.variables.append(var)
|
|
|
|
elif not iter_varlist:
|
|
|
|
cfg_arguments.append(var)
|
|
|
|
|
|
|
|
# Parse valueflows (list of values)
|
|
|
|
elif node.tag == 'valueflow' and event == 'start':
|
2017-04-09 10:11:54 +02:00
|
|
|
continue
|
2019-12-27 08:50:56 +01:00
|
|
|
elif node.tag == 'values':
|
|
|
|
if event == 'start':
|
|
|
|
cfg_valueflow = ValueFlow(node)
|
|
|
|
continue
|
|
|
|
elif event == 'end':
|
|
|
|
cfg.valueflow.append(cfg_valueflow)
|
|
|
|
cfg_valueflow = None
|
|
|
|
|
|
|
|
# Parse values
|
|
|
|
elif node.tag == 'value' and event == 'start':
|
|
|
|
cfg_valueflow.values.append(Value(node))
|
|
|
|
|
|
|
|
# Remove links to the sibling nodes
|
|
|
|
node.clear()
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2019-12-09 19:08:45 +01:00
|
|
|
def __repr__(self):
|
2019-12-15 19:46:54 +01:00
|
|
|
attrs = ["configurations", "platform"]
|
2019-12-09 19:08:45 +01:00
|
|
|
return "{}({})".format(
|
|
|
|
"CppcheckData",
|
|
|
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
|
|
|
)
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2019-06-23 13:54:33 +02:00
|
|
|
# Get function arguments
|
|
|
|
def getArgumentsRecursive(tok, arguments):
|
|
|
|
if tok is None:
|
|
|
|
return
|
|
|
|
if tok.str == ',':
|
|
|
|
getArgumentsRecursive(tok.astOperand1, arguments)
|
|
|
|
getArgumentsRecursive(tok.astOperand2, arguments)
|
|
|
|
else:
|
|
|
|
arguments.append(tok)
|
|
|
|
|
|
|
|
|
|
|
|
def getArguments(ftok):
|
|
|
|
if (not ftok.isName) or (ftok.next is None) or ftok.next.str != '(':
|
|
|
|
return None
|
|
|
|
args = []
|
|
|
|
getArgumentsRecursive(ftok.next.astOperand2, args)
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
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)
|
2017-06-05 13:23:00 +02:00
|
|
|
if token.str in '+-*/%':
|
2017-06-04 22:51:48 +02:00
|
|
|
return astIsFloat(token.astOperand1) or astIsFloat(token.astOperand2)
|
2014-07-20 15:24:40 +02:00
|
|
|
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)
|
|
|
|
|
2017-06-04 22:51:48 +02:00
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
def ArgumentParser():
|
2016-07-25 13:47:26 +02:00
|
|
|
"""
|
2019-11-15 20:14:30 +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")
|
2019-11-15 20:14:30 +01:00
|
|
|
parser.add_argument("dumpfile", nargs='*',
|
|
|
|
help="Path of dump files from cppcheck.")
|
|
|
|
parser.add_argument("--cli",
|
|
|
|
help="Addon is executed from Cppcheck",
|
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("-q", "--quiet",
|
|
|
|
help='do not print "Checking ..." lines',
|
|
|
|
action="store_true")
|
2015-12-07 19:54:41 +01:00
|
|
|
return parser
|
|
|
|
|
2017-06-04 22:51:48 +02:00
|
|
|
|
2019-06-06 15:38:15 +02:00
|
|
|
def simpleMatch(token, pattern):
|
|
|
|
for p in pattern.split(' '):
|
|
|
|
if not token or token.str != p:
|
|
|
|
return False
|
|
|
|
token = token.next
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-06-24 19:28:52 +02:00
|
|
|
def reportError(location, severity, message, addon, errorId, extra=''):
|
2019-04-09 20:36:24 +02:00
|
|
|
if '--cli' in sys.argv:
|
2019-06-24 19:28:52 +02:00
|
|
|
msg = { 'file': location.file,
|
|
|
|
'linenr': location.linenr,
|
2019-08-18 12:19:05 +02:00
|
|
|
'column': location.column,
|
2019-06-24 19:28:52 +02:00
|
|
|
'severity': severity,
|
|
|
|
'message': message,
|
|
|
|
'addon': addon,
|
|
|
|
'errorId': errorId,
|
|
|
|
'extra': extra}
|
|
|
|
sys.stdout.write(json.dumps(msg) + '\n')
|
2019-04-09 20:36:24 +02:00
|
|
|
else:
|
2019-04-14 08:54:53 +02:00
|
|
|
loc = '[%s:%i]' % (location.file, location.linenr)
|
2019-06-24 19:28:52 +02:00
|
|
|
if len(extra) > 0:
|
|
|
|
message += ' (' + extra + ')'
|
|
|
|
sys.stderr.write('%s (%s) %s [%s-%s]\n' % (loc, severity, message, addon, errorId))
|
2020-08-29 07:44:13 +02:00
|
|
|
global EXIT_CODE
|
|
|
|
EXIT_CODE = 1
|