From a2945357fa413e18e2a5deb513cace684dbb6a2f Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 4 May 2024 11:04:24 +0200 Subject: [PATCH] testrunner: prefer to pass code as sized `const char[]` --- test/helpers.h | 22 +- test/test64bit.cpp | 3 +- test/testassert.cpp | 3 +- test/testastutils.cpp | 297 +++++++++------- test/testautovariables.cpp | 3 +- test/testbool.cpp | 3 +- test/testboost.cpp | 3 +- test/testbufferoverrun.cpp | 31 +- test/testcharvar.cpp | 3 +- test/testclass.cpp | 60 ++-- test/testcondition.cpp | 3 +- test/testconstructors.cpp | 6 +- test/testexceptionsafety.cpp | 3 +- test/testfunctions.cpp | 3 +- test/testgarbage.cpp | 41 ++- test/testinternal.cpp | 3 +- test/testio.cpp | 20 +- test/testleakautovar.cpp | 61 ++-- test/testmemleak.cpp | 15 +- test/testnullpointer.cpp | 9 +- test/testother.cpp | 38 +- test/testpostfixoperator.cpp | 3 +- test/testsimplifytemplate.cpp | 6 +- test/testsimplifytokens.cpp | 114 +++--- test/testsimplifytypedef.cpp | 344 +++++++++--------- test/testsimplifyusing.cpp | 3 +- test/testsizeof.cpp | 6 +- test/teststl.cpp | 436 ++++++++++++----------- test/testsummaries.cpp | 3 +- test/testsymboldatabase.cpp | 471 ++++++++++++++----------- test/testtokenize.cpp | 643 +++++++++++++++++++--------------- test/testtype.cpp | 18 +- test/testunusedfunctions.cpp | 15 +- test/testvaarg.cpp | 3 +- test/testvalueflow.cpp | 212 ++++++----- test/testvarid.cpp | 175 ++++----- 36 files changed, 1735 insertions(+), 1347 deletions(-) diff --git a/test/helpers.h b/test/helpers.h index 107db754069..2360b159f1a 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -43,7 +43,8 @@ namespace simplecpp { // TODO: make Tokenizer private class SimpleTokenizer : public Tokenizer { public: - SimpleTokenizer(ErrorLogger& errorlogger, const char code[], bool cpp = true) + template + SimpleTokenizer(ErrorLogger& errorlogger, const char (&code)[size], bool cpp = true) : Tokenizer{s_settings, errorlogger} { if (!tokenize(code, cpp)) @@ -71,7 +72,20 @@ class SimpleTokenizer : public Tokenizer { * @param configuration E.g. "A" for code where "#ifdef A" is true * @return false if source code contains syntax errors */ - bool tokenize(const char code[], + template + bool tokenize(const char (&code)[size], + bool cpp = true, + const std::string &configuration = emptyString) + { + std::istringstream istr(code); + if (!list.createTokens(istr, cpp ? "test.cpp" : "test.c")) + return false; + + return simplifyTokens1(configuration); + } + + // TODO: get rid of this + bool tokenize(const std::string& code, bool cpp = true, const std::string &configuration = emptyString) { @@ -90,8 +104,8 @@ class SimpleTokenizer : public Tokenizer { class SimpleTokenList { public: - - explicit SimpleTokenList(const char code[], Standards::Language lang = Standards::Language::CPP) + template + explicit SimpleTokenList(const char (&code)[size], Standards::Language lang = Standards::Language::CPP) { std::istringstream iss(code); if (!list.createTokens(iss, lang)) diff --git a/test/test64bit.cpp b/test/test64bit.cpp index 68c3b34b129..0b04ee6146a 100644 --- a/test/test64bit.cpp +++ b/test/test64bit.cpp @@ -40,7 +40,8 @@ class Test64BitPortability : public TestFixture { } #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testassert.cpp b/test/testassert.cpp index af77d8d4d55..98e90a0e77a 100644 --- a/test/testassert.cpp +++ b/test/testassert.cpp @@ -31,7 +31,8 @@ class TestAssert : public TestFixture { const Settings settings = settingsBuilder().severity(Severity::warning).build(); #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testastutils.cpp b/test/testastutils.cpp index c68e5344b5e..01d66d01614 100644 --- a/test/testastutils.cpp +++ b/test/testastutils.cpp @@ -50,7 +50,8 @@ class TestAstUtils : public TestFixture { } #define findLambdaEndToken(...) findLambdaEndToken_(__FILE__, __LINE__, __VA_ARGS__) - bool findLambdaEndToken_(const char* file, int line, const char code[], const char pattern[] = nullptr, bool checkNext = true) { + template + bool findLambdaEndToken_(const char* file, int line, const char (&code)[size], const char pattern[] = nullptr, bool checkNext = true) { const Settings settings; SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -88,7 +89,8 @@ class TestAstUtils : public TestFixture { } #define findLambdaStartToken(code) findLambdaStartToken_(code, __FILE__, __LINE__) - bool findLambdaStartToken_(const char code[], const char* file, int line) { + template + bool findLambdaStartToken_(const char (&code)[size], const char* file, int line) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const tokStart = (::findLambdaStartToken)(tokenizer.list.back()); @@ -119,7 +121,8 @@ class TestAstUtils : public TestFixture { } #define isNullOperand(code) isNullOperand_(code, __FILE__, __LINE__) - bool isNullOperand_(const char code[], const char* file, int line) { + template + bool isNullOperand_(const char (&code)[size], const char* file, int line) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); return (::isNullOperand)(tokenizer.tokens()); @@ -139,7 +142,8 @@ class TestAstUtils : public TestFixture { } #define isReturnScope(code, offset) isReturnScope_(code, offset, __FILE__, __LINE__) - bool isReturnScope_(const char code[], int offset, const char* file, int line) { + template + bool isReturnScope_(const char (&code)[size], int offset, const char* file, int line) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const tok = (offset < 0) @@ -168,7 +172,8 @@ class TestAstUtils : public TestFixture { } #define isSameExpression(...) isSameExpression_(__FILE__, __LINE__, __VA_ARGS__) - bool isSameExpression_(const char* file, int line, const char code[], const char tokStr1[], const char tokStr2[], bool cpp) { + template + bool isSameExpression_(const char* file, int line, const char (&code)[size], const char tokStr1[], const char tokStr2[], bool cpp) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), tokStr1, strlen(tokStr1)); @@ -215,7 +220,8 @@ class TestAstUtils : public TestFixture { } #define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__) - bool isVariableChanged_(const char code[], const char startPattern[], const char endPattern[], const char* file, int line) { + template + bool isVariableChanged_(const char (&code)[size], const char startPattern[], const char endPattern[], const char* file, int line) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern, strlen(startPattern)); @@ -248,7 +254,8 @@ class TestAstUtils : public TestFixture { } #define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__) - bool isVariableChangedByFunctionCall_(const char code[], const char pattern[], bool *inconclusive, const char* file, int line) { + template + bool isVariableChangedByFunctionCall_(const char (&code)[size], const char pattern[], bool *inconclusive, const char* file, int line) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern); @@ -258,132 +265,158 @@ class TestAstUtils : public TestFixture { } void isVariableChangedByFunctionCallTest() { - const char *code; - bool inconclusive; - - // #8271 - template method - code = "void f(int x) {\n" - " a(x);\n" - "}"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); - ASSERT_EQUALS(true, inconclusive); - - code = "int f(int x) {\n" - "return int(x);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(int* p);\n" - "void f(int x) {\n" - " return g(&x);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(const int* p);\n" - "void f(int x) {\n" - " return g(&x);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(int** pp);\n" - "void f(int* p) {\n" - " return g(&p);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "p ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(int* const* pp);\n" - "void f(int* p) {\n" - " return g(&p);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "p ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(int a[2]);\n" - "void f() {\n" - " int b[2] = {};\n" - " return g(b);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(const int a[2]);\n" - "void f() {\n" - " int b[2] = {};\n" - " return g(b);\n" - "}\n"; - inconclusive = false; - TODO_ASSERT_EQUALS(false, true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(std::array a);\n" - "void f() {\n" - " std::array b = {};\n" - " return g(b);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(std::array& a);\n" - "void f() {\n" - " std::array b = {};\n" - " return g(b);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(const std::array& a);\n" - "void f() {\n" - " std::array b = {};\n" - " return g(b);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "void g(std::array* p);\n" - "void f() {\n" - " std::array b = {};\n" - " return g(&b);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "struct S {};\n" - "void g(S);\n" - "void f(S* s) {\n" - " g(*s);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "s ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); - - code = "struct S {};\n" - "void g(const S&);\n" - "void f(S* s) {\n" - " g(*s);\n" - "}\n"; - inconclusive = false; - ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "s ) ;", &inconclusive)); - ASSERT_EQUALS(false, inconclusive); + + { // #8271 - template method + const char code[] = "void f(int x) {\n" + " a(x);\n" + "}"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); + ASSERT_EQUALS(true, inconclusive); + } + + { + const char code[] = "int f(int x) {\n" + "return int(x);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(int* p);\n" + "void f(int x) {\n" + " return g(&x);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(const int* p);\n" + "void f(int x) {\n" + " return g(&x);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(int** pp);\n" + "void f(int* p) {\n" + " return g(&p);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "p ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(int* const* pp);\n" + "void f(int* p) {\n" + " return g(&p);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "p ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(int a[2]);\n" + "void f() {\n" + " int b[2] = {};\n" + " return g(b);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(const int a[2]);\n" + "void f() {\n" + " int b[2] = {};\n" + " return g(b);\n" + "}\n"; + bool inconclusive = false; + TODO_ASSERT_EQUALS(false, true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(std::array a);\n" + "void f() {\n" + " std::array b = {};\n" + " return g(b);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(std::array& a);\n" + "void f() {\n" + " std::array b = {};\n" + " return g(b);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(const std::array& a);\n" + "void f() {\n" + " std::array b = {};\n" + " return g(b);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "void g(std::array* p);\n" + "void f() {\n" + " std::array b = {};\n" + " return g(&b);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(true, isVariableChangedByFunctionCall(code, "b ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "struct S {};\n" + "void g(S);\n" + "void f(S* s) {\n" + " g(*s);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "s ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } + + { + const char code[] = "struct S {};\n" + "void g(const S&);\n" + "void f(S* s) {\n" + " g(*s);\n" + "}\n"; + bool inconclusive = false; + ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "s ) ;", &inconclusive)); + ASSERT_EQUALS(false, inconclusive); + } } #define isExpressionChanged(code, var, startPattern, endPattern) \ isExpressionChanged_(code, var, startPattern, endPattern, __FILE__, __LINE__) - bool isExpressionChanged_(const char code[], + template + bool isExpressionChanged_(const char (&code)[size], const char var[], const char startPattern[], const char endPattern[], @@ -420,7 +453,8 @@ class TestAstUtils : public TestFixture { } #define nextAfterAstRightmostLeaf(code, parentPattern, rightPattern) nextAfterAstRightmostLeaf_(code, parentPattern, rightPattern, __FILE__, __LINE__) - bool nextAfterAstRightmostLeaf_(const char code[], const char parentPattern[], const char rightPattern[], const char* file, int line) { + template + bool nextAfterAstRightmostLeaf_(const char (&code)[size], const char parentPattern[], const char rightPattern[], const char* file, int line) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * tok = Token::findsimplematch(tokenizer.tokens(), parentPattern, strlen(parentPattern)); @@ -443,7 +477,8 @@ class TestAstUtils : public TestFixture { enum class Result {False, True, Fail}; - Result isUsedAsBool(const char code[], const char pattern[]) { + template + Result isUsedAsBool(const char (&code)[size], const char pattern[]) { SimpleTokenizer tokenizer(settingsDefault, *this); if (!tokenizer.tokenize(code)) return Result::Fail; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 3527b744727..dabc9ec3380 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -30,7 +30,8 @@ class TestAutoVariables : public TestFixture { const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).library("std.cfg").library("qt.cfg").build(); #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool inconclusive = true, bool cpp = true) { + template + void check_(const char* file, int line, const char (&code)[size], bool inconclusive = true, bool cpp = true) { const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build(); // Tokenize.. diff --git a/test/testbool.cpp b/test/testbool.cpp index b0fb5b25a5d..f1de81d942e 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -74,7 +74,8 @@ class TestBool : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool cpp = true) { + template + void check_(const char* file, int line, const char (&code)[size], bool cpp = true) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); diff --git a/test/testboost.cpp b/test/testboost.cpp index b3641b1a49f..b52d5c7330d 100644 --- a/test/testboost.cpp +++ b/test/testboost.cpp @@ -34,7 +34,8 @@ class TestBoost : public TestFixture { } #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index b3d0bfaa628..f0ee3128010 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -40,7 +40,8 @@ class TestBufferOverrun : public TestFixture { /*const*/ Settings settings0 = settingsBuilder().library("std.cfg").severity(Severity::warning).severity(Severity::style).severity(Severity::portability).build(); #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool cpp = true) { + template + void check_(const char* file, int line, const char (&code)[size], bool cpp = true) { const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).build(); // Tokenize.. @@ -51,7 +52,8 @@ class TestBufferOverrun : public TestFixture { runChecks(tokenizer, this); } - void check_(const char* file, int line, const char code[], const Settings &settings, bool cpp = true) { + template + void check_(const char* file, int line, const char (&code)[size], const Settings &settings, bool cpp = true) { SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); @@ -59,6 +61,18 @@ class TestBufferOverrun : public TestFixture { runChecks(tokenizer, this); } + // TODO: get rid of this + void check_(const char* file, int line, const std::string& code) { + const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).build(); + + // Tokenize.. + SimpleTokenizer tokenizer(settings, *this); + ASSERT_LOC(tokenizer.tokenize(code), file, line); + + // Check for buffer overruns.. + runChecks(tokenizer, this); + } + #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp") { @@ -918,11 +932,11 @@ class TestBufferOverrun : public TestFixture { void array_index_24() { // ticket #1492 and #1539 const std::string charMaxPlusOne(settings0.platform.defaultSign == 'u' ? "256" : "128"); - check(("void f(char n) {\n" - " int a[n];\n" // n <= CHAR_MAX - " a[-1] = 0;\n" // negative index - " a[" + charMaxPlusOne + "] = 0;\n" // 128/256 > CHAR_MAX - "}\n").c_str()); + check("void f(char n) {\n" + " int a[n];\n" // n <= CHAR_MAX + " a[-1] = 0;\n" // negative index + " a[" + charMaxPlusOne + "] = 0;\n" // 128/256 > CHAR_MAX + "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Array 'a[" + charMaxPlusOne + "]' accessed at index -1, which is out of bounds.\n" "[test.cpp:4]: (error) Array 'a[" + charMaxPlusOne + "]' accessed at index " + charMaxPlusOne + ", which is out of bounds.\n", errout_str()); @@ -5158,7 +5172,8 @@ class TestBufferOverrun : public TestFixture { } #define ctu(code) ctu_(code, __FILE__, __LINE__) - void ctu_(const char code[], const char* file, int line) { + template + void ctu_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index 09e1befbce9..bbb10b81218 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -37,7 +37,8 @@ class TestCharVar : public TestFixture { } #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testclass.cpp b/test/testclass.cpp index accc15af5b5..f8713702e41 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -248,7 +248,8 @@ class TestClass : public TestFixture { } #define checkCopyCtorAndEqOperator(code) checkCopyCtorAndEqOperator_(code, __FILE__, __LINE__) - void checkCopyCtorAndEqOperator_(const char code[], const char* file, int line) { + template + void checkCopyCtorAndEqOperator_(const char (&code)[size], const char* file, int line) { const Settings settings = settingsBuilder().severity(Severity::warning).build(); // Tokenize.. @@ -350,7 +351,8 @@ class TestClass : public TestFixture { } #define checkExplicitConstructors(code) checkExplicitConstructors_(code, __FILE__, __LINE__) - void checkExplicitConstructors_(const char code[], const char* file, int line) { + template + void checkExplicitConstructors_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -498,7 +500,8 @@ class TestClass : public TestFixture { } #define checkDuplInheritedMembers(code) checkDuplInheritedMembers_(code, __FILE__, __LINE__) - void checkDuplInheritedMembers_(const char code[], const char* file, int line) { + template + void checkDuplInheritedMembers_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -714,7 +717,8 @@ class TestClass : public TestFixture { } #define checkCopyConstructor(code) checkCopyConstructor_(code, __FILE__, __LINE__) - void checkCopyConstructor_(const char code[], const char* file, int line) { + template + void checkCopyConstructor_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings3, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1157,7 +1161,8 @@ class TestClass : public TestFixture { // Check that operator Equal returns reference to this #define checkOpertorEqRetRefThis(code) checkOpertorEqRetRefThis_(code, __FILE__, __LINE__) - void checkOpertorEqRetRefThis_(const char code[], const char* file, int line) { + template + void checkOpertorEqRetRefThis_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1627,7 +1632,8 @@ class TestClass : public TestFixture { // Check that operator Equal checks for assignment to self #define checkOpertorEqToSelf(code) checkOpertorEqToSelf_(code, __FILE__, __LINE__) - void checkOpertorEqToSelf_(const char code[], const char* file, int line) { + template + void checkOpertorEqToSelf_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -2582,7 +2588,8 @@ class TestClass : public TestFixture { // Check that base classes have virtual destructors #define checkVirtualDestructor(...) checkVirtualDestructor_(__FILE__, __LINE__, __VA_ARGS__) - void checkVirtualDestructor_(const char* file, int line, const char code[], bool inconclusive = false) { + template + void checkVirtualDestructor_(const char* file, int line, const char (&code)[size], bool inconclusive = false) { const Settings s = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).severity(Severity::warning).build(); // Tokenize.. @@ -2915,12 +2922,14 @@ class TestClass : public TestFixture { #define checkNoMemset(...) checkNoMemset_(__FILE__, __LINE__, __VA_ARGS__) - void checkNoMemset_(const char* file, int line, const char code[]) { + template + void checkNoMemset_(const char* file, int line, const char (&code)[size]) { const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).library("std.cfg").library("posix.cfg").build(); checkNoMemset_(file, line, code, settings); } - void checkNoMemset_(const char* file, int line, const char code[], const Settings &settings) { + template + void checkNoMemset_(const char* file, int line, const char (&code)[size], const Settings &settings) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -3565,7 +3574,8 @@ class TestClass : public TestFixture { } #define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__) - void checkThisSubtraction_(const char code[], const char* file, int line) { + template + void checkThisSubtraction_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -3594,7 +3604,8 @@ class TestClass : public TestFixture { } #define checkConst(...) checkConst_(__FILE__, __LINE__, __VA_ARGS__) - void checkConst_(const char* file, int line, const char code[], const Settings *s = nullptr, bool inconclusive = true) { + template + void checkConst_(const char* file, int line, const char (&code)[size], const Settings *s = nullptr, bool inconclusive = true) { const Settings settings = settingsBuilder(s ? *s : settings0).certainty(Certainty::inconclusive, inconclusive).build(); // Tokenize.. @@ -7514,7 +7525,8 @@ class TestClass : public TestFixture { } #define checkInitializerListOrder(code) checkInitializerListOrder_(code, __FILE__, __LINE__) - void checkInitializerListOrder_(const char code[], const char* file, int line) { + template + void checkInitializerListOrder_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings2, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -7664,7 +7676,8 @@ class TestClass : public TestFixture { } #define checkInitializationListUsage(code) checkInitializationListUsage_(code, __FILE__, __LINE__) - void checkInitializationListUsage_(const char code[], const char* file, int line) { + template + void checkInitializationListUsage_(const char (&code)[size], const char* file, int line) { // Check.. const Settings settings = settingsBuilder().severity(Severity::performance).build(); @@ -7875,7 +7888,8 @@ class TestClass : public TestFixture { #define checkSelfInitialization(code) checkSelfInitialization_(code, __FILE__, __LINE__) - void checkSelfInitialization_(const char code[], const char* file, int line) { + template + void checkSelfInitialization_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -7982,7 +7996,8 @@ class TestClass : public TestFixture { #define checkVirtualFunctionCall(...) checkVirtualFunctionCall_(__FILE__, __LINE__, __VA_ARGS__) - void checkVirtualFunctionCall_(const char* file, int line, const char code[], bool inconclusive = true) { + template + void checkVirtualFunctionCall_(const char* file, int line, const char (&code)[size], bool inconclusive = true) { // Check.. const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).certainty(Certainty::inconclusive, inconclusive).build(); @@ -8326,7 +8341,8 @@ class TestClass : public TestFixture { #define checkOverride(code) checkOverride_(code, __FILE__, __LINE__) - void checkOverride_(const char code[], const char* file, int line) { + template + void checkOverride_(const char (&code)[size], const char* file, int line) { const Settings settings = settingsBuilder().severity(Severity::style).build(); // Tokenize.. @@ -8707,7 +8723,8 @@ class TestClass : public TestFixture { } #define checkUnsafeClassRefMember(code) checkUnsafeClassRefMember_(code, __FILE__, __LINE__) - void checkUnsafeClassRefMember_(const char code[], const char* file, int line) { + template + void checkUnsafeClassRefMember_(const char (&code)[size], const char* file, int line) { /*const*/ Settings settings = settingsBuilder().severity(Severity::warning).build(); settings.safeChecks.classes = true; @@ -8727,7 +8744,8 @@ class TestClass : public TestFixture { #define checkThisUseAfterFree(code) checkThisUseAfterFree_(code, __FILE__, __LINE__) - void checkThisUseAfterFree_(const char code[], const char* file, int line) { + template + void checkThisUseAfterFree_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -8926,7 +8944,8 @@ class TestClass : public TestFixture { #define getFileInfo(code) getFileInfo_(code, __FILE__, __LINE__) - void getFileInfo_(const char code[], const char* file, int line) { + template + void getFileInfo_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -8944,7 +8963,8 @@ class TestClass : public TestFixture { } #define checkReturnByReference(...) checkReturnByReference_(__FILE__, __LINE__, __VA_ARGS__) - void checkReturnByReference_(const char* file, int line, const char code[]) { + template + void checkReturnByReference_(const char* file, int line, const char (&code)[size]) { const Settings settings = settingsBuilder().severity(Severity::performance).library("std.cfg").build(); std::vector files(1, "test.cpp"); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 1a535ff0bf0..8005c400e50 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -538,7 +538,8 @@ class TestCondition : public TestFixture { ASSERT_EQUALS("", errout_str()); } - void checkPureFunction_(const char code[], const char* file, int line) { + template + void checkPureFunction_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 52ddf9615d0..6b265f6fe84 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -31,7 +31,8 @@ class TestConstructors : public TestFixture { const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build(); #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool inconclusive = false) { + template + void check_(const char* file, int line, const char (&code)[size], bool inconclusive = false) { const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build(); // Tokenize.. @@ -43,7 +44,8 @@ class TestConstructors : public TestFixture { checkClass.constructors(); } - void check_(const char* file, int line, const char code[], const Settings &s) { + template + void check_(const char* file, int line, const char (&code)[size], const Settings &s) { // Tokenize.. SimpleTokenizer tokenizer(s, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index b1c5a2b087c..9e10e1e2290 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -56,7 +56,8 @@ class TestExceptionSafety : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool inconclusive = false, const Settings *s = nullptr) { + template + void check_(const char* file, int line, const char (&code)[size], bool inconclusive = false, const Settings *s = nullptr) { const Settings settings1 = settingsBuilder(s ? *s : settings).certainty(Certainty::inconclusive, inconclusive).build(); // Tokenize.. diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 487eefc2fc8..b622207f1e2 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -104,7 +104,8 @@ class TestFunctions : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool cpp = true, const Settings* settings_ = nullptr) { + template + void check_(const char* file, int line, const char (&code)[size], bool cpp = true, const Settings* settings_ = nullptr) { if (!settings_) settings_ = &settings; diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 1850d594345..93b2d12ee78 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -271,7 +271,8 @@ class TestGarbage : public TestFixture { } #define checkCodeInternal(code, filename) checkCodeInternal_(code, filename, __FILE__, __LINE__) - std::string checkCode(const char code[], bool cpp = true) { + template + std::string checkCode(const char (&code)[size], bool cpp = true) { // double the tests - run each example as C as well as C++ // run alternate check first. It should only ensure stability - so we catch exceptions here. @@ -282,7 +283,8 @@ class TestGarbage : public TestFixture { return checkCodeInternal(code, cpp); } - std::string checkCodeInternal_(const char code[], bool cpp, const char* file, int line) { + template + std::string checkCodeInternal_(const char (&code)[size], bool cpp, const char* file, int line) { // tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); @@ -296,7 +298,8 @@ class TestGarbage : public TestFixture { } #define getSyntaxError(code) getSyntaxError_(code, __FILE__, __LINE__) - std::string getSyntaxError_(const char code[], const char* file, int line) { + template + std::string getSyntaxError_(const char (&code)[size], const char* file, int line) { SimpleTokenizer tokenizer(settings, *this); try { ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1241,9 +1244,9 @@ class TestGarbage : public TestFixture { } void garbageCode152() { // happened in travis, originally from llvm clang code - const char* code = "template \n" - "static std::string foo(char *Bla) {\n" - " while (Bla[1] && Bla[1] != ',') }\n"; + const char code[] = "template \n" + "static std::string foo(char *Bla) {\n" + " while (Bla[1] && Bla[1] != ',') }\n"; checkCode(code); ignore_errout(); // we are not interested in the output } @@ -1290,19 +1293,23 @@ class TestGarbage : public TestFixture { } void garbageValueFlow() { - // #6089 - const char* code = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n" - "{\n" - " (foo(s, , 2, , , 5, , 7)) abort()\n" - "}\n"; - ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX); + { // #6089 + const char code[] = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n" + "{\n" + " (foo(s, , 2, , , 5, , 7)) abort()\n" + "}\n"; + ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX); + } - // 6122 survive garbage code - code = "; { int i ; for ( i = 0 ; = 123 ; ) - ; }"; - ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX); + { // 6122 survive garbage code + const char code[] = "; { int i ; for ( i = 0 ; = 123 ; ) - ; }"; + ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX); + } - code = "void f1() { for (int n = 0 n < 10 n++); }"; - ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX); + { + const char code[] = "void f1() { for (int n = 0 n < 10 n++); }"; + ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX); + } } void garbageSymbolDatabase() { diff --git a/test/testinternal.cpp b/test/testinternal.cpp index 403cd3647ef..519e1aca892 100644 --- a/test/testinternal.cpp +++ b/test/testinternal.cpp @@ -47,7 +47,8 @@ class TestInternal : public TestFixture { } #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testio.cpp b/test/testio.cpp index 41ad948ea64..5c70a4deac8 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -89,7 +89,8 @@ class TestIO : public TestFixture { }; #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char* code, const CheckOptions& options = make_default_obj()) { + template + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { // TODO: using dedicated Settings (i.e. copying it) object causes major slowdown settings1.severity.setEnabled(Severity::portability, options.portability); settings1.certainty.setEnabled(Certainty::inconclusive, options.inconclusive); @@ -825,7 +826,8 @@ class TestIO : public TestFixture { #define TEST_PRINTF_ERR_AKA_CPP(format, requiredType, actualType, akaType) \ TEST_PRINTF_ERR_AKA_("test.cpp", format, requiredType, actualType, akaType) - void testFormatStrNoWarn(const char *filename, unsigned int linenr, const char* code, + template + void testFormatStrNoWarn(const char *filename, unsigned int linenr, const char (&code)[size], bool cpp = false) { check(code, dinit(CheckOptions, $.inconclusive = true, $.platform = Platform::Type::Unix32, $.onlyFormatStr = true, $.cpp = cpp)); assertEquals(filename, linenr, emptyString, errout_str()); @@ -837,8 +839,9 @@ class TestIO : public TestFixture { assertEquals(filename, linenr, emptyString, errout_str()); } + template void testFormatStrWarn(const char *filename, unsigned int linenr, - const char* code, const char* testScanfErrString, + const char (&code)[size], const char* testScanfErrString, bool cpp = false) { check(code, dinit(CheckOptions, $.inconclusive = true, $.platform = Platform::Type::Unix32, $.onlyFormatStr = true, $.cpp = cpp)); assertEquals(filename, linenr, testScanfErrString, errout_str()); @@ -850,8 +853,9 @@ class TestIO : public TestFixture { assertEquals(filename, linenr, testScanfErrString, errout_str()); } + template void testFormatStrWarnAka(const char *filename, unsigned int linenr, - const char* code, const char* testScanfErrAkaString, const char* testScanfErrAkaWin64String, + const char (&code)[size], const char* testScanfErrAkaString, const char* testScanfErrAkaWin64String, bool cpp = false) { check(code, dinit(CheckOptions, $.inconclusive = true, $.portability = true, $.platform = Platform::Type::Unix32, $.onlyFormatStr = true, $.cpp = cpp)); assertEquals(filename, linenr, testScanfErrAkaString, errout_str()); @@ -863,8 +867,9 @@ class TestIO : public TestFixture { assertEquals(filename, linenr, testScanfErrAkaWin64String, errout_str()); } + template void testFormatStrWarnAkaWin64(const char *filename, unsigned int linenr, - const char* code, const char* testScanfErrAkaWin64String, + const char (&code)[size], const char* testScanfErrAkaWin64String, bool cpp = false) { check(code, dinit(CheckOptions, $.inconclusive = true, $.portability = true, $.platform = Platform::Type::Unix32, $.onlyFormatStr = true, $.cpp = cpp)); assertEquals(filename, linenr, emptyString, errout_str()); @@ -876,8 +881,9 @@ class TestIO : public TestFixture { assertEquals(filename, linenr, testScanfErrAkaWin64String, errout_str()); } + template void testFormatStrWarnAkaWin32(const char *filename, unsigned int linenr, - const char* code, const char* testScanfErrAkaString, + const char (&code)[size], const char* testScanfErrAkaString, bool cpp = false) { check(code, dinit(CheckOptions, $.inconclusive = true, $.portability = true, $.platform = Platform::Type::Unix32, $.onlyFormatStr = true, $.cpp = cpp)); assertEquals(filename, linenr, testScanfErrAkaString, errout_str()); @@ -2126,7 +2132,7 @@ class TestIO : public TestFixture { ASSERT_EQUALS("", errout_str()); { - const char * code = "void g() {\n" // #5348 + const char code[] = "void g() {\n" // #5348 " size_t s1;\n" " ptrdiff_t s2;\n" " ssize_t s3;\n" diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 1075774f51f..06266aa2dd8 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -228,7 +228,8 @@ class TestLeakAutoVar : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool cpp = false, const Settings *s = nullptr) { + template + void check_(const char* file, int line, const char (&code)[size], bool cpp = false, const Settings *s = nullptr) { const Settings settings1 = settingsBuilder(s ? *s : settings).checkLibrary().build(); // Tokenize.. @@ -239,7 +240,8 @@ class TestLeakAutoVar : public TestFixture { runChecks(tokenizer, this); } - void check_(const char* file, int line, const char code[], const Settings & s) { + template + void check_(const char* file, int line, const char (&code)[size], const Settings & s) { const Settings settings0 = settingsBuilder(s).checkLibrary().build(); // Tokenize.. @@ -2838,27 +2840,31 @@ class TestLeakAutoVar : public TestFixture { } void configuration3() { - const char * code = "void f() {\n" - " char *p = malloc(10);\n" - " if (set_data(p)) { }\n" - "}"; - check(code); - ASSERT_EQUALS("[test.c:4]: (information) --check-library: Function set_data() should have / configuration\n", errout_str()); - check(code, true); - ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function set_data() should have / configuration\n", errout_str()); - - code = "void f() {\n" - " char *p = malloc(10);\n" - " if (set_data(p)) { return; }\n" - "}"; - check(code); - ASSERT_EQUALS("[test.c:3]: (information) --check-library: Function set_data() should have / configuration\n" - "[test.c:4]: (information) --check-library: Function set_data() should have / configuration\n" - , errout_str()); - check(code, true); - ASSERT_EQUALS("[test.cpp:3]: (information) --check-library: Function set_data() should have / configuration\n" - "[test.cpp:4]: (information) --check-library: Function set_data() should have / configuration\n" - , errout_str()); + { + const char code[] = "void f() {\n" + " char *p = malloc(10);\n" + " if (set_data(p)) { }\n" + "}"; + check(code); + ASSERT_EQUALS("[test.c:4]: (information) --check-library: Function set_data() should have / configuration\n", errout_str()); + check(code, true); + ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function set_data() should have / configuration\n", errout_str()); + } + + { + const char code[] = "void f() {\n" + " char *p = malloc(10);\n" + " if (set_data(p)) { return; }\n" + "}"; + check(code); + ASSERT_EQUALS("[test.c:3]: (information) --check-library: Function set_data() should have / configuration\n" + "[test.c:4]: (information) --check-library: Function set_data() should have / configuration\n" + , errout_str()); + check(code, true); + ASSERT_EQUALS("[test.cpp:3]: (information) --check-library: Function set_data() should have / configuration\n" + "[test.cpp:4]: (information) --check-library: Function set_data() should have / configuration\n" + , errout_str()); + } } void configuration4() { @@ -3131,7 +3137,8 @@ class TestLeakAutoVarStrcpy : public TestFixture { private: const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build(); - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -3234,7 +3241,8 @@ class TestLeakAutoVarWindows : public TestFixture { private: const Settings settings = settingsBuilder().library("windows.cfg").build(); - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, false), file, line); @@ -3305,7 +3313,8 @@ class TestLeakAutoVarPosix : public TestFixture { private: const Settings settings = settingsBuilder().library("std.cfg").library("posix.cfg").build(); - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 639e6fd6cc7..a9c8c34c465 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -39,7 +39,8 @@ class TestMemleak : private TestFixture { } #define functionReturnType(code) functionReturnType_(code, __FILE__, __LINE__) - CheckMemoryLeak::AllocType functionReturnType_(const char code[], const char* file, int line) { + template + CheckMemoryLeak::AllocType functionReturnType_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -113,7 +114,8 @@ class TestMemleakInFunction : public TestFixture { const Settings settings = settingsBuilder().library("std.cfg").library("posix.cfg").build(); #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -438,7 +440,8 @@ class TestMemleakInClass : public TestFixture { * Tokenize and execute leak check for given code * @param code Source code */ - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1641,7 +1644,8 @@ class TestMemleakStructMember : public TestFixture { private: const Settings settings = settingsBuilder().library("std.cfg").library("posix.cfg").build(); - void check_(const char* file, int line, const char code[], bool cpp = true) { + template + void check_(const char* file, int line, const char (&code)[size], bool cpp = true) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); @@ -2248,7 +2252,8 @@ class TestMemleakNoVar : public TestFixture { private: const Settings settings = settingsBuilder().certainty(Certainty::inconclusive).severity(Severity::warning).library("std.cfg").library("posix.cfg").build(); - void check_(const char* file, int line, const char code[]) { + template + void check_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 78e39e3af53..e2bf2c82dc3 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -176,7 +176,8 @@ class TestNullPointer : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool inconclusive = false, bool cpp = true) { + template + void check_(const char* file, int line, const char (&code)[size], bool inconclusive = false, bool cpp = true) { const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build(); // Tokenize.. @@ -188,7 +189,8 @@ class TestNullPointer : public TestFixture { } #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) - void checkP_(const char* file, int line, const char code[]) { + template + void checkP_(const char* file, int line, const char (&code)[size]) { const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, false).build(); std::vector files(1, "test.cpp"); @@ -4478,7 +4480,8 @@ class TestNullPointer : public TestFixture { } #define ctu(code) ctu_(code, __FILE__, __LINE__) - void ctu_(const char code[], const char* file, int line) { + template + void ctu_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testother.cpp b/test/testother.cpp index bc9ea2861e9..65164d7ddc9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -294,7 +294,8 @@ class TestOther : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool cpp = true, bool inconclusive = true, bool runSimpleChecks=true, bool verbose=false, Settings* settings = nullptr) { + template + void check_(const char* file, int line, const char (&code)[size], bool cpp = true, bool inconclusive = true, bool runSimpleChecks=true, bool verbose=false, Settings* settings = nullptr) { if (!settings) { settings = &_settings; } @@ -317,12 +318,14 @@ class TestOther : public TestFixture { (void)runSimpleChecks; // TODO Remove this } - void check_(const char* file, int line, const char code[], Settings *s) { + template + void check_(const char* file, int line, const char (&code)[size], Settings *s) { check_(file, line, code, true, true, true, false, s); } #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) - void checkP_(const char* file, int line, const char code[], const char *filename = "test.cpp") { + template + void checkP_(const char* file, int line, const char (&code)[size], const char *filename = "test.cpp") { Settings* settings = &_settings; settings->severity.enable(Severity::style); settings->severity.enable(Severity::warning); @@ -343,7 +346,8 @@ class TestOther : public TestFixture { runChecks(tokenizer, this); } - void checkInterlockedDecrement(const char code[]) { + template + void checkInterlockedDecrement(const char (&code)[size]) { /*const*/ Settings settings = settingsBuilder().platform(Platform::Type::Win32A).build(); check(code, true, false, true, false, &settings); @@ -1748,7 +1752,8 @@ class TestOther : public TestFixture { } #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) - void checkOldStylePointerCast_(const char code[], const char* file, int line) { + template + void checkOldStylePointerCast_(const char (&code)[size], const char* file, int line) { // #5560 - set c++03 const Settings settings = settingsBuilder().severity(Severity::style).cpp(Standards::CPP03).build(); @@ -1967,7 +1972,8 @@ class TestOther : public TestFixture { } #define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__) - void checkInvalidPointerCast_(const char* file, int line, const char code[], bool portability = true, bool inconclusive = false) { + template + void checkInvalidPointerCast_(const char* file, int line, const char (&code)[size], bool portability = true, bool inconclusive = false) { /*const*/ Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability, portability).certainty(Certainty::inconclusive, inconclusive).build(); settings.platform.defaultSign = 's'; @@ -8864,16 +8870,16 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); // #5618 - const char* code5618 = "class Token {\n" - "public:\n" - " const std::string& str();\n" - "};\n" - "void simplifyArrayAccessSyntax() {\n" - " for (Token *tok = list.front(); tok; tok = tok->next()) {\n" - " const std::string temp = tok->str();\n" - " tok->str(tok->strAt(2));\n" - " }\n" - "}"; + const char code5618[] = "class Token {\n" + "public:\n" + " const std::string& str();\n" + "};\n" + "void simplifyArrayAccessSyntax() {\n" + " for (Token *tok = list.front(); tok; tok = tok->next()) {\n" + " const std::string temp = tok->str();\n" + " tok->str(tok->strAt(2));\n" + " }\n" + "}"; check(code5618, true, true); ASSERT_EQUALS("", errout_str()); check(code5618, true, false); diff --git a/test/testpostfixoperator.cpp b/test/testpostfixoperator.cpp index dba5f9319da..b33f33b5e41 100644 --- a/test/testpostfixoperator.cpp +++ b/test/testpostfixoperator.cpp @@ -30,7 +30,8 @@ class TestPostfixOperator : public TestFixture { const Settings settings = settingsBuilder().severity(Severity::performance).build(); #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 01f5e31c274..bb7155382e8 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -308,7 +308,8 @@ class TestSimplifyTemplate : public TestFixture { } #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) - std::string tok_(const char* file, int line, const char code[], bool debugwarnings = false, Platform::Type type = Platform::Type::Native) { + template + std::string tok_(const char* file, int line, const char (&code)[size], bool debugwarnings = false, Platform::Type type = Platform::Type::Native) { const Settings settings1 = settingsBuilder(settings).library("std.cfg").debugwarnings(debugwarnings).platform(type).build(); SimpleTokenizer tokenizer(settings1, *this); @@ -5774,7 +5775,8 @@ class TestSimplifyTemplate : public TestFixture { } #define instantiateMatch(code, numberOfArguments, patternAfter) instantiateMatch_(code, numberOfArguments, patternAfter, __FILE__, __LINE__) - bool instantiateMatch_(const char code[], const std::size_t numberOfArguments, const char patternAfter[], const char* file, int line) { + template + bool instantiateMatch_(const char (&code)[size], const std::size_t numberOfArguments, const char patternAfter[], const char* file, int line) { SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 747afcf955d..6af24e798af 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -164,7 +164,8 @@ class TestSimplifyTokens : public TestFixture { } #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) - std::string tok_(const char* file, int line, const char code[], bool cpp = true, Platform::Type type = Platform::Type::Native) { + template + std::string tok_(const char* file, int line, const char (&code)[size], bool cpp = true, Platform::Type type = Platform::Type::Native) { const Settings settings = settingsBuilder(settings0).platform(type).build(); SimpleTokenizer tokenizer(settings, *this); @@ -174,7 +175,8 @@ class TestSimplifyTokens : public TestFixture { } #define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool expand = true, Platform::Type platform = Platform::Type::Native, bool cpp = true, bool cpp11 = true) { + template + std::string tokenizeAndStringify_(const char* file, int linenr, const char (&code)[size], bool expand = true, Platform::Type platform = Platform::Type::Native, bool cpp = true, bool cpp11 = true) { const Settings settings = settingsBuilder(settings1).debugwarnings().platform(platform).cpp(cpp11 ? Standards::CPP11 : Standards::CPP03).build(); // tokenize.. @@ -187,7 +189,8 @@ class TestSimplifyTokens : public TestFixture { } #define tokenizeDebugListing(...) tokenizeDebugListing_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeDebugListing_(const char* file, int line, const char code[], bool cpp = true) { + template + std::string tokenizeDebugListing_(const char* file, int line, const char (&code)[size], bool cpp = true) { SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); @@ -1218,54 +1221,60 @@ class TestSimplifyTokens : public TestFixture { } void simplifyStructDecl9() { - const char *code{}, *exp{}; - code = "enum E : int;\n" // #12588 - "void f() {}\n" - "namespace {\n" - " struct S {\n" - " explicit S(int i) : m(i) {}\n" - " int m;\n" - " };\n" - "}\n" - "void g() { S s(0); }\n"; - exp = "enum E : int ; " - "void f ( ) { } " - "namespace { " - "struct S { " - "explicit S ( int i ) : m ( i ) { } " - "int m ; " - "} ; " - "} " - "void g ( ) { S s ( 0 ) ; }"; - ASSERT_EQUALS(exp, tok(code)); - - code = "struct S {\n" // 12595 - " explicit S() {\n" - " e = E1;\n" - " }\n" - " enum { E1 } e = E1;\n" - "};\n"; - exp = "struct S { " - "explicit S ( ) { e = E1 ; } " - "enum Anonymous0 { E1 } ; " - "enum Anonymous0 e ; " - "e = E1 ; " - "} ;"; - ASSERT_EQUALS(exp, tok(code)); - - code = "struct S {\n" - " explicit S() {\n" - " e = E1;\n" - " }\n" - " enum : std::uint8_t { E1 } e = E1;\n" - "};\n"; - exp = "struct S { " - "explicit S ( ) { e = E1 ; } " - "enum Anonymous0 : std :: uint8_t { E1 } ; " - "enum Anonymous0 e ; " - "e = E1 ; " - "} ;"; - ASSERT_EQUALS(exp, tok(code)); + const char *exp{}; + { + const char code[] = "enum E : int;\n" // #12588 + "void f() {}\n" + "namespace {\n" + " struct S {\n" + " explicit S(int i) : m(i) {}\n" + " int m;\n" + " };\n" + "}\n" + "void g() { S s(0); }\n"; + exp = "enum E : int ; " + "void f ( ) { } " + "namespace { " + "struct S { " + "explicit S ( int i ) : m ( i ) { } " + "int m ; " + "} ; " + "} " + "void g ( ) { S s ( 0 ) ; }"; + ASSERT_EQUALS(exp, tok(code)); + } + + { + const char code[] = "struct S {\n" // 12595 + " explicit S() {\n" + " e = E1;\n" + " }\n" + " enum { E1 } e = E1;\n" + "};\n"; + exp = "struct S { " + "explicit S ( ) { e = E1 ; } " + "enum Anonymous0 { E1 } ; " + "enum Anonymous0 e ; " + "e = E1 ; " + "} ;"; + ASSERT_EQUALS(exp, tok(code)); + } + + { + const char code[] = "struct S {\n" + " explicit S() {\n" + " e = E1;\n" + " }\n" + " enum : std::uint8_t { E1 } e = E1;\n" + "};\n"; + exp = "struct S { " + "explicit S ( ) { e = E1 ; } " + "enum Anonymous0 : std :: uint8_t { E1 } ; " + "enum Anonymous0 e ; " + "e = E1 ; " + "} ;"; + ASSERT_EQUALS(exp, tok(code)); + } } void removeUnwantedKeywords() { @@ -1511,7 +1520,8 @@ class TestSimplifyTokens : public TestFixture { } #define simplifyKnownVariables(code) simplifyKnownVariables_(code, __FILE__, __LINE__) - std::string simplifyKnownVariables_(const char code[], const char* file, int line) { + template + std::string simplifyKnownVariables_(const char (&code)[size], const char* file, int line) { SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 44abd19da3c..158434f2245 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -239,7 +239,8 @@ class TestSimplifyTypedef : public TestFixture { } #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) - std::string tok_(const char* file, int line, const char code[], bool simplify = true, Platform::Type type = Platform::Type::Native, bool debugwarnings = true) { + template + std::string tok_(const char* file, int line, const char (&code)[size], bool simplify = true, Platform::Type type = Platform::Type::Native, bool debugwarnings = true) { // show warnings about unhandled typedef const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings(debugwarnings).platform(type).build(); SimpleTokenizer tokenizer(settings, *this); @@ -275,7 +276,8 @@ class TestSimplifyTypedef : public TestFixture { } #define checkSimplifyTypedef(code) checkSimplifyTypedef_(code, __FILE__, __LINE__) - void checkSimplifyTypedef_(const char code[], const char* file, int line) { + template + void checkSimplifyTypedef_(const char (&code)[size], const char* file, int line) { // Tokenize.. // show warnings about unhandled typedef const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings().build(); @@ -479,9 +481,8 @@ class TestSimplifyTypedef : public TestFixture { } void carray4() { - const char* code{}; - code = "typedef int arr[12];\n" // #12019 - "void foo() { arr temp = {0}; }\n"; + const char code[] = "typedef int arr[12];\n" // #12019 + "void foo() { arr temp = {0}; }\n"; ASSERT_EQUALS("void foo ( ) { int temp [ 12 ] = { 0 } ; }", tok(code)); } @@ -3369,160 +3370,179 @@ class TestSimplifyTypedef : public TestFixture { } void simplifyTypedef145() { - // #11634 - const char* code{}; - code = "int typedef i;\n" - "i main() {}\n"; - ASSERT_EQUALS("int main ( ) { }", tok(code)); - - code = "struct {} typedef S;\n" - "void f() {\n" - " S();\n" - "}\n"; - ASSERT_EQUALS("struct S { } ; void f ( ) { struct S ( ) ; }", tok(code)); - - code = "struct {} typedef S;\n" // don't crash - "S();\n"; - ASSERT_EQUALS("struct S { } ; struct S ( ) ;", tok(code)); - - // #11693 - code = "typedef unsigned char unsigned char;\n" // don't hang - "void f(char);\n"; - ASSERT_EQUALS("void f ( char ) ;", tok(code)); - - // #10796 - code = "typedef unsigned a[7];\n" - "void f(unsigned N) {\n" - " a** p = new a * [N];\n" - "}\n"; - TODO_ASSERT_EQUALS("does not compile", "void f ( int N ) { int ( * * p ) [ 7 ] ; p = new int ( * ) [ N ] [ 7 ] ; }", tok(code)); - - // #11772 - code = "typedef t;\n" // don't crash on implicit int - "void g() {\n" - " sizeof(t);\n" - "}\n"; - ASSERT_EQUALS("void g ( ) { sizeof ( t ) ; }", tok(code)); // TODO: handle implicit int - ignore_errout(); // we do not care about the output + { // #11634 + const char code[] = "int typedef i;\n" + "i main() {}\n"; + ASSERT_EQUALS("int main ( ) { }", tok(code)); + } - code = "typedef t[3];\n" - "void g() {\n" - " sizeof(t);\n" - "}\n"; - ASSERT_EQUALS("void g ( ) { sizeof ( t ) ; }", tok(code)); // TODO: handle implicit int - ignore_errout(); // we do not care about the output + { + const char code[] = "struct {} typedef S;\n" + "void f() {\n" + " S();\n" + "}\n"; + ASSERT_EQUALS("struct S { } ; void f ( ) { struct S ( ) ; }", tok(code)); + } + + { + const char code[] = "struct {} typedef S;\n" // don't crash + "S();\n"; + ASSERT_EQUALS("struct S { } ; struct S ( ) ;", tok(code)); + } + + { // #11693 + const char code[] = "typedef unsigned char unsigned char;\n" // don't hang + "void f(char);\n"; + ASSERT_EQUALS("void f ( char ) ;", tok(code)); + } + + { // #10796 + const char code[] = "typedef unsigned a[7];\n" + "void f(unsigned N) {\n" + " a** p = new a * [N];\n" + "}\n"; + TODO_ASSERT_EQUALS("does not compile", "void f ( int N ) { int ( * * p ) [ 7 ] ; p = new int ( * ) [ N ] [ 7 ] ; }", tok(code)); + } + + { // #11772 + const char code[] = "typedef t;\n" // don't crash on implicit int + "void g() {\n" + " sizeof(t);\n" + "}\n"; + ASSERT_EQUALS("void g ( ) { sizeof ( t ) ; }", tok(code)); // TODO: handle implicit int + ignore_errout(); // we do not care about the output + } + + { + const char code[] = "typedef t[3];\n" + "void g() {\n" + " sizeof(t);\n" + "}\n"; + ASSERT_EQUALS("void g ( ) { sizeof ( t ) ; }", tok(code)); // TODO: handle implicit int + ignore_errout(); // we do not care about the output + } } void simplifyTypedef146() { - const char* code{}; - code = "namespace N {\n" // #11978 - " typedef int T;\n" - " struct C {\n" - " C(T*);\n" - " void* p;\n" - " };\n" - "}\n" - "N::C::C(T*) : p(nullptr) {}\n"; - ASSERT_EQUALS("namespace N { struct C { C ( int * ) ; void * p ; } ; } N :: C :: C ( int * ) : p ( nullptr ) { }", tok(code)); - - code = "namespace N {\n" // #11986 - " typedef char U;\n" - " typedef int V;\n" - " struct S {};\n" - " struct T { void f(V*); };\n" - "}\n" - "void N::T::f(V*) {}\n" - "namespace N {}\n"; - ASSERT_EQUALS("namespace N { struct S { } ; struct T { void f ( int * ) ; } ; } void N :: T :: f ( int * ) { }", tok(code)); - - code = "namespace N {\n" // #12008 - " typedef char U;\n" - " typedef int V;\n" - " struct S {\n" - " S(V* v);\n" - " };\n" - "}\n" - "void f() {}\n" - "N::S::S(V* v) {}\n" - "namespace N {}\n"; - ASSERT_EQUALS("namespace N { struct S { S ( int * v ) ; } ; } void f ( ) { } N :: S :: S ( int * v ) { }", tok(code)); + { + const char code[] = "namespace N {\n" // #11978 + " typedef int T;\n" + " struct C {\n" + " C(T*);\n" + " void* p;\n" + " };\n" + "}\n" + "N::C::C(T*) : p(nullptr) {}\n"; + ASSERT_EQUALS("namespace N { struct C { C ( int * ) ; void * p ; } ; } N :: C :: C ( int * ) : p ( nullptr ) { }", tok(code)); + } + + { + const char code[] = "namespace N {\n" // #11986 + " typedef char U;\n" + " typedef int V;\n" + " struct S {};\n" + " struct T { void f(V*); };\n" + "}\n" + "void N::T::f(V*) {}\n" + "namespace N {}\n"; + ASSERT_EQUALS("namespace N { struct S { } ; struct T { void f ( int * ) ; } ; } void N :: T :: f ( int * ) { }", tok(code)); + } + + { + const char code[] = "namespace N {\n" // #12008 + " typedef char U;\n" + " typedef int V;\n" + " struct S {\n" + " S(V* v);\n" + " };\n" + "}\n" + "void f() {}\n" + "N::S::S(V* v) {}\n" + "namespace N {}\n"; + ASSERT_EQUALS("namespace N { struct S { S ( int * v ) ; } ; } void f ( ) { } N :: S :: S ( int * v ) { }", tok(code)); + } } void simplifyTypedef147() { - const char* code{}; - code = "namespace N {\n" // #12014 - " template\n" - " struct S {};\n" - "}\n" - "typedef N::S S;\n" - "namespace N {\n" - " template\n" - " struct U {\n" - " S operator()() {\n" - " return {};\n" - " }\n" - " };\n" - "}\n"; + const char code[] = "namespace N {\n" // #12014 + " template\n" + " struct S {};\n" + "}\n" + "typedef N::S S;\n" + "namespace N {\n" + " template\n" + " struct U {\n" + " S operator()() {\n" + " return {};\n" + " }\n" + " };\n" + "}\n"; ASSERT_EQUALS("namespace N { template < typename T > struct S { } ; } namespace N { template < typename T > struct U { S < T > operator() ( ) { return { } ; } } ; }", tok(code)); } void simplifyTypedef148() { - const char* code{}; - code = "typedef int& R;\n" // #12166 - "R r = i;\n"; + const char code[] = "typedef int& R;\n" // #12166 + "R r = i;\n"; ASSERT_EQUALS("int & r = i ;", tok(code)); } void simplifyTypedef149() { // #12218 - const char* code{}; - code = "namespace N {\n" - " typedef struct S {} S;\n" - "}\n" - "void g(int);\n" - "void f() {\n" - " g(sizeof(struct N::S));\n" - "}\n"; - ASSERT_EQUALS("namespace N { struct S { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( struct N :: S ) ) ; }", tok(code)); - - code = "namespace N {\n" - " typedef class C {} C;\n" - "}\n" - "void g(int);\n" - "void f() {\n" - " g(sizeof(class N::C));\n" - "}\n"; - ASSERT_EQUALS("namespace N { class C { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( class N :: C ) ) ; }", tok(code)); - - code = "namespace N {\n" - " typedef union U {} U;\n" - "}\n" - "void g(int);\n" - "void f() {\n" - " g(sizeof(union N::U));\n" - "}\n"; - ASSERT_EQUALS("namespace N { union U { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( union N :: U ) ) ; }", tok(code)); - - code = "namespace N {\n" - " typedef enum E {} E;\n" - "}\n" - "void g(int);\n" - "void f() {\n" - " g(sizeof(enum N::E));\n" - "}\n"; - ASSERT_EQUALS("namespace N { enum E { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( enum N :: E ) ) ; }", tok(code)); + { + const char code[] = "namespace N {\n" + " typedef struct S {} S;\n" + "}\n" + "void g(int);\n" + "void f() {\n" + " g(sizeof(struct N::S));\n" + "}\n"; + ASSERT_EQUALS("namespace N { struct S { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( struct N :: S ) ) ; }", tok(code)); + } + + { + const char code[] = "namespace N {\n" + " typedef class C {} C;\n" + "}\n" + "void g(int);\n" + "void f() {\n" + " g(sizeof(class N::C));\n" + "}\n"; + ASSERT_EQUALS("namespace N { class C { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( class N :: C ) ) ; }", tok(code)); + } + + { + const char code[] = "namespace N {\n" + " typedef union U {} U;\n" + "}\n" + "void g(int);\n" + "void f() {\n" + " g(sizeof(union N::U));\n" + "}\n"; + ASSERT_EQUALS("namespace N { union U { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( union N :: U ) ) ; }", tok(code)); + } + + { + const char code[] = "namespace N {\n" + " typedef enum E {} E;\n" + "}\n" + "void g(int);\n" + "void f() {\n" + " g(sizeof(enum N::E));\n" + "}\n"; + ASSERT_EQUALS("namespace N { enum E { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( enum N :: E ) ) ; }", tok(code)); + } } void simplifyTypedef150() { // #12475 - const char* code{}, *exp{}; - code = "struct S {\n" - " std::vector const& h(int);\n" - "};\n" - "typedef std::vector const& (S::* func_t)(int);\n" - "void g(func_t, int);\n" - "void f() {\n" - " g(func_t(&S::h), 5);\n" - "}\n"; + const char *exp{}; + const char code[] = "struct S {\n" + " std::vector const& h(int);\n" + "};\n" + "typedef std::vector const& (S::* func_t)(int);\n" + "void g(func_t, int);\n" + "void f() {\n" + " g(func_t(&S::h), 5);\n" + "}\n"; exp = "struct S { " "const std :: vector < int > & h ( int ) ; " "} ; " @@ -3534,11 +3554,11 @@ class TestSimplifyTypedef : public TestFixture { } void simplifyTypedef151() { - const char* code{}, *exp{}; - code = "namespace N {\n" // #12597 - " typedef int T[10];\n" - " const T* f() { return static_cast(nullptr); }\n" - "}\n"; + const char *exp{}; + const char code[] = "namespace N {\n" // #12597 + " typedef int T[10];\n" + " const T* f() { return static_cast(nullptr); }\n" + "}\n"; exp = "namespace N { " "const int ( * f ( ) ) [ 10 ] { return static_cast < const int ( * ) [ 10 ] > ( nullptr ) ; } " "}"; @@ -3546,14 +3566,14 @@ class TestSimplifyTypedef : public TestFixture { } void simplifyTypedef152() { - const char* code{}, *exp{}; - code = "namespace O { struct T {}; }\n" - "typedef O::T S;\n" - "namespace M {\n" - " enum E { E0, S = 1 };\n" - " enum class F : ::std::int8_t { F0, S = 1 };\n" - "}\n" - "namespace N { enum { G0, S = 1 }; }\n"; + const char *exp{}; + const char code[] = "namespace O { struct T {}; }\n" + "typedef O::T S;\n" + "namespace M {\n" + " enum E { E0, S = 1 };\n" + " enum class F : ::std::int8_t { F0, S = 1 };\n" + "}\n" + "namespace N { enum { G0, S = 1 }; }\n"; exp = "namespace O { struct T { } ; } " "namespace M { " "enum E { E0 , S = 1 } ; " @@ -3565,15 +3585,15 @@ class TestSimplifyTypedef : public TestFixture { } void simplifyTypedef153() { - const char* code{}, *exp{}; // #12647 - code = "typedef unsigned long X;\n" - "typedef unsigned long X;\n" - "typedef X Y;\n" - "typedef X* Yp;\n" - "typedef X Ya[3];\n" - "Y y;\n" - "Yp yp;\n" - "Ya ya;\n"; + const char *exp{}; // #12647 + const char code[] = "typedef unsigned long X;\n" + "typedef unsigned long X;\n" + "typedef X Y;\n" + "typedef X* Yp;\n" + "typedef X Ya[3];\n" + "Y y;\n" + "Yp yp;\n" + "Ya ya;\n"; exp = "long y ; long * yp ; long ya [ 3 ] ;"; ASSERT_EQUALS(exp, tok(code)); } diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index da8318c64a2..e39214e88ab 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -95,7 +95,8 @@ class TestSimplifyUsing : public TestFixture { } #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) - std::string tok_(const char* file, int line, const char code[], Platform::Type type = Platform::Type::Native, bool debugwarnings = true, bool preprocess = false) { + template + std::string tok_(const char* file, int line, const char (&code)[size], Platform::Type type = Platform::Type::Native, bool debugwarnings = true, bool preprocess = false) { const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings(debugwarnings).platform(type).build(); if (preprocess) { diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index f88b3565bae..ba11c1ff6af 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -48,7 +48,8 @@ class TestSizeof : public TestFixture { } #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -58,7 +59,8 @@ class TestSizeof : public TestFixture { } #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) - void checkP_(const char* file, int line, const char code[]) { + template + void checkP_(const char* file, int line, const char (&code)[size]) { std::vector files(1, "test.cpp"); Tokenizer tokenizer(settings, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); diff --git a/test/teststl.cpp b/test/teststl.cpp index f32d4b40713..d1ce9d58806 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -177,7 +177,8 @@ class TestStl : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], const bool inconclusive = false, const Standards::cppstd_t cppstandard = Standards::CPPLatest) { + template + void check_(const char* file, int line, const char (&code)[size], const bool inconclusive = false, const Standards::cppstd_t cppstandard = Standards::CPPLatest) { const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).cpp(cppstandard).build(); // Tokenize.. @@ -188,12 +189,19 @@ class TestStl : public TestFixture { runChecks(tokenizer, this); } - void check_(const char* file, int line, const std::string& code, const bool inconclusive = false) { - check_(file, line, code.c_str(), inconclusive); + // TODO: get rid of this + void check_(const char* file, int line, const std::string& code) { + // Tokenize.. + SimpleTokenizer tokenizer(settings, *this); + + ASSERT_LOC(tokenizer.tokenize(code), file, line); + + runChecks(tokenizer, this); } #define checkNormal(code) checkNormal_(code, __FILE__, __LINE__) - void checkNormal_(const char code[], const char* file, int line) { + template + void checkNormal_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -3716,148 +3724,176 @@ class TestStl : public TestFixture { void size1() { - const char* code = "struct Fred {\n" - " void foo();\n" - " std::list x;\n" - "};\n" - "void Fred::foo()\n" - "{\n" - " if (x.size() == 0) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:7]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "struct Fred {\n" + " void foo();\n" + " std::list x;\n" + "};\n" + "void Fred::foo()\n" + "{\n" + " if (x.size() == 0) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:7]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "std::list x;\n" - "void f()\n" - "{\n" - " if (x.size() == 0) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "std::list x;\n" + "void f()\n" + "{\n" + " if (x.size() == 0) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (x.size() == 0) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() == 0) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (0 == x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (0 == x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (x.size() != 0) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() != 0) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (0 != x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (0 != x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (x.size() > 0) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() > 0) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (0 < x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (0 < x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (x.size() >= 1) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() >= 1) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (x.size() < 1) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() < 1) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (1 <= x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (1 <= x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (1 > x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (1 > x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " if (!x.size()) {}\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " if (!x.size()) {}\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } check("void f()\n" "{\n" @@ -3866,25 +3902,29 @@ class TestStl : public TestFixture { "}"); ASSERT_EQUALS("", errout_str()); - code ="void f()\n" - "{\n" - " std::list x;\n" - " fun(!x.size());\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] ="void f()\n" + "{\n" + " std::list x;\n" + " fun(!x.size());\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } - code = "void f()\n" - "{\n" - " std::list x;\n" - " fun(a && x.size());\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + { + const char code[] = "void f()\n" + "{\n" + " std::list x;\n" + " fun(a && x.size());\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } check("void f() {\n" // #4039 " std::list x;\n" @@ -3906,17 +3946,17 @@ class TestStl : public TestFixture { } void size2() { - const char* code = "struct Fred {\n" - " std::list x;\n" - "};\n" - "struct Wilma {\n" - " Fred f;\n" - " void foo();\n" - "};\n" - "void Wilma::foo()\n" - "{\n" - " if (f.x.size() == 0) {}\n" - "}"; + const char code[] = "struct Fred {\n" + " std::list x;\n" + "};\n" + "struct Wilma {\n" + " Fred f;\n" + " void foo();\n" + "};\n" + "void Wilma::foo()\n" + "{\n" + " if (f.x.size() == 0) {}\n" + "}"; check(code, false, Standards::CPP03); ASSERT_EQUALS( "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n" @@ -3928,42 +3968,46 @@ class TestStl : public TestFixture { } void size3() { - const char* code = "namespace N {\n" - " class Zzz {\n" - " public:\n" - " std::list x;\n" - " };\n" - "}\n" - "using namespace N;\n" - "Zzz * zzz;\n" - "int main() {\n" - " if (zzz->x.size() > 0) { }\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS( - "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n" - "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n", // duplicate - errout_str()); + { + const char code[] = "namespace N {\n" + " class Zzz {\n" + " public:\n" + " std::list x;\n" + " };\n" + "}\n" + "using namespace N;\n" + "Zzz * zzz;\n" + "int main() {\n" + " if (zzz->x.size() > 0) { }\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS( + "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n" + "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n", // duplicate + errout_str()); + } - code = "namespace N {\n" - " class Zzz {\n" - " public:\n" - " std::list x;\n" - " };\n" - "}\n" - "using namespace N;\n" - "int main() {\n" - " Zzz * zzz;\n" - " if (zzz->x.size() > 0) { }\n" - "}"; - check(code, false, Standards::CPP03); - ASSERT_EQUALS( - "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n" - "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n", // duplicate - errout_str()); + { + const char code[] = "namespace N {\n" + " class Zzz {\n" + " public:\n" + " std::list x;\n" + " };\n" + "}\n" + "using namespace N;\n" + "int main() {\n" + " Zzz * zzz;\n" + " if (zzz->x.size() > 0) { }\n" + "}"; + check(code, false, Standards::CPP03); + ASSERT_EQUALS( + "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n" + "[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n", // duplicate + errout_str()); - check(code); - ASSERT_EQUALS("", errout_str()); + check(code); + ASSERT_EQUALS("", errout_str()); + } } void size4() { // #2652 - don't warn about vector/deque diff --git a/test/testsummaries.cpp b/test/testsummaries.cpp index 0915068f20b..fde4e51f5a8 100644 --- a/test/testsummaries.cpp +++ b/test/testsummaries.cpp @@ -35,7 +35,8 @@ class TestSummaries : public TestFixture { } #define createSummaries(...) createSummaries_(__FILE__, __LINE__, __VA_ARGS__) - std::string createSummaries_(const char* file, int line, const char code[], bool cpp = true) { + template + std::string createSummaries_(const char* file, int line, const char (&code)[size], bool cpp = true) { // tokenize.. SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 7edf21f4aba..2b37bf04486 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -74,7 +74,8 @@ class TestSymbolDatabase : public TestFixture { typetok = nullptr; } - static const SymbolDatabase* getSymbolDB_inner(SimpleTokenizer& tokenizer, const char* code, bool cpp) { + template + static const SymbolDatabase* getSymbolDB_inner(SimpleTokenizer& tokenizer, const char (&code)[size], bool cpp) { return tokenizer.tokenize(code, cpp) ? tokenizer.getSymbolDatabase() : nullptr; } @@ -93,7 +94,8 @@ class TestSymbolDatabase : public TestFixture { return tok->expressionString() + "@" + std::to_string(tok->exprId()); } - std::string testExprIdEqual(const char code[], + template + std::string testExprIdEqual(const char (&code)[size], const std::string& expr1, unsigned int exprline1, const std::string& expr2, @@ -120,7 +122,8 @@ class TestSymbolDatabase : public TestFixture { return ""; } - bool testExprIdNotEqual(const char code[], + template + bool testExprIdNotEqual(const char (&code)[size], const std::string& expr1, unsigned int exprline1, const std::string& expr2, @@ -2578,7 +2581,8 @@ class TestSymbolDatabase : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool debug = true, bool cpp = true, const Settings* pSettings = nullptr) { + template + void check_(const char* file, int line, const char (&code)[size], bool debug = true, bool cpp = true, const Settings* pSettings = nullptr) { // Check.. const Settings settings = settingsBuilder(pSettings ? *pSettings : settings1).debugwarnings(debug).build(); @@ -8719,7 +8723,8 @@ class TestSymbolDatabase : public TestFixture { } } #define typeOf(...) typeOf_(__FILE__, __LINE__, __VA_ARGS__) - std::string typeOf_(const char* file, int line, const char code[], const char pattern[], bool cpp = true, const Settings *settings = nullptr) { + template + std::string typeOf_(const char* file, int line, const char (&code)[size], const char pattern[], bool cpp = true, const Settings *settings = nullptr) { SimpleTokenizer tokenizer(settings ? *settings : settings2, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); const Token* tok; @@ -10367,210 +10372,258 @@ class TestSymbolDatabase : public TestFixture { void exprIds() { - const char* code; - - code = "int f(int a) {\n" - " return a +\n" - " a;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "a", 2U, "a", 3U)); - - code = "int f(int a, int b) {\n" - " return a +\n" - " b;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, "a", 2U, "b", 3U)); - - code = "int f(int a) {\n" - " int x = a++;\n" - " int y = a++;\n" - " return x + a;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, "++", 2U, "++", 3U)); - - code = "int f(int a) {\n" - " int x = a;\n" - " return x + a;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, "x", 3U, "a", 3U)); - - code = "int f(int a) {\n" - " int& x = a;\n" - " return x + a;\n" - "}\n"; - TODO_ASSERT_EQUALS("", "x@2 != a@1", testExprIdEqual(code, "x", 3U, "a", 3U)); - - code = "int f(int a) {\n" - " int& x = a;\n" - " return (x + 1) +\n" - " (a + 1);\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "+", 3U, "+", 4U)); - - code = "int& g(int& x) { return x; }\n" - "int f(int a) {\n" - " return (g(a) + 1) +\n" - " (a + 1);\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "+", 3U, "+", 4U)); - - code = "int f(int a, int b) {\n" - " int x = (b-a)-a;\n" - " int y = (b-a)-a;\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "- a ;", 2U, "- a ;", 3U)); - ASSERT_EQUALS("", testExprIdEqual(code, "- a )", 2U, "- a )", 3U)); - ASSERT_EQUALS(true, testExprIdNotEqual(code, "- a )", 2U, "- a ;", 3U)); - - code = "int f(int a, int b) {\n" - " int x = a-(b-a);\n" - " int y = a-(b-a);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "- ( b", 2U, "- ( b", 3U)); - ASSERT_EQUALS("", testExprIdEqual(code, "- a )", 2U, "- a )", 3U)); - ASSERT_EQUALS(true, testExprIdNotEqual(code, "- a )", 2U, "- ( b", 3U)); - - code = "void f(int a, int b) {\n" - " int x = (b+a)+a;\n" - " int y = a+(b+a);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "+ a ;", 2U, "+ ( b", 3U)); - ASSERT_EQUALS("", testExprIdEqual(code, "+ a ) +", 2U, "+ a ) ;", 3U)); - ASSERT_EQUALS(true, testExprIdNotEqual(code, "+ a ;", 2U, "+ a )", 3U)); - - code = "void f(int a, int b) {\n" - " int x = (b+a)+a;\n" - " int y = a+(a+b);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "+ a ;", 2U, "+ ( a", 3U)); - ASSERT_EQUALS("", testExprIdEqual(code, "+ a ) +", 2U, "+ b ) ;", 3U)); - ASSERT_EQUALS(true, testExprIdNotEqual(code, "+ a ;", 2U, "+ b", 3U)); - - code = "struct A { int x; };\n" - "void f(A a, int b) {\n" - " int x = (b-a.x)-a.x;\n" - " int y = (b-a.x)-a.x;\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "- a . x ;", 3U, "- a . x ;", 4U)); - ASSERT_EQUALS("", testExprIdEqual(code, "- a . x )", 3U, "- a . x )", 4U)); - - code = "struct A { int x; };\n" - "void f(A a, int b) {\n" - " int x = a.x-(b-a.x);\n" - " int y = a.x-(b-a.x);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "- ( b", 3U, "- ( b", 4U)); - ASSERT_EQUALS("", testExprIdEqual(code, "- a . x )", 3U, "- a . x )", 4U)); - - code = "struct A { int x; };\n" - "void f(A a) {\n" - " int x = a.x;\n" - " int y = a.x;\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, ". x", 3U, ". x", 4U)); - - code = "struct A { int x; };\n" - "void f(A a, A b) {\n" - " int x = a.x;\n" - " int y = b.x;\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, ". x", 3U, ". x", 4U)); - - code = "struct A { int y; };\n" - "struct B { A x; }\n" - "void f(B a) {\n" - " int x = a.x.y;\n" - " int y = a.x.y;\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, ". x . y", 4U, ". x . y", 5U)); - ASSERT_EQUALS("", testExprIdEqual(code, ". y", 4U, ". y", 5U)); - - code = "struct A { int y; };\n" - "struct B { A x; }\n" - "void f(B a, B b) {\n" - " int x = a.x.y;\n" - " int y = b.x.y;\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, ". x . y", 4U, ". x . y", 5U)); - ASSERT_EQUALS(true, testExprIdNotEqual(code, ". y", 4U, ". y", 5U)); - - code = "struct A { int g(); };\n" - "struct B { A x; }\n" - "void f(B a) {\n" - " int x = a.x.g();\n" - " int y = a.x.g();\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, ". x . g ( )", 4U, ". x . g ( )", 5U)); - ASSERT_EQUALS("", testExprIdEqual(code, ". g ( )", 4U, ". g ( )", 5U)); - - code = "struct A { int g(int, int); };\n" - "struct B { A x; }\n" - "void f(B a, int b, int c) {\n" - " int x = a.x.g(b, c);\n" - " int y = a.x.g(b, c);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, ". x . g ( b , c )", 4U, ". x . g ( b , c )", 5U)); - ASSERT_EQUALS("", testExprIdEqual(code, ". g ( b , c )", 4U, ". g ( b , c )", 5U)); - - code = "int g();\n" - "void f() {\n" - " int x = g();\n" - " int y = g();\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); - - code = "struct A { int g(); };\n" - "void f() {\n" - " int x = A::g();\n" - " int y = A::g();\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); - - code = "int g();\n" - "void f(int a, int b) {\n" - " int x = g(a, b);\n" - " int y = g(a, b);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); - - code = "struct A { int g(); };\n" - "void f() {\n" - " int x = A::g(a, b);\n" - " int y = A::g(a, b);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); - - code = "int g();\n" - "void f(int a, int b) {\n" - " int x = g(a, b);\n" - " int y = g(b, a);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, "(", 3U, "(", 4U)); - - code = "struct A { int g(); };\n" - "void f() {\n" - " int x = A::g(a, b);\n" - " int y = A::g(b, a);\n" - " return x + y;\n" - "}\n"; - ASSERT_EQUALS(true, testExprIdNotEqual(code, "(", 3U, "(", 4U)); + { + const char code[] = "int f(int a) {\n" + " return a +\n" + " a;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "a", 2U, "a", 3U)); + } + + { + const char code[] = "int f(int a, int b) {\n" + " return a +\n" + " b;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, "a", 2U, "b", 3U)); + } + + { + const char code[] = "int f(int a) {\n" + " int x = a++;\n" + " int y = a++;\n" + " return x + a;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, "++", 2U, "++", 3U)); + } + + { + const char code[] = "int f(int a) {\n" + " int x = a;\n" + " return x + a;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, "x", 3U, "a", 3U)); + } + + { + const char code[] = "int f(int a) {\n" + " int& x = a;\n" + " return x + a;\n" + "}\n"; + TODO_ASSERT_EQUALS("", "x@2 != a@1", testExprIdEqual(code, "x", 3U, "a", 3U)); + } + + { + const char code[] = "int f(int a) {\n" + " int& x = a;\n" + " return (x + 1) +\n" + " (a + 1);\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "+", 3U, "+", 4U)); + } + + { + const char code[] = "int& g(int& x) { return x; }\n" + "int f(int a) {\n" + " return (g(a) + 1) +\n" + " (a + 1);\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "+", 3U, "+", 4U)); + } + + { + const char code[] = "int f(int a, int b) {\n" + " int x = (b-a)-a;\n" + " int y = (b-a)-a;\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "- a ;", 2U, "- a ;", 3U)); + ASSERT_EQUALS("", testExprIdEqual(code, "- a )", 2U, "- a )", 3U)); + ASSERT_EQUALS(true, testExprIdNotEqual(code, "- a )", 2U, "- a ;", 3U)); + } + + { + const char code[] = "int f(int a, int b) {\n" + " int x = a-(b-a);\n" + " int y = a-(b-a);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "- ( b", 2U, "- ( b", 3U)); + ASSERT_EQUALS("", testExprIdEqual(code, "- a )", 2U, "- a )", 3U)); + ASSERT_EQUALS(true, testExprIdNotEqual(code, "- a )", 2U, "- ( b", 3U)); + } + + { + const char code[] = "void f(int a, int b) {\n" + " int x = (b+a)+a;\n" + " int y = a+(b+a);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "+ a ;", 2U, "+ ( b", 3U)); + ASSERT_EQUALS("", testExprIdEqual(code, "+ a ) +", 2U, "+ a ) ;", 3U)); + ASSERT_EQUALS(true, testExprIdNotEqual(code, "+ a ;", 2U, "+ a )", 3U)); + } + + { + const char code[] = "void f(int a, int b) {\n" + " int x = (b+a)+a;\n" + " int y = a+(a+b);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "+ a ;", 2U, "+ ( a", 3U)); + ASSERT_EQUALS("", testExprIdEqual(code, "+ a ) +", 2U, "+ b ) ;", 3U)); + ASSERT_EQUALS(true, testExprIdNotEqual(code, "+ a ;", 2U, "+ b", 3U)); + } + + { + const char code[] = "struct A { int x; };\n" + "void f(A a, int b) {\n" + " int x = (b-a.x)-a.x;\n" + " int y = (b-a.x)-a.x;\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "- a . x ;", 3U, "- a . x ;", 4U)); + ASSERT_EQUALS("", testExprIdEqual(code, "- a . x )", 3U, "- a . x )", 4U)); + } + + { + const char code[] = "struct A { int x; };\n" + "void f(A a, int b) {\n" + " int x = a.x-(b-a.x);\n" + " int y = a.x-(b-a.x);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "- ( b", 3U, "- ( b", 4U)); + ASSERT_EQUALS("", testExprIdEqual(code, "- a . x )", 3U, "- a . x )", 4U)); + } + + { + const char code[] = "struct A { int x; };\n" + "void f(A a) {\n" + " int x = a.x;\n" + " int y = a.x;\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, ". x", 3U, ". x", 4U)); + } + + { + const char code[] = "struct A { int x; };\n" + "void f(A a, A b) {\n" + " int x = a.x;\n" + " int y = b.x;\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, ". x", 3U, ". x", 4U)); + } + + { + const char code[] = "struct A { int y; };\n" + "struct B { A x; }\n" + "void f(B a) {\n" + " int x = a.x.y;\n" + " int y = a.x.y;\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, ". x . y", 4U, ". x . y", 5U)); + ASSERT_EQUALS("", testExprIdEqual(code, ". y", 4U, ". y", 5U)); + } + + { + const char code[] = "struct A { int y; };\n" + "struct B { A x; }\n" + "void f(B a, B b) {\n" + " int x = a.x.y;\n" + " int y = b.x.y;\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, ". x . y", 4U, ". x . y", 5U)); + ASSERT_EQUALS(true, testExprIdNotEqual(code, ". y", 4U, ". y", 5U)); + } + + { + const char code[] = "struct A { int g(); };\n" + "struct B { A x; }\n" + "void f(B a) {\n" + " int x = a.x.g();\n" + " int y = a.x.g();\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, ". x . g ( )", 4U, ". x . g ( )", 5U)); + ASSERT_EQUALS("", testExprIdEqual(code, ". g ( )", 4U, ". g ( )", 5U)); + } + + { + const char code[] = "struct A { int g(int, int); };\n" + "struct B { A x; }\n" + "void f(B a, int b, int c) {\n" + " int x = a.x.g(b, c);\n" + " int y = a.x.g(b, c);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, ". x . g ( b , c )", 4U, ". x . g ( b , c )", 5U)); + ASSERT_EQUALS("", testExprIdEqual(code, ". g ( b , c )", 4U, ". g ( b , c )", 5U)); + } + + { + const char code[] = "int g();\n" + "void f() {\n" + " int x = g();\n" + " int y = g();\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); + } + + { + const char code[] = "struct A { int g(); };\n" + "void f() {\n" + " int x = A::g();\n" + " int y = A::g();\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); + } + + { + const char code[] = "int g();\n" + "void f(int a, int b) {\n" + " int x = g(a, b);\n" + " int y = g(a, b);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); + } + + { + const char code[] = "struct A { int g(); };\n" + "void f() {\n" + " int x = A::g(a, b);\n" + " int y = A::g(a, b);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS("", testExprIdEqual(code, "(", 3U, "(", 4U)); + } + + { + const char code[] = "int g();\n" + "void f(int a, int b) {\n" + " int x = g(a, b);\n" + " int y = g(b, a);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, "(", 3U, "(", 4U)); + } + + { + const char code[] = "struct A { int g(); };\n" + "void f() {\n" + " int x = A::g(a, b);\n" + " int y = A::g(b, a);\n" + " return x + y;\n" + "}\n"; + ASSERT_EQUALS(true, testExprIdNotEqual(code, "(", 3U, "(", 4U)); + } } }; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5dacd24e065..a2005b255fe 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -460,7 +460,8 @@ class TestTokenizer : public TestFixture { } #define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool expand = true, Platform::Type platform = Platform::Type::Native, + template + std::string tokenizeAndStringify_(const char* file, int linenr, const char (&code)[size], bool expand = true, Platform::Type platform = Platform::Type::Native, bool cpp = true, Standards::cppstd_t cppstd = Standards::CPP11, Standards::cstd_t cstd = Standards::C11) { const Settings settings = settingsBuilder(settings1).debugwarnings().cpp(cppstd).c(cstd).platform(platform).build(); @@ -473,8 +474,22 @@ class TestTokenizer : public TestFixture { return ""; } + // TODO: get rid of this + std::string tokenizeAndStringify_(const char* file, int linenr, const std::string& code) { + const Settings settings = settingsBuilder(settings1).debugwarnings().cpp(Standards::CPP11).c(Standards::C11).build(); + + // tokenize.. + SimpleTokenizer tokenizer(settings, *this); + ASSERT_LOC(tokenizer.tokenize(code), file, linenr); + + if (tokenizer.tokens()) + return tokenizer.tokens()->stringifyList(false, true, false, true, false, nullptr, nullptr); + return ""; + } + #define tokenizeAndStringifyWindows(...) tokenizeAndStringifyWindows_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeAndStringifyWindows_(const char* file, int linenr, const char code[], bool expand = true, Platform::Type platform = Platform::Type::Native, bool cpp = true, bool cpp11 = true) { + template + std::string tokenizeAndStringifyWindows_(const char* file, int linenr, const char (&code)[size], bool expand = true, Platform::Type platform = Platform::Type::Native, bool cpp = true, bool cpp11 = true) { const Settings settings = settingsBuilder(settings_windows).debugwarnings().cpp(cpp11 ? Standards::CPP11 : Standards::CPP03).platform(platform).build(); // tokenize.. @@ -486,7 +501,8 @@ class TestTokenizer : public TestFixture { return ""; } - std::string tokenizeAndStringify_(const char* file, int line, const char code[], const Settings &settings, bool cpp = true) { + template + std::string tokenizeAndStringify_(const char* file, int line, const char (&code)[size], const Settings &settings, bool cpp = true) { // tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); @@ -496,7 +512,8 @@ class TestTokenizer : public TestFixture { } #define tokenizeDebugListing(...) tokenizeDebugListing_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeDebugListing_(const char* file, int line, const char code[], bool cpp = true) { + template + std::string tokenizeDebugListing_(const char* file, int line, const char (&code)[size], bool cpp = true) { const Settings settings = settingsBuilder(settings0).c(Standards::C89).cpp(Standards::CPP03).build(); SimpleTokenizer tokenizer(settings, *this); @@ -714,13 +731,13 @@ class TestTokenizer : public TestFixture { // #5884 - Avoid left shift of negative integer value. void tokenize32() { // Do not simplify negative integer left shifts. - const char * code = "void f ( ) { int max_x ; max_x = -10000 << 16 ; }"; + const char code[] = "void f ( ) { int max_x ; max_x = -10000 << 16 ; }"; ASSERT_EQUALS(code, tokenizeAndStringify(code)); } // #5780 Various crashes on valid template code in Tokenizer::setVarId() void tokenize33() { - const char * code = "template> struct vector {};\n" + const char code[] = "template> struct vector {};\n" "void z() {\n" " vector VI;\n" "}\n"; @@ -2574,17 +2591,21 @@ class TestTokenizer : public TestFixture { } void vardecl29() { // #9282 - const char* code = "double f1() noexcept, f2(double) noexcept;"; - ASSERT_EQUALS("double f1 ( ) noexcept ( true ) ; double f2 ( double ) noexcept ( true ) ;", - tokenizeAndStringify(code)); + { + const char code[] = "double f1() noexcept, f2(double) noexcept;"; + ASSERT_EQUALS("double f1 ( ) noexcept ( true ) ; double f2 ( double ) noexcept ( true ) ;", + tokenizeAndStringify(code)); + } - code = "class C {\n" - " double f1() const noexcept, f2 (double) const noexcept;\n" - "};\n"; - ASSERT_EQUALS("class C {\n" - "double f1 ( ) const noexcept ( true ) ; double f2 ( double ) const noexcept ( true ) ;\n" - "} ;", - tokenizeAndStringify(code)); + { + const char code[] = "class C {\n" + " double f1() const noexcept, f2 (double) const noexcept;\n" + "};\n"; + ASSERT_EQUALS("class C {\n" + "double f1 ( ) const noexcept ( true ) ; double f2 ( double ) const noexcept ( true ) ;\n" + "} ;", + tokenizeAndStringify(code)); + } } void vardecl30() { @@ -2596,11 +2617,15 @@ class TestTokenizer : public TestFixture { } void vardecl31() { - const char code1[] = "void foo() { int (*fptr)() = 0; }"; - ASSERT_EQUALS("void foo ( ) { int ( * fptr ) ( ) ; fptr = 0 ; }", tokenizeAndStringify(code1)); + { + const char code[] = "void foo() { int (*fptr)() = 0; }"; + ASSERT_EQUALS("void foo ( ) { int ( * fptr ) ( ) ; fptr = 0 ; }", tokenizeAndStringify(code)); + } - const char code2[] = "void foo() { int (*fptr)(int) = 0; }"; - ASSERT_EQUALS("void foo ( ) { int ( * fptr ) ( int ) ; fptr = 0 ; }", tokenizeAndStringify(code2)); + { + const char code[] = "void foo() { int (*fptr)(int) = 0; }"; + ASSERT_EQUALS("void foo ( ) { int ( * fptr ) ( int ) ; fptr = 0 ; }", tokenizeAndStringify(code)); + } } void volatile_variables() { @@ -4028,27 +4053,27 @@ class TestTokenizer : public TestFixture { void cpp03template1() { { - const char *code = "template struct extent {};"; + const char code[] = "template struct extent {};"; ASSERT_EQUALS("template < typename > struct extent { } ;", tokenizeAndStringify(code)); } { - const char *code = "template struct extent;"; + const char code[] = "template struct extent;"; ASSERT_EQUALS("template < typename > struct extent ;", tokenizeAndStringify(code)); } { - const char *code = "template struct extent;"; + const char code[] = "template struct extent;"; ASSERT_EQUALS("template < typename , unsigned int = 0 > struct extent ;", tokenizeAndStringify(code)); } } void cpp0xtemplate1() { - const char *code = "template \n" - "void fn2 (T t = []{return 1;}())\n" - "{}\n" - "int main()\n" - "{\n" - " fn2();\n" - "}\n"; + const char code[] = "template \n" + "void fn2 (T t = []{return 1;}())\n" + "{}\n" + "int main()\n" + "{\n" + " fn2();\n" + "}\n"; ASSERT_EQUALS("void fn2 ( int t = [ ] { return 1 ; } ( ) ) ;\n" "\n" "int main ( )\n" @@ -4061,16 +4086,16 @@ class TestTokenizer : public TestFixture { void cpp0xtemplate2() { // tokenize ">>" into "> >" - const char *code = "list> ints;\n"; + const char code[] = "list> ints;\n"; ASSERT_EQUALS("list < list < int > > ints ;", tokenizeAndStringify(code)); } void cpp0xtemplate3() { // #2549 - const char *code = "template\n" - "struct S\n" - "{};\n" - "S s;\n"; + const char code[] = "template\n" + "struct S\n" + "{};\n" + "S s;\n"; ASSERT_EQUALS("struct S ;\n" "\n" "\n" @@ -4111,27 +4136,27 @@ class TestTokenizer : public TestFixture { void cpp0xtemplate5() { // #9154 { - const char *code = "struct s>;"; + const char code[] = "struct s>;"; ASSERT_EQUALS("struct s < x < u ... > > ;", tokenizeAndStringify(code)); } { - const char *code = "template using c = e,b...>>;"; + const char code[] = "template using c = e,b...>>;"; ASSERT_EQUALS("template < class f > using c = e < i < q < f , r > , b ... > > ;", tokenizeAndStringify(code)); } { - const char *code = "struct s> { };"; + const char code[] = "struct s> { };"; ASSERT_EQUALS("struct s < x < u ... > > { } ;", tokenizeAndStringify(code)); } { - const char *code = "struct q : s> { };"; + const char code[] = "struct q : s> { };"; ASSERT_EQUALS("struct q : s < x < u ... > > { } ;", tokenizeAndStringify(code)); } { - const char *code = "struct q : private s> { };"; + const char code[] = "struct q : private s> { };"; ASSERT_EQUALS("struct q : private s < x < u ... > > { } ;", tokenizeAndStringify(code)); } @@ -4637,94 +4662,120 @@ class TestTokenizer : public TestFixture { } void simplifyNamespaceStd() { - const char *code, *expected; - - code = "map m;"; // namespace std is not used - ASSERT_EQUALS("map < foo , bar > m ;", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "map m;"; - ASSERT_EQUALS("std :: map < foo , bar > m ;", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "string s;"; - ASSERT_EQUALS("std :: string s ;", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "void foo() {swap(a, b); }"; - ASSERT_EQUALS("void foo ( ) { std :: swap ( a , b ) ; }", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "void search() {}"; - ASSERT_EQUALS("void search ( ) { }", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "void search();\n" - "void dostuff() { search(); }"; - ASSERT_EQUALS("void search ( ) ;\nvoid dostuff ( ) { search ( ) ; }", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "void foo() {map(a, b); }"; // That's obviously not std::map<> - ASSERT_EQUALS("void foo ( ) { map ( a , b ) ; }", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "string s;"; // That's obviously not std::string - TODO_ASSERT_EQUALS("string < wchar_t > s ;", "std :: string < wchar_t > s ;", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "swap s;"; // That's obviously not std::swap - ASSERT_EQUALS("swap s ;", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "std::string s;"; - ASSERT_EQUALS("std :: string s ;", tokenizeAndStringify(code)); - - // #4042 (Do not add 'std ::' to variables) - code = "using namespace std;\n" - "const char * string = \"Hi\";"; - ASSERT_EQUALS("const char * string ; string = \"Hi\" ;", tokenizeAndStringify(code)); - - code = "using namespace std;\n" - "string f(const char * string) {\n" - " cout << string << endl;\n" - " return string;\n" - "}"; - expected = "std :: string f ( const char * string ) {\n" - "std :: cout << string << std :: endl ;\n" - "return string ;\n" - "}"; - TODO_ASSERT_EQUALS(expected, - "std :: string f ( const char * string ) {\n" - "cout << string << endl ;\n" - "return string ;\n" - "}", - tokenizeAndStringify(code)); - ASSERT_EQUALS("[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cout\n", errout_str()); - - code = "using namespace std;\n" - "void f() {\n" - " try { }\n" - " catch(std::exception &exception) { }\n" - "}"; - expected = "void f ( ) {\n" - "try { }\n" - "catch ( std :: exception & exception ) { }\n" - "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + const char *expected; - // #5773 (Don't prepend 'std ::' to function definitions) - code = "using namespace std;\n" - "class C {\n" - " void search() {}\n" - " void search() const {}\n" - " void search() THROW_MACRO {}\n" - "};"; - expected = "class C {\n" - "void search ( ) { }\n" - "void search ( ) const { }\n" - "void search ( ) { }\n" - "} ;"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + { + const char code[] = "map m;"; // namespace std is not used + ASSERT_EQUALS("map < foo , bar > m ;", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "map m;"; + ASSERT_EQUALS("std :: map < foo , bar > m ;", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "string s;"; + ASSERT_EQUALS("std :: string s ;", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "void foo() {swap(a, b); }"; + ASSERT_EQUALS("void foo ( ) { std :: swap ( a , b ) ; }", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "void search() {}"; + ASSERT_EQUALS("void search ( ) { }", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "void search();\n" + "void dostuff() { search(); }"; + ASSERT_EQUALS("void search ( ) ;\nvoid dostuff ( ) { search ( ) ; }", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "void foo() {map(a, b); }"; // That's obviously not std::map<> + ASSERT_EQUALS("void foo ( ) { map ( a , b ) ; }", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "string s;"; // That's obviously not std::string + TODO_ASSERT_EQUALS("string < wchar_t > s ;", "std :: string < wchar_t > s ;", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "swap s;"; // That's obviously not std::swap + ASSERT_EQUALS("swap s ;", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "std::string s;"; + ASSERT_EQUALS("std :: string s ;", tokenizeAndStringify(code)); + } + + { // #4042 (Do not add 'std ::' to variables) + const char code[] = "using namespace std;\n" + "const char * string = \"Hi\";"; + ASSERT_EQUALS("const char * string ; string = \"Hi\" ;", tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" + "string f(const char * string) {\n" + " cout << string << endl;\n" + " return string;\n" + "}"; + expected = "std :: string f ( const char * string ) {\n" + "std :: cout << string << std :: endl ;\n" + "return string ;\n" + "}"; + TODO_ASSERT_EQUALS(expected, + "std :: string f ( const char * string ) {\n" + "cout << string << endl ;\n" + "return string ;\n" + "}", + tokenizeAndStringify(code)); + ASSERT_EQUALS("[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cout\n", errout_str()); + } + + { + const char code[] = "using namespace std;\n" + "void f() {\n" + " try { }\n" + " catch(std::exception &exception) { }\n" + "}"; + expected = "void f ( ) {\n" + "try { }\n" + "catch ( std :: exception & exception ) { }\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } + + { // #5773 (Don't prepend 'std ::' to function definitions) + const char code[] = "using namespace std;\n" + "class C {\n" + " void search() {}\n" + " void search() const {}\n" + " void search() THROW_MACRO {}\n" + "};"; + expected = "class C {\n" + "void search ( ) { }\n" + "void search ( ) const { }\n" + "void search ( ) { }\n" + "} ;"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } // Ticket #8091 ASSERT_EQUALS("enum Anonymous0 { string } ;", @@ -4755,37 +4806,43 @@ class TestTokenizer : public TestFixture { ASSERT_NO_THROW(tokenizeAndStringify("NS_BEGIN(IMAGEIO_2D_DICOM) using namespace std; NS_END")); // #11045 - code = "using namespace std;\n" - "void f(const unique_ptr& p) {\n" - " if (!p)\n" - " throw runtime_error(\"abc\");\n" - "}"; - expected = "void f ( const std :: unique_ptr < int > & p ) {\n" - "if ( ! p ) {\n" - "throw std :: runtime_error ( \"abc\" ) ; }\n" - "}"; - TODO_ASSERT_EQUALS(expected, - "void f ( const std :: unique_ptr < int > & p ) {\n" - "if ( ! p ) {\n" - "throw runtime_error ( \"abc\" ) ; }\n" - "}", - tokenizeAndStringify(code)); - - code = "using namespace std;\n" // #8454 - "void f() { string str = to_string(1); }\n"; - expected = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + { + const char code[] = "using namespace std;\n" + "void f(const unique_ptr& p) {\n" + " if (!p)\n" + " throw runtime_error(\"abc\");\n" + "}"; + expected = "void f ( const std :: unique_ptr < int > & p ) {\n" + "if ( ! p ) {\n" + "throw std :: runtime_error ( \"abc\" ) ; }\n" + "}"; + TODO_ASSERT_EQUALS(expected, + "void f ( const std :: unique_ptr < int > & p ) {\n" + "if ( ! p ) {\n" + "throw runtime_error ( \"abc\" ) ; }\n" + "}", + tokenizeAndStringify(code)); + } + + { + const char code[] = "using namespace std;\n" // #8454 + "void f() { string str = to_string(1); }\n"; + expected = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } - code = "using namespace std;\n" - "vector&& f(vector& v) {\n" - " v.push_back(1);\n" - " return move(v);\n" - "}\n"; - expected = "std :: vector < int > && f ( std :: vector < int > & v ) {\n" - "v . push_back ( 1 ) ;\n" - "return std :: move ( v ) ;\n" - "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + { + const char code[] = "using namespace std;\n" + "vector&& f(vector& v) {\n" + " v.push_back(1);\n" + " return move(v);\n" + "}\n"; + expected = "std :: vector < int > && f ( std :: vector < int > & v ) {\n" + "v . push_back ( 1 ) ;\n" + "return std :: move ( v ) ;\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } } void microsoftMemory() { @@ -6107,140 +6164,140 @@ class TestTokenizer : public TestFixture { void astexpr2() { // limit for large expressions // #7724 - wrong AST causes hang // Ideally a proper AST is created for this code. - const char* code = "const char * a(int type) {\n" - " return (\n" - " (type == 1) ? \"\"\n" - " : (type == 2) ? \"\"\n" - " : (type == 3) ? \"\"\n" - " : (type == 4) ? \"\"\n" - " : (type == 5) ? \"\"\n" - " : (type == 6) ? \"\"\n" - " : (type == 7) ? \"\"\n" - " : (type == 8) ? \"\"\n" - " : (type == 9) ? \"\"\n" - " : (type == 10) ? \"\"\n" - " : (type == 11) ? \"\"\n" - " : (type == 12) ? \"\"\n" - " : (type == 13) ? \"\"\n" - " : (type == 14) ? \"\"\n" - " : (type == 15) ? \"\"\n" - " : (type == 16) ? \"\"\n" - " : (type == 17) ? \"\"\n" - " : (type == 18) ? \"\"\n" - " : (type == 19) ? \"\"\n" - " : (type == 20) ? \"\"\n" - " : (type == 21) ? \"\"\n" - " : (type == 22) ? \"\"\n" - " : (type == 23) ? \"\"\n" - " : (type == 24) ? \"\"\n" - " : (type == 25) ? \"\"\n" - " : (type == 26) ? \"\"\n" - " : (type == 27) ? \"\"\n" - " : (type == 28) ? \"\"\n" - " : (type == 29) ? \"\"\n" - " : (type == 30) ? \"\"\n" - " : (type == 31) ? \"\"\n" - " : (type == 32) ? \"\"\n" - " : (type == 33) ? \"\"\n" - " : (type == 34) ? \"\"\n" - " : (type == 35) ? \"\"\n" - " : (type == 36) ? \"\"\n" - " : (type == 37) ? \"\"\n" - " : (type == 38) ? \"\"\n" - " : (type == 39) ? \"\"\n" - " : (type == 40) ? \"\"\n" - " : (type == 41) ? \"\"\n" - " : (type == 42) ? \"\"\n" - " : (type == 43) ? \"\"\n" - " : (type == 44) ? \"\"\n" - " : (type == 45) ? \"\"\n" - " : (type == 46) ? \"\"\n" - " : (type == 47) ? \"\"\n" - " : (type == 48) ? \"\"\n" - " : (type == 49) ? \"\"\n" - " : (type == 50) ? \"\"\n" - " : (type == 51) ? \"\"\n" - " : \"\");\n" - "}\n"; + const char code1[] = "const char * a(int type) {\n" + " return (\n" + " (type == 1) ? \"\"\n" + " : (type == 2) ? \"\"\n" + " : (type == 3) ? \"\"\n" + " : (type == 4) ? \"\"\n" + " : (type == 5) ? \"\"\n" + " : (type == 6) ? \"\"\n" + " : (type == 7) ? \"\"\n" + " : (type == 8) ? \"\"\n" + " : (type == 9) ? \"\"\n" + " : (type == 10) ? \"\"\n" + " : (type == 11) ? \"\"\n" + " : (type == 12) ? \"\"\n" + " : (type == 13) ? \"\"\n" + " : (type == 14) ? \"\"\n" + " : (type == 15) ? \"\"\n" + " : (type == 16) ? \"\"\n" + " : (type == 17) ? \"\"\n" + " : (type == 18) ? \"\"\n" + " : (type == 19) ? \"\"\n" + " : (type == 20) ? \"\"\n" + " : (type == 21) ? \"\"\n" + " : (type == 22) ? \"\"\n" + " : (type == 23) ? \"\"\n" + " : (type == 24) ? \"\"\n" + " : (type == 25) ? \"\"\n" + " : (type == 26) ? \"\"\n" + " : (type == 27) ? \"\"\n" + " : (type == 28) ? \"\"\n" + " : (type == 29) ? \"\"\n" + " : (type == 30) ? \"\"\n" + " : (type == 31) ? \"\"\n" + " : (type == 32) ? \"\"\n" + " : (type == 33) ? \"\"\n" + " : (type == 34) ? \"\"\n" + " : (type == 35) ? \"\"\n" + " : (type == 36) ? \"\"\n" + " : (type == 37) ? \"\"\n" + " : (type == 38) ? \"\"\n" + " : (type == 39) ? \"\"\n" + " : (type == 40) ? \"\"\n" + " : (type == 41) ? \"\"\n" + " : (type == 42) ? \"\"\n" + " : (type == 43) ? \"\"\n" + " : (type == 44) ? \"\"\n" + " : (type == 45) ? \"\"\n" + " : (type == 46) ? \"\"\n" + " : (type == 47) ? \"\"\n" + " : (type == 48) ? \"\"\n" + " : (type == 49) ? \"\"\n" + " : (type == 50) ? \"\"\n" + " : (type == 51) ? \"\"\n" + " : \"\");\n" + "}\n"; // Ensure that the AST is validated for the simplified token list - TODO_ASSERT_THROW(tokenizeAndStringify(code), InternalError); // this should not crash/hang - - code = "template\n" // #11515 - "struct ConstCTZ {\n" - " static constexpr uint32_t value =\n" - " (kInput & (uint64_t(1) << 0)) ? 0 :\n" - " (kInput & (uint64_t(1) << 1)) ? 1 :\n" - " (kInput & (uint64_t(1) << 2)) ? 2 :\n" - " (kInput & (uint64_t(1) << 3)) ? 3 :\n" - " (kInput & (uint64_t(1) << 4)) ? 4 :\n" - " (kInput & (uint64_t(1) << 5)) ? 5 :\n" - " (kInput & (uint64_t(1) << 6)) ? 6 :\n" - " (kInput & (uint64_t(1) << 7)) ? 7 :\n" - " (kInput & (uint64_t(1) << 8)) ? 8 :\n" - " (kInput & (uint64_t(1) << 9)) ? 9 :\n" - " (kInput & (uint64_t(1) << 10)) ? 10 :\n" - " (kInput & (uint64_t(1) << 11)) ? 11 :\n" - " (kInput & (uint64_t(1) << 12)) ? 12 :\n" - " (kInput & (uint64_t(1) << 13)) ? 13 :\n" - " (kInput & (uint64_t(1) << 14)) ? 14 :\n" - " (kInput & (uint64_t(1) << 15)) ? 15 :\n" - " (kInput & (uint64_t(1) << 16)) ? 16 :\n" - " (kInput & (uint64_t(1) << 17)) ? 17 :\n" - " (kInput & (uint64_t(1) << 18)) ? 18 :\n" - " (kInput & (uint64_t(1) << 19)) ? 19 :\n" - " (kInput & (uint64_t(1) << 20)) ? 20 :\n" - " (kInput & (uint64_t(1) << 21)) ? 21 :\n" - " (kInput & (uint64_t(1) << 22)) ? 22 :\n" - " (kInput & (uint64_t(1) << 23)) ? 23 :\n" - " (kInput & (uint64_t(1) << 24)) ? 24 :\n" - " (kInput & (uint64_t(1) << 25)) ? 25 :\n" - " (kInput & (uint64_t(1) << 26)) ? 26 :\n" - " (kInput & (uint64_t(1) << 27)) ? 27 :\n" - " (kInput & (uint64_t(1) << 28)) ? 28 :\n" - " (kInput & (uint64_t(1) << 29)) ? 29 :\n" - " (kInput & (uint64_t(1) << 30)) ? 30 :\n" - " (kInput & (uint64_t(1) << 31)) ? 31 :\n" - " (kInput & (uint64_t(1) << 32)) ? 32 :\n" - " (kInput & (uint64_t(1) << 33)) ? 33 :\n" - " (kInput & (uint64_t(1) << 34)) ? 34 :\n" - " (kInput & (uint64_t(1) << 35)) ? 35 :\n" - " (kInput & (uint64_t(1) << 36)) ? 36 :\n" - " (kInput & (uint64_t(1) << 37)) ? 37 :\n" - " (kInput & (uint64_t(1) << 38)) ? 38 :\n" - " (kInput & (uint64_t(1) << 39)) ? 39 :\n" - " (kInput & (uint64_t(1) << 40)) ? 40 :\n" - " (kInput & (uint64_t(1) << 41)) ? 41 :\n" - " (kInput & (uint64_t(1) << 42)) ? 42 :\n" - " (kInput & (uint64_t(1) << 43)) ? 43 :\n" - " (kInput & (uint64_t(1) << 44)) ? 44 :\n" - " (kInput & (uint64_t(1) << 45)) ? 45 :\n" - " (kInput & (uint64_t(1) << 46)) ? 46 :\n" - " (kInput & (uint64_t(1) << 47)) ? 47 :\n" - " (kInput & (uint64_t(1) << 48)) ? 48 :\n" - " (kInput & (uint64_t(1) << 49)) ? 49 :\n" - " (kInput & (uint64_t(1) << 50)) ? 50 :\n" - " (kInput & (uint64_t(1) << 51)) ? 51 :\n" - " (kInput & (uint64_t(1) << 52)) ? 52 :\n" - " (kInput & (uint64_t(1) << 53)) ? 53 :\n" - " (kInput & (uint64_t(1) << 54)) ? 54 :\n" - " (kInput & (uint64_t(1) << 55)) ? 55 :\n" - " (kInput & (uint64_t(1) << 56)) ? 56 :\n" - " (kInput & (uint64_t(1) << 57)) ? 57 :\n" - " (kInput & (uint64_t(1) << 58)) ? 58 :\n" - " (kInput & (uint64_t(1) << 59)) ? 59 :\n" - " (kInput & (uint64_t(1) << 60)) ? 60 :\n" - " (kInput & (uint64_t(1) << 61)) ? 61 :\n" - " (kInput & (uint64_t(1) << 62)) ? 62 :\n" - " (kInput & (uint64_t(1) << 63)) ? 63 : 64;\n" - "};\n"; - ASSERT_NO_THROW(tokenizeAndStringify(code)); - - code = "void f(const std::vector& v) {\n" // #12569 - " ::std::for_each(v.begin(), v.end(), [](int i) {\n" - " int j(i ? i : 5);\n" - " });\n" - "}\n"; - ASSERT_NO_THROW(tokenizeAndStringify(code)); + TODO_ASSERT_THROW(tokenizeAndStringify(code1), InternalError); // this should not crash/hang + + const char code2[] = "template\n" // #11515 + "struct ConstCTZ {\n" + " static constexpr uint32_t value =\n" + " (kInput & (uint64_t(1) << 0)) ? 0 :\n" + " (kInput & (uint64_t(1) << 1)) ? 1 :\n" + " (kInput & (uint64_t(1) << 2)) ? 2 :\n" + " (kInput & (uint64_t(1) << 3)) ? 3 :\n" + " (kInput & (uint64_t(1) << 4)) ? 4 :\n" + " (kInput & (uint64_t(1) << 5)) ? 5 :\n" + " (kInput & (uint64_t(1) << 6)) ? 6 :\n" + " (kInput & (uint64_t(1) << 7)) ? 7 :\n" + " (kInput & (uint64_t(1) << 8)) ? 8 :\n" + " (kInput & (uint64_t(1) << 9)) ? 9 :\n" + " (kInput & (uint64_t(1) << 10)) ? 10 :\n" + " (kInput & (uint64_t(1) << 11)) ? 11 :\n" + " (kInput & (uint64_t(1) << 12)) ? 12 :\n" + " (kInput & (uint64_t(1) << 13)) ? 13 :\n" + " (kInput & (uint64_t(1) << 14)) ? 14 :\n" + " (kInput & (uint64_t(1) << 15)) ? 15 :\n" + " (kInput & (uint64_t(1) << 16)) ? 16 :\n" + " (kInput & (uint64_t(1) << 17)) ? 17 :\n" + " (kInput & (uint64_t(1) << 18)) ? 18 :\n" + " (kInput & (uint64_t(1) << 19)) ? 19 :\n" + " (kInput & (uint64_t(1) << 20)) ? 20 :\n" + " (kInput & (uint64_t(1) << 21)) ? 21 :\n" + " (kInput & (uint64_t(1) << 22)) ? 22 :\n" + " (kInput & (uint64_t(1) << 23)) ? 23 :\n" + " (kInput & (uint64_t(1) << 24)) ? 24 :\n" + " (kInput & (uint64_t(1) << 25)) ? 25 :\n" + " (kInput & (uint64_t(1) << 26)) ? 26 :\n" + " (kInput & (uint64_t(1) << 27)) ? 27 :\n" + " (kInput & (uint64_t(1) << 28)) ? 28 :\n" + " (kInput & (uint64_t(1) << 29)) ? 29 :\n" + " (kInput & (uint64_t(1) << 30)) ? 30 :\n" + " (kInput & (uint64_t(1) << 31)) ? 31 :\n" + " (kInput & (uint64_t(1) << 32)) ? 32 :\n" + " (kInput & (uint64_t(1) << 33)) ? 33 :\n" + " (kInput & (uint64_t(1) << 34)) ? 34 :\n" + " (kInput & (uint64_t(1) << 35)) ? 35 :\n" + " (kInput & (uint64_t(1) << 36)) ? 36 :\n" + " (kInput & (uint64_t(1) << 37)) ? 37 :\n" + " (kInput & (uint64_t(1) << 38)) ? 38 :\n" + " (kInput & (uint64_t(1) << 39)) ? 39 :\n" + " (kInput & (uint64_t(1) << 40)) ? 40 :\n" + " (kInput & (uint64_t(1) << 41)) ? 41 :\n" + " (kInput & (uint64_t(1) << 42)) ? 42 :\n" + " (kInput & (uint64_t(1) << 43)) ? 43 :\n" + " (kInput & (uint64_t(1) << 44)) ? 44 :\n" + " (kInput & (uint64_t(1) << 45)) ? 45 :\n" + " (kInput & (uint64_t(1) << 46)) ? 46 :\n" + " (kInput & (uint64_t(1) << 47)) ? 47 :\n" + " (kInput & (uint64_t(1) << 48)) ? 48 :\n" + " (kInput & (uint64_t(1) << 49)) ? 49 :\n" + " (kInput & (uint64_t(1) << 50)) ? 50 :\n" + " (kInput & (uint64_t(1) << 51)) ? 51 :\n" + " (kInput & (uint64_t(1) << 52)) ? 52 :\n" + " (kInput & (uint64_t(1) << 53)) ? 53 :\n" + " (kInput & (uint64_t(1) << 54)) ? 54 :\n" + " (kInput & (uint64_t(1) << 55)) ? 55 :\n" + " (kInput & (uint64_t(1) << 56)) ? 56 :\n" + " (kInput & (uint64_t(1) << 57)) ? 57 :\n" + " (kInput & (uint64_t(1) << 58)) ? 58 :\n" + " (kInput & (uint64_t(1) << 59)) ? 59 :\n" + " (kInput & (uint64_t(1) << 60)) ? 60 :\n" + " (kInput & (uint64_t(1) << 61)) ? 61 :\n" + " (kInput & (uint64_t(1) << 62)) ? 62 :\n" + " (kInput & (uint64_t(1) << 63)) ? 63 : 64;\n" + "};\n"; + ASSERT_NO_THROW(tokenizeAndStringify(code2)); + + const char code3[] = "void f(const std::vector& v) {\n" // #12569 + " ::std::for_each(v.begin(), v.end(), [](int i) {\n" + " int j(i ? i : 5);\n" + " });\n" + "}\n"; + ASSERT_NO_THROW(tokenizeAndStringify(code3)); } void astnewdelete() { @@ -6881,7 +6938,8 @@ class TestTokenizer : public TestFixture { } #define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__) - bool isStartOfExecutableScope_(int offset, const char code[], const char* file, int line) { + template + bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) { SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -7664,7 +7722,8 @@ class TestTokenizer : public TestFixture { "}\n"), SYNTAX); } - void checkConfig(const char code[]) { + template + void checkConfig(const char (&code)[size]) { const Settings s = settingsBuilder().checkConfiguration().build(); // tokenize.. diff --git a/test/testtype.cpp b/test/testtype.cpp index 0617ab99211..f619dd48a10 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -49,7 +49,20 @@ class TestType : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], const Settings& settings, bool cpp = true, Standards::cppstd_t standard = Standards::cppstd_t::CPP11) { + template + void check_(const char* file, int line, const char (&code)[size], const Settings& settings, bool cpp = true, Standards::cppstd_t standard = Standards::cppstd_t::CPP11) { + const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).cpp(standard).build(); + + // Tokenize.. + SimpleTokenizer tokenizer(settings1, *this); + ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + + // Check.. + runChecks(tokenizer, this); + } + + // TODO: get rid of this + void check_(const char* file, int line, const std::string& code, const Settings& settings, bool cpp = true, Standards::cppstd_t standard = Standards::cppstd_t::CPP11) { const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).cpp(standard).build(); // Tokenize.. @@ -61,7 +74,8 @@ class TestType : public TestFixture { } #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) - void checkP_(const char* file, int line, const char code[], const Settings& settings, const char filename[] = "test.cpp", const simplecpp::DUI& dui = simplecpp::DUI()) { + template + void checkP_(const char* file, int line, const char (&code)[size], const Settings& settings, const char filename[] = "test.cpp", const simplecpp::DUI& dui = simplecpp::DUI()) { const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).build(); std::vector files(1, filename); diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 19a01fe6227..c919318585f 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -83,7 +83,8 @@ class TestUnusedFunctions : public TestFixture { } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], Platform::Type platform = Platform::Type::Native, const Settings *s = nullptr) { + template + void check_(const char* file, int line, const char (&code)[size], Platform::Type platform = Platform::Type::Native, const Settings *s = nullptr) { const Settings settings1 = settingsBuilder(s ? *s : settings).platform(platform).build(); // Tokenize.. @@ -96,6 +97,18 @@ class TestUnusedFunctions : public TestFixture { (checkUnusedFunctions.check)(settings1, *this); // TODO: check result } + // TODO: get rid of this + void check_(const char* file, int line, const std::string& code ) { + // Tokenize.. + SimpleTokenizer tokenizer(settings, *this); + ASSERT_LOC(tokenizer.tokenize(code), file, line); + + // Check for unused functions.. + CheckUnusedFunctions checkUnusedFunctions; + checkUnusedFunctions.parseTokens(tokenizer, settings); + (checkUnusedFunctions.check)(settings, *this); // TODO: check result + } + void incondition() { check("int f1()\n" "{\n" diff --git a/test/testvaarg.cpp b/test/testvaarg.cpp index a0067b6b4a4..aab291c405f 100644 --- a/test/testvaarg.cpp +++ b/test/testvaarg.cpp @@ -30,7 +30,8 @@ class TestVaarg : public TestFixture { const Settings settings = settingsBuilder().severity(Severity::warning).build(); #define check(code) check_(code, __FILE__, __LINE__) - void check_(const char code[], const char* file, int line) { + template + void check_(const char (&code)[size], const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 0d3e4f6b08c..3e05232d251 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -402,7 +402,8 @@ class TestValueFlow : public TestFixture { } #define testLifetimeOfX(...) testLifetimeOfX_(__FILE__, __LINE__, __VA_ARGS__) - bool testLifetimeOfX_(const char* file, int line, const char code[], unsigned int linenr, const char value[], ValueFlow::Value::LifetimeScope lifetimeScope = ValueFlow::Value::LifetimeScope::Local) { + template + bool testLifetimeOfX_(const char* file, int line, const char (&code)[size], unsigned int linenr, const char value[], ValueFlow::Value::LifetimeScope lifetimeScope = ValueFlow::Value::LifetimeScope::Local) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -455,7 +456,8 @@ class TestValueFlow : public TestFixture { } #define testConditionalValueOfX(code, linenr, value) testConditionalValueOfX_(code, linenr, value, __FILE__, __LINE__) - bool testConditionalValueOfX_(const char code[], unsigned int linenr, int value, const char* file, int line) { + template + bool testConditionalValueOfX_(const char (&code)[size], unsigned int linenr, int value, const char* file, int line) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -473,7 +475,8 @@ class TestValueFlow : public TestFixture { } #define bailout(...) bailout_(__FILE__, __LINE__, __VA_ARGS__) - void bailout_(const char* file, int line, const char code[]) { + template + void bailout_(const char* file, int line, const char (&code)[size]) { const Settings s = settingsBuilder().debugwarnings().build(); std::vector files(1, "test.cpp"); @@ -501,7 +504,8 @@ class TestValueFlow : public TestFixture { } #define lifetimeValues(...) lifetimeValues_(__FILE__, __LINE__, __VA_ARGS__) - std::vector lifetimeValues_(const char* file, int line, const char code[], const char tokstr[], const Settings *s = nullptr) { + template + std::vector lifetimeValues_(const char* file, int line, const char (&code)[size], const char tokstr[], const Settings *s = nullptr) { std::vector result; SimpleTokenizer tokenizer(s ? *s : settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -614,98 +618,119 @@ class TestValueFlow : public TestFixture { } void valueFlowLifetime() { - const char *code; std::vector lifetimes; - code = "void f() {\n" - " int a = 1;\n" - " auto x = [&]() { return a + 1; };\n" - " auto b = x;\n" - "}\n"; - ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "a + 1")); + { + const char code[] = "void f() {\n" + " int a = 1;\n" + " auto x = [&]() { return a + 1; };\n" + " auto b = x;\n" + "}\n"; + ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "a + 1")); + } - code = "void f() {\n" - " int a = 1;\n" - " auto x = [=]() { return a + 1; };\n" - " auto b = x;\n" - "}\n"; - ASSERT_EQUALS(false, testLifetimeOfX(code, 4, "a ;")); + { + const char code[] = "void f() {\n" + " int a = 1;\n" + " auto x = [=]() { return a + 1; };\n" + " auto b = x;\n" + "}\n"; + ASSERT_EQUALS(false, testLifetimeOfX(code, 4, "a ;")); + } - code = "void f(int v) {\n" - " int a = v;\n" - " int * p = &a;\n" - " auto x = [=]() { return p + 1; };\n" - " auto b = x;\n" - "}\n"; - ASSERT_EQUALS(true, testLifetimeOfX(code, 5, "a ;")); + { + const char code[] = "void f(int v) {\n" + " int a = v;\n" + " int * p = &a;\n" + " auto x = [=]() { return p + 1; };\n" + " auto b = x;\n" + "}\n"; + ASSERT_EQUALS(true, testLifetimeOfX(code, 5, "a ;")); + } - code = "void f() {\n" - " std::vector v;\n" - " auto x = v.begin();\n" - " auto it = x;\n" - "}\n"; - ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . begin")); + { + const char code[] = "void f() {\n" + " std::vector v;\n" + " auto x = v.begin();\n" + " auto it = x;\n" + "}\n"; + ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . begin")); + } - code = "void f() {\n" - " std::vector v;\n" - " auto x = v.begin() + 1;\n" - " auto it = x;\n" - "}\n"; - ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . begin")); + { + const char code[] = "void f() {\n" + " std::vector v;\n" + " auto x = v.begin() + 1;\n" + " auto it = x;\n" + "}\n"; + ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . begin")); + } - code = "int* f() {\n" - " std::vector v;\n" - " int * x = v.data();\n" - " return x;\n" - "}\n"; - ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . data")); + { + const char code[] = "int* f() {\n" + " std::vector v;\n" + " int * x = v.data();\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . data")); + } - code = "int* f() {\n" - " std::vector v;\n" - " int * x = v.data() + 1;\n" - " return x;\n" - "}\n"; - ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . data")); + { + const char code[] = "int* f() {\n" + " std::vector v;\n" + " int * x = v.data() + 1;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testLifetimeOfX(code, 4, "v . data")); + } - code = "int f(int* a) {\n" - " int **p = &a;\n" - " int * x = *p;\n" - " return x; \n" - "}\n"; - ASSERT_EQUALS(false, testLifetimeOfX(code, 4, "a")); + { + const char code[] = "int f(int* a) {\n" + " int **p = &a;\n" + " int * x = *p;\n" + " return x; \n" + "}\n"; + ASSERT_EQUALS(false, testLifetimeOfX(code, 4, "a")); + } - code = "void f() {\n" - " int i = 0;\n" - " void* x = (void*)&i;\n" - "}\n"; - lifetimes = lifetimeValues(code, "( void * )"); - ASSERT_EQUALS(true, lifetimes.size() == 1); - ASSERT_EQUALS(true, lifetimes.front() == "i"); - - code = "struct T {\n" // #10810 - " static int g() { return 0; }\n" - "};\n" - "T t;\n" - "struct S { int i; };\n" - "S f() {\n" - " S s = { decltype(t)::g() };\n" - " return s;\n" - "};\n"; - lifetimes = lifetimeValues(code, "="); - ASSERT_EQUALS(true, lifetimes.empty()); - - code = "struct T {\n" // #10838 - " void f();\n" - " double d[4][4];\n" - "};\n" - "void T::f() {\n" - " auto g = [this]() -> double(&)[4] {\n" - " double(&q)[4] = d[0];\n" - " return q;\n" - " };\n" - "}\n"; - lifetimes = lifetimeValues(code, "return"); // don't crash - ASSERT_EQUALS(true, lifetimes.empty()); + { + const char code[] = "void f() {\n" + " int i = 0;\n" + " void* x = (void*)&i;\n" + "}\n"; + lifetimes = lifetimeValues(code, "( void * )"); + ASSERT_EQUALS(true, lifetimes.size() == 1); + ASSERT_EQUALS(true, lifetimes.front() == "i"); + } + + { + const char code[] = "struct T {\n" // #10810 + " static int g() { return 0; }\n" + "};\n" + "T t;\n" + "struct S { int i; };\n" + "S f() {\n" + " S s = { decltype(t)::g() };\n" + " return s;\n" + "};\n"; + lifetimes = lifetimeValues(code, "="); + ASSERT_EQUALS(true, lifetimes.empty()); + } + + { + const char code[] = "struct T {\n" // #10838 + " void f();\n" + " double d[4][4];\n" + "};\n" + "void T::f() {\n" + " auto g = [this]() -> double(&)[4] {\n" + " double(&q)[4] = d[0];\n" + " return q;\n" + " };\n" + "}\n"; + lifetimes = lifetimeValues(code, "return"); // don't crash + ASSERT_EQUALS(true, lifetimes.empty()); + } } void valueFlowArrayElement() { @@ -4143,14 +4168,13 @@ class TestValueFlow : public TestFixture { } void valueFlowSwitchVariable() { - const char *code; - code = "void f(int x) {\n" - " a = x - 1;\n" // <- x can be 14 - " switch (x) {\n" - " case 14: a=x+2; break;\n" // <- x is 14 - " };\n" - " a = x;\n" // <- x can be 14 - "}"; + const char code[] = "void f(int x) {\n" + " a = x - 1;\n" // <- x can be 14 + " switch (x) {\n" + " case 14: a=x+2; break;\n" // <- x is 14 + " };\n" + " a = x;\n" // <- x can be 14 + "}"; ASSERT_EQUALS(true, testConditionalValueOfX(code, 2U, 14)); TODO_ASSERT_EQUALS(true, false, testConditionalValueOfX(code, 4U, 14)); TODO_ASSERT_EQUALS(true, false, testConditionalValueOfX(code, 6U, 14)); diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 02ea62f6f3d..e35f9af0379 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -254,7 +254,8 @@ class TestVarID : public TestFixture { } #define tokenize(...) tokenize_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenize_(const char* file, int line, const char code[], bool cpp = true, const Settings *s = nullptr) { + template + std::string tokenize_(const char* file, int line, const char (&code)[size], bool cpp = true, const Settings *s = nullptr) { const Settings *settings1 = s ? s : &settings; SimpleTokenizer tokenizer(*settings1, *this); @@ -267,7 +268,8 @@ class TestVarID : public TestFixture { } #define tokenizeHeader(...) tokenizeHeader_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeHeader_(const char* file, int line, const char code[], const char filename[]) { + template + std::string tokenizeHeader_(const char* file, int line, const char (&code)[size], const char filename[]) { Tokenizer tokenizer(settings, *this); std::istringstream istr(code); ASSERT_LOC(tokenizer.list.createTokens(istr, filename), file, line); @@ -280,7 +282,8 @@ class TestVarID : public TestFixture { } #define tokenizeExpr(...) tokenizeExpr_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeExpr_(const char* file, int line, const char code[], const char filename[] = "test.cpp") { + template + std::string tokenizeExpr_(const char* file, int line, const char (&code)[size], const char filename[] = "test.cpp") { std::vector files(1, filename); Tokenizer tokenizer(settings, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -294,7 +297,8 @@ class TestVarID : public TestFixture { } #define compareVaridsForVariable(...) compareVaridsForVariable_(__FILE__, __LINE__, __VA_ARGS__) - std::string compareVaridsForVariable_(const char* file, int line, const char code[], const char varname[], bool cpp = true) { + template + std::string compareVaridsForVariable_(const char* file, int line, const char (&code)[size], const char varname[], bool cpp = true) { SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC((tokenizer.tokenize)(code, cpp), file, line); @@ -2186,78 +2190,86 @@ class TestVarID : public TestFixture { } void varid_in_class24() { - const char *code{}, *expected{}; - - code = "class A {\n" - " Q_OBJECT\n" - "public:\n" - " using QPtr = QPointer;\n" - "};\n"; - expected = "1: class A {\n" - "2: Q_OBJECT\n" - "3: public:\n" - "4:\n" - "5: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + const char *expected{}; - code = "class A {\n" - " Q_OBJECT\n" - " using QPtr = QPointer;\n" - "};\n"; - expected = "1: class A {\n" - "2: Q_OBJECT\n" - "3:\n" - "4: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + { + const char code[] = "class A {\n" + " Q_OBJECT\n" + "public:\n" + " using QPtr = QPointer;\n" + "};\n"; + expected = "1: class A {\n" + "2: Q_OBJECT\n" + "3: public:\n" + "4:\n" + "5: } ;\n"; + ASSERT_EQUALS(expected, tokenize(code, true)); + } + + { + const char code[] = "class A {\n" + " Q_OBJECT\n" + " using QPtr = QPointer;\n" + "};\n"; + expected = "1: class A {\n" + "2: Q_OBJECT\n" + "3:\n" + "4: } ;\n"; + ASSERT_EQUALS(expected, tokenize(code, true)); + } } void varid_in_class25() { const Settings s = settingsBuilder(settings).library("std.cfg").build(); - const char *code{}, *expected{}; - code = "struct F {\n" // #11497 - " int i;\n" - " void f(const std::vector&v) {\n" - " if (v.front().i) {}\n" - " }\n" - "};\n"; - expected = "1: struct F {\n" - "2: int i@1 ;\n" - "3: void f ( const std :: vector < F > & v@2 ) {\n" - "4: if ( v@2 . front ( ) . i@3 ) { }\n" - "5: }\n" - "6: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true, &s)); - - code = "struct T { };\n" // 11533 - "struct U { T t; };\n" - "std::vector* g();\n" - "void f() {\n" - " std::vector* p = g();\n" - " auto t = p->front()->t;\n" - "}\n"; - expected = "1: struct T { } ;\n" - "2: struct U { T t@1 ; } ;\n" - "3: std :: vector < U * > * g ( ) ;\n" - "4: void f ( ) {\n" - "5: std :: vector < U * > * p@2 ; p@2 = g ( ) ;\n" - "6: auto t@3 ; t@3 = p@2 . front ( ) . t@4 ;\n" - "7: }\n"; - ASSERT_EQUALS(expected, tokenize(code, true, &s)); + const char *expected{}; + { + const char code[] = "struct F {\n" // #11497 + " int i;\n" + " void f(const std::vector&v) {\n" + " if (v.front().i) {}\n" + " }\n" + "};\n"; + expected = "1: struct F {\n" + "2: int i@1 ;\n" + "3: void f ( const std :: vector < F > & v@2 ) {\n" + "4: if ( v@2 . front ( ) . i@3 ) { }\n" + "5: }\n" + "6: } ;\n"; + ASSERT_EQUALS(expected, tokenize(code, true, &s)); + } + + { + const char code[] = "struct T { };\n" // 11533 + "struct U { T t; };\n" + "std::vector* g();\n" + "void f() {\n" + " std::vector* p = g();\n" + " auto t = p->front()->t;\n" + "}\n"; + expected = "1: struct T { } ;\n" + "2: struct U { T t@1 ; } ;\n" + "3: std :: vector < U * > * g ( ) ;\n" + "4: void f ( ) {\n" + "5: std :: vector < U * > * p@2 ; p@2 = g ( ) ;\n" + "6: auto t@3 ; t@3 = p@2 . front ( ) . t@4 ;\n" + "7: }\n"; + ASSERT_EQUALS(expected, tokenize(code, true, &s)); + } } void varid_in_class26() { - const char *code{}, *expected{}; // #11334 - code = "struct S {\n" - " union {\n" - " uint8_t u8[4];\n" - " uint32_t u32;\n" - " };\n" - " void f();\n" - "};\n" - "void S::f() {\n" - " u8[0] = 0;\n" - "}\n"; + const char *expected{}; // #11334 + const char code[] = "struct S {\n" + " union {\n" + " uint8_t u8[4];\n" + " uint32_t u32;\n" + " };\n" + " void f();\n" + "};\n" + "void S::f() {\n" + " u8[0] = 0;\n" + "}\n"; expected = "1: struct S {\n" "2: union {\n" "3: uint8_t u8@1 [ 4 ] ;\n" @@ -4176,21 +4188,20 @@ class TestVarID : public TestFixture { void exprid11() { - const char* code{}, *exp{}; - code = "struct S { void f(); };\n" // #12713 - "int g(int, S*);\n" - "int h(void (*)(), S*);\n" - "void S::f() {\n" - " std::make_unique(g({}, this)).release();\n" - " std::make_unique(h([]() {}, this)).release();\n" - "}\n"; - exp = "1: struct S { void f ( ) ; } ;\n" - "2: int g ( int , S * ) ;\n" - "3: int h ( void ( * ) ( ) , S * ) ;\n" - "4: void S :: f ( ) {\n" - "5: std ::@UNIQUE make_unique < int > (@UNIQUE g (@UNIQUE { } ,@6 this ) ) .@UNIQUE release (@UNIQUE ) ;\n" - "6: std ::@UNIQUE make_unique < int > (@UNIQUE h (@UNIQUE [ ] ( ) { } ,@6 this ) ) .@UNIQUE release (@UNIQUE ) ;\n" - "7: }\n"; + const char code[] = "struct S { void f(); };\n" // #12713 + "int g(int, S*);\n" + "int h(void (*)(), S*);\n" + "void S::f() {\n" + " std::make_unique(g({}, this)).release();\n" + " std::make_unique(h([]() {}, this)).release();\n" + "}\n"; + const char* exp = "1: struct S { void f ( ) ; } ;\n" + "2: int g ( int , S * ) ;\n" + "3: int h ( void ( * ) ( ) , S * ) ;\n" + "4: void S :: f ( ) {\n" + "5: std ::@UNIQUE make_unique < int > (@UNIQUE g (@UNIQUE { } ,@6 this ) ) .@UNIQUE release (@UNIQUE ) ;\n" + "6: std ::@UNIQUE make_unique < int > (@UNIQUE h (@UNIQUE [ ] ( ) { } ,@6 this ) ) .@UNIQUE release (@UNIQUE ) ;\n" + "7: }\n"; ASSERT_EQUALS(exp, tokenizeExpr(code)); }