String concatenation in array: Improved handling of macros
This commit is contained in:
parent
e571e598b6
commit
975be66c63
|
@ -50,7 +50,18 @@ def isStringLiteral(tokenString):
|
||||||
return tokenString.startswith('"')
|
return tokenString.startswith('"')
|
||||||
|
|
||||||
# check data
|
# check data
|
||||||
def stringConcatInArrayInit(rawTokens):
|
def stringConcatInArrayInit(configurations, rawTokens):
|
||||||
|
# Get all string macros
|
||||||
|
stringMacros = []
|
||||||
|
for cfg in configurations:
|
||||||
|
for directive in cfg.directives:
|
||||||
|
res = re.match(r'#define[ ]+([A-Za-z0-9_]+)[ ]+".*', directive.str)
|
||||||
|
if res:
|
||||||
|
macroName = res.group(1)
|
||||||
|
if macroName not in stringMacros:
|
||||||
|
stringMacros.append(macroName)
|
||||||
|
|
||||||
|
# Check code
|
||||||
arrayInit = False
|
arrayInit = False
|
||||||
for i in range(len(rawTokens)):
|
for i in range(len(rawTokens)):
|
||||||
if i < 2:
|
if i < 2:
|
||||||
|
@ -62,8 +73,12 @@ def stringConcatInArrayInit(rawTokens):
|
||||||
arrayInit = False
|
arrayInit = False
|
||||||
elif tok1 == ']' and tok2 == '=' and tok3 == '{':
|
elif tok1 == ']' and tok2 == '=' and tok3 == '{':
|
||||||
arrayInit = True
|
arrayInit = True
|
||||||
elif arrayInit and (tok1 in [',', '{']) and isStringLiteral(tok2) and isStringLiteral(tok3):
|
elif arrayInit and (tok1 in [',', '{']):
|
||||||
reportError(rawTokens[i], 'style', 'String concatenation in array initialization, missing comma?', 'stringConcatInArrayInit')
|
isString2 = (isStringLiteral(tok2) or (tok2 in stringMacros))
|
||||||
|
isString3 = (isStringLiteral(tok3) or (tok3 in stringMacros))
|
||||||
|
if isString2 and isString3:
|
||||||
|
reportError(rawTokens[i], 'style', 'String concatenation in array initialization, missing comma?', 'stringConcatInArrayInit')
|
||||||
|
|
||||||
|
|
||||||
def implicitlyVirtual(data):
|
def implicitlyVirtual(data):
|
||||||
for cfg in data.configurations:
|
for cfg in data.configurations:
|
||||||
|
@ -135,7 +150,7 @@ for arg in sys.argv[1:]:
|
||||||
if word in ['stringConcatInArrayInit', 'implicitlyVirtual', 'ellipsisStructArg']:
|
if word in ['stringConcatInArrayInit', 'implicitlyVirtual', 'ellipsisStructArg']:
|
||||||
VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word)
|
VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word)
|
||||||
|
|
||||||
stringConcatInArrayInit(data.rawTokens)
|
stringConcatInArrayInit(data.configurations, data.rawTokens)
|
||||||
implicitlyVirtual(data)
|
implicitlyVirtual(data)
|
||||||
ellipsisStructArg(data)
|
ellipsisStructArg(data)
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
// To test:
|
// To test:
|
||||||
// ~/cppcheck/cppcheck --dump misc-test.cpp && python ../misc.py -verify misc-test.cpp.dump
|
// ~/cppcheck/cppcheck --dump misc-test.cpp && python ../misc.py -verify misc-test.cpp.dump
|
||||||
|
|
||||||
|
|
||||||
// Warn about string concatenation in array initializers..
|
// Warn about string concatenation in array initializers..
|
||||||
const char *a[] = {"a" "b"}; // stringConcatInArrayInit
|
const char *a[] = {"a" "b"}; // stringConcatInArrayInit
|
||||||
const char *b[] = {"a","b" "c"}; // stringConcatInArrayInit
|
const char *b[] = {"a","b" "c"}; // stringConcatInArrayInit
|
||||||
|
#define MACRO "MACRO"
|
||||||
|
const char *c[] = { MACRO "text" }; // stringConcatInArrayInit
|
||||||
|
|
||||||
|
|
||||||
// Function is implicitly virtual
|
// Function is implicitly virtual
|
||||||
|
|
Loading…
Reference in New Issue