cppcheckdata: Add Scope.varlist

This commit is contained in:
Daniel Marjamäki 2020-11-13 21:03:02 +01:00
parent b7f812739c
commit 9a81d17a58
1 changed files with 18 additions and 14 deletions

View File

@ -361,6 +361,8 @@ class Scope:
nestedIn = None nestedIn = None
type = None type = None
isExecutable = None isExecutable = None
varlistId = None
varlist = None
def __init__(self, element): def __init__(self, element):
self.Id = element.get('id') self.Id = element.get('id')
@ -376,6 +378,8 @@ class Scope:
self.type = element.get('type') self.type = element.get('type')
self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do', self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do',
'Switch', 'Try', 'Catch', 'Unconditional', 'Lambda')) 'Switch', 'Try', 'Catch', 'Unconditional', 'Lambda'))
self.varlistId = list()
self.varlist = list()
def __repr__(self): def __repr__(self):
attrs = ["Id", "className", "functionId", "bodyStartId", "bodyEndId", attrs = ["Id", "className", "functionId", "bodyStartId", "bodyEndId",
@ -390,6 +394,8 @@ class Scope:
self.bodyEnd = IdMap[self.bodyEndId] self.bodyEnd = IdMap[self.bodyEndId]
self.nestedIn = IdMap[self.nestedInId] self.nestedIn = IdMap[self.nestedInId]
self.function = IdMap[self.functionId] self.function = IdMap[self.functionId]
for v in self.varlistId:
self.varlist.append(IdMap[v])
class Function: class Function:
@ -909,13 +915,8 @@ class CppcheckData:
cfg_function = None cfg_function = None
cfg_valueflow = None cfg_valueflow = None
# Scopes contains <varlist> with all occurred variables. Some of them # Iterating <varlist> in a <scope>.
# appearaed in <variables> node for this configuration. iter_scope_varlist = False
# 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. # Use iterable objects to traverse XML tree for dump files incrementally.
# Iterative approach is required to avoid large memory consumption. # Iterative approach is required to avoid large memory consumption.
@ -959,9 +960,9 @@ class CppcheckData:
cfg.scopes.append(Scope(node)) cfg.scopes.append(Scope(node))
elif node.tag == 'varlist': elif node.tag == 'varlist':
if event == 'start': if event == 'start':
iter_varlist = True iter_scope_varlist = True
elif event == 'end': elif event == 'end':
iter_varlist = False iter_scope_varlist = False
# Parse functions # Parse functions
elif node.tag == 'functionList' and event == 'start': elif node.tag == 'functionList' and event == 'start':
@ -982,10 +983,13 @@ class CppcheckData:
# Parse variables # Parse variables
elif node.tag == 'var' and event == 'start': elif node.tag == 'var' and event == 'start':
if iter_scope_varlist:
cfg.scopes[-1].varlistId.append(node.get('id'))
else:
var = Variable(node) var = Variable(node)
if var.nameTokenId: if var.nameTokenId:
cfg.variables.append(var) cfg.variables.append(var)
elif not iter_varlist: else:
cfg_arguments.append(var) cfg_arguments.append(var)
# Parse valueflows (list of values) # Parse valueflows (list of values)