Addons/naming.py: verify variable/function names against regular expressions

This commit is contained in:
Daniel Marjamäki 2015-07-28 14:20:38 +02:00
parent 5a57e4030a
commit 8b8d898b96
1 changed files with 33 additions and 0 deletions

33
addons/naming.py Normal file
View File

@ -0,0 +1,33 @@
#/usr/bin/python
# cppcheck addon for naming conventions
import cppcheckdata
import sys
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:]
def reportError(token, severity, msg):
sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') ' + 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 function in data.functions:
res = re.match(RE_FUNCTIONNAME, function.name)
if not res:
reportError(var.typeStartToken, 'style', 'Function ' + function.name + ' violates naming convention')