addons/naming.py: Fix crash on unnamed arguments, add regex validation. (#1762)
Crash has been reported in the forum: https://sourceforge.net/p/cppcheck/discussion/development/thread/138788ac6b/ A small test has been added to avoid regression.
This commit is contained in:
parent
15676612c0
commit
4735b6ca1b
|
@ -113,6 +113,11 @@ matrix:
|
|||
- python ../misra.py --rule-texts=misra2012_rules_dummy_windows1250.txt -verify misra-test.cpp.dump
|
||||
- python3 ../misra.py --rule-texts=misra2012_rules_dummy_windows1250.txt -verify misra-test.cpp.dump
|
||||
- cd ../../
|
||||
# check addons/naming.py
|
||||
- cd addons/test
|
||||
- ${CPPCHECK} --dump naming_test.c
|
||||
- python3 ../naming.py --var='[a-z].*' --function='[a-z].*' naming_test.c.dump
|
||||
- cd ../..
|
||||
# check addons/namingng.py
|
||||
- cd addons/test
|
||||
- ${CPPCHECK} --dump namingng_test.c
|
||||
|
|
|
@ -11,16 +11,28 @@ import cppcheckdata
|
|||
import sys
|
||||
import re
|
||||
|
||||
|
||||
def validate_regex(expr):
|
||||
try:
|
||||
re.compile(expr)
|
||||
except re.error:
|
||||
print('Error: "{}" is not a valid regular expression.'.format(expr))
|
||||
exit(1)
|
||||
|
||||
|
||||
RE_VARNAME = None
|
||||
RE_PRIVATE_MEMBER_VARIABLE = None
|
||||
RE_FUNCTIONNAME = None
|
||||
for arg in sys.argv[1:]:
|
||||
if arg[:6] == '--var=':
|
||||
RE_VARNAME = arg[6:]
|
||||
validate_regex(RE_VARNAME)
|
||||
elif arg.startswith('--private-member-variable='):
|
||||
RE_PRIVATE_MEMBER_VARIABLE = arg[arg.find('=')+1:]
|
||||
validate_regex(RE_PRIVATE_MEMBER_VARIABLE)
|
||||
elif arg[:11] == '--function=':
|
||||
RE_FUNCTIONNAME = arg[11:]
|
||||
validate_regex(RE_FUNCTIONNAME)
|
||||
|
||||
|
||||
FoundError = False
|
||||
|
@ -41,10 +53,11 @@ for arg in sys.argv[1:]:
|
|||
print('Checking ' + arg + ', config "' + cfg.name + '"...')
|
||||
if RE_VARNAME:
|
||||
for var in cfg.variables:
|
||||
res = re.match(RE_VARNAME, var.nameToken.str)
|
||||
if not res:
|
||||
reportError(var.typeStartToken, 'style', 'Variable ' +
|
||||
var.nameToken.str + ' violates naming convention')
|
||||
if var.nameToken:
|
||||
res = re.match(RE_VARNAME, var.nameToken.str)
|
||||
if not res:
|
||||
reportError(var.typeStartToken, 'style', 'Variable ' +
|
||||
var.nameToken.str + ' violates naming convention')
|
||||
if RE_PRIVATE_MEMBER_VARIABLE:
|
||||
for var in cfg.variables:
|
||||
if (var.access is None) or var.access != 'Private':
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
// To test:
|
||||
// ~/cppcheck/cppcheck --dump naming_test.c && python ../naming.py --var='[a-z].*' --function='[a-z].*' naming_test.c.dump
|
||||
|
||||
// Should not crash when there is no name
|
||||
void func(int number, bool);
|
Loading…
Reference in New Issue