diff --git a/addons/namingng.py b/addons/namingng.py index 17e3b307a..7a7e2ddf7 100755 --- a/addons/namingng.py +++ b/addons/namingng.py @@ -28,13 +28,15 @@ import re import argparse import json -## Auxiliary class -class dataStruct: + +# Auxiliary class +class DataStruct: def __init__(self, file, linenr, string): self.file = file self.linenr = linenr self.str = string + def reportError(filename, linenr, severity, msg): message = "[{filename}:{linenr}] ( {severity} ) naming.py: {msg}\n".format( filename=filename, @@ -45,27 +47,31 @@ def reportError(filename, linenr, severity, msg): sys.stderr.write(message) return message + def loadConfig(configfile): with open(configfile) as fh: data = json.load(fh) return data + def checkTrueRegex(data, expr, msg, errors): res = re.match(expr, data.str) if res: errors.append(reportError(data.file, data.linenr, 'style', msg)) + def checkFalseRegex(data, expr, msg, errors): res = re.match(expr, data.str) if not res: errors.append(reportError(data.file, data.linenr, 'style', msg)) + def evalExpr(conf, exp, mockToken, msgType, errors): if isinstance(conf, dict): - if (conf[exp][0]): + if conf[exp][0]: msg = msgType + ' ' + mockToken.str + ' violates naming convention : ' + conf[exp][1] checkTrueRegex(mockToken, exp, msg, errors) - elif (~conf[exp][0]): + elif ~conf[exp][0]: msg = msgType + ' ' + mockToken.str + ' violates naming convention : ' + conf[exp][1] checkFalseRegex(mockToken, exp, msg, errors) else: @@ -75,6 +81,7 @@ def evalExpr(conf, exp, mockToken, msgType, errors): msg = msgType + ' ' + mockToken.str + ' violates naming convention' checkFalseRegex(mockToken, exp, msg, errors) + def process(dumpfiles, configfile, debugprint=False): errors = [] @@ -87,18 +94,18 @@ def process(dumpfiles, configfile, debugprint=False): print('Checking ' + afile + '...') data = cppcheckdata.parsedump(afile) - ## Check File naming + # Check File naming if "RE_FILE" in conf and conf["RE_FILE"]: - mockToken = dataStruct(afile[:-5], "0", afile[afile.rfind('/')+1:-5]) + mockToken = DataStruct(afile[:-5], "0", afile[afile.rfind('/') + 1:-5]) msgType = 'File name' for exp in conf["RE_FILE"]: evalExpr(conf["RE_FILE"], exp, mockToken, msgType, errors) - ## Check Namespace naming + # Check Namespace naming if "RE_NAMESPACE" in conf and conf["RE_NAMESPACE"]: for tk in data.rawTokens: - if (tk.str == 'namespace'): - mockToken = dataStruct(tk.next.file, tk.next.linenr, tk.next.str) + if tk.str == 'namespace': + mockToken = DataStruct(tk.next.file, tk.next.linenr, tk.next.str) msgType = 'Namespace' for exp in conf["RE_NAMESPACE"]: evalExpr(conf["RE_NAMESPACE"], exp, mockToken, msgType, errors) @@ -136,43 +143,43 @@ def process(dumpfiles, configfile, debugprint=False): var.nameToken.str + ' violates naming convention')) - mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) + mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) msgType = 'Variable' for exp in conf["RE_VARNAME"]: evalExpr(conf["RE_VARNAME"], exp, mockToken, msgType, errors) - ## Check Private Variable naming + # Check Private Variable naming if "RE_PRIVATE_MEMBER_VARIABLE" in conf and conf["RE_PRIVATE_MEMBER_VARIABLE"]: # TODO: Not converted yet for var in cfg.variables: if (var.access is None) or var.access != 'Private': continue - mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) + mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) msgType = 'Private member variable' for exp in conf["RE_PRIVATE_MEMBER_VARIABLE"]: evalExpr(conf["RE_PRIVATE_MEMBER_VARIABLE"], exp, mockToken, msgType, errors) - ## Check Public Member Variable naming + # Check Public Member Variable naming if "RE_PUBLIC_MEMBER_VARIABLE" in conf and conf["RE_PUBLIC_MEMBER_VARIABLE"]: for var in cfg.variables: if (var.access is None) or var.access != 'Public': continue - mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) + mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) msgType = 'Public member variable' for exp in conf["RE_PUBLIC_MEMBER_VARIABLE"]: evalExpr(conf["RE_PUBLIC_MEMBER_VARIABLE"], exp, mockToken, msgType, errors) - ## Check Global Variable naming + # Check Global Variable naming if "RE_GLOBAL_VARNAME" in conf and conf["RE_GLOBAL_VARNAME"]: for var in cfg.variables: if (var.access is None) or var.access != 'Global': continue - mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) + mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str) msgType = 'Public member variable' for exp in conf["RE_GLOBAL_VARNAME"]: evalExpr(conf["RE_GLOBAL_VARNAME"], exp, mockToken, msgType, errors) - ## Check Functions naming + # Check Functions naming if "RE_FUNCTIONNAME" in conf and conf["RE_FUNCTIONNAME"]: for token in cfg.tokenlist: if token.function: @@ -190,22 +197,23 @@ def process(dumpfiles, configfile, debugprint=False): if not token.function.name.startswith(conf["function_prefixes"][retval]): errors.append(reportError( token.file, token.linenr, 'style', 'Function ' + token.function.name + ' violates naming convention')) - mockToken = dataStruct(token.file, token.linenr, token.function.name) + mockToken = DataStruct(token.file, token.linenr, token.function.name) msgType = 'Function' for exp in conf["RE_FUNCTIONNAME"]: evalExpr(conf["RE_FUNCTIONNAME"], exp, mockToken, msgType, errors) - ## Check Class naming + # Check Class naming if "RE_CLASS_NAME" in conf and conf["RE_CLASS_NAME"]: for fnc in cfg.functions: - #Check if it is Constructor/Destructor - if (fnc.type == 'Constructor' or fnc.type == 'Destructor'): - mockToken = dataStruct(fnc.tokenDef.file, fnc.tokenDef.linenr, fnc.name) + # Check if it is Constructor/Destructor + if fnc.type == 'Constructor' or fnc.type == 'Destructor': + mockToken = DataStruct(fnc.tokenDef.file, fnc.tokenDef.linenr, fnc.name) msgType = 'Class ' + fnc.type for exp in conf["RE_CLASS_NAME"]: evalExpr(conf["RE_CLASS_NAME"], exp, mockToken, msgType, errors) return errors + if __name__ == "__main__": parser = argparse.ArgumentParser(description='Naming verification') parser.add_argument('dumpfiles', type=str, nargs='+',