From 975be66c63e0922a1cf444651a2637662cf7ae29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Apr 2018 23:01:29 +0200 Subject: [PATCH] String concatenation in array: Improved handling of macros --- addons/misc.py | 23 +++++++++++++++++++---- addons/test/misc-test.cpp | 3 ++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/addons/misc.py b/addons/misc.py index 33df09369..ec53c99a8 100644 --- a/addons/misc.py +++ b/addons/misc.py @@ -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) diff --git a/addons/test/misc-test.cpp b/addons/test/misc-test.cpp index 758c158d2..3ea89bbd1 100644 --- a/addons/test/misc-test.cpp +++ b/addons/test/misc-test.cpp @@ -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