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('"')
|
||||
|
||||
# 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
|
||||
for i in range(len(rawTokens)):
|
||||
if i < 2:
|
||||
|
@ -62,8 +73,12 @@ def stringConcatInArrayInit(rawTokens):
|
|||
arrayInit = False
|
||||
elif tok1 == ']' and tok2 == '=' and tok3 == '{':
|
||||
arrayInit = True
|
||||
elif arrayInit and (tok1 in [',', '{']) and isStringLiteral(tok2) and isStringLiteral(tok3):
|
||||
reportError(rawTokens[i], 'style', 'String concatenation in array initialization, missing comma?', 'stringConcatInArrayInit')
|
||||
elif arrayInit and (tok1 in [',', '{']):
|
||||
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):
|
||||
for cfg in data.configurations:
|
||||
|
@ -135,7 +150,7 @@ for arg in sys.argv[1:]:
|
|||
if word in ['stringConcatInArrayInit', 'implicitlyVirtual', 'ellipsisStructArg']:
|
||||
VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word)
|
||||
|
||||
stringConcatInArrayInit(data.rawTokens)
|
||||
stringConcatInArrayInit(data.configurations, data.rawTokens)
|
||||
implicitlyVirtual(data)
|
||||
ellipsisStructArg(data)
|
||||
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
// To test:
|
||||
// ~/cppcheck/cppcheck --dump misc-test.cpp && python ../misc.py -verify misc-test.cpp.dump
|
||||
|
||||
|
||||
// Warn about string concatenation in array initializers..
|
||||
const char *a[] = {"a" "b"}; // stringConcatInArrayInit
|
||||
const char *b[] = {"a","b" "c"}; // stringConcatInArrayInit
|
||||
#define MACRO "MACRO"
|
||||
const char *c[] = { MACRO "text" }; // stringConcatInArrayInit
|
||||
|
||||
|
||||
// Function is implicitly virtual
|
||||
|
|
Loading…
Reference in New Issue