2017-06-04 22:51:48 +02:00
|
|
|
#!/usr/bin/env python
|
2015-07-28 14:50:30 +02:00
|
|
|
#
|
2015-07-28 14:20:38 +02:00
|
|
|
# cppcheck addon for naming conventions
|
2015-07-28 14:50:30 +02:00
|
|
|
#
|
|
|
|
# Example usage (variable name must start with lowercase, function name must start with uppercase):
|
|
|
|
# $ cppcheck --dump path-to-src/
|
|
|
|
# $ python addons/naming.py --var='[a-z].*' --function='[A-Z].*' path-to-src/*.dump
|
|
|
|
#
|
2015-07-28 14:20:38 +02:00
|
|
|
|
|
|
|
import cppcheckdata
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
|
|
|
RE_VARNAME = None
|
2018-06-17 14:46:59 +02:00
|
|
|
RE_PRIVATE_MEMBER_VARIABLE = None
|
2015-07-28 14:20:38 +02:00
|
|
|
RE_FUNCTIONNAME = None
|
|
|
|
for arg in sys.argv[1:]:
|
2015-08-21 10:55:19 +02:00
|
|
|
if arg[:6] == '--var=':
|
|
|
|
RE_VARNAME = arg[6:]
|
2018-06-17 14:46:59 +02:00
|
|
|
elif arg.startswith('--private-member-variable='):
|
|
|
|
RE_PRIVATE_MEMBER_VARIABLE = arg[arg.find('=')+1:]
|
2015-08-21 10:55:19 +02:00
|
|
|
elif arg[:11] == '--function=':
|
|
|
|
RE_FUNCTIONNAME = arg[11:]
|
|
|
|
|
2015-07-28 14:20:38 +02:00
|
|
|
|
2018-06-18 09:55:43 +02:00
|
|
|
FoundError = False
|
|
|
|
|
2015-07-28 14:20:38 +02:00
|
|
|
def reportError(token, severity, msg):
|
2018-06-18 09:55:43 +02:00
|
|
|
global FoundError
|
|
|
|
FoundError = True
|
2015-12-14 09:37:26 +01:00
|
|
|
sys.stderr.write(
|
|
|
|
'[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') naming.py: ' + msg + '\n')
|
2015-07-28 14:20:38 +02:00
|
|
|
|
|
|
|
for arg in sys.argv[1:]:
|
2015-08-21 10:55:19 +02:00
|
|
|
if not arg[-5:] == '.dump':
|
|
|
|
continue
|
|
|
|
print('Checking ' + arg + '...')
|
|
|
|
data = cppcheckdata.parsedump(arg)
|
2015-12-14 09:37:26 +01:00
|
|
|
for cfg in data.configurations:
|
|
|
|
if len(data.configurations) > 1:
|
|
|
|
print('Checking ' + arg + ', config "' + cfg.name + '"...')
|
|
|
|
if RE_VARNAME:
|
|
|
|
for var in cfg.variables:
|
|
|
|
res = re.match(RE_VARNAME, var.nameToken.str)
|
2015-08-21 10:55:19 +02:00
|
|
|
if not res:
|
2015-12-14 09:37:26 +01:00
|
|
|
reportError(var.typeStartToken, 'style', 'Variable ' +
|
|
|
|
var.nameToken.str + ' violates naming convention')
|
2018-06-17 14:46:59 +02:00
|
|
|
if RE_PRIVATE_MEMBER_VARIABLE:
|
|
|
|
for var in cfg.variables:
|
|
|
|
if (var.access is None) or var.access != 'Private':
|
|
|
|
continue
|
|
|
|
res = re.match(RE_PRIVATE_MEMBER_VARIABLE, var.nameToken.str)
|
|
|
|
if not res:
|
|
|
|
reportError(var.typeStartToken, 'style', 'Private member variable ' +
|
|
|
|
var.nameToken.str + ' violates naming convention')
|
2015-12-14 09:37:26 +01:00
|
|
|
if RE_FUNCTIONNAME:
|
|
|
|
for scope in cfg.scopes:
|
|
|
|
if scope.type == 'Function':
|
|
|
|
res = re.match(RE_FUNCTIONNAME, scope.className)
|
|
|
|
if not res:
|
|
|
|
reportError(
|
2018-04-28 23:06:54 +02:00
|
|
|
scope.bodyStart, 'style', 'Function ' + scope.className + ' violates naming convention')
|
2018-06-18 09:55:43 +02:00
|
|
|
|
|
|
|
if FoundError:
|
|
|
|
print('FoundError')
|
|
|
|
sys.exit(1)
|
|
|
|
|