From 7d0075357e0754f54886bf5c404fd132b2297789 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 21 Aug 2015 11:55:19 +0300 Subject: [PATCH] PEP8 fixes. [ci skip] --- addons/cert.py | 18 +++++++---- addons/cppcheckdata.py | 60 ++++++++++++++++++++----------------- addons/naming.py | 41 ++++++++++++------------- addons/threadsafety.py | 16 +++++----- tools/daca2-addons.py | 6 ++-- tools/matchcompiler.py | 7 ++--- tools/readme.md | 1 - tools/rundaca2-addons.py | 9 +++--- tools/rundaca2.py | 8 +++-- tools/test_matchcompiler.py | 3 +- tools/times-tags.sh | 2 -- 11 files changed, 93 insertions(+), 78 deletions(-) diff --git a/addons/cert.py b/addons/cert.py index 931abdd05..257a8e53a 100644 --- a/addons/cert.py +++ b/addons/cert.py @@ -11,17 +11,21 @@ import cppcheckdata import sys + def reportError(token, severity, msg): - sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') cert.py: ' + msg + '\n') + sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') cert.py: ' + msg + '\n') + def isLocalStruct(arg): if arg and arg.str == '&' and not arg.astOperand2: arg = arg.astOperand1 return arg and arg.variable and arg.variable.isClass and (arg.variable.isLocal or arg.variable.isArgument) + def isBitwiseOp(token): return token and (token.str in ['&', '|', '^']) + def isComparisonOp(token): return token and (token.str in ['==', '!=', '>', '>=', '<', '<=']) @@ -42,19 +46,21 @@ def exp42(data): if token.astOperand1.str == 'memcmp' and (isLocalStruct(arg1) or isLocalStruct(arg2)): reportError(token, 'style', 'EXP42-C Comparison of struct padding data') - if (token.astOperand1.str in ['memcpy','memmove']) and isLocalStruct(arg2): + if (token.astOperand1.str in ['memcpy', 'memmove']) and isLocalStruct(arg2): reportError(token, 'style', 'EXP42-C Reading struct padding data') # EXP46-C # Do not use a bitwise operator with a Boolean-like operand # int x = (a == b) & c; + + def exp46(data): for token in data.tokenlist: if isBitwiseOp(token) and (isComparisonOp(token.astOperand1) or isComparisonOp(token.astOperand2)): reportError(token, 'style', 'EXP46-C Bitwise operator is used with a Boolean-like operand') for arg in sys.argv[1:]: - print('Checking ' + arg + '...') - data = cppcheckdata.parsedump(arg) - exp42(data) - exp46(data) + print('Checking ' + arg + '...') + data = cppcheckdata.parsedump(arg) + exp42(data) + exp46(data) diff --git a/addons/cppcheckdata.py b/addons/cppcheckdata.py index ef9754bfb..5602c1b1e 100644 --- a/addons/cppcheckdata.py +++ b/addons/cppcheckdata.py @@ -3,72 +3,75 @@ import xml.etree.ElementTree as ET -## Token class. Contains information about each token in the source code. +# Token class. Contains information about each token in the source code. + + class Token: Id = None - ## Token string + # Token string str = None - ## Next token in tokenlist. For last token, next is None. + # Next token in tokenlist. For last token, next is None. next = None - ## Previous token in tokenlist. For first token, previous is None, + # Previous token in tokenlist. For first token, previous is None, previous = None linkId = None - ## Linked token in tokenlist. Each '(', '[' and '{' are linked to the + # Linked token in tokenlist. Each '(', '[' and '{' are linked to the # corresponding '}', ']' and ')'. For templates, the '<' is linked to # the corresponding '>'. link = None scopeId = None - ## Scope information for this token. See the Scope class. + # Scope information for this token. See the Scope class. scope = None - ## Is this token a symbol name + # Is this token a symbol name isName = False - ## Is this token a number, for example 123, 12.34 + # Is this token a number, for example 123, 12.34 isNumber = False - ## Is this token a int value such as 1234 + # Is this token a int value such as 1234 isInt = False - ## Is this token a int value such as 12.34 + # Is this token a int value such as 12.34 isFloat = False - ## Is this token a string literal such as "hello" + # Is this token a string literal such as "hello" isString = False - ## string length for string literal + # string length for string literal strlen = None - ## Is this token a char literal such as 'x' + # Is this token a char literal such as 'x' isChar = False - ## Is this token a operator + # Is this token a operator isOp = False - ## Is this token a arithmetic operator + # Is this token a arithmetic operator isArithmeticalOp = False - ## Is this token a assignment operator + # Is this token a assignment operator isAssignmentOp = False - ## Is this token a comparison operator + # Is this token a comparison operator isComparisonOp = False - ## Is this token a logical operator: && || + # Is this token a logical operator: && || isLogicalOp = False - ## varId for token, each variable has a unique non-zero id + # varId for token, each variable has a unique non-zero id varId = None variableId = None - ## Variable information for this token. See the Variable class. + # Variable information for this token. See the Variable class. variable = None functionId = None - ## If this token points at a function call, this attribute has the Function information. See the Function class. + # If this token points at a function call, this attribute has the Function + # information. See the Function class. function = None valuesId = None - ## Possible values of token + # Possible values of token values = None - + astParentId = None - ## syntax tree parent + # syntax tree parent astParent = None astOperand1Id = None - ## syntax tree operand1 + # syntax tree operand1 astOperand1 = None astOperand2Id = None - ## syntax tree operand2 + # syntax tree operand2 astOperand2 = None - ## file name + # file name file = None - ## line number + # line number linenr = None def __init__(self, element): @@ -189,6 +192,7 @@ class Function: self.argument[argnr] = IdMap[argid] self.tokenDef = IdMap[self.tokenDefId] + class Variable: Id = None nameTokenId = None diff --git a/addons/naming.py b/addons/naming.py index c53f6224b..f3188862f 100644 --- a/addons/naming.py +++ b/addons/naming.py @@ -14,27 +14,28 @@ import re RE_VARNAME = None RE_FUNCTIONNAME = None for arg in sys.argv[1:]: - if arg[:6]=='--var=': - RE_VARNAME = arg[6:] - elif arg[:11]=='--function=': - RE_FUNCTIONNAME = arg[11:] + if arg[:6] == '--var=': + RE_VARNAME = arg[6:] + elif arg[:11] == '--function=': + RE_FUNCTIONNAME = arg[11:] + def reportError(token, severity, msg): - sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') naming.py: ' + msg + '\n') + sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') naming.py: ' + msg + '\n') for arg in sys.argv[1:]: - if not arg[-5:]=='.dump': - continue - print('Checking ' + arg + '...') - data = cppcheckdata.parsedump(arg) - if RE_VARNAME: - for var in data.variables: - res = re.match(RE_VARNAME, var.nameToken.str) - if not res: - reportError(var.typeStartToken, 'style', 'Variable ' + var.nameToken.str + ' violates naming convention') - if RE_FUNCTIONNAME: - for scope in data.scopes: - if scope.type == 'Function': - res = re.match(RE_FUNCTIONNAME, scope.className) - if not res: - reportError(scope.classStart, 'style', 'Function ' + scope.className + ' violates naming convention') + if not arg[-5:] == '.dump': + continue + print('Checking ' + arg + '...') + data = cppcheckdata.parsedump(arg) + if RE_VARNAME: + for var in data.variables: + res = re.match(RE_VARNAME, var.nameToken.str) + if not res: + reportError(var.typeStartToken, 'style', 'Variable ' + var.nameToken.str + ' violates naming convention') + if RE_FUNCTIONNAME: + for scope in data.scopes: + if scope.type == 'Function': + res = re.match(RE_FUNCTIONNAME, scope.className) + if not res: + reportError(scope.classStart, 'style', 'Function ' + scope.className + ' violates naming convention') diff --git a/addons/threadsafety.py b/addons/threadsafety.py index 29599a451..c192c6528 100644 --- a/addons/threadsafety.py +++ b/addons/threadsafety.py @@ -7,15 +7,17 @@ import cppcheckdata import sys + def reportError(token, severity, msg): - sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') threadsafety.py: ' + msg + '\n') + sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') threadsafety.py: ' + msg + '\n') + def checkstatic(data): - for var in data.variables: - if var.isStatic==True and var.isLocal==True and var.isClass==True: - reportError(var.typeStartToken, 'warning', 'Local static object') + for var in data.variables: + if var.isStatic == True and var.isLocal == True and var.isClass == True: + reportError(var.typeStartToken, 'warning', 'Local static object') for arg in sys.argv[1:]: - print('Checking ' + arg + '...') - data = cppcheckdata.parsedump(arg) - checkstatic(data) + print('Checking ' + arg + '...') + data = cppcheckdata.parsedump(arg) + checkstatic(data) diff --git a/tools/daca2-addons.py b/tools/daca2-addons.py index fe2a65108..8921cdc3f 100644 --- a/tools/daca2-addons.py +++ b/tools/daca2-addons.py @@ -120,6 +120,7 @@ def removeLargeFiles(path): if path.find('/clang/INPUTS/') > 0 or statinfo.st_size > 100000: os.remove(g) + def dumpfiles(path): ret = [] for g in glob.glob(path + '*'): @@ -132,6 +133,7 @@ def dumpfiles(path): ret.append(g) return ret + def scanarchive(filepath, jobs): # remove all files/folders except results.txt removeAllExceptResults() @@ -199,8 +201,8 @@ def scanarchive(filepath, jobs): 'python', addon, dumpfile], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) comm = p2.communicate() results.write(comm[1]) results.close() diff --git a/tools/matchcompiler.py b/tools/matchcompiler.py index 65ff97fbd..38d53d1bc 100755 --- a/tools/matchcompiler.py +++ b/tools/matchcompiler.py @@ -274,7 +274,7 @@ class MatchCompiler: elif line[pos] == '"': inString = True pos += 1 - return inString + return inString def _parseStringComparison(self, line, pos1): startPos = 0 @@ -650,7 +650,7 @@ def main(): help='verify compiled matches against on-the-fly parser. Slow!') parser.add_argument('--show-skipped', action='store_true', default=False, help='show skipped (non-static) patterns') - parser.add_argument('--read-dir', default="lib", + parser.add_argument('--read-dir', default="lib", help='directory from which files are read') parser.add_argument('--write-dir', default="build", help='directory into which files are written') @@ -683,7 +683,7 @@ def main(): if not files: # select all *.cpp files in lib_dir for f in glob.glob(lib_dir + '/*.cpp'): - files.append(f[len(lib_dir)+1:]) + files.append(f[len(lib_dir) + 1:]) # convert files for fi in files: @@ -695,4 +695,3 @@ def main(): if __name__ == '__main__': main() - diff --git a/tools/readme.md b/tools/readme.md index e0f146037..f2800a0b2 100644 --- a/tools/readme.md +++ b/tools/readme.md @@ -68,4 +68,3 @@ $ make reduce ### * tools/times.sh Script to generate a `times.log` file that contains timing information of the last 20 revisions. - diff --git a/tools/rundaca2-addons.py b/tools/rundaca2-addons.py index 12bd2ca76..c42c4eb46 100644 --- a/tools/rundaca2-addons.py +++ b/tools/rundaca2-addons.py @@ -12,6 +12,8 @@ if len(sys.argv) == 2: PASSWORD = sys.argv[1] # Upload file to sourceforge web server using scp + + def upload(file_to_upload, destination): if not os.path.isfile(file_to_upload): return @@ -29,6 +31,7 @@ def upload(file_to_upload, destination): except pexpect.TIMEOUT: pass + def daca2(foldernum): folders = '0123456789abcdefghijklmnopqrstuvwxyz' folder = folders[foldernum % len(folders)] @@ -47,13 +50,11 @@ def daca2(foldernum): subprocess.call(['mv', 'cppcheck', os.path.expanduser('~/daca2-addons/cppcheck-O2')]) subprocess.call(['nice', '--adjustment=19', 'python', os.path.expanduser('~/cppcheck/tools/daca2-addons.py'), folder, '--rev=' + rev]) - upload(os.path.expanduser('~/daca2-addons/'+folder+'/results.txt'), 'evidente/addons-'+folder+'.txt') + upload(os.path.expanduser('~/daca2-addons/' + folder + '/results.txt'), 'evidente/addons-' + folder + '.txt') subprocess.call(['nice', '--adjustment=19', 'python', os.path.expanduser('~/cppcheck/tools/daca2-addons.py'), 'lib' + folder, '--rev=' + rev]) - upload(os.path.expanduser('~/daca2-addons/lib'+folder+'/results.txt'), 'evidente/addons-lib'+folder+'.txt') + upload(os.path.expanduser('~/daca2-addons/lib' + folder + '/results.txt'), 'evidente/addons-lib' + folder + '.txt') foldernum = 0 while True: daca2(foldernum) foldernum = foldernum + 1 - - diff --git a/tools/rundaca2.py b/tools/rundaca2.py index 18d83612b..2b593357d 100644 --- a/tools/rundaca2.py +++ b/tools/rundaca2.py @@ -12,6 +12,8 @@ if len(sys.argv) == 2: PASSWORD = sys.argv[1] # Upload file to sourceforge web server using scp + + def upload(file_to_upload, destination): if not os.path.isfile(file_to_upload): return @@ -29,6 +31,7 @@ def upload(file_to_upload, destination): except pexpect.TIMEOUT: pass + def daca2(foldernum): folders = '0123456789abcdefghijklmnopqrstuvwxyz' folder = folders[foldernum % len(folders)] @@ -47,12 +50,11 @@ def daca2(foldernum): subprocess.call(['mv', 'cppcheck', os.path.expanduser('~/daca2/cppcheck-O2')]) subprocess.call(['nice', '--adjustment=19', 'python', os.path.expanduser('~/cppcheck/tools/daca2.py'), folder, '--rev=' + rev]) - upload(os.path.expanduser('~/daca2/'+folder+'/results.txt'), 'evidente/results-'+folder+'.txt') + upload(os.path.expanduser('~/daca2/' + folder + '/results.txt'), 'evidente/results-' + folder + '.txt') subprocess.call(['nice', '--adjustment=19', 'python', os.path.expanduser('~/cppcheck/tools/daca2.py'), 'lib' + folder, '--rev=' + rev]) - upload(os.path.expanduser('~/daca2/lib'+folder+'/results.txt'), 'evidente/results-lib'+folder+'.txt') + upload(os.path.expanduser('~/daca2/lib' + folder + '/results.txt'), 'evidente/results-lib' + folder + '.txt') foldernum = 0 while True: daca2(foldernum) foldernum = foldernum + 1 - diff --git a/tools/test_matchcompiler.py b/tools/test_matchcompiler.py index 7864f9042..bf811d3ff 100755 --- a/tools/test_matchcompiler.py +++ b/tools/test_matchcompiler.py @@ -137,7 +137,8 @@ class MatchCompilerTest(unittest.TestCase): def test_parseStringComparison(self): input = 'str == "abc"' - res = self.mc._parseStringComparison(input, 5) # offset '5' is chosen as an abritary start offset to look for " + # offset '5' is chosen as an abritary start offset to look for + res = self.mc._parseStringComparison(input, 5) self.assertEqual(2, len(res)) self.assertEqual('str == MatchCompiler::makeConstString("abc")', input[:res[0]] + "MatchCompiler::makeConstString(" + input[res[0]:res[1]] + ")" + input[res[1]:]) diff --git a/tools/times-tags.sh b/tools/times-tags.sh index 6c15358a9..566e3e918 100755 --- a/tools/times-tags.sh +++ b/tools/times-tags.sh @@ -21,5 +21,3 @@ do git checkout master git branch -D "$i" done - -