cppcheck/addons/threadsafety.py

36 lines
1.2 KiB
Python
Raw Normal View History

2017-06-04 22:51:48 +02:00
#!/usr/bin/env python
#
# This script analyses Cppcheck dump files to locate threadsafety issues
# - warn about static local objects
#
import cppcheckdata
import sys
2015-08-21 10:55:19 +02:00
2017-08-15 21:44:21 +02:00
def reportError(token, severity, msg, id):
sys.stderr.write(
2017-08-15 21:44:21 +02:00
'[' + token.file + ':' + str(token.linenr) + '] (' + severity + '): ' + msg + ' [' + id + ']\n')
2015-08-21 10:55:19 +02:00
def checkstatic(data):
2015-08-21 10:55:19 +02:00
for var in data.variables:
if var.isStatic and var.isLocal:
type = None
if var.isClass:
type = 'object'
else:
type = 'variable'
if var.isConst:
reportError(var.typeStartToken, 'warning', 'Local constant static ' + type + ' \'' + var.nameToken.str + '\', dangerous if it is initialized in parallell threads', 'threadsafety')
else:
reportError(var.typeStartToken, 'warning', 'Local static ' + type + ': ' + var.nameToken.str, 'threadsafety')
for arg in sys.argv[1:]:
2015-08-21 10:55:19 +02:00
print('Checking ' + arg + '...')
data = cppcheckdata.parsedump(arg)
for cfg in data.configurations:
if len(data.configurations) > 1:
print('Checking ' + arg + ', config "' + cfg.name + '"...')
checkstatic(cfg)