diff --git a/test/test64bit.cpp b/test/test64bit.cpp index a33df481384..6b2e5576580 100644 --- a/test/test64bit.cpp +++ b/test/test64bit.cpp @@ -41,9 +41,9 @@ class Test64BitPortability : public TestFixture { TEST_CASE(assignment); } -#define check(code) check_(code, __FILE__, __LINE__) +#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char (&code)[size], const char* file, int line) { + 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 d1e6d18fdf8..970391ceadb 100644 --- a/test/testastutils.cpp +++ b/test/testastutils.cpp @@ -88,9 +88,9 @@ class TestAstUtils : public TestFixture { ASSERT_EQUALS(true, findLambdaEndToken("int i = 5 * []{ return 7; }();", "[", /*checkNext*/ false)); } -#define findLambdaStartToken(code) findLambdaStartToken_(code, __FILE__, __LINE__) +#define findLambdaStartToken(...) findLambdaStartToken_(__FILE__, __LINE__, __VA_ARGS__) template - bool findLambdaStartToken_(const char (&code)[size], const char* file, int line) { + bool findLambdaStartToken_(const char* file, int line, const char (&code)[size]) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const tokStart = (::findLambdaStartToken)(tokenizer.list.back()); @@ -120,9 +120,9 @@ class TestAstUtils : public TestFixture { ASSERT_EQUALS(true, findLambdaStartToken("[](void) constexpr -> const * const* int { return x; }")); } -#define isNullOperand(code) isNullOperand_(code, __FILE__, __LINE__) +#define isNullOperand(...) isNullOperand_(__FILE__, __LINE__, __VA_ARGS__) template - bool isNullOperand_(const char (&code)[size], const char* file, int line) { + bool isNullOperand_(const char* file, int line, const char (&code)[size]) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); return (::isNullOperand)(tokenizer.tokens()); @@ -141,9 +141,9 @@ class TestAstUtils : public TestFixture { ASSERT_EQUALS(false, isNullOperand("(void*)1;")); } -#define isReturnScope(code, offset) isReturnScope_(code, offset, __FILE__, __LINE__) +#define isReturnScope(...) isReturnScope_(__FILE__, __LINE__, __VA_ARGS__) template - bool isReturnScope_(const char (&code)[size], int offset, const char* file, int line) { + bool isReturnScope_(const char* file, int line, const char (&code)[size], int offset) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const tok = (offset < 0) @@ -219,9 +219,9 @@ class TestAstUtils : public TestFixture { isSameExpressionTestInternal(false); } -#define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__) +#define isVariableChanged(...) isVariableChanged_(__FILE__, __LINE__, __VA_ARGS__) template - bool isVariableChanged_(const char (&code)[size], const char startPattern[], const char endPattern[], const char* file, int line) { + bool isVariableChanged_(const char* file, int line, const char (&code)[size], const char startPattern[], const char endPattern[]) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern, strlen(startPattern)); @@ -252,9 +252,9 @@ class TestAstUtils : public TestFixture { ASSERT_EQUALS(false, isVariableChanged("const int A[] = { 1, 2, 3 };", "[", "]")); } -#define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__) +#define isVariableChangedByFunctionCall(...) isVariableChangedByFunctionCall_( __FILE__, __LINE__, __VA_ARGS__) template - bool isVariableChangedByFunctionCall_(const char (&code)[size], const char pattern[], bool *inconclusive, const char* file, int line) { + bool isVariableChangedByFunctionCall_(const char* file, int line, const char (&code)[size], const char pattern[], bool *inconclusive) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern); @@ -412,15 +412,14 @@ class TestAstUtils : public TestFixture { } } -#define isExpressionChanged(code, var, startPattern, endPattern) \ - isExpressionChanged_(code, var, startPattern, endPattern, __FILE__, __LINE__) +#define isExpressionChanged(...) isExpressionChanged_(__FILE__, __LINE__, __VA_ARGS__) template - bool isExpressionChanged_(const char (&code)[size], + bool isExpressionChanged_(const char* file, + int line, + const char (&code)[size], const char var[], const char startPattern[], - const char endPattern[], - const char* file, - int line) + const char endPattern[]) { const Settings settings = settingsBuilder().library("std.cfg").build(); SimpleTokenizer tokenizer(settings, *this); @@ -451,9 +450,9 @@ class TestAstUtils : public TestFixture { "}")); } -#define nextAfterAstRightmostLeaf(code, parentPattern, rightPattern) nextAfterAstRightmostLeaf_(code, parentPattern, rightPattern, __FILE__, __LINE__) +#define nextAfterAstRightmostLeaf(...) nextAfterAstRightmostLeaf_(__FILE__, __LINE__, __VA_ARGS__) template - bool nextAfterAstRightmostLeaf_(const char (&code)[size], const char parentPattern[], const char rightPattern[], const char* file, int line) { + bool nextAfterAstRightmostLeaf_(const char* file, int line, const char (&code)[size], const char parentPattern[], const char rightPattern[]) { SimpleTokenizer tokenizer(settingsDefault, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); const Token * tok = Token::findsimplematch(tokenizer.tokens(), parentPattern, strlen(parentPattern)); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 3d4803cbbcb..57e98474c65 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -31,14 +31,21 @@ class TestAutoVariables : public TestFixture { private: const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).library("std.cfg").library("qt.cfg").build(); + struct CheckOptions + { + CheckOptions() = default; + bool inconclusive = true; + bool cpp = true; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); runChecks(tokenizer, this); } @@ -248,14 +255,14 @@ class TestAutoVariables : public TestFixture { "{\n" " char a[10];\n" " x->str = a;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("void foo(struct X *x)\n" "{\n" " char a[10];\n" " x->str = a;\n" - "}", true); + "}"); ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Address of local auto-variable assigned to a function parameter.\n", errout_str()); } @@ -265,7 +272,7 @@ class TestAutoVariables : public TestFixture { " struct txt_scrollpane_s * scrollpane;\n" " target->parent = &scrollpane->widget;\n" " return scrollpane;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } @@ -273,12 +280,12 @@ class TestAutoVariables : public TestFixture { check("void foo(int*& p) {\n" " int i = 0;\n" " p = &i;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout_str()); check("void foo(std::string& s) {\n" " s = foo;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } @@ -289,7 +296,7 @@ class TestAutoVariables : public TestFixture { " FN fn;\n" " FP fp;\n" " p = &fn.i;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout_str()); check("struct FN {int i;};\n" @@ -298,7 +305,7 @@ class TestAutoVariables : public TestFixture { " FN fn;\n" " FP fp;\n" " p = &p_fp->i;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("struct FN {int i;};\n" @@ -307,7 +314,7 @@ class TestAutoVariables : public TestFixture { " FN fn;\n" " FP fp;\n" " p = &fp.f->i;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } @@ -385,7 +392,7 @@ class TestAutoVariables : public TestFixture { " int i = d;\n" " d = i;\n" " return d;" - "}",false); + "}",dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("void foo(int* ptr) {\n" // #4793 @@ -482,7 +489,7 @@ class TestAutoVariables : public TestFixture { " if (lumdiff > 5.0f)\n" " return &darkOutline;\n" " return 0;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } @@ -917,7 +924,7 @@ class TestAutoVariables : public TestFixture { check("void svn_repos_dir_delta2() {\n" " struct context c;\n" " SVN_ERR(delete(&c, root_baton, src_entry, pool));\n" - "}\n", false, /* cpp= */ false); + "}\n", dinit(CheckOptions, $.inconclusive = false, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -1323,7 +1330,7 @@ class TestAutoVariables : public TestFixture { " double ret = getValue();\n" " rd = ret;\n" " return rd;\n" - "}", false); + "}", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } @@ -1799,8 +1806,7 @@ class TestAutoVariables : public TestFixture { "const int& bar(const std::unordered_map& m, int k) {\n" " auto x = 0;\n" " return get_default(m, k, x);\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:4] -> [test.cpp:9] -> [test.cpp:9]: (error, inconclusive) Reference to local variable returned.\n", errout_str()); @@ -1813,8 +1819,7 @@ class TestAutoVariables : public TestFixture { "}\n" "const int& bar(const std::unordered_map& m, int k) {\n" " return get_default(m, k, 0);\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:4] -> [test.cpp:8] -> [test.cpp:8]: (error, inconclusive) Reference to temporary returned.\n", errout_str()); @@ -2564,8 +2569,7 @@ class TestAutoVariables : public TestFixture { "const int* bar(const std::unordered_map& m, int k) {\n" " auto x = 0;\n" " return get_default(m, k, &x);\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS( "[test.cpp:9] -> [test.cpp:9] -> [test.cpp:8] -> [test.cpp:9]: (error, inconclusive) Returning pointer to local variable 'x' that will be invalid when returning.\n", errout_str()); @@ -2798,15 +2802,13 @@ class TestAutoVariables : public TestFixture { check("std::string f(std::string Str, int first, int last) {\n" " return { Str.begin() + first, Str.begin() + last + 1 };\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); check("std::string f(std::string s) {\n" " std::string r = { s.begin(), s.end() };\n" " return r;\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" @@ -3550,8 +3552,7 @@ class TestAutoVariables : public TestFixture { " int i = 0;\n" " A a{i};\n" " return a;\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS( "[test.cpp:7] -> [test.cpp:6] -> [test.cpp:8]: (error, inconclusive) Returning object that points to local variable 'i' that will be invalid when returning.\n", errout_str()); @@ -3564,8 +3565,7 @@ class TestAutoVariables : public TestFixture { " int i = 0;\n" " A a{i};\n" " return a;\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" @@ -3718,8 +3718,7 @@ class TestAutoVariables : public TestFixture { "S f() {\n" " std::string m(\"abc\");\n" " return S(m);\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" @@ -3729,8 +3728,7 @@ class TestAutoVariables : public TestFixture { "S f() {\n" " std::string s(\"abc\");\n" " return S(s.c_str());\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" @@ -3742,8 +3740,7 @@ class TestAutoVariables : public TestFixture { "void f(const std::stringstream& buffer) {\n" " S s(buffer.str().c_str());\n" " s.g();\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); } @@ -4101,16 +4098,14 @@ class TestAutoVariables : public TestFixture { "void T::f() {\n" " U u(p->g().c_str());\n" " if (u.h()) {}\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); // #11442 check("const std::string& f(const P< std::string >& value) {\n" " static const std::string empty;\n" " return value.get() == nullptr ? empty : *value;\n" - "}\n", - true); + "}\n"); ASSERT_EQUALS("", errout_str()); // #11472 diff --git a/test/testbool.cpp b/test/testbool.cpp index a6cf31dd832..4361f53bc54 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -75,12 +75,18 @@ class TestBool : public TestFixture { TEST_CASE(returnNonBoolClass); } + struct CheckOptions + { + CheckOptions() = default; + bool cpp = true; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char* file, int line, const char (&code)[size], bool cpp = true) { + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { // Tokenize.. SimpleTokenizer tokenizer(settings, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); // Check... runChecks(tokenizer, this); @@ -146,7 +152,7 @@ class TestBool : public TestFixture { " const int *rmat = n < 4 ? " /* OK */ " ctx->q_intra_matrix :" " ctx->q_chroma_intra_matrix;\n" - "}", false); + "}", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Boolean value assigned to pointer.\n", errout_str()); // ticket #6588 (c++ mode) @@ -165,7 +171,7 @@ class TestBool : public TestFixture { " char* m1 = compare(a, b) < 0\n" " ? (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))\n" " : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));\n" - "}", false); + "}", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); // #7381 diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index e4bce607f3c..3402d8452bf 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -39,23 +39,21 @@ class TestBufferOverrun : public TestFixture { private: /*const*/ Settings settings0 = settingsBuilder().library("std.cfg").severity(Severity::warning).severity(Severity::style).severity(Severity::portability).build(); + struct CheckOptions + { + CheckOptions() = default; + const Settings* s = nullptr; + bool cpp = true; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char* file, int line, const char (&code)[size], bool cpp = true) { - const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).build(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings = options.s ? *options.s : settingsBuilder(settings0).certainty(Certainty::inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); - - // Check for buffer overruns.. - runChecks(tokenizer, this); - } - - 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); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); // Check for buffer overruns.. runChecks(tokenizer, this); @@ -2506,7 +2504,7 @@ class TestBufferOverrun : public TestFixture { " c++;\n" " }\n" " return c;\n" - "}", false); + "}", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -2725,7 +2723,7 @@ class TestBufferOverrun : public TestFixture { " char str[6] = \"\\0\";\n" " unsigned short port = 65535;\n" " snprintf(str, sizeof(str), \"%hu\", port);\n" - "}", settings0, false); + "}", dinit(CheckOptions, $.s = &settings0, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("int f(int x) {\n" // #11020 @@ -3691,26 +3689,26 @@ class TestBufferOverrun : public TestFixture { check("void f() {\n" " u8 str[256];\n" " mystrcpy(str, \"abcd\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " u8 str[2];\n" " mystrcpy(str, \"abcd\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout_str()); // The same for structs, where the message comes from a different check check("void f() {\n" " struct { u8 str[256]; } ms;\n" " mystrcpy(ms.str, \"abcd\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " struct { u8 str[2]; } ms;\n" " mystrcpy(ms.str, \"abcd\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: ms.str\n", errout_str()); } @@ -3755,7 +3753,7 @@ class TestBufferOverrun : public TestFixture { check("void f() {\n" // #6350 - fp when there is cast of buffer " wchar_t buf[64];\n" " p = (unsigned char *) buf + sizeof (buf);\n" - "}", false); + "}", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("int f() {\n" @@ -4249,13 +4247,13 @@ class TestBufferOverrun : public TestFixture { check("void f() {\n" " char c[10];\n" " mymemset(c, 0, 10);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char c[10];\n" " mymemset(c, 0, 11);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout_str()); check("struct S {\n" @@ -4264,13 +4262,13 @@ class TestBufferOverrun : public TestFixture { "void f() {\n" " S s;\n" " mymemset(s.a, 0, 10);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:6]: (error) Buffer is accessed out of bounds: s.a\n", errout_str()); check("void foo() {\n" " char s[10];\n" " mymemset(s, 0, '*');\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) The size argument is given as a char constant.\n" "[test.cpp:3]: (error) Buffer is accessed out of bounds: s\n", "[test.cpp:3]: (error) Buffer is accessed out of bounds: s\n", errout_str()); @@ -4278,65 +4276,65 @@ class TestBufferOverrun : public TestFixture { check("void f(void) {\n" " char a[10];\n" " mymemset(a+5, 0, 10);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: a\n", "", errout_str()); // Ticket #909 check("void f(void) {\n" " char str[] = \"abcd\";\n" " mymemset(str, 0, 6);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout_str()); check("void f(void) {\n" " char str[] = \"abcd\";\n" " mymemset(str, 0, 5);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f(void) {\n" " wchar_t str[] = L\"abcd\";\n" " mymemset(str, 0, 21);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout_str()); check("void f(void) {\n" " wchar_t str[] = L\"abcd\";\n" " mymemset(str, 0, 20);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); // ticket #1659 - overflowing variable when using memcpy check("void f(void) {\n" " char c;\n" " mymemset(&c, 0, 4);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", "", errout_str()); // ticket #2121 - buffer access out of bounds when using uint32_t check("void f(void) {\n" " unknown_type_t buf[4];\n" " mymemset(buf, 0, 100);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); // #3124 - multidimensional array check("int main() {\n" " char b[5][6];\n" " mymemset(b, 0, 5 * 6);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("int main() {\n" " char b[5][6];\n" " mymemset(b, 0, 6 * 6);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: b\n", errout_str()); check("int main() {\n" " char b[5][6];\n" " mymemset(b, 0, 31);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: b\n", errout_str()); // #4968 - not standard function @@ -4345,26 +4343,26 @@ class TestBufferOverrun : public TestFixture { " foo.mymemset(str, 0, 100);\n" " foo::mymemset(str, 0, 100);\n" " std::mymemset(str, 0, 100);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: str\n", "", errout_str()); // #5257 - check strings check("void f() {\n" " mymemset(\"abc\", 0, 20);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Buffer is accessed out of bounds.\n", "", errout_str()); check("void f() {\n" " mymemset(temp, \"abc\", 4);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #6816 - fp when array has known string value " char c[10] = \"c\";\n" " mymemset(c, 0, 10);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); } @@ -4386,43 +4384,43 @@ class TestBufferOverrun : public TestFixture { check("void f() {\n" " char c[7];\n" " mystrncpy(c, \"hello\", 7);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char c[6];\n" " mystrncpy(c,\"hello\",6);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char c[5];\n" " mystrncpy(c,\"hello\",6);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout_str()); check("void f() {\n" " char c[6];\n" " mystrncpy(c,\"hello!\",7);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout_str()); check("void f(unsigned int addr) {\n" " memset((void *)addr, 0, 1000);\n" - "}", settings0); + "}", dinit(CheckOptions, $.s = &settings0)); ASSERT_EQUALS("", errout_str()); check("struct AB { char a[10]; };\n" "void foo(AB *ab) {\n" " mystrncpy(x, ab->a, 100);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void a(char *p) { mystrncpy(p,\"hello world!\",10); }\n" // #3168 "void b() {\n" " char buf[5];\n" " a(buf);" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Buffer is accessed out of bounds: buf\n", "", errout_str()); @@ -4448,13 +4446,13 @@ class TestBufferOverrun : public TestFixture { check("void f() {\n" " char str[3];\n" " mysprintf(str, \"test\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout_str()); check("void f() {\n" " char str[5];\n" " mysprintf(str, \"%s\", \"abcde\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout_str()); check("int getnumber();\n" @@ -4462,51 +4460,51 @@ class TestBufferOverrun : public TestFixture { "{\n" " char str[5];\n" " mysprintf(str, \"%d: %s\", getnumber(), \"abcde\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: str\n", errout_str()); check("void f() {\n" " char str[5];\n" " mysprintf(str, \"test%s\", \"\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char *str = new char[5];\n" " mysprintf(str, \"abcde\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout_str()); check("void f(int condition) {\n" " char str[5];\n" " mysprintf(str, \"test%s\", condition ? \"12\" : \"34\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f(int condition) {\n" " char str[5];\n" " mysprintf(str, \"test%s\", condition ? \"12\" : \"345\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("error", "", errout_str()); check("struct Foo { char a[1]; };\n" "void f() {\n" " struct Foo x;\n" " mysprintf(x.a, \"aa\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: x.a\n", errout_str()); // ticket #900 check("void f() {\n" " char *a = new char(30);\n" " mysprintf(a, \"a\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: a\n", errout_str()); check("void f(char value) {\n" " char *a = new char(value);\n" " mysprintf(a, \"a\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: a\n", errout_str()); // This is out of bounds if 'sizeof(ABC)' is 1 (No padding) @@ -4514,21 +4512,21 @@ class TestBufferOverrun : public TestFixture { "void f() {\n" " struct Foo *x = malloc(sizeof(Foo));\n" " mysprintf(x->a, \"aa\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); TODO_ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Buffer is accessed out of bounds: x.a\n", "", errout_str()); check("struct Foo { char a[1]; };\n" "void f() {\n" " struct Foo *x = malloc(sizeof(Foo) + 10);\n" " mysprintf(x->a, \"aa\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("struct Foo { char a[1]; };\n" "void f() {\n" " struct Foo x;\n" " mysprintf(x.a, \"aa\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: x.a\n", errout_str()); check("struct Foo {\n" // #6668 - unknown size @@ -4537,7 +4535,7 @@ class TestBufferOverrun : public TestFixture { "};" "void Foo::f() {\n" " mysprintf(a, \"abcd\");\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); } @@ -4558,13 +4556,13 @@ class TestBufferOverrun : public TestFixture { check("void f() {\n" " char c[5];\n" " myfread(c, 1, 5, stdin);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char c[5];\n" " myfread(c, 1, 6, stdin);\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout_str()); } @@ -5585,7 +5583,7 @@ class TestBufferOverrun : public TestFixture { " (*str)[applen] = '\\0';\n" " }\n" " free(*str);\n" - "}\n", false); + "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("template \n" @@ -5676,7 +5674,7 @@ class TestBufferOverrun : public TestFixture { "if (pipe(pipefd) == -1) {\n" " return;\n" " }\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: pipefd\n", errout_str()); check("void f(){\n" @@ -5684,7 +5682,7 @@ class TestBufferOverrun : public TestFixture { "if (pipe(pipefd) == -1) {\n" " return;\n" " }\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); check("void f(){\n" @@ -5692,7 +5690,7 @@ class TestBufferOverrun : public TestFixture { "if (pipe((int*)pipefd) == -1) {\n" " return;\n" " }\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: (int*)pipefd\n", errout_str()); check("void f(){\n" @@ -5700,7 +5698,7 @@ class TestBufferOverrun : public TestFixture { "if (pipe((int*)pipefd) == -1) {\n" " return;\n" " }\n" - "}", settings); + "}", dinit(CheckOptions, $.s = &settings)); ASSERT_EQUALS("", errout_str()); } }; diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index dcbe49cca26..49cdcebdcb8 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -38,9 +38,9 @@ class TestCharVar : public TestFixture { TEST_CASE(bitop); } -#define check(code) check_(code, __FILE__, __LINE__) +#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char (&code)[size], const char* file, int line) { + 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/testclass.cpp b/test/testclass.cpp index c32995446f4..c3c63abec85 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -254,9 +254,9 @@ class TestClass : public TestFixture { TEST_CASE(returnByReference); } -#define checkCopyCtorAndEqOperator(code) checkCopyCtorAndEqOperator_(code, __FILE__, __LINE__) +#define checkCopyCtorAndEqOperator(...) checkCopyCtorAndEqOperator_(__FILE__, __LINE__, __VA_ARGS__) template - void checkCopyCtorAndEqOperator_(const char (&code)[size], const char* file, int line) { + void checkCopyCtorAndEqOperator_(const char* file, int line, const char (&code)[size]) { const Settings settings = settingsBuilder().severity(Severity::warning).build(); // Tokenize.. @@ -357,9 +357,9 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } -#define checkExplicitConstructors(code) checkExplicitConstructors_(code, __FILE__, __LINE__) +#define checkExplicitConstructors(...) checkExplicitConstructors_(__FILE__, __LINE__, __VA_ARGS__) template - void checkExplicitConstructors_(const char (&code)[size], const char* file, int line) { + void checkExplicitConstructors_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -506,9 +506,9 @@ class TestClass : public TestFixture { errout_str()); } -#define checkDuplInheritedMembers(code) checkDuplInheritedMembers_(code, __FILE__, __LINE__) +#define checkDuplInheritedMembers(...) checkDuplInheritedMembers_( __FILE__, __LINE__, __VA_ARGS__) template - void checkDuplInheritedMembers_(const char (&code)[size], const char* file, int line) { + void checkDuplInheritedMembers_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -723,9 +723,9 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } -#define checkCopyConstructor(code) checkCopyConstructor_(code, __FILE__, __LINE__) +#define checkCopyConstructor(...) checkCopyConstructor_( __FILE__, __LINE__, __VA_ARGS__) template - void checkCopyConstructor_(const char (&code)[size], const char* file, int line) { + void checkCopyConstructor_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings3, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1167,9 +1167,9 @@ class TestClass : public TestFixture { } // Check that operator Equal returns reference to this -#define checkOpertorEqRetRefThis(code) checkOpertorEqRetRefThis_(code, __FILE__, __LINE__) +#define checkOpertorEqRetRefThis(...) checkOpertorEqRetRefThis_( __FILE__, __LINE__, __VA_ARGS__) template - void checkOpertorEqRetRefThis_(const char (&code)[size], const char* file, int line) { + void checkOpertorEqRetRefThis_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1638,9 +1638,9 @@ class TestClass : public TestFixture { } // Check that operator Equal checks for assignment to self -#define checkOpertorEqToSelf(code) checkOpertorEqToSelf_(code, __FILE__, __LINE__) +#define checkOpertorEqToSelf(...) checkOpertorEqToSelf_( __FILE__, __LINE__, __VA_ARGS__) template - void checkOpertorEqToSelf_(const char (&code)[size], const char* file, int line) { + void checkOpertorEqToSelf_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -2593,11 +2593,17 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } + struct CheckVirtualDestructorOptions + { + CheckVirtualDestructorOptions() = default; + bool inconclusive = false; + }; + // Check that base classes have virtual destructors #define checkVirtualDestructor(...) checkVirtualDestructor_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void checkVirtualDestructor_(const char* file, int line, const char (&code)[size], const CheckVirtualDestructorOptions& options = make_default_obj()) { + const Settings s = settingsBuilder(settings0).certainty(Certainty::inconclusive, options.inconclusive).severity(Severity::warning).build(); // Tokenize.. SimpleTokenizer tokenizer(s, *this); @@ -2892,7 +2898,7 @@ class TestClass : public TestFixture { "public:\n" " ~Base(){}\n" " virtual void foo(){}\n" - "};\n", true); + "};\n", dinit(CheckVirtualDestructorOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Class 'Base' which has virtual members does not have a virtual destructor.\n", errout_str()); checkVirtualDestructor("class Base {\n" @@ -2907,7 +2913,7 @@ class TestClass : public TestFixture { "void foo() {\n" " Base * base = new Derived();\n" " delete base;\n" - "}\n", true); + "}\n", dinit(CheckVirtualDestructorOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Class 'Base' which is inherited by class 'Derived' does not have a virtual destructor.\n", errout_str()); // class Base destructor is not virtual but protected -> no error @@ -2916,14 +2922,14 @@ class TestClass : public TestFixture { " virtual void foo(){}\n" "protected:\n" " ~Base(){}\n" - "};\n", true); + "};\n", dinit(CheckVirtualDestructorOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); checkVirtualDestructor("class C {\n" "private:\n" " C();\n" " virtual ~C();\n" - "};\n", true); + "};\n", dinit(CheckVirtualDestructorOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3580,9 +3586,9 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } -#define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__) +#define checkThisSubtraction(...) checkThisSubtraction_(__FILE__, __LINE__, __VA_ARGS__) template - void checkThisSubtraction_(const char (&code)[size], const char* file, int line) { + void checkThisSubtraction_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -3610,10 +3616,17 @@ class TestClass : public TestFixture { "[test.cpp:3]: (warning) Suspicious pointer subtraction. Did you intend to write '->'?\n", errout_str()); } + struct CheckConstOptions + { + CheckConstOptions() = default; + const Settings *s = nullptr; + bool inconclusive = true; + }; + #define checkConst(...) checkConst_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void checkConst_(const char* file, int line, const char (&code)[size], const CheckConstOptions& options = make_default_obj()) { + const Settings settings = settingsBuilder(options.s ? *options.s : settings0).certainty(Certainty::inconclusive, options.inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings, *this); @@ -4831,7 +4844,7 @@ class TestClass : public TestFixture { " if( m_d != 0 )\n" " return m_iRealVal / m_d;\n" " return dRet;\n" - "};", nullptr, true); + "};"); ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (style, inconclusive) Technically the member function 'A::dGetValue' can be const.\n", errout_str()); } @@ -5829,7 +5842,7 @@ class TestClass : public TestFixture { " void set(const Key& key) {\n" " inherited::set(inherited::Key(key));\n" " }\n" - "};\n", nullptr, false); + "};\n", dinit(CheckConstOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (performance, inconclusive) Either there is a missing 'override', or the member function 'MixerParticipant::GetAudioFrame' can be static.\n", errout_str()); } @@ -7485,10 +7498,10 @@ class TestClass : public TestFixture { " }\n" "};"; - checkConst(code, &settings0, true); + checkConst(code, dinit(CheckConstOptions, $.s = &settings0, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance, inconclusive) Technically the member function 'foo::f' can be static (but you may consider moving to unnamed namespace).\n", errout_str()); - checkConst(code, &settings0, false); // TODO: Set inconclusive to true (preprocess it) + checkConst(code, dinit(CheckConstOptions, $.s = &settings0, $.inconclusive = false)); // TODO: Set inconclusive to true (preprocess it) ASSERT_EQUALS("", errout_str()); } @@ -7614,13 +7627,13 @@ class TestClass : public TestFixture { "};\n" "void S::f() {\n" " std::vector::const_iterator end = std.end();\n" - "}\n", &s); + "}\n", dinit(CheckConstOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (style, inconclusive) Technically the member function 'S::f' can be const.\n", errout_str()); } -#define checkInitializerListOrder(code) checkInitializerListOrder_(code, __FILE__, __LINE__) +#define checkInitializerListOrder(...) checkInitializerListOrder_(__FILE__, __LINE__, __VA_ARGS__) template - void checkInitializerListOrder_(const char (&code)[size], const char* file, int line) { + void checkInitializerListOrder_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings2, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -7769,9 +7782,9 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } -#define checkInitializationListUsage(code) checkInitializationListUsage_(code, __FILE__, __LINE__) +#define checkInitializationListUsage(...) checkInitializationListUsage_(__FILE__, __LINE__, __VA_ARGS__) template - void checkInitializationListUsage_(const char (&code)[size], const char* file, int line) { + void checkInitializationListUsage_(const char* file, int line, const char (&code)[size]) { // Check.. const Settings settings = settingsBuilder().severity(Severity::performance).build(); @@ -7981,9 +7994,9 @@ class TestClass : public TestFixture { } -#define checkSelfInitialization(code) checkSelfInitialization_(code, __FILE__, __LINE__) +#define checkSelfInitialization(...) checkSelfInitialization_(__FILE__, __LINE__, __VA_ARGS__) template - void checkSelfInitialization_(const char (&code)[size], const char* file, int line) { + void checkSelfInitialization_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -8088,12 +8101,11 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } - #define checkVirtualFunctionCall(...) checkVirtualFunctionCall_(__FILE__, __LINE__, __VA_ARGS__) template - void checkVirtualFunctionCall_(const char* file, int line, const char (&code)[size], bool inconclusive = true) { + void checkVirtualFunctionCall_(const char* file, int line, const char (&code)[size]) { // Check.. - const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).certainty(Certainty::inconclusive, inconclusive).build(); + const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).certainty(Certainty::inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings, *this); @@ -8434,9 +8446,9 @@ class TestClass : public TestFixture { } -#define checkOverride(code) checkOverride_(code, __FILE__, __LINE__) +#define checkOverride(...) checkOverride_(__FILE__, __LINE__, __VA_ARGS__) template - void checkOverride_(const char (&code)[size], const char* file, int line) { + void checkOverride_(const char* file, int line, const char (&code)[size]) { const Settings settings = settingsBuilder().severity(Severity::style).build(); // Tokenize.. @@ -8826,9 +8838,9 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } -#define checkUnsafeClassRefMember(code) checkUnsafeClassRefMember_(code, __FILE__, __LINE__) +#define checkUnsafeClassRefMember(...) checkUnsafeClassRefMember_(__FILE__, __LINE__, __VA_ARGS__) template - void checkUnsafeClassRefMember_(const char (&code)[size], const char* file, int line) { + void checkUnsafeClassRefMember_(const char* file, int line, const char (&code)[size]) { /*const*/ Settings settings = settingsBuilder().severity(Severity::warning).build(); settings.safeChecks.classes = true; @@ -8847,9 +8859,9 @@ class TestClass : public TestFixture { } -#define checkThisUseAfterFree(code) checkThisUseAfterFree_(code, __FILE__, __LINE__) +#define checkThisUseAfterFree(...) checkThisUseAfterFree_(__FILE__, __LINE__, __VA_ARGS__) template - void checkThisUseAfterFree_(const char (&code)[size], const char* file, int line) { + void checkThisUseAfterFree_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -9064,9 +9076,9 @@ class TestClass : public TestFixture { } -#define getFileInfo(code) getFileInfo_(code, __FILE__, __LINE__) +#define getFileInfo(...) getFileInfo_(__FILE__, __LINE__, __VA_ARGS__) template - void getFileInfo_(const char (&code)[size], const char* file, int line) { + void getFileInfo_(const char* file, int line, const char (&code)[size]) { // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index db2f41e6da2..4c3a58d967e 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -126,10 +126,19 @@ class TestCondition : public TestFixture { TEST_CASE(knownConditionIncDecOperator); } + struct CheckOptions + { + CheckOptions() = default; + const Settings* s = nullptr; + const char* filename = "test.cpp"; + bool inconclusive = false; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], const Settings &settings, const char* filename = "test.cpp") { - std::vector files(1, filename); + void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) { + const Settings settings = settingsBuilder(options.s ? *options.s : settings0).certainty(Certainty::inconclusive, options.inconclusive).build(); Tokenizer tokenizer(settings, *this); + std::vector files(1, options.filename); PreprocessorHelper::preprocess(code, files, tokenizer, *this); // Tokenizer.. @@ -139,17 +148,12 @@ class TestCondition : public TestFixture { runChecks(tokenizer, this); } - void check_(const char* file, int line, const char code[], const char* filename = "test.cpp", bool inconclusive = false) { - const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build(); - check_(file, line, code, settings, filename); - } - #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) - void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp") + void checkP_(const char* file, int line, const char code[]) { const Settings settings = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build(); - std::vector files(1, filename); + std::vector files(1, "test.cpp"); Tokenizer tokenizer(settings, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -1307,27 +1311,27 @@ class TestCondition : public TestFixture { void incorrectLogicOperator6() { // char literals check("void f(char x) {\n" " if (x == '1' || x == '2') {}\n" - "}", "test.cpp", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(char x) {\n" " if (x == '1' && x == '2') {}\n" - "}", "test.cpp", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x == '1' && x == '2'.\n", errout_str()); check("int f(char c) {\n" " return (c >= 'a' && c <= 'z');\n" - "}", "test.cpp", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int f(char c) {\n" " return (c <= 'a' && c >= 'z');\n" - "}", "test.cpp", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Logical conjunction always evaluates to false: c <= 'a' && c >= 'z'.\n", errout_str()); check("int f(char c) {\n" " return (c <= 'a' && c >= 'z');\n" - "}", "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return value 'c>='z'' is always false\n", errout_str()); } @@ -3140,10 +3144,10 @@ class TestCondition : public TestFixture { check("void f() { A a; }"); ASSERT_EQUALS("", errout_str()); - check("void f() { a(x there are never templates + check("void f() { a(x there are never templates ASSERT_EQUALS("[test.c:1]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n", errout_str()); - check("class A;", "test.cpp"); + check("class A;"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -6050,65 +6054,65 @@ class TestCondition : public TestFixture { check("void f(unsigned char c) {\n" " if (c == 256) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always false.\n", errout_str()); check("void f(unsigned char* b, int i) {\n" // #6372 " if (b[i] == 256) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always false.\n", errout_str()); check("void f(unsigned char c) {\n" " if (c == 255) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("void f(bool b) {\n" " if (b == true) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); // #10372 check("void f(signed char x) {\n" " if (x == 0xff) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed char' against value 255. Condition is always false.\n", errout_str()); check("void f(short x) {\n" " if (x == 0xffff) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed short' against value 65535. Condition is always false.\n", errout_str()); check("void f(int x) {\n" " if (x == 0xffffffff) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("void f(long x) {\n" " if (x == ~0L) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("void f(long long x) {\n" " if (x == ~0LL) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("int f(int x) {\n" " const int i = 0xFFFFFFFF;\n" " if (x == i) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char c;\n" " if ((c = foo()) != -1) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("void f(int x) {\n" " if (x < 3000000000) {}\n" - "}", settingsUnix64); + "}", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed int' against value 3000000000. Condition is always true.\n", errout_str()); check("void f(const signed char i) {\n" // #8545 @@ -6118,7 +6122,7 @@ class TestCondition : public TestFixture { " if (i < +128) {}\n" // warn " if (i <= +127) {}\n" // warn " if (i <= +126) {}\n" - "}\n", settingsUnix64); + "}\n", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'const signed char' against value -129. Condition is always true.\n" "[test.cpp:3]: (style) Comparing expression of type 'const signed char' against value -128. Condition is always true.\n" "[test.cpp:5]: (style) Comparing expression of type 'const signed char' against value 128. Condition is always true.\n" @@ -6142,7 +6146,7 @@ class TestCondition : public TestFixture { " if (255 > u) {}\n" " if (255 <= u) {}\n" " if (255 >= u) {}\n" // warn - "}\n", settingsUnix64); + "}\n", dinit(CheckOptions, $.s = &settingsUnix64)); ASSERT_EQUALS("[test.cpp:3]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always false.\n" "[test.cpp:4]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always true.\n" "[test.cpp:6]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false.\n" diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 226e4e502da..9a6874de65a 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -32,10 +32,17 @@ class TestConstructors : public TestFixture { private: const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build(); + struct CheckOptions + { + CheckOptions() = default; + bool inconclusive = false; + const Settings* s = nullptr; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).certainty(Certainty::inconclusive, options.inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); @@ -46,17 +53,6 @@ class TestConstructors : public TestFixture { checkClass.constructors(); } - 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); - - // Check class constructors.. - CheckClass checkClass(&tokenizer, &s, this); - checkClass.constructors(); - } - void run() override { TEST_CASE(simple1); TEST_CASE(simple2); @@ -344,7 +340,7 @@ class TestConstructors : public TestFixture { "Fred::Fred(int _i)\n" "{\n" " i = _i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:8]: (warning, inconclusive) Member variable 'Fred::i' is not initialized in the constructor.\n", errout_str()); } @@ -534,7 +530,7 @@ class TestConstructors : public TestFixture { " S() = default;\n" " S(const S& s) {}\n" " S& operator=(const S& s) { return *this; }\n" - "};\n", /*inconclusive*/ true); + "};\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning, inconclusive) Member variable 'S::i' is not assigned in the copy constructor. Should it be copied?\n" "[test.cpp:5]: (warning) Member variable 'S::i' is not assigned a value in 'S::operator='.\n", errout_str()); @@ -947,7 +943,7 @@ class TestConstructors : public TestFixture { " Fred & operator=(const Fred &rhs) {\n" " return *this;\n" " }\n" - "};",true); + "};",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'Fred::data' is not assigned a value in 'Fred::operator='.\n", errout_str()); check("struct Fred {\n" @@ -955,7 +951,7 @@ class TestConstructors : public TestFixture { " Fred & operator=(const Fred &rhs) {\n" " return *this;\n" " }\n" - "};",true); + "};",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='.\n", errout_str()); check("struct Fred {\n" @@ -963,7 +959,7 @@ class TestConstructors : public TestFixture { " Fred & operator=(const Fred &rhs) {\n" " return *this;\n" " }\n" - "};",true); + "};",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'Fred::data' is not assigned a value in 'Fred::operator='.\n", errout_str()); } @@ -982,7 +978,7 @@ class TestConstructors : public TestFixture { " if (this != &Src)\n" " Copy(Src);\n" " return *this;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1333,7 +1329,7 @@ class TestConstructors : public TestFixture { "public:\n" " A(int n) : A() { }\n" " A() {}\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'A::number' is not initialized in the constructor.\n" "[test.cpp:5]: (warning, inconclusive) Member variable 'A::number' is not initialized in the constructor.\n", errout_str()); @@ -1367,7 +1363,7 @@ class TestConstructors : public TestFixture { "public:\n" " A(int n) : A{} { }\n" " A() {}\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'A::number' is not initialized in the constructor.\n" "[test.cpp:5]: (warning, inconclusive) Member variable 'A::number' is not initialized in the constructor.\n", errout_str()); @@ -1594,7 +1590,7 @@ class TestConstructors : public TestFixture { " Fred();\n" "};\n" "Fred::Fred()\n" - "{ }", s); + "{ }", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout_str()); } @@ -1607,7 +1603,7 @@ class TestConstructors : public TestFixture { " Fred();\n" "};\n" "Fred::Fred()\n" - "{ }", s); + "{ }", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } } @@ -1630,7 +1626,7 @@ class TestConstructors : public TestFixture { "public:\n" " Fred() { };\n" " Fred(const Fred &) { };\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:7]: (warning, inconclusive) Member variable 'Fred::var' is not assigned in the copy constructor. Should it be copied?\n", errout_str()); check("class Fred\n" @@ -1642,7 +1638,7 @@ class TestConstructors : public TestFixture { " Fred(const Fred &);\n" "};\n" "Fred::Fred() { };\n" - "Fred::Fred(const Fred &) { };\n", true); + "Fred::Fred(const Fred &) { };\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:10]: (warning, inconclusive) Member variable 'Fred::var' is not assigned in the copy constructor. Should it be copied?\n", errout_str()); check("class Baz {};\n" // #8496 @@ -1651,7 +1647,7 @@ class TestConstructors : public TestFixture { " explicit Bar(Baz* pBaz = NULL) : i(0) {}\n" " Bar(const Bar& bar) {}\n" " int i;\n" - "};\n", true); + "};\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Bar::i' is not initialized in the copy constructor.\n", errout_str()); } @@ -1825,7 +1821,7 @@ class TestConstructors : public TestFixture { " B(const B&){}\n" " B(B &&){}\n" " const B& operator=(const B&){return *this;}\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:11]: (warning, inconclusive) Member variable 'B::a' is not assigned in the copy constructor. Should it be copied?\n" "[test.cpp:12]: (warning, inconclusive) Member variable 'B::a' is not assigned in the move constructor. Should it be moved?\n" "[test.cpp:13]: (warning, inconclusive) Member variable 'B::a' is not assigned a value in 'B::operator='.\n", @@ -1891,7 +1887,7 @@ class TestConstructors : public TestFixture { " A(){}\n" " A(const A&){}\n" " const A& operator=(const A&){return *this;}\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("class B\n" @@ -1908,7 +1904,7 @@ class TestConstructors : public TestFixture { " A(){}\n" " A(const A&){}\n" " const A& operator=(const A&){return *this;}\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:12]: (warning) Member variable 'A::m_SemVar' is not initialized in the constructor.\n" "[test.cpp:13]: (warning) Member variable 'A::m_SemVar' is not initialized in the copy constructor.\n" "[test.cpp:14]: (warning) Member variable 'A::m_SemVar' is not assigned a value in 'A::operator='.\n", errout_str()); @@ -1926,7 +1922,7 @@ class TestConstructors : public TestFixture { " B b;\n" " A() {}\n" " A(const A& rhs) {}\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning, inconclusive) Member variable 'A::b' is not assigned in the copy constructor. Should it be copied?\n", errout_str()); } @@ -1942,7 +1938,7 @@ class TestConstructors : public TestFixture { "}\n" "void S::Set(const T& val) {\n" " t = val;\n" - "}", /*inconclusive*/ true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -2056,7 +2052,7 @@ class TestConstructors : public TestFixture { " d = rhs.get();\n" " }\n" " double d;\n" - "};", s); + "};", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #8485 @@ -2083,14 +2079,14 @@ class TestConstructors : public TestFixture { "{ }\n" "\n" "void Fred::operator=(const Fred &f)\n" - "{ }", true); + "{ }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:13]: (warning, inconclusive) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='.\n", errout_str()); const Settings s = settingsBuilder().certainty(Certainty::inconclusive).severity(Severity::style).severity(Severity::warning).library("std.cfg").build(); check("struct S {\n" " S& operator=(const S& s) { return *this; }\n" " std::mutex m;\n" - "};\n", s); + "};\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -2774,7 +2770,7 @@ class TestConstructors : public TestFixture { "::Foo::Sub::Sub() { }\n" "class Foo;\n" "class Bar;\n" - "class Sub;\n", true); + "class Sub;\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:9]: (warning, inconclusive) Member variable 'Sub::b' is not initialized in the constructor.\n" "[test.cpp:12]: (warning) Member variable 'Sub::b' is not initialized in the constructor.\n" @@ -2823,7 +2819,7 @@ class TestConstructors : public TestFixture { " int c;\n" " F(int x = 0, int y = 0, int z = 0);\n" "};\n" - "F::F(int, int, int) { }\n", true); + "F::F(int, int, int) { }\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'A::a' is not initialized in the constructor.\n" "[test.cpp:7]: (warning) Member variable 'A::b' is not initialized in the constructor.\n" "[test.cpp:7]: (warning) Member variable 'A::c' is not initialized in the constructor.\n" @@ -3230,7 +3226,7 @@ class TestConstructors : public TestFixture { " std::array e;\n" " std::array f;\n" "S() {}\n" - "};\n", s); + "};\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'S::a' is not initialized in the constructor.\n" "[test.cpp:10]: (warning) Member variable 'S::b' is not initialized in the constructor.\n" @@ -3665,7 +3661,7 @@ class TestConstructors : public TestFixture { check("class Foo {\n" " int foo;\n" " Foo() { }\n" - "};", s); + "};", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -3674,7 +3670,7 @@ class TestConstructors : public TestFixture { check("class Foo {\n" " int foo;\n" " Foo() { }\n" - "};", s); + "};", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor.\n", errout_str()); } } @@ -3736,7 +3732,7 @@ class TestConstructors : public TestFixture { " Fred() { }\n" "private:\n" " int x;\n" - "};", s); + "};", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -4221,7 +4217,7 @@ class TestConstructors : public TestFixture { " x = 1;\n" " return true;\n" " }\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor.\n", "[test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor.\n", errout_str()); @@ -4235,7 +4231,7 @@ class TestConstructors : public TestFixture { " x = 1;\n" " return true;\n" " }\n" - "};", true); + "};", dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor.\n", "[test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor.\n", errout_str()); diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index 8b7b49952b3..e9578324014 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -57,10 +57,17 @@ class TestExceptionSafety : public TestFixture { TEST_CASE(rethrowNoCurrentException3); } + struct CheckOptions + { + CheckOptions() = default; + bool inconclusive = false; + const Settings *s = nullptr; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).certainty(Certainty::inconclusive, options.inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); @@ -145,7 +152,7 @@ class TestExceptionSafety : public TestFixture { " if (foo)\n" " throw 1;\n" " p = new int;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -153,7 +160,7 @@ class TestExceptionSafety : public TestFixture { " delete p;\n" " reset(p);\n" " throw 1;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -169,7 +176,7 @@ class TestExceptionSafety : public TestFixture { " static int* p = 0;\n" " delete p;\n" " throw 1;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning) Exception thrown in invalid state, 'p' points at deallocated memory.\n", errout_str()); } @@ -385,7 +392,7 @@ class TestExceptionSafety : public TestFixture { " try {\n" " myThrowingFoo();\n" " } catch(MyException &) {}\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function myThrowingFoo().\n", errout_str()); } @@ -394,7 +401,7 @@ class TestExceptionSafety : public TestFixture { "int main()\n" "{\n" " f();\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -411,12 +418,12 @@ class TestExceptionSafety : public TestFixture { " f();\n" "}\n"; - check(code, true); + check(code, dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function f().\n" "[test.cpp:6] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function f().\n", errout_str()); const Settings s = settingsBuilder().library("gnu.cfg").build(); - check(code, true, &s); + check(code, dinit(CheckOptions, $.inconclusive = true, $.s = &s)); ASSERT_EQUALS("", errout_str()); } diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index ea586270cd7..ece2887240b 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -112,15 +112,21 @@ class TestFunctions : public TestFixture { TEST_CASE(checkUseStandardLibrary14); } + struct CheckOptions + { + CheckOptions() = default; + bool cpp = true; + const Settings* s = nullptr; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char* file, int line, const char (&code)[size], bool cpp = true, const Settings* settings_ = nullptr) { - if (!settings_) - settings_ = &settings; + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings& s = options.s ? *options.s : settings; // Tokenize.. - SimpleTokenizer tokenizer(*settings_, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + SimpleTokenizer tokenizer(s, *this); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); runChecks(tokenizer, this); } @@ -265,26 +271,26 @@ class TestFunctions : public TestFixture { check("void f()\n" "{\n" " char *x = alloca(10);\n" - "}", false); + "}", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (warning) Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n", errout_str()); const Settings s = settingsBuilder(settings).c(Standards::C89).cpp(Standards::CPP03).build(); check("void f()\n" "{\n" " char *x = alloca(10);\n" - "}", true, &s); // #4382 - there are no VLAs in C++ + "}", dinit(CheckOptions, $.s = &s)); // #4382 - there are no VLAs in C++ ASSERT_EQUALS("", errout_str()); check("void f()\n" "{\n" " char *x = alloca(10);\n" - "}", false, &s); // #7558 - no alternative to alloca in C89 + "}", dinit(CheckOptions, $.cpp = false, $.s = &s)); // #7558 - no alternative to alloca in C89 ASSERT_EQUALS("", errout_str()); check("void f()\n" "{\n" " char *x = alloca(10);\n" - "}", false, &s); + "}", dinit(CheckOptions, $.cpp = false, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -452,7 +458,7 @@ class TestFunctions : public TestFixture { check("void record(char* buf, int n) {\n" " memset(buf, 0, n < 255);\n" /* KO */ " memset(buf, 0, n < 255 ? n : 255);\n" /* OK */ - "}", false); + "}", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:2]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str()); // Ticket #6588 (c++ mode) @@ -754,7 +760,7 @@ class TestFunctions : public TestFixture { check("size_t f() { wchar_t x = L'x'; return wcslen(&x); }"); ASSERT_EQUALS("[test.cpp:1]: (error) Invalid wcslen() argument nr 1. A nul-terminated string is required.\n", errout_str()); - check("void f() { char a[10] = \"1234567890\"; puts(a); }", false); // #1770 + check("void f() { char a[10] = \"1234567890\"; puts(a); }", dinit(CheckOptions, $.cpp = false)); // #1770 ASSERT_EQUALS("[test.c:1]: (error) Invalid puts() argument nr 1. A nul-terminated string is required.\n", errout_str()); } @@ -769,7 +775,7 @@ class TestFunctions : public TestFixture { " memmove(&tgt, &src, sizeof src);\n" " memset(&tgt + sizeof src, ' ', sizeof tgt - sizeof src);\n" " }\n" - "}\n", false, &settingsUnix32); + "}\n", dinit(CheckOptions, $.cpp = false, $.s = &settingsUnix32)); ASSERT_EQUALS("", errout_str()); } @@ -1321,67 +1327,67 @@ class TestFunctions : public TestFixture { check("void foo() {\n" " mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function mystrcmp() is not used.\n", errout_str()); check("void foo() {\n" " foo::mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function foo::mystrcmp() is not used.\n", errout_str()); check("void f() {\n" " foo x;\n" " x.mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function x.mystrcmp() is not used.\n", errout_str()); check("bool mystrcmp(char* a, char* b);\n" // cppcheck sees a custom strcmp definition, but it returns a value. Assume it is the one specified in the library. "void foo() {\n" " mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function mystrcmp() is not used.\n", errout_str()); check("void mystrcmp(char* a, char* b);\n" // cppcheck sees a custom strcmp definition which returns void! "void foo() {\n" " mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " class mystrcmp { mystrcmp() {} };\n" // strcmp is a constructor definition here - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " return mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " return foo::mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " if(mystrcmp(a, b));\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " bool b = mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); // #6194 check("void foo() {\n" " MyStrCmp mystrcmp(x, y);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); // #6197 check("void foo() {\n" " abc::def.mystrcmp(a,b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); // #6233 @@ -1404,18 +1410,18 @@ class TestFunctions : public TestFixture { // #7447 check("void foo() {\n" " int x{mystrcmp(a,b)};\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); // #7905 check("void foo() {\n" " int x({mystrcmp(a,b)});\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" // don't crash " DEBUG(123)(mystrcmp(a,b))(fd);\n" - "}", false, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); check("struct teststruct {\n" " int testfunc1() __attribute__ ((warn_unused_result)) { return 1; }\n" " [[nodiscard]] int testfunc2() { return 1; }\n" @@ -1426,7 +1432,7 @@ class TestFunctions : public TestFixture { " TestStruct1.testfunc1();\n" " TestStruct1.testfunc2();\n" " return 0;\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:4]: (warning) Return value of function testfunc1() is not used.\n" "[test.cpp:4]: (warning) Return value of function testfunc2() is not used.\n" "[test.cpp:8]: (warning) Return value of function TestStruct1.testfunc1() is not used.\n" @@ -1450,7 +1456,7 @@ class TestFunctions : public TestFixture { // #8412 - unused operator result check("void foo() {\n" " !mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function mystrcmp() is not used.\n", errout_str()); check("void f(std::vector v) {\n" @@ -1482,7 +1488,7 @@ class TestFunctions : public TestFixture { check("void foo() {\n" " mystrcmp(a, b);\n" - "}", true, &settings2); + "}", dinit(CheckOptions, $.s = &settings2)); ASSERT_EQUALS("[test.cpp:2]: (style) Error code from the return value of function mystrcmp() is not used.\n", errout_str()); } @@ -1588,16 +1594,16 @@ class TestFunctions : public TestFixture { { const Settings s = settingsBuilder().c(Standards::C89).build(); - check(code, false, &s); // c code (c89) + check(code, dinit(CheckOptions, $.cpp = false, $.s = &s)); // c code (c89) ASSERT_EQUALS("[test.c:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str()); } { const Settings s = settingsBuilder().c(Standards::C99).build(); - check(code, false, &s); // c code (c99) + check(code, dinit(CheckOptions, $.cpp = false, $.s = &s)); // c code (c99) ASSERT_EQUALS("", errout_str()); - check(code, true, &s); // c++ code + check(code, dinit(CheckOptions, $.s = &s)); // c++ code ASSERT_EQUALS("", errout_str()); } } @@ -1947,12 +1953,12 @@ class TestFunctions : public TestFixture { check("void f() {\n" " lib_func();" - "}", true, &s); + "}", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout_str()); check("void f(void* v) {\n" " lib_func(v);" - "}", true, &s); + "}", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout_str()); // #10105 @@ -1968,7 +1974,7 @@ class TestFunctions : public TestFixture { "\n" " void testFunctionReturnType() {\n" " }\n" - "};", true, &s); + "};", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); // #11183 @@ -1978,7 +1984,7 @@ class TestFunctions : public TestFixture { "\n" "void f() {\n" " cb(std::string(\"\"));\n" - "}", true, &s); + "}", dinit(CheckOptions, $.s = &s)); TODO_ASSERT_EQUALS("", "[test.cpp:6]: (information) --check-library: There is no matching configuration for function cb()\n", errout_str()); // #7375 @@ -1986,38 +1992,38 @@ class TestFunctions : public TestFixture { " struct S { int i; char c; };\n" " size_t s = sizeof(S);\n" " static_assert(s == 9);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f(char) {}\n" "void g() {\n" " f(int8_t(1));\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f(std::uint64_t& u) {\n" " u = std::uint32_t(u) * std::uint64_t(100);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); - check("void f() { throw(1); }\n", true, &s); // #8958 + check("void f() { throw(1); }\n", dinit(CheckOptions, $.s = &s)); // #8958 ASSERT_EQUALS("", errout_str()); check("using namespace std;\n" - "void f() { throw range_error(\"abc\"); }\n", true, &s); + "void f() { throw range_error(\"abc\"); }\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("class C {\n" // #9002 "public:\n" " static int f() { return 1; }\n" "};\n" - "void g() { C::f(); }\n", true, &s); + "void g() { C::f(); }\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector& v) {\n" // #11223 " for (const auto& s : v)\n" " s.find(\"\");\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function s.find() is not used.\n", errout_str()); check("void f() {\n" @@ -2027,19 +2033,19 @@ class TestFunctions : public TestFixture { " q->push_back(1);\n" " auto* r = new std::vector;\n" " r->push_back(1);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " auto p = std::make_shared>();\n" " p->push_back(1);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f(std::vector>& v) {\n" " auto it = v.begin();\n" " it->push_back(1);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -2049,7 +2055,7 @@ class TestFunctions : public TestFixture { " w.push_back(1);\n" " auto x = std::vector(1);\n" " x.push_back(1);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -2057,7 +2063,7 @@ class TestFunctions : public TestFixture { " p->push_back(1);\n" " auto q{ std::make_shared>{} };\n" " q->push_back(1);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); TODO_ASSERT_EQUALS("", "[test.cpp:2]: (debug) auto token with no type.\n" "[test.cpp:4]: (debug) auto token with no type.\n" @@ -2070,12 +2076,12 @@ class TestFunctions : public TestFixture { " std::list::iterator it;\n" " for (it = l.begin(); it != l.end(); ++it)\n" " it->g(0);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", filter_valueflow(errout_str())); check("auto f() {\n" " return std::runtime_error(\"abc\");\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); TODO_ASSERT_EQUALS("", "[test.cpp:1]: (debug) auto token with no type.\n", errout_str()); @@ -2088,7 +2094,7 @@ class TestFunctions : public TestFixture { "void S::f(int i) const {\n" " for (const std::shared_ptr& c : v)\n" " c->f(i);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("namespace N {\n" @@ -2097,29 +2103,29 @@ class TestFunctions : public TestFixture { "void f() {\n" " const auto& t = N::S::s;\n" " if (t.find(\"abc\") != t.end()) {}\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", filter_valueflow(errout_str())); check("void f(std::vector>>& v, int i, int j) {\n" " auto& s = v[i][j];\n" " s.insert(0);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("int f(const std::vector& v, int i, char c) {\n" " const auto& s = v[i];\n" " return s.find(c);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #11604 " int (*g)() = nullptr;\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " INT (*g)() = nullptr;\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("struct T;\n" @@ -2128,13 +2134,13 @@ class TestFunctions : public TestFixture { " auto p = get();\n" " p->h(i);\n" " p.reset(nullptr);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("[test.cpp:5]: (information) --check-library: There is no matching configuration for function T::h()\n", errout_str()); check("struct S : std::vector {\n" " void f(int i) { push_back(i); }\n" - "};\n", true, &s); + "};\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -2144,18 +2150,18 @@ class TestFunctions : public TestFixture { " auto h{ []() -> std::string { return \"xyz\"; } };\n" " auto t = h();\n" " if (t.at(0)) {}\n" - "};\n", true, &s); + "};\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", filter_valueflow(errout_str())); check("::std::string f(const char* c) {\n" // #12365 " return ::std::string(c);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("template \n" "struct S : public std::vector {\n" " void resize(size_t n) { std::vector::resize(n); }\n" - "};\n", true, &s); + "};\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); check("struct P {\n" // #13105 @@ -2166,7 +2172,7 @@ class TestFunctions : public TestFixture { " auto it = m->find(i);\n" " const bool b = it != m->end();\n" " return b;\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.s = &s)); TODO_ASSERT_EQUALS("", "[test.cpp:6]: (debug) auto token with no type.\n", errout_str()); } diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 2a77ca86b0b..c2135cbef4f 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -271,7 +271,7 @@ class TestGarbage : public TestFixture { TEST_CASE(nonGarbageCode1); // #8346 } -#define checkCodeInternal(code, filename) checkCodeInternal_(code, filename, __FILE__, __LINE__) +#define checkCodeInternal(...) checkCodeInternal_(__FILE__, __LINE__, __VA_ARGS__) template std::string checkCode(const char (&code)[size], bool cpp = true) { // double the tests - run each example as C as well as C++ @@ -285,7 +285,7 @@ class TestGarbage : public TestFixture { } template - std::string checkCodeInternal_(const char (&code)[size], bool cpp, const char* file, int line) { + std::string checkCodeInternal_(const char* file, int line, const char (&code)[size], bool cpp) { // tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); @@ -298,9 +298,9 @@ class TestGarbage : public TestFixture { return tokenizer.tokens()->stringifyList(false, false, false, true, false, nullptr, nullptr); } -#define getSyntaxError(code) getSyntaxError_(code, __FILE__, __LINE__) +#define getSyntaxError(...) getSyntaxError_(__FILE__, __LINE__, __VA_ARGS__) template - std::string getSyntaxError_(const char (&code)[size], const char* file, int line) { + std::string getSyntaxError_(const char* file, int line, const char (&code)[size]) { SimpleTokenizer tokenizer(settings, *this); try { ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 48637912628..db363258106 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -33,11 +33,18 @@ class TestIncompleteStatement : public TestFixture { private: const Settings settings = settingsBuilder().severity(Severity::warning).build(); + struct CheckOptions + { + CheckOptions() = default; + bool inconclusive = false; + bool cpp = true; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], bool inconclusive = false, bool cpp = true) { - const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build(); + void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build(); - std::vector files(1, cpp ? "test.cpp" : "test.c"); + std::vector files(1, options.cpp ? "test.cpp" : "test.c"); Tokenizer tokenizer(settings1, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -422,7 +429,7 @@ class TestIncompleteStatement : public TestFixture { " (!x);\n" " (unsigned int)!x;\n" " ~x;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:3]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:4]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" @@ -433,7 +440,7 @@ class TestIncompleteStatement : public TestFixture { "[test.cpp:9]: (warning, inconclusive) Found suspicious operator '~', result is not used.\n", errout_str()); - check("void f1(int x) { x; }", true); + check("void f1(int x) { x; }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:1]: (warning) Unused variable value 'x'\n", errout_str()); check("void f() { if (Type t; g(t)) {} }"); // #9776 @@ -657,17 +664,17 @@ class TestIncompleteStatement : public TestFixture { check("void f(std::string a, std::string b) {\n" // #7529 " const std::string s = \" x \" + a;\n" " +\" y = \" + b;\n" - "}\n", /*inconclusive*/ true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n", errout_str()); check("void f() {\n" " *new int;\n" - "}\n", /*inconclusive*/ true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n", errout_str()); check("void f(int x, int y) {\n" // #12525 " x * y;\n" - "}\n", /*inconclusive*/ true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n", errout_str()); check("void f() {\n" // #5475 @@ -675,14 +682,14 @@ class TestIncompleteStatement : public TestFixture { "}\n" "void f(std::string& a) {\n" " a.erase(3) + \"suf\";\n" - "}\n", /*inconclusive*/ true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n" "[test.cpp:5]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n", errout_str()); check("void f(XMLElement& parent) {\n" // #11234 " auto** elem = &parent.firstChild;\n" - "}\n", /*inconclusive*/ true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #11301 @@ -729,62 +736,62 @@ class TestIncompleteStatement : public TestFixture { void vardecl() { // #8984 - check("void f() { a::b *c = d(); }", true); + check("void f() { a::b *c = d(); }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { std::vector *c; }", true); + check("void f() { std::vector *c; }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { a::b &c = d(); }", true); + check("void f() { a::b &c = d(); }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { std::vector &c; }", true); + check("void f() { std::vector &c; }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { a::b &&c = d(); }", true); + check("void f() { a::b &&c = d(); }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { std::vector &&c; }", true); + check("void f() { std::vector &&c; }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { char * const * a, * const * b; }", true); + check("void f() { char * const * a, * const * b; }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { char * const * a = 0, * volatile restrict * b; }", true, /*cpp*/ false); + check("void f() { char * const * a = 0, * volatile restrict * b; }", dinit(CheckOptions, $.inconclusive = true, $.cpp = false)); ASSERT_EQUALS("", errout_str()); - check("void f() { char * const * a = 0, * volatile const * b; }", true); + check("void f() { char * const * a = 0, * volatile const * b; }", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } void archive() { check("void f(Archive &ar) {\n" " ar & x;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(int ar) {\n" " ar & x;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '&', result is not used.\n", errout_str()); } void ast() { - check("struct c { void a() const { for (int x=0; x;); } };", true); + check("struct c { void a() const { for (int x=0; x;); } };", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } void oror() { check("void foo() {\n" " params_given (params, \"overrides\") || (overrides = \"1\");\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::ifstream& file) {\n" // #10930 " int a{}, b{};\n" " (file >> a) || (file >> b);\n" " (file >> a) && (file >> b);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } }; diff --git a/test/testinternal.cpp b/test/testinternal.cpp index f0a9926b775..eb4feddde46 100644 --- a/test/testinternal.cpp +++ b/test/testinternal.cpp @@ -48,9 +48,9 @@ class TestInternal : public TestFixture { TEST_CASE(checkRedundantTokCheck); } -#define check(code) check_(code, __FILE__, __LINE__) +#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char (&code)[size], const char* file, int line) { + 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/testleakautovar.cpp b/test/testleakautovar.cpp index c97fb349774..7838679d6dd 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -214,14 +214,21 @@ class TestLeakAutoVar : public TestFixture { TEST_CASE(functionCallLeakIgnoreConfig); // #7923 } + struct CheckOptions + { + CheckOptions() = default; + bool cpp = false; + const Settings *s = nullptr; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).checkLibrary().build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); // Check for leaks.. runChecks(tokenizer, this); @@ -352,7 +359,7 @@ class TestLeakAutoVar : public TestFixture { " char * &ref = p;\n" " p = malloc(10);\n" " free(ref);\n" - "}", /*cpp*/ true); + "}", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("", "[test.cpp:6]: (error) Memory leak: p\n", errout_str()); } @@ -366,7 +373,7 @@ class TestLeakAutoVar : public TestFixture { check("void f(int x) {\n" " char *p;\n" " if (x && (p = new char[10])) { }" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout_str()); } @@ -428,7 +435,7 @@ class TestLeakAutoVar : public TestFixture { void assign20() { // #9187 check("void f() {\n" " char *p = static_cast(malloc(10));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout_str()); } @@ -436,14 +443,14 @@ class TestLeakAutoVar : public TestFixture { check("void f(int **x) {\n" // #10186 " void *p = malloc(10);\n" " *x = (int*)p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" // #10996 "void f() {\n" " S* s = new S();\n" " (void)s;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: s\n", errout_str()); } @@ -451,12 +458,12 @@ class TestLeakAutoVar : public TestFixture { const Settings s = settingsBuilder().library("posix.cfg").build(); check("void f(char tempFileName[256]) {\n" " const int fd = socket(AF_INET, SOCK_PACKET, 0 );\n" - "}", true, &s); + "}", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: fd\n", errout_str()); check("void f() {\n" " const void * const p = malloc(10);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout_str()); } @@ -478,7 +485,7 @@ class TestLeakAutoVar : public TestFixture { " ((*&n12)) = open(\"xx.log\", O_RDONLY);\n" " *(&(*&n13)) = open(\"xx.log\", O_RDONLY);\n" " ((*&(*&n14))) = open(\"xx.log\", O_RDONLY);\n" - "}\n", true, &s); + "}\n", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("[test.cpp:17]: (error) Resource leak: n1\n" "[test.cpp:17]: (error) Resource leak: n2\n" "[test.cpp:17]: (error) Resource leak: n3\n" @@ -501,7 +508,7 @@ class TestLeakAutoVar : public TestFixture { " char* data = new char[100];\n" " char** dataPtr = &data;\n" " delete[] *dataPtr;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -509,14 +516,14 @@ class TestLeakAutoVar : public TestFixture { " char** dataPtr = &data;\n" " printf(\"test\");\n" " delete[] *dataPtr;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #9279 " int* p = new int;\n" " *p = 42;\n" " g();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function g() should have configuration\n", errout_str()); @@ -525,7 +532,7 @@ class TestLeakAutoVar : public TestFixture { " int* p = new int;\n" " *p = 42;\n" " g();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: p\n", errout_str()); check("void g() {}\n" @@ -533,7 +540,7 @@ class TestLeakAutoVar : public TestFixture { " int* p = new int;\n" " *p = 42;\n" " g();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: p\n", errout_str()); check("[[noreturn]] void g();\n" @@ -541,7 +548,7 @@ class TestLeakAutoVar : public TestFixture { " int* p = new int;\n" " *p = 42;\n" " g();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void g() { exit(1); }\n" @@ -549,7 +556,7 @@ class TestLeakAutoVar : public TestFixture { " int* p = new int;\n" " *p = 42;\n" " g();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void g() {}\n" // #10517 @@ -564,7 +571,7 @@ class TestLeakAutoVar : public TestFixture { check("void f() {\n" // #11796 " int* p{ new int };\n" " int* q(new int);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n" "[test.cpp:4]: (error) Memory leak: q\n", errout_str()); @@ -580,7 +587,7 @@ class TestLeakAutoVar : public TestFixture { "void S::g() {\n" " FD* fd{ new FD(this) };\n" " fd->exec();\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct C {\n" // #12327 @@ -607,7 +614,7 @@ class TestLeakAutoVar : public TestFixture { " C c(p);\n" " li.push_back(c);\n" " delete[] li.front().m_p;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #12890 @@ -620,7 +627,7 @@ class TestLeakAutoVar : public TestFixture { " delete p[0];\n" " std::free(p);\n" " }\n" - "};\n", true); + "};\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -628,13 +635,13 @@ class TestLeakAutoVar : public TestFixture { check("void f(int*& x) {\n" // #8235 " int* p = (int*)malloc(10);\n" " x = p ? p : nullptr;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(int*& x) {\n" " int* p = (int*)malloc(10);\n" " x = p != nullptr ? p : nullptr;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -643,7 +650,7 @@ class TestLeakAutoVar : public TestFixture { check("void f(char** old, char* value) {\n" " char *str = strdup(value);\n" " memcpy(old, &str, sizeof(char*));\n" - "}\n", &s); + "}\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -652,24 +659,24 @@ class TestLeakAutoVar : public TestFixture { check("void f(char* old, char* value, size_t len) {\n" " char *str = strdup(value);\n" " memcpy(old, str, len);\n" - "}\n", &s); + "}\n", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: str\n", errout_str()); } void isAutoDealloc() { check("void f() {\n" " char *p = new char[100];" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2]: (error) Memory leak: p\n", errout_str()); check("void f() {\n" " Fred *fred = new Fred;" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " std::string *str = new std::string;" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2]: (error) Memory leak: str\n", errout_str()); check("class TestType {\n" // #9028 @@ -678,13 +685,13 @@ class TestLeakAutoVar : public TestFixture { "};\n" "void f() {\n" " TestType *tt = new TestType();\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: tt\n", errout_str()); check("void f(Bar& b) {\n" // #7622 " char* data = new char[10];\n" " b = Bar(*new Foo(data));\n" - "}", /*cpp*/ true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function Foo() should have / configuration\n", errout_str()); check("class B {};\n" @@ -692,7 +699,7 @@ class TestLeakAutoVar : public TestFixture { " void g() {\n" " auto d = new D();\n" " if (d) {}\n" - "}", /*cpp*/ true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: d\n", errout_str()); check("struct S {\n" // #12354 @@ -703,7 +710,7 @@ class TestLeakAutoVar : public TestFixture { " if (b)\n" " p = new S();\n" " p->f();\n" - "}", /*cpp*/ true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: p\n", errout_str()); } @@ -821,13 +828,13 @@ class TestLeakAutoVar : public TestFixture { check("void f(char *p) {\n" " if (!p) delete p;\n" " return p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(char *p) {\n" " if (!p) delete [] p;\n" " return p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(void* p) {\n" @@ -862,14 +869,14 @@ class TestLeakAutoVar : public TestFixture { "void f(Foo* foo) {\n" " delete foo->ptr;\n" " foo->ptr = new Foo;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct Foo { int* ptr; };\n" "void f(Foo* foo) {\n" " delete foo->ptr;\n" " x = *foo->ptr;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'ptr' after it is deallocated / released\n", "", errout_str()); check("void parse() {\n" @@ -878,7 +885,7 @@ class TestLeakAutoVar : public TestFixture { " ~Buf() { delete[]m_buf; }\n" " uint8_t *m_buf;\n" " };\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct Foo {\n" @@ -890,7 +897,7 @@ class TestLeakAutoVar : public TestFixture { " delete foo->ptr;\n" " foo->ptr = new Foo;\n" " foo->ptr->func();\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void foo(void (*conv)(char**)) {\n" @@ -906,7 +913,7 @@ class TestLeakAutoVar : public TestFixture { " int *ptr = new int;\n" " delete(ptr);\n" " *ptr = 0;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'ptr' after it is deallocated / released\n", errout_str()); } @@ -915,7 +922,7 @@ class TestLeakAutoVar : public TestFixture { " std::shared_ptr sp(p);\n" " bool b = p->foo();\n" " return b;\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct A {\n" // #8635 @@ -925,7 +932,7 @@ class TestLeakAutoVar : public TestFixture { " array_.push_back(std::unique_ptr(a));\n" " return a;\n" " }\n" - "};\n", /*cpp*/ true); + "};\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("int g(int *p) {\n" // #9838 @@ -934,7 +941,7 @@ class TestLeakAutoVar : public TestFixture { "}\n" "int f() {\n" " return g(new int(3));\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -957,7 +964,7 @@ class TestLeakAutoVar : public TestFixture { " int *array = new int[42];\n" " delete [] array;\n" " return array[1];" // << - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Returning/dereferencing 'array' after it is deallocated / released\n", errout_str()); check("int f() {\n" @@ -987,7 +994,7 @@ class TestLeakAutoVar : public TestFixture { " auto* b = static_cast(malloc(8));\n" " free(b);\n" " b[1] = 0;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'a' after it is deallocated / released\n" "[test.cpp:7]: (error) Dereferencing 'b' after it is deallocated / released\n", errout_str()); @@ -999,7 +1006,7 @@ class TestLeakAutoVar : public TestFixture { " S* s = new S;\n" " delete s;\n" " s->f();\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 's' after it is deallocated / released\n", errout_str()); @@ -1066,7 +1073,7 @@ class TestLeakAutoVar : public TestFixture { " free(p);\n" " }, 1);\n" " return 0;\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -1083,7 +1090,7 @@ class TestLeakAutoVar : public TestFixture { " if (fd == nullptr)\n" " return false;\n" " return fclose(fd) == 0;\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("int f(const char* fileName) {\n" // #13136 @@ -1102,7 +1109,7 @@ class TestLeakAutoVar : public TestFixture { " int *c = new int;\n" " delete (a, c);\n" " *c = 10;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'c' after it is deallocated / released\n", errout_str()); } @@ -1249,28 +1256,28 @@ class TestLeakAutoVar : public TestFixture { "void foo(char *p) {\n" " delete p;\n" " delete p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Memory pointed to by 'p' is freed twice.\n", errout_str()); check( "void foo(char *p, char *r) {\n" " delete p;\n" " delete r;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( "void foo(P p) {\n" " delete p.x;\n" " delete p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( "void foo(char **p) {\n" " delete p[0];\n" " delete p[1];\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1278,7 +1285,7 @@ class TestLeakAutoVar : public TestFixture { " delete p;\n" " getNext(&p);\n" " delete p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1286,21 +1293,21 @@ class TestLeakAutoVar : public TestFixture { " delete p;\n" " bar();\n" " delete p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice.\n", errout_str()); check( "void foo(char *p) {\n" " delete[] p;\n" " delete[] p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Memory pointed to by 'p' is freed twice.\n", errout_str()); check( "void foo(char *p, char *r) {\n" " delete[] p;\n" " delete[] r;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1308,7 +1315,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] p;\n" " getNext(&p);\n" " delete[] p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1316,7 +1323,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] p;\n" " bar();\n" " delete[] p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice.\n", errout_str()); check( @@ -1327,7 +1334,7 @@ class TestLeakAutoVar : public TestFixture { " delete pxpm;\n" " pxpm = NULL;\n" " return *this;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1344,7 +1351,7 @@ class TestLeakAutoVar : public TestFixture { " throw;\n" " }\n" " delete ptr;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1360,7 +1367,7 @@ class TestLeakAutoVar : public TestFixture { " if(doDelete)\n" " delete a;\n" " return 0;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("", "[test.cpp:8] -> [test.cpp:11]: (error) Memory pointed to by 'a' is freed twice.\n", errout_str()); check( @@ -1374,7 +1381,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] x;\n" " }\n" " delete[] x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1386,7 +1393,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] x;\n" " }\n" " delete[] x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("[test.cpp:8]: (error) Memory pointed to by 'x' is freed twice.\n", "", errout_str()); check( @@ -1398,7 +1405,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] x;\n" " }\n" " delete[] x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("[test.cpp:8]: (error) Memory pointed to by 'x' is freed twice.\n", "", errout_str()); check( @@ -1424,7 +1431,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] x;\n" " }\n" " delete[] x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1438,7 +1445,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] x;\n" " } while (true);\n" " delete[] x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1471,7 +1478,7 @@ class TestLeakAutoVar : public TestFixture { "void MyThrow(err)\n" "{\n" " throw(err);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( @@ -1492,7 +1499,7 @@ class TestLeakAutoVar : public TestFixture { "void MyExit(err)\n" "{\n" " exit(err);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check( // #6252 @@ -1507,7 +1514,7 @@ class TestLeakAutoVar : public TestFixture { " delete m_thing;\n" " m_thing = new Thing;\n" " }\n" - "};", true); + "};", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); // #7401 @@ -1581,7 +1588,7 @@ class TestLeakAutoVar : public TestFixture { check("void do_wordexp(FILE *f) {\n" " free(getword(f));\n" " fclose(f);\n" - "}", /*cpp=*/ false); + "}"); ASSERT_EQUALS("", errout_str()); } @@ -1597,7 +1604,7 @@ class TestLeakAutoVar : public TestFixture { " delete[] p;\n" " if (x && (p = new char[10]))\n" " delete[] p;\n" - "}", /*cpp*/ true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -1606,35 +1613,35 @@ class TestLeakAutoVar : public TestFixture { " int * i = new int;\n" " std::unique_ptr x(i);\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); check("void f() {\n" " int * i = new int;\n" " delete i;\n" " std::unique_ptr x(i);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); check("void f() {\n" " int * i = new int;\n" " std::unique_ptr x{i};\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); check("void f() {\n" " int * i = new int;\n" " std::shared_ptr x(i);\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); check("void f() {\n" " int * i = new int;\n" " std::shared_ptr x{i};\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); // Check for use-after-free FP @@ -1642,14 +1649,14 @@ class TestLeakAutoVar : public TestFixture { " int * i = new int;\n" " std::shared_ptr x{i};\n" " *i = 123;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int * i = new int[1];\n" " std::unique_ptr x(i);\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); check("using namespace std;\n" // #9708 @@ -1657,7 +1664,7 @@ class TestLeakAutoVar : public TestFixture { " int* i = new int;\n" " unique_ptr x(i);\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); } @@ -1668,7 +1675,7 @@ class TestLeakAutoVar : public TestFixture { "void f(foo* b) {\n" " std::unique_ptr x(b->get(0));\n" " std::unique_ptr y(b->get(1));\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -1682,7 +1689,7 @@ class TestLeakAutoVar : public TestFixture { " }\n" " if (p != NULL)\n" " free(p);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(char* s) {\n" @@ -1694,7 +1701,7 @@ class TestLeakAutoVar : public TestFixture { " }\n" " if (p != NULL)\n" " free(p);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -1746,7 +1753,7 @@ class TestLeakAutoVar : public TestFixture { " int *i = new int;\n" " unique_ptr x(i);\n" " delete i;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:7]: (error) Memory pointed to by 'i' is freed twice.\n", errout_str()); check("using namespace std;\n" @@ -1755,14 +1762,14 @@ class TestLeakAutoVar : public TestFixture { "{\n" " int *i = new int;\n" " unique_ptr x(i);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } void doublefree15() { // #11966 check("void f(FILE* fp) {\n" " static_cast(fclose(fp));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -1792,7 +1799,7 @@ class TestLeakAutoVar : public TestFixture { " int *c = new int;\n" " delete (a, c);\n" " delete (b, c);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:6]: (error) Memory pointed to by 'c' is freed twice.\n", errout_str()); } @@ -1821,7 +1828,7 @@ class TestLeakAutoVar : public TestFixture { " ::exit(0);\n" " }" " free(p);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -1831,7 +1838,7 @@ class TestLeakAutoVar : public TestFixture { " std::exit(0);\n" " }" " free(p);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -1870,13 +1877,13 @@ class TestLeakAutoVar : public TestFixture { " auto s = new S;\n" " v.push_back(std::unique_ptr(s));\n" " }\n" - "}\n", /*cpp*/ true, &s); + "}\n", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("", errout_str()); // don't crash check("void g(size_t len) {\n" // #12365 " char* b = new char[len + 1]{};\n" " std::string str = std::string(b);\n" - "}", /*cpp*/ true, &s); + "}", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: b\n", errout_str()); } @@ -2109,7 +2116,7 @@ class TestLeakAutoVar : public TestFixture { "void test_alloc() {\n" " if ( global_ptr = new SSS()) {}\n" " return;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("FILE* hFile;\n" @@ -2352,7 +2359,7 @@ class TestLeakAutoVar : public TestFixture { "};\n" "void S::f(func_t pfn) {\n" " if (pfn == (func_t)&S::g) {}\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); // don't crash } @@ -2426,30 +2433,30 @@ class TestLeakAutoVar : public TestFixture { " delete[] cPtr;\n" " cPtr = new char[100];\n" " delete cPtr;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:7]: (error) Mismatching allocation and deallocation: cPtr\n", errout_str()); check("void f() {\n" " char *cPtr = new char[100];\n" " free(cPtr);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: cPtr\n", errout_str()); check("void f() {\n" " char *cPtr = new (buf) char[100];\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int * i = new int[1];\n" " std::unique_ptr x(i);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: i\n", errout_str()); check("void f() {\n" " int * i = new int;\n" " std::unique_ptr x(i);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: i\n", errout_str()); check("void f() {\n" @@ -2471,7 +2478,7 @@ class TestLeakAutoVar : public TestFixture { " int * i = new int;\n" " int * j = realloc(i, 2 * sizeof(int));\n" " delete[] j;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: i\n" "[test.cpp:3] -> [test.cpp:4]: (error) Mismatching allocation and deallocation: j\n", errout_str()); @@ -2480,20 +2487,20 @@ class TestLeakAutoVar : public TestFixture { " if (int* p = static_cast(malloc(4))) {\n" " std::unique_ptr guard(p, &deleter);\n" " }\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " if (int* p = static_cast(malloc(4))) {\n" " std::unique_ptr guard(p, &deleter);\n" " }\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(int i) {\n" " int* a = new int[i] {};\n" " delete[] a;\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2501,26 +2508,26 @@ class TestLeakAutoVar : public TestFixture { check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::unique_ptr fp{f};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: f\n", errout_str()); check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::unique_ptr fp{f, &fclose};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::shared_ptr fp{f, &fclose};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct deleter { void operator()(FILE* f) { fclose(f); }};\n" "void f() {\n" " FILE*f=fopen(fname,a);\n" " std::unique_ptr fp{f};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("int * create();\n" @@ -2528,7 +2535,7 @@ class TestLeakAutoVar : public TestFixture { "void f() {\n" " int x * = create()\n" " std::unique_ptr xp{x, &destroy()};\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("int * create();\n" @@ -2536,45 +2543,45 @@ class TestLeakAutoVar : public TestFixture { "void f() {\n" " int x * = create()\n" " std::unique_ptr xp(x, &destroy());\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::shared_ptr fp{f, [](FILE* x) { fclose(x); }};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::shared_ptr fp{f, +[](FILE* x) { fclose(x); }};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::shared_ptr fp{f, [](FILE* x) { free(f); }};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: f\n", errout_str()); check("void f() {\n" " FILE*f=fopen(fname,a);\n" " std::shared_ptr fp{f, [](FILE* x) {}};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: f\n", errout_str()); check("class C;\n" "void f() {\n" " C* c = new C{};\n" " std::shared_ptr a{c, [](C*) {}};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("class C;\n" "void f() {\n" " C* c = new C{};\n" " std::shared_ptr a{c, [](C* x) { delete x; }};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct DirDeleter {\n" // #12544 @@ -2582,7 +2589,7 @@ class TestLeakAutoVar : public TestFixture { "};\n" "void openDir(DIR* dir) {\n" " std::shared_ptr dp(dir, DirDeleter());\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: Function closedir() should have configuration\n", errout_str()); // don't crash } void smartPointerRelease() { @@ -2591,14 +2598,14 @@ class TestLeakAutoVar : public TestFixture { " std::unique_ptr x(i);\n" " x.release();\n" " delete i;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int * i = new int;\n" " std::unique_ptr x(i);\n" " x.release();\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: i\n", errout_str()); } @@ -2634,7 +2641,7 @@ class TestLeakAutoVar : public TestFixture { " throw 1;\n" " }\n" " free(p);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(char *p, int x) {\n" @@ -2643,7 +2650,7 @@ class TestLeakAutoVar : public TestFixture { " throw 1;\n" " }\n" " delete p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(char *p, int x) {\n" @@ -2652,7 +2659,7 @@ class TestLeakAutoVar : public TestFixture { " throw 1;\n" " }\n" " delete [] p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2666,7 +2673,7 @@ class TestLeakAutoVar : public TestFixture { " return;\n" " }\n" " *p = 0;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2674,7 +2681,7 @@ class TestLeakAutoVar : public TestFixture { check("std::pair f(size_t n) {\n" " char* p = (char* )malloc(n);\n" " return {p, p};\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2682,43 +2689,43 @@ class TestLeakAutoVar : public TestFixture { check("uint8_t *f() {\n" " void *x = malloc(1);\n" " return (uint8_t *)x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("uint8_t f() {\n" " void *x = malloc(1);\n" " return (uint8_t)x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: x\n", errout_str()); check("void** f() {\n" " void *x = malloc(1);\n" " return (void**)x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void* f() {\n" " void *x = malloc(1);\n" " return (long long)x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void* f() {\n" " void *x = malloc(1);\n" " return (void*)(short)x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: x\n", errout_str()); check("void* f() {\n" " void *x = malloc(1);\n" " return (mytype)x;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void* f() {\n" // Do not crash " void *x = malloc(1);\n" " return (mytype)y;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: x\n", errout_str()); } @@ -2726,25 +2733,25 @@ class TestLeakAutoVar : public TestFixture { check("void* f() {\n" " void *x = malloc(1);\n" " return (x);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void* f() {\n" " void *x = malloc(1);\n" " return ((x));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void* f() {\n" " void *x = malloc(1);\n" " return ((((x))));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("char* f() {\n" " void *x = malloc(1);\n" " return (char*)(x);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2752,7 +2759,7 @@ class TestLeakAutoVar : public TestFixture { check("void* f() {\n" " void *x = malloc (sizeof (struct alloc));\n" " return x + sizeof (struct alloc);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2769,14 +2776,14 @@ class TestLeakAutoVar : public TestFixture { " S* s = new S;\n" " delete s;\n" " return s->f();\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (error) Returning/dereferencing 's' after it is deallocated / released\n", errout_str()); check("int f() {\n" " int* p = new int(3);\n" " delete p;\n" " return *p;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Returning/dereferencing 'p' after it is deallocated / released\n", errout_str()); } @@ -2788,7 +2795,7 @@ class TestLeakAutoVar : public TestFixture { " free(ptr);\n" " }\n" " return 'a';\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: ptr\n", errout_str()); check("char malloc_memleak(void) {\n" @@ -2798,27 +2805,27 @@ class TestLeakAutoVar : public TestFixture { " free(ptr);\n" " }\n" " return 'a';\n" - "}\n", false); + "}\n"); ASSERT_EQUALS("[test.c:7]: (error) Memory leak: ptr\n", errout_str()); } void test1() { check("void f(double*&p) {\n" // 3809 " p = malloc(0x100);\n" - "}", /*cpp*/ true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(int*& p) {\n" // #4400 " p = (int*)malloc(4);\n" " p = (int*)malloc(4);\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout_str()); check("void f() {\n" " int* p = (int*)malloc(4);\n" " int*& r = p;\n" " r = (int*)malloc(4);\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", "", errout_str()); check("void f() {\n" @@ -2826,7 +2833,7 @@ class TestLeakAutoVar : public TestFixture { " int*& r = p;\n" " free(r);\n" " p = (int*)malloc(4);\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("", "[test.cpp:6]: (error) Memory leak: p\n", errout_str()); } @@ -2842,7 +2849,7 @@ class TestLeakAutoVar : public TestFixture { check("void f() {\n" " char *&p = x();\n" " p = malloc(10);\n" - "};", /*cpp*/ true); + "};", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2856,16 +2863,16 @@ class TestLeakAutoVar : public TestFixture { } void test5() { // unknown type - check("void f() { Fred *p = malloc(10); }", true); + check("void f() { Fred *p = malloc(10); }", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:1]: (error) Memory leak: p\n", errout_str()); - check("void f() { Fred *p = malloc(10); }", false); + check("void f() { Fred *p = malloc(10); }"); ASSERT_EQUALS("[test.c:1]: (error) Memory leak: p\n", errout_str()); - check("void f() { Fred *p = new Fred; }", true); + check("void f() { Fred *p = new Fred; }", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); - check("void f() { Fred fred = malloc(10); }", true); + check("void f() { Fred fred = malloc(10); }", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2873,7 +2880,7 @@ class TestLeakAutoVar : public TestFixture { check("void f() {\n" " char *p = malloc(10);\n" " throw 123;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout_str()); check("void f() {\n" @@ -2883,7 +2890,7 @@ class TestLeakAutoVar : public TestFixture { " throw 123;\n" " } catch (...) { }\n" " free(p);\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2900,7 +2907,7 @@ class TestLeakAutoVar : public TestFixture { " throw ::NS::Except();\n" " }\n" " delete pi;\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -2945,7 +2952,7 @@ class TestLeakAutoVar : public TestFixture { "}"; check(code); ASSERT_EQUALS("[test.c:4]: (information) --check-library: Function set_data() should have / configuration\n", errout_str()); - check(code, true); + check(code, dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function set_data() should have / configuration\n", errout_str()); } @@ -2958,7 +2965,7 @@ class TestLeakAutoVar : public TestFixture { 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); + check(code, dinit(CheckOptions, $.cpp = 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()); @@ -2982,7 +2989,7 @@ class TestLeakAutoVar : public TestFixture { check("void f() {\n" " static_assert(1 == sizeof(char), \"test\");\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("namespace pal {\n" // #11237 @@ -2990,13 +2997,13 @@ class TestLeakAutoVar : public TestFixture { "}\n" "int main() {\n" " pal::AutoTimer();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct AutoTimer {};\n" "int main() {\n" " AutoTimer();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #8666 @@ -3025,7 +3032,7 @@ class TestLeakAutoVar : public TestFixture { " void* buf = malloc(10);\n" " p = reinterpret_cast(buf);\n" " }\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5]: (error) Resource leak: file\n", errout_str()); } @@ -3041,7 +3048,7 @@ class TestLeakAutoVar : public TestFixture { "}\n" "void g(void (*cb)()) {\n" " cb();\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } @@ -3056,19 +3063,19 @@ class TestLeakAutoVar : public TestFixture { check("void QueueDSMCCPacket(unsigned char *data, int length) {\n" " unsigned char *dataCopy = malloc(length * sizeof(unsigned char));\n" " m_dsmccQueue.enqueue(new DSMCCPacket(dataCopy));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function DSMCCPacket() should have / configuration\n", errout_str()); check("void QueueDSMCCPacket(unsigned char *data, int length) {\n" " unsigned char *dataCopy = malloc(length * sizeof(unsigned char));\n" " m_dsmccQueue.enqueue(new DSMCCPacket(somethingunrelated));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: dataCopy\n", errout_str()); check("void f() {\n" " char *buf = new char[1000];\n" " clist.push_back(new (std::nothrow) C(buf));\n" - "}", true); + "}", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function C() should have / configuration\n", errout_str()); } @@ -3077,7 +3084,7 @@ class TestLeakAutoVar : public TestFixture { " double *new = malloc(1*sizeof(double));\n" " free(new);\n" " return 0;\n" - "}", false); + "}"); ASSERT_EQUALS("", errout_str()); } @@ -3101,7 +3108,7 @@ class TestLeakAutoVar : public TestFixture { " int *pt = new int(1);\n" " mList.push_back(std::shared_ptr(pt));\n" "}\n", - true + dinit(CheckOptions, $.cpp = true) ); ASSERT_EQUALS("", errout_str()); } @@ -3137,7 +3144,7 @@ class TestLeakAutoVar : public TestFixture { "void f2() {\n" " int* p = new int;\n" " h(1, static_cast(p));\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6]: (information) --check-library: Function g() should have / configuration\n" "[test.cpp:10]: (information) --check-library: Function h() should have / configuration\n", errout_str()); @@ -3172,7 +3179,7 @@ class TestLeakAutoVar : public TestFixture { check("void f(int n) {\n" " char* p = new char[n];\n" " v.push_back(p);\n" - "}\n", /*cpp*/ true); + "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function unknown::push_back() should have / configuration\n", errout_str()); } }; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 572122b5965..093e1879989 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -39,9 +39,9 @@ class TestMemleak : public TestFixture { TEST_CASE(open); } -#define functionReturnType(code) functionReturnType_(code, __FILE__, __LINE__) +#define functionReturnType(...) functionReturnType_(__FILE__, __LINE__, __VA_ARGS__) template - CheckMemoryLeak::AllocType functionReturnType_(const char (&code)[size], const char* file, int line) { + CheckMemoryLeak::AllocType functionReturnType_(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 7d8d3586e84..0632f6c1093 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -176,14 +176,21 @@ class TestNullPointer : public TestFixture { TEST_CASE(ctuTest); } + struct CheckOptions + { + CheckOptions() = default; + bool inconclusive = false; + bool cpp = true; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); // Check for null pointer dereferences.. runChecks(tokenizer, this); @@ -213,7 +220,7 @@ class TestNullPointer : public TestFixture { "{\n" " while (tok);\n" " tok = tok->next();\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'tok' is redundant or there is possible null pointer dereference: tok.\n", errout_str()); // #2681 @@ -299,14 +306,14 @@ class TestNullPointer : public TestFixture { check("int foo(const Token *tok)\n" "{\n" " while (tok){;}\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int foo(const Token *tok)\n" "{\n" " while (tok){;}\n" " char a[2] = {0,0};\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct b {\n" @@ -353,7 +360,7 @@ class TestNullPointer : public TestFixture { " sizeof(*list);\n" " if (!list)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // ticket #2245 - sizeof doesn't dereference @@ -361,7 +368,7 @@ class TestNullPointer : public TestFixture { " if (!p) {\n" " int sz = sizeof(p->x);\n" " }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -373,7 +380,7 @@ class TestNullPointer : public TestFixture { " Fred fred;\n" " while (fred);\n" " fred.hello();\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -459,13 +466,13 @@ class TestNullPointer : public TestFixture { " abc = abc->next;\n" " if (!abc)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(struct ABC *abc) {\n" " abc = (ABC *)(abc->_next);\n" " if (abc) { }" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // reassign struct.. @@ -475,7 +482,7 @@ class TestNullPointer : public TestFixture { " abc = abc->next;\n" " if (!abc)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo(struct ABC *abc)\n" @@ -484,7 +491,7 @@ class TestNullPointer : public TestFixture { " f(&abc);\n" " if (!abc)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // goto.. @@ -709,7 +716,7 @@ class TestNullPointer : public TestFixture { " int **p = f();\n" " if (!p)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo(int *p)\n" @@ -728,7 +735,7 @@ class TestNullPointer : public TestFixture { " int a = 2 * x;" " if (x == 0)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo(int *p)\n" @@ -785,7 +792,7 @@ class TestNullPointer : public TestFixture { " int * a=0;\n" " if (!a) {};\n" " int c = a ? 0 : 1;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #3686 @@ -793,14 +800,14 @@ class TestNullPointer : public TestFixture { " int * a=0;\n" " if (!a) {};\n" " int c = a ? b : b+1;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int * a=0;\n" " if (!a) {};\n" " int c = (a) ? b : b+1;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo(P *p)\n" @@ -948,7 +955,7 @@ class TestNullPointer : public TestFixture { " int c = sizeof(test[0]);\n" " if (!test)\n" " ;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(type* p) {\n" // #4983 @@ -1160,14 +1167,14 @@ class TestNullPointer : public TestFixture { " for (int i = 0; i < 3; i++) {\n" " if (a && (p[i] == '1'));\n" " }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // ticket #2251: taking the address of member check("void f() {\n" " Fred *fred = 0;\n" " int x = &fred->x;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // ticket #3220: dereferencing a null pointer is UB @@ -1184,14 +1191,14 @@ class TestNullPointer : public TestFixture { " if (x)\n" " p = q;\n" " if (p && *p) { }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int *p = NULL;\n" " if (x)\n" " p = q;\n" " if (!p || *p) { }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int *p = NULL;\n" @@ -1214,7 +1221,7 @@ class TestNullPointer : public TestFixture { "\n" "void g() {\n" " f(NULL, NULL);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1279,7 +1286,7 @@ class TestNullPointer : public TestFixture { "{\n" " wxLongLong x = 0;\n" " int y = x.GetValue();\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1321,10 +1328,10 @@ class TestNullPointer : public TestFixture { " return *i;\n" "}\n"; - check(code, false, true); // C++ file => nullptr means NULL + check(code); // C++ file => nullptr means NULL ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: i\n", errout_str()); - check(code, false, false); // C file => nullptr does not mean NULL + check(code, dinit(CheckOptions, $.cpp = false)); // C file => nullptr does not mean NULL ASSERT_EQUALS("", errout_str()); } @@ -1342,7 +1349,7 @@ class TestNullPointer : public TestFixture { " int *p = 0;\n" " bar(&p);\n" " *p = 0;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1351,14 +1358,14 @@ class TestNullPointer : public TestFixture { " int *p = 0;\n" " if (x) { return 0; }\n" " return !p || *p;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int foo() {\n" " int *p = 0;\n" " if (x) { return 0; }\n" " return p && *p;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1378,7 +1385,7 @@ class TestNullPointer : public TestFixture { void nullpointer19() { // #3811 check("int foo() {\n" " perror(0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1490,7 +1497,7 @@ class TestNullPointer : public TestFixture { " values->push_back(\"test\");\n" " }\n" " }\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:4] -> [test.cpp:3]: (warning) Either the condition 'values' is redundant or there is possible null pointer dereference: values.\n", errout_str()); @@ -1507,7 +1514,7 @@ class TestNullPointer : public TestFixture { " if( f ) {}\n" " else { return; }\n" " (void)f->x;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("typedef struct\n" @@ -1524,7 +1531,7 @@ class TestNullPointer : public TestFixture { " }\n" "\n" " (void)f->x;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1536,7 +1543,7 @@ class TestNullPointer : public TestFixture { " int *p1 = ptr;\n" " return *p1;\n" " }\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:6]: (warning) Either the condition 'ptr' is redundant or there is possible null pointer dereference: p1.\n", errout_str()); } @@ -1546,7 +1553,7 @@ class TestNullPointer : public TestFixture { " *x = 2;\n" " else\n" " *x = 3;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (warning) Either the condition 'x!=nullptr' is redundant or there is possible null pointer dereference: x.\n", errout_str()); } @@ -1558,7 +1565,7 @@ class TestNullPointer : public TestFixture { " if (x) *x += 1;\n" " if (!x) g();\n" " return *x;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1571,7 +1578,7 @@ class TestNullPointer : public TestFixture { "}\n" "void h() {\n" " g(0);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool f(int*);\n" @@ -1584,7 +1591,7 @@ class TestNullPointer : public TestFixture { "void h() {\n" " g(0);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1596,7 +1603,7 @@ class TestNullPointer : public TestFixture { " while (isspace(*start))\n" " start++;\n" " return (start);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1615,7 +1622,7 @@ class TestNullPointer : public TestFixture { " ptr1++;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -1627,7 +1634,7 @@ class TestNullPointer : public TestFixture { " *x;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -2306,7 +2313,7 @@ class TestNullPointer : public TestFixture { " if (!(p0 != nullptr && p1 != nullptr))\n" " return {};\n" " return *p0 + *p1;\n" - "}\n", true /*inconclusive*/); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int test2() {\n" @@ -2314,7 +2321,7 @@ class TestNullPointer : public TestFixture { " if (!(getBaz(p0) && p0 != nullptr))\n" " return 0;\n" " return *p0;\n" - "}\n", true /*inconclusive*/); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int test3() {\n" @@ -2324,7 +2331,7 @@ class TestNullPointer : public TestFixture { " if (!PObj->foo())\n" " test();\n" " PObj->bar();\n" - "}\n", true /*inconclusive*/); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -2636,7 +2643,7 @@ class TestNullPointer : public TestFixture { " s->ppc = NULL;\n" " if (alloc(s))\n" " s->ppc[0] = \"\";\n" - "}\n", /*inconclusive*/ false, false); + "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -2950,7 +2957,7 @@ class TestNullPointer : public TestFixture { " break;\n" " }\n" " return p;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:7]: (warning) Possible null pointer dereference: p\n", errout_str()); } @@ -2975,7 +2982,7 @@ class TestNullPointer : public TestFixture { check("void f () {\n" " int *buf; buf = NULL;\n" " buf;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -2987,7 +2994,7 @@ class TestNullPointer : public TestFixture { "}\n" "void g() {\n" " f(nullptr, nullptr);\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3044,10 +3051,10 @@ class TestNullPointer : public TestFixture { " }\n" " *p = 0;\n" "}"; - check(code, false); + check(code); ASSERT_EQUALS("", errout_str()); - check(code, true); + check(code, dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3079,14 +3086,14 @@ class TestNullPointer : public TestFixture { " if (!p) {\n" " switch (x) { }\n" " }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo(char *p) {\n" " if (!p) {\n" " }\n" " return *x;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int foo(int *p) {\n" @@ -3178,7 +3185,7 @@ class TestNullPointer : public TestFixture { "\n" "void Fred::b() {\n" " wilma->Reload();\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void test(int *i) {\n" @@ -3186,7 +3193,7 @@ class TestNullPointer : public TestFixture { " else {\n" " int b = *i;\n" " }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #2696 - false positives nr 1 @@ -3199,7 +3206,7 @@ class TestNullPointer : public TestFixture { "\n" " if (pFoo)\n" " bar();\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #2696 - false positives nr 2 @@ -3212,7 +3219,7 @@ class TestNullPointer : public TestFixture { " pFoo = pFoo->next;\n" "\n" " len = sizeof(pFoo->data);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #2696 - false positives nr 3 @@ -3225,7 +3232,7 @@ class TestNullPointer : public TestFixture { " pFoo = pFoo->next;\n" "\n" " len = decltype(*pFoo);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int foo(struct Fred *fred) {\n" @@ -3324,7 +3331,7 @@ class TestNullPointer : public TestFixture { check("void f(struct fred_t *fred) {\n" " if (!fred)\n" " int sz = sizeof(fred->x);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // check in macro @@ -3376,7 +3383,7 @@ class TestNullPointer : public TestFixture { check("void f() {\n" " typeof(*NULL) y;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int * f() {\n" @@ -3400,7 +3407,7 @@ class TestNullPointer : public TestFixture { // Ticket #2840 check("void f() {\n" " int bytes = snprintf(0, 0, \"%u\", 1);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3566,7 +3573,7 @@ class TestNullPointer : public TestFixture { check("int foo(int* iVal) {\n" " return iVal[0];\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3576,20 +3583,20 @@ class TestNullPointer : public TestFixture { "bool foo() {\n" " PolymorphicA* a = 0;\n" " return typeid(*a) == typeid(*a);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct NonPolymorphicA { ~A() {} };\n" "bool foo() {\n" " NonPolymorphicA* a = 0;\n" " return typeid(*a) == typeid(*a);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool foo() {\n" " char* c = 0;\n" " return typeid(*c) == typeid(*c);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3598,12 +3605,12 @@ class TestNullPointer : public TestFixture { check("size_t foo() {\n" " char* c = 0;\n" " return alignof(*c);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("size_t foo() {\n" " return alignof(*0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo(int *p) {\n" @@ -3616,22 +3623,22 @@ class TestNullPointer : public TestFixture { check("size_t foo() {\n" " char* c = 0;\n" " return _Alignof(*c);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("size_t foo() {\n" " return _alignof(*0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("size_t foo() {\n" " return __alignof(*0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("size_t foo() {\n" " return __alignof__(*0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3705,7 +3712,7 @@ class TestNullPointer : public TestFixture { " if (k)\n" " k->doStuff();\n" " delete k;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -3714,7 +3721,7 @@ class TestNullPointer : public TestFixture { " k[0] = ptr;\n" " delete [] k;\n" " k = new K[10];\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3733,7 +3740,7 @@ class TestNullPointer : public TestFixture { " if (!k)\n" " exit(1);\n" " k->f();\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3750,7 +3757,7 @@ class TestNullPointer : public TestFixture { " std::string s4 = p;\n" " std::string s5(p);\n" " foo(std::string(p));\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:9]: (error) Null pointer dereference: p\n" "[test.cpp:10]: (error) Null pointer dereference: p\n" "[test.cpp:11]: (error) Null pointer dereference: p\n" @@ -3766,7 +3773,7 @@ class TestNullPointer : public TestFixture { " std::string s2 = nullptr;\n" " std::string s3(nullptr);\n" " foo(std::string(nullptr));\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n" "[test.cpp:3]: (error) Null pointer dereference\n" "[test.cpp:4]: (error) Null pointer dereference\n" @@ -3778,7 +3785,7 @@ class TestNullPointer : public TestFixture { " std::string s2 = NULL;\n" " std::string s3(NULL);\n" " foo(std::string(NULL));\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n" "[test.cpp:3]: (error) Null pointer dereference\n" "[test.cpp:4]: (error) Null pointer dereference\n" @@ -3794,7 +3801,7 @@ class TestNullPointer : public TestFixture { " foo(p == s1);\n" " foo(p == s2);\n" " foo(p == s3);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n" "[test.cpp:5]: (error) Null pointer dereference: p\n" "[test.cpp:7]: (error) Null pointer dereference: p\n" @@ -3809,7 +3816,7 @@ class TestNullPointer : public TestFixture { " foo(s1.size() == 0);\n" " foo(s2.size() == 0);\n" " foo(s3->size() == 0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::string s1, const std::string& s2) {\n" @@ -3818,7 +3825,7 @@ class TestNullPointer : public TestFixture { " foo(0 == s2[0]);\n" " foo(s1[0] == 0);\n" " foo(s2[0] == 0);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::string s1, const std::string& s2) {\n" @@ -3827,7 +3834,7 @@ class TestNullPointer : public TestFixture { " foo(s2 == '\\0');\n" " foo('\\0' == s1);\n" " foo('\\0' == s2);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("class Bar {\n" @@ -3844,7 +3851,7 @@ class TestNullPointer : public TestFixture { check("void f() {\n" " std::string s = 0 == x ? \"a\" : \"b\";\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -3874,7 +3881,7 @@ class TestNullPointer : public TestFixture { " return \"\";\n" " std::string s(p);\n" " return s;\n" - "}\n", /*inconclusive*/ true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #11078 @@ -3905,7 +3912,7 @@ class TestNullPointer : public TestFixture { " oss << foo << p;\n" " if(q == 0)\n" " oss << foo << q;\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Null pointer dereference: p\n" "[test.cpp:4]: (error) Null pointer dereference: p\n" "[test.cpp:5] -> [test.cpp:6]: (warning) Either the condition 'q==0' is redundant or there is possible null pointer dereference: q.\n", errout_str()); @@ -3917,7 +3924,7 @@ class TestNullPointer : public TestFixture { " std::cin >> p;\n" " std::cout << abc << p;\n" " }\n" - "}", false); + "}"); TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p.\n" "[test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p.\n" "[test.cpp:2] -> [test.cpp:5]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p.\n" @@ -3932,7 +3939,7 @@ class TestNullPointer : public TestFixture { " char* p2 = 0;\n" " std::cin >> (int)p;\n" // result casted " std::cout << (int)p;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(const std::string& str) {\n" @@ -3940,13 +3947,13 @@ class TestNullPointer : public TestFixture { " std::istringstream istr(str);\n" " istr >> std::hex >> ret;\n" // Read integer " return ret;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(int* i) {\n" " if(i) return;\n" " std::cout << i;\n" // Its no char* (#4240) - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #5811 false positive: (error) Null pointer dereference @@ -3955,7 +3962,7 @@ class TestNullPointer : public TestFixture { " stringstream out;\n" " out << ((ip >> 0) & 0xFF);\n" " return out.str();\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // avoid regression from first fix attempt for #5811... check("void deserialize(const std::string &data) {\n" @@ -3963,7 +3970,7 @@ class TestNullPointer : public TestFixture { "unsigned int len = 0;\n" "if (!(iss >> len))\n" " return;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -4067,7 +4074,7 @@ class TestNullPointer : public TestFixture { " std::unique_ptr x(g());\n" " if( x ) {}\n" " return x.release();\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9496 @@ -4077,7 +4084,7 @@ class TestNullPointer : public TestFixture { "void g() {\n" " int a = *f();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (error) Null pointer dereference: f()\n", errout_str()); } @@ -4127,7 +4134,7 @@ class TestNullPointer : public TestFixture { " *p = 0;\n" " foo(p);\n" " if (p) { }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // function implementation not seen @@ -4147,7 +4154,7 @@ class TestNullPointer : public TestFixture { " *p = 0;\n" " foo(p);\n" " if (p) { }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:4] -> [test.cpp:2]: (warning, inconclusive) Either the condition 'p' is redundant or there is possible null pointer dereference: p.\n", errout_str()); @@ -4192,7 +4199,7 @@ class TestNullPointer : public TestFixture { " abc->a = 0;\n" " foo(abc);\n" " if (abc) { }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:4] -> [test.cpp:2]: (warning, inconclusive) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc.\n", errout_str()); @@ -4307,7 +4314,7 @@ class TestNullPointer : public TestFixture { check("void f(int a, int *p = 0) {\n" " if (a != 0)\n" " *p = 0;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:3]: (warning) Possible null pointer dereference if the default parameter value is used: p\n", errout_str()); @@ -4382,7 +4389,7 @@ class TestNullPointer : public TestFixture { check("void f(int *p = 0) {\n" " printf(\"%p\", p);\n" " *p = 0;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning) Possible null pointer dereference if the default parameter value is used: p\n", errout_str()); // The init() function may or may not initialize p, but since the address @@ -4450,7 +4457,7 @@ class TestNullPointer : public TestFixture { "}\n" "void bar() {\n" " foo(0);\n" - "}\n", true, false); + "}\n", dinit(CheckOptions, $.inconclusive = true, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } diff --git a/test/testpostfixoperator.cpp b/test/testpostfixoperator.cpp index 62f07b4cab3..6ea9b521d00 100644 --- a/test/testpostfixoperator.cpp +++ b/test/testpostfixoperator.cpp @@ -31,9 +31,9 @@ class TestPostfixOperator : public TestFixture { private: const Settings settings = settingsBuilder().severity(Severity::performance).build(); -#define check(code) check_(code, __FILE__, __LINE__) +#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char (&code)[size], const char* file, int line) { + 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/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 73945f29b7b..a25e11b9304 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -309,10 +309,16 @@ class TestSimplifyTemplate : public TestFixture { TEST_CASE(explicitBool2); } + struct CheckOptions + { + CheckOptions() = default; + bool debugwarnings = false; + }; + #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) 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(); + std::string tok_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).library("std.cfg").debugwarnings(options.debugwarnings).build(); SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -1205,7 +1211,7 @@ class TestSimplifyTemplate : public TestFixture { "struct TypeMath { " "static const int mult = sizeof ( int ) * Constants :: fourtytwo ; " "} ;"; - ASSERT_EQUALS(expected, tok(code, true)); + ASSERT_EQUALS(expected, tok(code, dinit(CheckOptions, $.debugwarnings = true))); ASSERT_EQUALS("", errout_str()); } @@ -1333,7 +1339,7 @@ class TestSimplifyTemplate : public TestFixture { "struct Factorial<1> { " "enum Anonymous0 { value = 1 * Factorial<0> :: value } ; " "} ;"; - ASSERT_EQUALS(expected, tok(code, true)); + ASSERT_EQUALS(expected, tok(code, dinit(CheckOptions, $.debugwarnings = true))); ASSERT_EQUALS("", errout_str()); } @@ -1393,7 +1399,7 @@ class TestSimplifyTemplate : public TestFixture { "} struct Foo { " "std :: array < int , 1 > mfoo ; " "} ;"; - ASSERT_EQUALS(expected, tok(code, true)); + ASSERT_EQUALS(expected, tok(code, dinit(CheckOptions, $.debugwarnings = true))); ASSERT_EQUALS("", errout_str()); } @@ -5817,9 +5823,9 @@ class TestSimplifyTemplate : public TestFixture { ASSERT_EQUALS(expected, tok(code)); } -#define instantiateMatch(code, numberOfArguments, patternAfter) instantiateMatch_(code, numberOfArguments, patternAfter, __FILE__, __LINE__) +#define instantiateMatch(...) instantiateMatch_(__FILE__, __LINE__, __VA_ARGS__) template - bool instantiateMatch_(const char (&code)[size], const std::size_t numberOfArguments, const char patternAfter[], const char* file, int line) { + bool instantiateMatch_(const char* file, int line, const char (&code)[size], const std::size_t numberOfArguments, const char patternAfter[]) { SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d84690837f8..f34891af85d 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -163,36 +163,50 @@ class TestSimplifyTokens : public TestFixture { TEST_CASE(simplifyVarDeclInitLists); } + struct TokOptions + { + TokOptions() = default; + bool cpp = true; + Platform::Type type = Platform::Type::Native; + }; + #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) 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(); + std::string tok_(const char* file, int line, const char (&code)[size], const TokOptions& options = make_default_obj()) { + const Settings settings = settingsBuilder(settings0).platform(options.type).build(); SimpleTokenizer tokenizer(settings, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); return tokenizer.tokens()->stringifyList(nullptr, false); } + struct TokenizeAndStringifyOptions + { + TokenizeAndStringifyOptions() = default; + Platform::Type platform = Platform::Type::Native; + bool cpp = true; + }; + #define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__) 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(); + std::string tokenizeAndStringify_(const char* file, int linenr, const char (&code)[size], const TokenizeAndStringifyOptions& options = make_default_obj()) { + const Settings settings = settingsBuilder(settings1).debugwarnings().platform(options.platform).cpp(Standards::CPP03).build(); // tokenize.. SimpleTokenizer tokenizer(settings, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, linenr); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, linenr); if (tokenizer.tokens()) - return tokenizer.tokens()->stringifyList(false, expand, false, true, false, nullptr, nullptr); + return tokenizer.tokens()->stringifyList(false, true, false, true, false, nullptr, nullptr); return ""; } #define tokenizeDebugListing(...) tokenizeDebugListing_(__FILE__, __LINE__, __VA_ARGS__) template - std::string tokenizeDebugListing_(const char* file, int line, const char (&code)[size], bool cpp = true) { + std::string tokenizeDebugListing_(const char* file, int line, const char (&code)[size]) { SimpleTokenizer tokenizer(settings0, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code), file, line); // result.. return tokenizer.tokens()->stringifyList(true); @@ -220,10 +234,10 @@ class TestSimplifyTokens : public TestFixture { ASSERT_EQUALS(tok(code2), tok(code1)); const char code3[] = "x = L\"1\" TEXT(\"2\") L\"3\";"; - ASSERT_EQUALS("x = L\"123\" ;", tok(code3, true, Platform::Type::Win64)); + ASSERT_EQUALS("x = L\"123\" ;", tok(code3, dinit(TokOptions, $.type = Platform::Type::Win64))); const char code4[] = "x = TEXT(\"1\") L\"2\";"; - ASSERT_EQUALS("x = L\"1\" L\"2\" ;", tok(code4, true, Platform::Type::Win64)); + ASSERT_EQUALS("x = L\"1\" L\"2\" ;", tok(code4, dinit(TokOptions, $.type = Platform::Type::Win64))); } void combine_wstrings() { @@ -591,71 +605,71 @@ class TestSimplifyTokens : public TestFixture { } void not1() { - ASSERT_EQUALS("void f ( ) { if ( ! p ) { ; } }", tok("void f() { if (not p); }", false)); - ASSERT_EQUALS("void f ( ) { if ( p && ! q ) { ; } }", tok("void f() { if (p && not q); }", false)); - ASSERT_EQUALS("void f ( ) { a = ! ( p && q ) ; }", tok("void f() { a = not(p && q); }", false)); + ASSERT_EQUALS("void f ( ) { if ( ! p ) { ; } }", tok("void f() { if (not p); }", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("void f ( ) { if ( p && ! q ) { ; } }", tok("void f() { if (p && not q); }", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("void f ( ) { a = ! ( p && q ) ; }", tok("void f() { a = not(p && q); }", dinit(TokOptions, $.cpp = false))); // Don't simplify 'not' or 'compl' if they are defined as a type; // in variable declaration and in function declaration/definition - ASSERT_EQUALS("struct not { int x ; } ;", tok("struct not { int x; };", false)); - ASSERT_EQUALS("void f ( ) { not p ; compl c ; }", tok(" void f() { not p; compl c; }", false)); - ASSERT_EQUALS("void foo ( not i ) ;", tok("void foo(not i);", false)); - ASSERT_EQUALS("int foo ( not i ) { return g ( i ) ; }", tok("int foo(not i) { return g(i); }", false)); + ASSERT_EQUALS("struct not { int x ; } ;", tok("struct not { int x; };", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("void f ( ) { not p ; compl c ; }", tok(" void f() { not p; compl c; }", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("void foo ( not i ) ;", tok("void foo(not i);", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("int foo ( not i ) { return g ( i ) ; }", tok("int foo(not i) { return g(i); }", dinit(TokOptions, $.cpp = false))); } void and1() { ASSERT_EQUALS("void f ( ) { if ( p && q ) { ; } }", - tok("void f() { if (p and q) ; }", false)); + tok("void f() { if (p and q) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( foo ( ) && q ) { ; } }", - tok("void f() { if (foo() and q) ; }", false)); + tok("void f() { if (foo() and q) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( foo ( ) && bar ( ) ) { ; } }", - tok("void f() { if (foo() and bar()) ; }", false)); + tok("void f() { if (foo() and bar()) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( p && bar ( ) ) { ; } }", - tok("void f() { if (p and bar()) ; }", false)); + tok("void f() { if (p and bar()) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( p && ! q ) { ; } }", - tok("void f() { if (p and not q) ; }", false)); + tok("void f() { if (p and not q) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { r = a && b ; }", - tok("void f() { r = a and b; }", false)); + tok("void f() { r = a and b; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { r = ( a || b ) && ( c || d ) ; }", - tok("void f() { r = (a || b) and (c || d); }", false)); + tok("void f() { r = (a || b) and (c || d); }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( test1 [ i ] == 'A' && test2 [ i ] == 'C' ) { } }", - tok("void f() { if (test1[i] == 'A' and test2[i] == 'C') {} }", false)); + tok("void f() { if (test1[i] == 'A' and test2[i] == 'C') {} }", dinit(TokOptions, $.cpp = false))); } void or1() { ASSERT_EQUALS("void f ( ) { if ( p || q ) { ; } }", - tok("void f() { if (p or q) ; }", false)); + tok("void f() { if (p or q) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( foo ( ) || q ) { ; } }", - tok("void f() { if (foo() or q) ; }", false)); + tok("void f() { if (foo() or q) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( foo ( ) || bar ( ) ) { ; } }", - tok("void f() { if (foo() or bar()) ; }", false)); + tok("void f() { if (foo() or bar()) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( p || bar ( ) ) { ; } }", - tok("void f() { if (p or bar()) ; }", false)); + tok("void f() { if (p or bar()) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { if ( p || ! q ) { ; } }", - tok("void f() { if (p or not q) ; }", false)); + tok("void f() { if (p or not q) ; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { r = a || b ; }", - tok("void f() { r = a or b; }", false)); + tok("void f() { r = a or b; }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) { r = ( a && b ) || ( c && d ) ; }", - tok("void f() { r = (a && b) or (c && d); }", false)); + tok("void f() { r = (a && b) or (c && d); }", dinit(TokOptions, $.cpp = false))); } void cAlternativeTokens() { ASSERT_EQUALS("void f ( ) { err |= ( ( r & s ) && ! t ) ; }", - tok("void f() { err or_eq ((r bitand s) and not t); }", false)); + tok("void f() { err or_eq ((r bitand s) and not t); }", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void f ( ) const { r = f ( a [ 4 ] | 0x0F , ~ c , ! d ) ; }", - tok("void f() const { r = f(a[4] bitor 0x0F, compl c, not d) ; }", false)); + tok("void f() const { r = f(a[4] bitor 0x0F, compl c, not d) ; }", dinit(TokOptions, $.cpp = false))); } @@ -1285,20 +1299,20 @@ class TestSimplifyTokens : public TestFixture { ASSERT_EQUALS("constexpr int foo ( ) { }", tok("consteval int foo() { }")); ASSERT_EQUALS("int x ; x = 0 ;", tok("constinit int x = 0;")); ASSERT_EQUALS("void f ( ) { int final [ 10 ] ; }", tok("void f() { int final[10]; }")); - ASSERT_EQUALS("int * p ;", tok("int * __restrict p;", false)); - ASSERT_EQUALS("int * * p ;", tok("int * __restrict__ * p;", false)); - ASSERT_EQUALS("void foo ( float * a , float * b ) ;", tok("void foo(float * __restrict__ a, float * __restrict__ b);", false)); - ASSERT_EQUALS("int * p ;", tok("int * restrict p;", false)); - ASSERT_EQUALS("int * * p ;", tok("int * restrict * p;", false)); - ASSERT_EQUALS("void foo ( float * a , float * b ) ;", tok("void foo(float * restrict a, float * restrict b);", false)); + ASSERT_EQUALS("int * p ;", tok("int * __restrict p;", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("int * * p ;", tok("int * __restrict__ * p;", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("void foo ( float * a , float * b ) ;", tok("void foo(float * __restrict__ a, float * __restrict__ b);", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("int * p ;", tok("int * restrict p;", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("int * * p ;", tok("int * restrict * p;", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("void foo ( float * a , float * b ) ;", tok("void foo(float * restrict a, float * restrict b);", dinit(TokOptions, $.cpp = false))); ASSERT_EQUALS("void foo ( int restrict ) ;", tok("void foo(int restrict);")); - ASSERT_EQUALS("int * p ;", tok("typedef int * __restrict__ rint; rint p;", false)); + ASSERT_EQUALS("int * p ;", tok("typedef int * __restrict__ rint; rint p;", dinit(TokOptions, $.cpp = false))); // don't remove struct members: ASSERT_EQUALS("a = b . _inline ;", tok("a = b._inline;")); - ASSERT_EQUALS("int i ; i = 0 ;", tok("auto int i = 0;", false)); - ASSERT_EQUALS("auto i ; i = 0 ;", tok("auto i = 0;", true)); + ASSERT_EQUALS("int i ; i = 0 ;", tok("auto int i = 0;", dinit(TokOptions, $.cpp = false))); + ASSERT_EQUALS("auto i ; i = 0 ;", tok("auto i = 0;")); } void simplifyCallingConvention() { @@ -1318,12 +1332,12 @@ class TestSimplifyTokens : public TestFixture { ASSERT_EQUALS("int f ( ) ;", tok("int __far __syscall f();")); ASSERT_EQUALS("int f ( ) ;", tok("int __far __pascal f();")); ASSERT_EQUALS("int f ( ) ;", tok("int __far __fortran f();")); - ASSERT_EQUALS("int f ( ) ;", tok("int WINAPI f();", true, Platform::Type::Win32A)); - ASSERT_EQUALS("int f ( ) ;", tok("int APIENTRY f();", true, Platform::Type::Win32A)); - ASSERT_EQUALS("int f ( ) ;", tok("int CALLBACK f();", true, Platform::Type::Win32A)); + ASSERT_EQUALS("int f ( ) ;", tok("int WINAPI f();", dinit(TokOptions, $.type = Platform::Type::Win32A))); + ASSERT_EQUALS("int f ( ) ;", tok("int APIENTRY f();", dinit(TokOptions, $.type = Platform::Type::Win32A))); + ASSERT_EQUALS("int f ( ) ;", tok("int CALLBACK f();", dinit(TokOptions, $.type = Platform::Type::Win32A))); // don't simplify Microsoft defines in unix code (#7554) - ASSERT_EQUALS("enum E { CALLBACK } ;", tok("enum E { CALLBACK } ;", true, Platform::Type::Unix32)); + ASSERT_EQUALS("enum E { CALLBACK } ;", tok("enum E { CALLBACK } ;", dinit(TokOptions, $.type = Platform::Type::Unix32))); } void simplifyAttribute() { @@ -1517,9 +1531,9 @@ class TestSimplifyTokens : public TestFixture { "}\n")); } -#define simplifyKnownVariables(code) simplifyKnownVariables_(code, __FILE__, __LINE__) +#define simplifyKnownVariables(...) simplifyKnownVariables_(__FILE__, __LINE__, __VA_ARGS__) template - std::string simplifyKnownVariables_(const char (&code)[size], const char* file, int line) { + std::string simplifyKnownVariables_(const char* file, int line, const char (&code)[size]) { SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -2022,7 +2036,7 @@ class TestSimplifyTokens : public TestFixture { "iterator it2 ; it2 = it1 ;\n" "for ( ++ it2 ; it2 != ints . end ( ) ; ++ it2 ) { ; }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } void simplifyKnownVariables34() { @@ -2036,7 +2050,7 @@ class TestSimplifyTokens : public TestFixture { "do { cin >> x ; } while ( x > 5 ) ;\n" "a [ x ] = 0 ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cin\n", errout_str()); @@ -2050,7 +2064,7 @@ class TestSimplifyTokens : public TestFixture { " memset(buf, 0, 10);" "}"; const char expected2[] = "void f ( ) { char buf [ 10 ] = \"ab\" ; memset ( buf , 0 , 10 ) ; }"; - ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, true)); + ASSERT_EQUALS(expected2, tokenizeAndStringify(code2)); } void simplifyKnownVariables42() { @@ -2065,7 +2079,7 @@ class TestSimplifyTokens : public TestFixture { "strcpy ( a , \"hello\" ) ;\n" "strcat ( a , \"!\" ) ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, dinit(TokenizeAndStringifyOptions, $.cpp = false))); } { @@ -2079,7 +2093,7 @@ class TestSimplifyTokens : public TestFixture { " strcpy ( s , \"\" ) ;" " free ( s ) ; " "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } { @@ -2091,7 +2105,7 @@ class TestSimplifyTokens : public TestFixture { " strcpy ( p , \"abc\" ) ;" " q = p ; " "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } // 3538 @@ -2106,7 +2120,7 @@ class TestSimplifyTokens : public TestFixture { "strcpy ( s , \"123\" ) ;\n" "if ( s [ 6 ] == ' ' ) { ; }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS("", filter_valueflow(errout_str())); } } @@ -2121,7 +2135,7 @@ class TestSimplifyTokens : public TestFixture { "int a ; int * p ; p = & a ;\n" "{ int a ; a = * p ; }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } { @@ -2133,7 +2147,7 @@ class TestSimplifyTokens : public TestFixture { "int * a ; int * * p ; p = & a ;\n" "{ int * a ; a = * p ; }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } } @@ -2146,7 +2160,7 @@ class TestSimplifyTokens : public TestFixture { "static int i = 10 ;\n" "b ( i ++ ) ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } void simplifyKnownVariables46() { @@ -2161,7 +2175,7 @@ class TestSimplifyTokens : public TestFixture { "cin >> x ;\n" "return x ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cin\n", errout_str()); @@ -2177,7 +2191,7 @@ class TestSimplifyTokens : public TestFixture { "int x ; x = 0 ;\n" "cin >> std :: hex >> x ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cin\n", errout_str()); @@ -2193,7 +2207,7 @@ class TestSimplifyTokens : public TestFixture { "int i ;\n" "for ( i = 0 ; ( i < sz ) && ( sz > 3 ) ; ++ i ) { }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, dinit(TokenizeAndStringifyOptions, $.cpp = false))); } void simplifyKnownVariables49() { // #3691 @@ -2209,7 +2223,7 @@ class TestSimplifyTokens : public TestFixture { "case 2 : ; x = sz ; break ;\n" "}\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, dinit(TokenizeAndStringifyOptions, $.cpp = false))); ASSERT_EQUALS( "[test.c:2]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable x\n", errout_str()); @@ -2234,7 +2248,7 @@ class TestSimplifyTokens : public TestFixture { "const char x7 = 'b' ;\n" "return & x7 ;\n" "}"; - ASSERT_EQUALS(code, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(code, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:5]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable y\n", errout_str()); @@ -2257,7 +2271,7 @@ class TestSimplifyTokens : public TestFixture { "const int x7 = 1 ;\n" "return & x7 ;\n" "}"; - ASSERT_EQUALS(code, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(code, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:5]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable y\n", errout_str()); @@ -2270,16 +2284,16 @@ class TestSimplifyTokens : public TestFixture { " BENCH1(q = _mhz_M(n); n = 1;)\n" " use_pointer(q);\n" "}"; - ASSERT_THROW_INTERNAL(tokenizeAndStringify(code, true), UNKNOWN_MACRO); + ASSERT_THROW_INTERNAL(tokenizeAndStringify(code), UNKNOWN_MACRO); } void simplifyKnownVariables54() { // #4913 - ASSERT_EQUALS("void f ( int * p ) { * -- p = 0 ; * p = 0 ; }", tokenizeAndStringify("void f(int*p) { *--p=0; *p=0; }", true)); + ASSERT_EQUALS("void f ( int * p ) { * -- p = 0 ; * p = 0 ; }", tokenizeAndStringify("void f(int*p) { *--p=0; *p=0; }")); } void simplifyKnownVariables56() { // ticket #5301 - >> ASSERT_EQUALS("void f ( ) { int a ; a = 0 ; int b ; b = 0 ; * p >> a >> b ; return a / b ; }", - tokenizeAndStringify("void f() { int a=0,b=0; *p>>a>>b; return a/b; }", true)); + tokenizeAndStringify("void f() { int a=0,b=0; *p>>a>>b; return a/b; }")); ASSERT_EQUALS( "[test.cpp:1]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable p\n", errout_str()); @@ -2302,7 +2316,7 @@ class TestSimplifyTokens : public TestFixture { "} " "int baz ( ) { " "return sizeof ( char [ VAL2 ] ) ; " - "}", tokenizeAndStringify(code, true)); + "}", tokenizeAndStringify(code)); } void simplifyKnownVariables59() { // #5062 - for head @@ -2317,7 +2331,7 @@ class TestSimplifyTokens : public TestFixture { "for ( i = 0 , j = 1 ; i < 3 , j < 12 ; i ++ , j ++ ) {\n" "a [ i ] = 0 ;\n" "}\n" - "}", tokenizeAndStringify(code, true)); + "}", tokenizeAndStringify(code)); } void simplifyKnownVariables61() { // #7805 @@ -2329,7 +2343,7 @@ class TestSimplifyTokens : public TestFixture { " Other\n" " };\n" " enum { XX };\n" - "};", /*expand=*/ true)); + "};")); ASSERT_EQUALS("", errout_str()); } @@ -2341,7 +2355,7 @@ class TestSimplifyTokens : public TestFixture { tokenizeAndStringify("void foo(std::string str) {\n" " char *p = &str[0];\n" " *p = 0;\n" - "}", /*expand=*/ true)); + "}")); } void simplifyKnownVariables63() { // #10798 @@ -2361,7 +2375,7 @@ class TestSimplifyTokens : public TestFixture { "if ( x ) { i = 10 ; }\n" "return i ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable x\n", filter_valueflow(errout_str())); @@ -2377,7 +2391,7 @@ class TestSimplifyTokens : public TestFixture { "struct ABC * last ; last = NULL ;\n" "nr = ( last = list . prev ) . nr ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable nr\n", errout_str()); @@ -2390,7 +2404,7 @@ class TestSimplifyTokens : public TestFixture { const char expected[] = "void foo ( ) {\n" "for ( int i = 0 ; i < 10 ; ++ i ) { }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS("", errout_str()); // debug warnings } @@ -2403,7 +2417,7 @@ class TestSimplifyTokens : public TestFixture { "int i ; i = 0 ;\n" "while ( i < 10 ) { ++ i ; }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS("", errout_str()); // debug warnings } @@ -2416,7 +2430,7 @@ class TestSimplifyTokens : public TestFixture { "for ( std :: string :: size_type pos = 0 ; pos < 10 ; ++ pos )\n" "{ }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); ASSERT_EQUALS("", errout_str()); // debug warnings } @@ -2429,7 +2443,7 @@ class TestSimplifyTokens : public TestFixture { "obj b ; b = a ;\n" "b . f ( ) ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } void simplifyKnownVariablesBailOutConditionalIncrement() { @@ -2440,7 +2454,7 @@ class TestSimplifyTokens : public TestFixture { " }\n" " return a;\n" "}\n"; - (void)tokenizeAndStringify(code,true); + (void)tokenizeAndStringify(code); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable x\n", filter_valueflow(errout_str())); // no debug warnings @@ -2482,7 +2496,7 @@ class TestSimplifyTokens : public TestFixture { "}\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } void simplifyKnownVariablesFunctionCalls() { @@ -2493,7 +2507,7 @@ class TestSimplifyTokens : public TestFixture { " a(x);" // <- don't replace with a(123); "}"; const char expected[] = "void a ( int & x ) ; void b ( ) { int x ; x = 123 ; a ( x ) ; }"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } } @@ -2504,7 +2518,7 @@ class TestSimplifyTokens : public TestFixture { " x = 123;" " while (!x) { dostuff(); }" "}"; - ASSERT_EQUALS("static int x ; void f ( ) { x = 123 ; while ( ! x ) { dostuff ( ) ; } }", tokenizeAndStringify(code,true)); + ASSERT_EQUALS("static int x ; void f ( ) { x = 123 ; while ( ! x ) { dostuff ( ) ; } }", tokenizeAndStringify(code)); ASSERT_EQUALS("", filter_valueflow(errout_str())); } @@ -2581,7 +2595,7 @@ class TestSimplifyTokens : public TestFixture { " memcpy(a, s, 10);\n" // <- don't simplify "a" here "}\n"; - const std::string s(tokenizeAndStringify(code, true)); + const std::string s(tokenizeAndStringify(code)); ASSERT_EQUALS(true, s.find("memcpy ( a , s , 10 ) ;") != std::string::npos); } @@ -2593,7 +2607,7 @@ class TestSimplifyTokens : public TestFixture { " memcpy(a, s, 10);\n" // <- simplify "a" "}\n"; - const std::string s(tokenizeAndStringify(code, true)); + const std::string s(tokenizeAndStringify(code)); TODO_ASSERT_EQUALS(true, false, s.find("memcpy ( 0 , s , 10 ) ;") != std::string::npos); } } diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 188ec37c3e5..5ff7dce2513 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -253,16 +253,23 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(typedefInfo2); } + struct TokOptions + { + TokOptions() = default; + bool simplify = true; + bool debugwarnings = true; + }; + #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) 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) { + std::string tok_(const char* file, int line, const char (&code)[size], const TokOptions& options = make_default_obj()) { // show warnings about unhandled typedef - const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings(debugwarnings).platform(type).build(); + const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings(options.debugwarnings).build(); SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); - return tokenizer.tokens()->stringifyList(nullptr, !simplify); + return tokenizer.tokens()->stringifyList(nullptr, !options.simplify); } std::string simplifyTypedef(const char code[]) { @@ -290,9 +297,9 @@ class TestSimplifyTypedef : public TestFixture { return tokenizer.tokens()->stringifyList(nullptr, false); } -#define checkSimplifyTypedef(code) checkSimplifyTypedef_(code, __FILE__, __LINE__) +#define checkSimplifyTypedef(...) checkSimplifyTypedef_(__FILE__, __LINE__, __VA_ARGS__) template - void checkSimplifyTypedef_(const char (&code)[size], const char* file, int line) { + void checkSimplifyTypedef_(const char* file, int line, const char (&code)[size]) { // Tokenize.. // show warnings about unhandled typedef const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings().build(); @@ -571,7 +578,7 @@ class TestSimplifyTypedef : public TestFixture { "a . foo ( ) ; " "wchar_t c ; c = 0 ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef2() { @@ -641,7 +648,7 @@ class TestSimplifyTypedef : public TestFixture { "unsigned int uvar ; uvar = 2 ; " "return uvar / ivar ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef5() { @@ -659,7 +666,7 @@ class TestSimplifyTypedef : public TestFixture { "struct yy_buffer_state * state ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef6() { @@ -674,21 +681,21 @@ class TestSimplifyTypedef : public TestFixture { "" "float fast_atan2 ( float y , float x ) { } " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); // ticket #11373 const char code1[] = "typedef int foo;\n" "inline foo f();\n"; const char expected1[] = "int f ( ) ;"; - ASSERT_EQUALS(expected1, tok(code1, false)); + ASSERT_EQUALS(expected1, tok(code1, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef7() { const char code[] = "typedef int abc ; " "Fred :: abc f ;"; const char expected[] = "Fred :: abc f ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef8() { @@ -719,7 +726,7 @@ class TestSimplifyTypedef : public TestFixture { "const int & trci ; " "const unsigned int & trcui ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef9() { @@ -745,7 +752,7 @@ class TestSimplifyTypedef : public TestFixture { "struct U u ; " "struct V * v ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef10() { @@ -771,7 +778,7 @@ class TestSimplifyTypedef : public TestFixture { "union U u ; " "union V * v ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef11() { @@ -785,7 +792,7 @@ class TestSimplifyTypedef : public TestFixture { "enum abc e1 ; " "enum xyz e2 ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef12() { @@ -804,7 +811,7 @@ class TestSimplifyTypedef : public TestFixture { "std :: vector < std :: vector < int > > v3 ; " "std :: list < int > :: iterator iter ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef13() { @@ -833,7 +840,7 @@ class TestSimplifyTypedef : public TestFixture { "};"; // Tokenize and check output.. - TODO_ASSERT_THROW(tok(code, true, Platform::Type::Native, false), InternalError); // TODO: Do not throw exception + TODO_ASSERT_THROW(tok(code, dinit(TokOptions, $.debugwarnings = false)), InternalError); // TODO: Do not throw exception //ASSERT_EQUALS("", errout_str()); } @@ -844,7 +851,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "char f [ 10 ] ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -853,7 +860,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "unsigned char f [ 10 ] ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -879,7 +886,7 @@ class TestSimplifyTypedef : public TestFixture { "char * pc ; " "char c ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef18() { @@ -902,7 +909,7 @@ class TestSimplifyTypedef : public TestFixture { "struct A * b ; " "struct A * * c ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -915,7 +922,7 @@ class TestSimplifyTypedef : public TestFixture { "struct A a ; " "struct A * * * * * * * * * b ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -930,7 +937,7 @@ class TestSimplifyTypedef : public TestFixture { "struct Unnamed0 * b ; " "struct Unnamed0 c ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -970,7 +977,7 @@ class TestSimplifyTypedef : public TestFixture { "static void test ( ) { } " "} ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -987,7 +994,7 @@ class TestSimplifyTypedef : public TestFixture { "static void * test ( void * p ) { return p ; } " "} ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -1004,7 +1011,7 @@ class TestSimplifyTypedef : public TestFixture { "static unsigned int * test ( unsigned int * p ) { return p ; } " "} ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -1022,7 +1029,7 @@ class TestSimplifyTypedef : public TestFixture { "static const unsigned int * test ( const unsigned int * p ) { return p ; } " "} ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -1039,7 +1046,7 @@ class TestSimplifyTypedef : public TestFixture { "static void * test ( void * p ) { return p ; } " "} ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -1052,7 +1059,7 @@ class TestSimplifyTypedef : public TestFixture { "void addCallback ( bool ( * callback ) ( int ) ) { } " "void addCallback1 ( bool ( * callback ) ( int ) , int j ) { }"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef24() { @@ -1069,7 +1076,7 @@ class TestSimplifyTypedef : public TestFixture { "int ( * f2 ) ( ) ; f2 = ( int * ) f ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -1085,7 +1092,7 @@ class TestSimplifyTypedef : public TestFixture { "int ( * f2 ) ( ) ; f2 = static_cast < int * > ( f ) ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -1127,7 +1134,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "void addCallback ( void ( * ( * callback ) ( ) ) ( ) ) ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -1139,7 +1146,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "struct mscrtc6845 * pc_video_start ( void ( * ( * choosevideomode ) ( running_machine * machine , int * width , int * height , struct mscrtc6845 * crtc ) ) ( bitmap_t * bitmap , struct mscrtc6845 * crtc ) ) ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -1158,7 +1165,7 @@ class TestSimplifyTypedef : public TestFixture { "VERIFY ( ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ) ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS( "[test.cpp:4]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable value\n", errout_str()); @@ -1170,7 +1177,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "std :: pair < double , double > ( * f ) ( double ) ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef29() { @@ -1191,7 +1198,7 @@ class TestSimplifyTypedef : public TestFixture { "int t ; " "int ia [ N ] ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef30() { @@ -1207,7 +1214,7 @@ class TestSimplifyTypedef : public TestFixture { ":: std :: list < int > :: iterator ili ; " ":: std :: list < int > ila [ 10 ] ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef31() { @@ -1232,7 +1239,7 @@ class TestSimplifyTypedef : public TestFixture { "int A :: get ( ) const { return a ; } " "int i ; i = A :: a ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -1254,7 +1261,7 @@ class TestSimplifyTypedef : public TestFixture { "int A :: get ( ) const { return a ; } " "int i ; i = A :: a ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -1271,7 +1278,7 @@ class TestSimplifyTypedef : public TestFixture { "char * cp ; " "const char * ccp ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef33() { @@ -1388,9 +1395,9 @@ class TestSimplifyTypedef : public TestFixture { "int A :: B :: C :: funC ( ) { return c ; } " "long A :: B :: C :: D :: funD ( ) { return d ; }"; - ASSERT_EQUALS(expected, tok(code, false)); - ASSERT_EQUALS(expected, tok(codePartialSpecified, false)); - ASSERT_EQUALS(expected, tok(codeFullSpecified, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); + ASSERT_EQUALS(expected, tok(codePartialSpecified, dinit(TokOptions, $.simplify = false))); + ASSERT_EQUALS(expected, tok(codeFullSpecified, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef34() { @@ -1412,7 +1419,7 @@ class TestSimplifyTypedef : public TestFixture { // int (**Foo)(int); // Foo = new (int(*[2])(int)) ; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef35() { @@ -1483,7 +1490,7 @@ class TestSimplifyTypedef : public TestFixture { "return a + s ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS_WITHOUT_LINENUMBERS("", errout_str()); } @@ -1498,7 +1505,7 @@ class TestSimplifyTypedef : public TestFixture { "{ " "* va_arg ( ap , void ( * * ) ( ) ) = 0 ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef37() { @@ -1511,14 +1518,14 @@ class TestSimplifyTypedef : public TestFixture { "{ " "int i ; { } " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef38() { const char code[] = "typedef C A;\n" "struct AB : public A, public B { };"; const char expected[] = "struct AB : public C , public B { } ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -1910,7 +1917,7 @@ class TestSimplifyTypedef : public TestFixture { "}"; // The expected result.. const char expected[] = "enum qboolean { qfalse , qtrue } ; void f ( ) { enum qboolean b ; enum qboolean ( * f ) ( struct le_s * , entity_t * ) ; }"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -1939,7 +1946,7 @@ class TestSimplifyTypedef : public TestFixture { // The expected tokens.. const char expected1[] = "void f ( ) { char a [ 256 ] ; char b [ 256 ] ; }"; - ASSERT_EQUALS(expected1, tok(code1, false)); + ASSERT_EQUALS(expected1, tok(code1, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); const char code2[] = "typedef char TString[256];\n" @@ -1950,7 +1957,7 @@ class TestSimplifyTypedef : public TestFixture { // The expected tokens.. const char expected2[] = "void f ( ) { char a [ 256 ] = { 0 } ; char b [ 256 ] = { 0 } ; }"; - ASSERT_EQUALS(expected2, tok(code2, false, Platform::Type::Native, false)); + ASSERT_EQUALS(expected2, tok(code2, dinit(TokOptions, $.simplify = false, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); const char code3[] = "typedef char TString[256];\n" @@ -1961,7 +1968,7 @@ class TestSimplifyTypedef : public TestFixture { // The expected tokens.. const char expected3[] = "void f ( ) { char a [ 256 ] ; a = \"\" ; char b [ 256 ] ; b = \"\" ; }"; - ASSERT_EQUALS(expected3, tok(code3, false, Platform::Type::Native, false)); + ASSERT_EQUALS(expected3, tok(code3, dinit(TokOptions, $.simplify = false, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); const char code4[] = "typedef char TString[256];\n" @@ -1972,7 +1979,7 @@ class TestSimplifyTypedef : public TestFixture { // The expected tokens.. const char expected4[] = "void f ( ) { char a [ 256 ] ; a = \"1234\" ; char b [ 256 ] ; b = \"5678\" ; }"; - ASSERT_EQUALS(expected4, tok(code4, false, Platform::Type::Native, false)); + ASSERT_EQUALS(expected4, tok(code4, dinit(TokOptions, $.simplify = false, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -1998,7 +2005,7 @@ class TestSimplifyTypedef : public TestFixture { " Foo b(0);\n" " return b > Foo(10);\n" "}"; - const std::string actual(tok(code, true, Platform::Type::Native, false)); + const std::string actual(tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("int main ( ) { BAR < int > b ( 0 ) ; return b > BAR < int > ( 10 ) ; }", actual); ASSERT_EQUALS("", errout_str()); } @@ -2158,7 +2165,7 @@ class TestSimplifyTypedef : public TestFixture { void simplifyTypedef75() { // ticket #2426 const char code[] = "typedef _Packed struct S { long l; };"; - ASSERT_EQUALS(";", tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(";", tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -2480,7 +2487,7 @@ class TestSimplifyTypedef : public TestFixture { "public: " "expression_error :: error_code ( * f ) ( void * , const char * , expression_space ) ; " "} ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -2644,7 +2651,7 @@ class TestSimplifyTypedef : public TestFixture { "} ; " "} " "}"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -2732,7 +2739,7 @@ class TestSimplifyTypedef : public TestFixture { "unsigned t2 ;"; const char expected[] = "unsigned int t1 ; " "unsigned int t2 ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2748,7 +2755,7 @@ class TestSimplifyTypedef : public TestFixture { "for ( std :: vector < CharacterConversion > :: iterator it = c2c . begin ( ) ; it != c2c . end ( ) ; ++ it ) { } " "std :: vector < CharacterConversion > ( ) . swap ( c2c ) ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS_WITHOUT_LINENUMBERS("", errout_str()); } @@ -2761,7 +2768,7 @@ class TestSimplifyTypedef : public TestFixture { "struct bstr bstr0 ( const char * s ) { " "return ( struct bstr ) { ( unsigned char * ) s , s ? strlen ( s ) : 0 } ; " "}"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2777,7 +2784,7 @@ class TestSimplifyTypedef : public TestFixture { "operatorintClassyClass::* ( ) { " "return & ClassyClass :: id ; " "} }"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2789,7 +2796,7 @@ class TestSimplifyTypedef : public TestFixture { "namespace Baz { }\n" "enum Bar { XX = 1 };"; const char exp[] = "enum Bar { XX = 1 } ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2799,7 +2806,7 @@ class TestSimplifyTypedef : public TestFixture { "static void report_good(bool passed, test_utf8_char const c) { };"; const char exp[] = "static const char bad_chars [ ] [ 5 ] = { } ; " "static void report_good ( bool passed , const char c [ 5 ] ) { } ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2836,7 +2843,7 @@ class TestSimplifyTypedef : public TestFixture { "float * * & Fred :: m ( ) { return m3x3 ; } " "const float * & Fred :: vc ( ) const { return v3 ; } " "const float * * & Fred :: mc ( ) const { return m3x3 ; }"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2850,7 +2857,7 @@ class TestSimplifyTypedef : public TestFixture { const char code[] = "typedef int intvec[1];\n" "Dummy y;"; const char exp[] = "Dummy < int [ 1 ] > y ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -2860,7 +2867,7 @@ class TestSimplifyTypedef : public TestFixture { "size_t f(size_t s);"; const char exp[] = "long f ( long s ) ;"; - ASSERT_EQUALS(exp, tok(code, /*simplify*/ true)); + ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS("", errout_str()); const char code1[] = "typedef long unsigned int uint32_t;\n" @@ -2876,14 +2883,14 @@ class TestSimplifyTypedef : public TestFixture { const char code[] = "typedef char A[3];\n" "char (*p)[3] = new A[4];"; const char exp[] = "char ( * p ) [ 3 ] = new char [ 4 ] [ 3 ] ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef126() { // #5953 const char code[] = "typedef char automap_data_t[100];\n" "void write_array(automap_data_t *data) {}"; const char exp[] = "void write_array ( char ( * data ) [ 100 ] ) { }"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef127() { // #8878 @@ -2893,7 +2900,7 @@ class TestSimplifyTypedef : public TestFixture { const char exp[] = "class a ; " "template < long , class > struct c ; " "template < int g > struct d { enum Anonymous0 { e = c < g , int ( a :: * ) > :: f } ; } ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef128() { // #9053 @@ -2904,7 +2911,7 @@ class TestSimplifyTypedef : public TestFixture { const char exp[] = "void f ( ) { " "dostuff ( ( const int [ 4 ] ) { 1 , 2 , 3 , 4 } ) ; " "}"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef129() { @@ -2915,7 +2922,7 @@ class TestSimplifyTypedef : public TestFixture { "};"; const char exp[] = "class c { char ( & f ) [ 4 ] ; } ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -2925,7 +2932,7 @@ class TestSimplifyTypedef : public TestFixture { "};"; const char exp[] = "class c { const char ( & f ) [ 4 ] ; } ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -2936,7 +2943,7 @@ class TestSimplifyTypedef : public TestFixture { "};"; const char exp[] = "class c { char _a [ 4 ] ; const constexpr char ( & c_str ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -2948,7 +2955,7 @@ class TestSimplifyTypedef : public TestFixture { const char actual[] = "class c { char _a [ 4 ] ; constexpr char ( & operatorchar ( ) noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; const char exp[] = "class c { char _a [ 4 ] ; char ( & operatorchar ( ) noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; - TODO_ASSERT_EQUALS(exp, actual, tok(code, false)); + TODO_ASSERT_EQUALS(exp, actual, tok(code, dinit(TokOptions, $.simplify = false))); } { @@ -2960,7 +2967,7 @@ class TestSimplifyTypedef : public TestFixture { const char actual[] = "class c { char _a [ 4 ] ; constexpr const char ( & operatorconstchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; const char exp[] = "class c { char _a [ 4 ] ; const char ( & operatorconstchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;"; - TODO_ASSERT_EQUALS(exp, actual, tok(code, false)); + TODO_ASSERT_EQUALS(exp, actual, tok(code, dinit(TokOptions, $.simplify = false))); } } @@ -2974,7 +2981,7 @@ class TestSimplifyTypedef : public TestFixture { "a < int ( * ) [ 10 ] , int ( * ) [ 10 ] > ( ) ; " "}"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef131() { @@ -2990,7 +2997,7 @@ class TestSimplifyTypedef : public TestFixture { "a4p = & ( a4obj ) ; " "unsigned char ( * && a4p_rref ) [ 4 ] = std :: move ( a4p ) ;"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef132() { @@ -3011,14 +3018,14 @@ class TestSimplifyTypedef : public TestFixture { "} ; " "void A :: DoSomething ( int wrongName ) { }"; - ASSERT_EQUALS(exp, tok(code, false)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef133() { // #9812 const char code[] = "typedef unsigned char array_t[16];\n" "using array_p = const array_t *;\n" "array_p x;\n"; - ASSERT_EQUALS("using array_p = const unsigned char ( * ) [ 16 ] ; array_p x ;", tok(code, false)); + ASSERT_EQUALS("using array_p = const unsigned char ( * ) [ 16 ] ; array_p x ;", tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("[test.cpp:2]: (debug) Failed to parse 'using array_p = const unsigned char ( * ) [ 16 ] ;'. The checking continues anyway.\n", errout_str()); } @@ -3027,7 +3034,7 @@ class TestSimplifyTypedef : public TestFixture { "typedef int int32;\n" "namespace foo { int64 i; }\n" "int32 j;"; - ASSERT_EQUALS("; namespace foo { long long i ; } int j ;", tok(code, false)); + ASSERT_EQUALS("; namespace foo { long long i ; } int j ;", tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef135() { @@ -3087,7 +3094,7 @@ class TestSimplifyTypedef : public TestFixture { "class S3 s3 ; " "class S4 s4 ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); } void simplifyTypedef137() { // #10054 debug: Executable scope 'x' with unknown function. @@ -3123,7 +3130,7 @@ class TestSimplifyTypedef : public TestFixture { "void A :: f ( external :: ns1 :: B<1> ) { } " "} " "struct external :: ns1 :: B<1> { } ;"; - ASSERT_EQUALS(exp, tok(code, true, Platform::Type::Native, true)); + ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS("", errout_str()); } { @@ -3156,7 +3163,7 @@ class TestSimplifyTypedef : public TestFixture { "void A :: f ( external :: ns1 :: B<1> ) { } " "} " "struct external :: ns1 :: B<1> { } ;"; - ASSERT_EQUALS(exp, tok(code, true, Platform::Type::Native, true)); + ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS("", errout_str()); } { @@ -3206,7 +3213,7 @@ class TestSimplifyTypedef : public TestFixture { "void A :: f ( V ) { } " "} " "struct external :: ns1 :: B<1> { } ;"; - TODO_ASSERT_EQUALS(exp, act, tok(code, true, Platform::Type::Native, true)); + TODO_ASSERT_EQUALS(exp, act, tok(code)); TODO_ASSERT_EQUALS("", "[test.cpp:14]: (debug) Executable scope 'f' with unknown function.\n", errout_str()); } { @@ -3255,7 +3262,7 @@ class TestSimplifyTypedef : public TestFixture { "namespace ns { " "void A :: f ( V ) { } " "}"; - TODO_ASSERT_EQUALS(exp, act, tok(code, true, Platform::Type::Native, true)); + TODO_ASSERT_EQUALS(exp, act, tok(code)); ASSERT_EQUALS("", errout_str()); } { @@ -3285,7 +3292,7 @@ class TestSimplifyTypedef : public TestFixture { "void A :: f ( external :: B<1> ) { } " "} " "struct external :: B<1> { } ;"; - ASSERT_EQUALS(exp, tok(code, true, Platform::Type::Native, true)); + ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS("", errout_str()); } { @@ -3313,7 +3320,7 @@ class TestSimplifyTypedef : public TestFixture { "void A :: f ( B<1> ) { } " "} " "struct B<1> { } ;"; - ASSERT_EQUALS(exp, tok(code, true, Platform::Type::Native, true)); + ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS("", errout_str()); } } @@ -4013,7 +4020,7 @@ class TestSimplifyTypedef : public TestFixture { "C ( * f5 ) ( ) ; " "C ( * f6 ) ( ) ; " "C ( * f7 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -4042,7 +4049,7 @@ class TestSimplifyTypedef : public TestFixture { "const C ( * f5 ) ( ) ; " "const C ( * f6 ) ( ) ; " "const C ( * f7 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -4070,7 +4077,7 @@ class TestSimplifyTypedef : public TestFixture { "const C ( * f5 ) ( ) ; " "const C ( * f6 ) ( ) ; " "const C ( * f7 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -4098,7 +4105,7 @@ class TestSimplifyTypedef : public TestFixture { "C * ( * f5 ) ( ) ; " "C * ( * f6 ) ( ) ; " "C * ( * f7 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -4126,7 +4133,7 @@ class TestSimplifyTypedef : public TestFixture { "const C * ( * f5 ) ( ) ; " "const C * ( * f6 ) ( ) ; " "const C * ( * f7 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -4155,7 +4162,7 @@ class TestSimplifyTypedef : public TestFixture { "const C * ( * f5 ) ( ) ; " "const C * ( * f6 ) ( ) ; " "const C * ( * f7 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } } @@ -4172,7 +4179,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "int ( * ( * t1 ) ( bool ) ) ( int , int ) ; " "int * t2 ( bool ) ; " "int * t3 ( bool ) ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -4215,7 +4222,7 @@ class TestSimplifyTypedef : public TestFixture { "int ( :: C :: * const t10 ) ( float ) ; " "int ( :: C :: * volatile t11 ) ( float ) ; " "int ( :: C :: * const volatile t12 ) ( float ) ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -4242,7 +4249,7 @@ class TestSimplifyTypedef : public TestFixture { "void * Fred :: get3 ( ) { return 0 ; } " "void * Fred :: get4 ( ) { return 0 ; }"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -4253,7 +4260,7 @@ class TestSimplifyTypedef : public TestFixture { // The expected result.. const char expected[] = "void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * X ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.simplify = false))); ASSERT_EQUALS("", errout_str()); } @@ -4300,7 +4307,7 @@ class TestSimplifyTypedef : public TestFixture { "B :: C ( * f2 ) ( ) ; " "B :: C ( * f3 ) ( ) ; " "B :: C ( * f4 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } @@ -4338,7 +4345,7 @@ class TestSimplifyTypedef : public TestFixture { "A :: B :: C ( * f2 ) ( ) ; " "A :: B :: C ( * f3 ) ( ) ; " "A :: B :: C ( * f4 ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code, true, Platform::Type::Native, false)); + ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false))); ASSERT_EQUALS("", errout_str()); } } @@ -4358,7 +4365,7 @@ class TestSimplifyTypedef : public TestFixture { "} " "Format_E1 ( * * t1 ) ( ) ; " "MySpace :: Format_E2 ( * * t2 ) ( ) ;", - tok(code,false)); + tok(code,dinit(TokOptions, $.simplify = false))); } void simplifyTypedefFunction11() { @@ -4378,22 +4385,22 @@ class TestSimplifyTypedef : public TestFixture { "if ( g != void * ( p ) ) { } " "if ( g != ( void * ) p ) { } " "}", - tok(code,false)); + tok(code,dinit(TokOptions, $.simplify = false))); ignore_errout(); // we are not interested in the output } void simplifyTypedefStruct() { const char code1[] = "typedef struct S { int x; } xyz;\n" "xyz var;"; - ASSERT_EQUALS("struct S { int x ; } ; struct S var ;", tok(code1,false)); + ASSERT_EQUALS("struct S { int x ; } ; struct S var ;", tok(code1,dinit(TokOptions, $.simplify = false))); const char code2[] = "typedef const struct S { int x; } xyz;\n" "xyz var;"; - ASSERT_EQUALS("struct S { int x ; } ; const struct S var ;", tok(code2,false)); + ASSERT_EQUALS("struct S { int x ; } ; const struct S var ;", tok(code2,dinit(TokOptions, $.simplify = false))); const char code3[] = "typedef volatile struct S { int x; } xyz;\n" "xyz var;"; - ASSERT_EQUALS("struct S { int x ; } ; volatile struct S var ;", tok(code3,false)); + ASSERT_EQUALS("struct S { int x ; } ; volatile struct S var ;", tok(code3,dinit(TokOptions, $.simplify = false))); } void simplifyTypedefShadow() { // shadow variable (#4445) @@ -4402,7 +4409,7 @@ class TestSimplifyTypedef : public TestFixture { " int abc, xyz;\n" // <- shadow variable "}"; ASSERT_EQUALS("struct xyz { int x ; } ; void f ( ) { int abc ; int xyz ; }", - tok(code,false)); + tok(code,dinit(TokOptions, $.simplify = false))); } void simplifyTypedefMacro() { diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index c3dbd0a6788..e1b2ad7ac82 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -100,12 +100,20 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(scopeInfo2); } + struct TokOptions + { + TokOptions() = default; + Platform::Type type = Platform::Type::Native; + bool debugwarnings = true; + bool preprocess = false; + }; + #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) 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(); + std::string tok_(const char* file, int line, const char (&code)[size], const TokOptions& options = make_default_obj()) { + const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings(options.debugwarnings).platform(options.type).build(); - if (preprocess) { + if (options.preprocess) { Tokenizer tokenizer(settings, *this); std::vector files(1, "test.cpp"); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -415,7 +423,7 @@ class TestSimplifyUsing : public TestFixture { " FP_M(val);" "};"; - TODO_ASSERT_THROW(tok(code, Platform::Type::Native, false), InternalError); // TODO: Do not throw AST validation exception + TODO_ASSERT_THROW(tok(code, dinit(TokOptions, $.debugwarnings = false)), InternalError); // TODO: Do not throw AST validation exception //ASSERT_EQUALS("", errout_str()); } @@ -682,7 +690,7 @@ class TestSimplifyUsing : public TestFixture { " T* p{ new T };\n" "}\n"; const char expected[] = "void f ( ) { int * p { new int } ; }"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } @@ -690,7 +698,7 @@ class TestSimplifyUsing : public TestFixture { const char code[] = "using T = int*;\n" "void f(T = T()) {}\n"; const char expected[] = "void f ( int * = ( int * ) 0 ) { }"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } @@ -701,20 +709,20 @@ class TestSimplifyUsing : public TestFixture { " std::string str = to_string(1);\n" "}\n"; const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } { const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n" "cerr << \"abc\";\n"; const char expected[] = "std :: cerr << \"abc\" ;"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } { const char code[] = "using std::string_view_literals::operator\"\"sv;\n"; const char expected[] = "using std :: string_view_literals :: operator\"\"sv ;"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } { @@ -732,7 +740,7 @@ class TestSimplifyUsing : public TestFixture { "using vector = :: std :: vector :: vector ; " "vector ( ) { } " "} ;"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } { @@ -744,7 +752,7 @@ class TestSimplifyUsing : public TestFixture { "void f ( const char * c ) { " "cout << std :: string ( c ) << \"abc\" ; " "}"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS( "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cout\n", errout_str()); @@ -756,7 +764,7 @@ class TestSimplifyUsing : public TestFixture { const char expected[] = "class T : private std :: vector < std :: pair < std :: string , const int * > > { " "using empty = std :: vector < std :: pair < std :: string , const int * > > :: empty ; " "} ;"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } } @@ -784,7 +792,7 @@ class TestSimplifyUsing : public TestFixture { "} " "B b ; " "} ;"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } @@ -794,7 +802,7 @@ class TestSimplifyUsing : public TestFixture { "T g() { return T(malloc(4)); }\n"; const char expected[] = "int * f ( ) { return ( int * ) 0 ; } " "int * g ( ) { return ( int * ) ( malloc ( 4 ) ) ; }"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); const char code2[] = "struct S {\n" // #13095 @@ -810,7 +818,7 @@ class TestSimplifyUsing : public TestFixture { "int i ; " "} ; " "auto S :: get ( ) . int & { return i ; }"; - ASSERT_EQUALS(expected2, tok(code2, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected2, tok(code2)); TODO_ASSERT_EQUALS("", "[test.cpp:6]: (debug) auto token with no type.\n" "", errout_str()); @@ -831,7 +839,7 @@ class TestSimplifyUsing : public TestFixture { "struct S { " "int g ( ) { return ( int ) 0 ; } " "} ;"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } @@ -851,7 +859,7 @@ class TestSimplifyUsing : public TestFixture { "void f ( ) { " "g ( { A :: a } ) ; " "}"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout_str()); } @@ -913,11 +921,11 @@ class TestSimplifyUsing : public TestFixture { const char exp[] = "int i ;"; - ASSERT_EQUALS(exp, tok(code, Platform::Type::Unix32)); - ASSERT_EQUALS(exp, tok(code, Platform::Type::Unix64)); - ASSERT_EQUALS(exp, tok(code, Platform::Type::Win32A)); - ASSERT_EQUALS(exp, tok(code, Platform::Type::Win32W)); - ASSERT_EQUALS(exp, tok(code, Platform::Type::Win64)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Unix32))); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Unix64))); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Win32A))); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Win32W))); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Win64))); } void simplifyUsing9042() { @@ -937,7 +945,7 @@ class TestSimplifyUsing : public TestFixture { "} ; " "template < class T > class s { } ;"; - ASSERT_EQUALS(exp, tok(code, Platform::Type::Win64)); + ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Win64))); } void simplifyUsing9191() { @@ -1573,7 +1581,7 @@ class TestSimplifyUsing : public TestFixture { "STAMP(A, int);\n" "STAMP(B, A);\n" "STAMP(C, B);\n"; - (void)tok(code, Platform::Type::Native, /*debugwarnings*/ true, /*preprocess*/ true); + (void)tok(code, dinit(TokOptions, $.preprocess = true)); ASSERT(startsWith(errout_str(), "[test.cpp:6]: (debug) Failed to parse 'using C = S < S < S < int")); } diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index f0e05c8de44..4fac4a3ebfc 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -48,9 +48,9 @@ class TestSizeof : public TestFixture { TEST_CASE(customStrncat); } -#define check(code) check_(code, __FILE__, __LINE__) +#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char (&code)[size], const char* file, int line) { + 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/teststl.cpp b/test/teststl.cpp index 6f82250e33e..0e83a9dca6e 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -177,10 +177,17 @@ class TestStl : public TestFixture { TEST_CASE(checkMutexes); } + struct CheckOptions + { + CheckOptions() = default; + bool inconclusive = false; + Standards::cppstd_t cppstandard = Standards::CPPLatest; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).cpp(options.cppstandard).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); @@ -403,7 +410,7 @@ class TestStl : public TestFixture { " S s = { { &ArrS } };\n" " g(s);\n" " return ArrS[0];\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct T {\n" @@ -419,7 +426,7 @@ class TestStl : public TestFixture { " g(s);\n" " return ArrS[0];\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct T {\n" @@ -435,7 +442,7 @@ class TestStl : public TestFixture { " g(s);\n" " return ArrS[0];\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct T {\n" @@ -451,7 +458,7 @@ class TestStl : public TestFixture { " g(s);\n" " return ArrS[0];\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct T {\n" @@ -467,7 +474,7 @@ class TestStl : public TestFixture { " g(s);\n" " return ArrS[0];\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct T {\n" @@ -483,7 +490,7 @@ class TestStl : public TestFixture { " g(s);\n" " return ArrS[0];\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); checkNormal("extern void Bar(const double, const double);\n" @@ -710,7 +717,7 @@ class TestStl : public TestFixture { " std::string buf;\n" " b(buf.begin());\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression 'd+c.length()' because 'buf' is empty.\n", errout_str()); @@ -723,13 +730,13 @@ class TestStl : public TestFixture { " std::string buf;\n" " b(buf.begin());\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("int f(const std::vector &v) {\n" " return !v.empty() ? 42 : v.back();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "test.cpp:2:warning:Either the condition 'v.empty()' is redundant or expression 'v.back()' causes access out of bounds.\n" "test.cpp:2:note:condition 'v.empty()'\n" @@ -2345,7 +2352,7 @@ class TestStl : public TestFixture { " iter = ints.begin() + 2;\n" " ints.erase(iter);\n" " std::cout << (*iter) << std::endl;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:6] -> [test.cpp:3] -> [test.cpp:7]: (error) Using iterator to local container 'ints' that may be invalid.\n", "[test.cpp:5] -> [test.cpp:6] -> [test.cpp:3] -> [test.cpp:7]: (error, inconclusive) Using iterator to local container 'ints' that may be invalid.\n", errout_str()); // #6554 "False positive eraseDereference - erase in while() loop" @@ -2420,7 +2427,7 @@ class TestStl : public TestFixture { " auto iter = ints.begin() + 2;\n" " ints.erase(iter);\n" " std::cout << (*iter) << std::endl;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5] -> [test.cpp:3] -> [test.cpp:6]: (error) Using iterator to local container 'ints' that may be invalid.\n", "[test.cpp:4] -> [test.cpp:5] -> [test.cpp:3] -> [test.cpp:6]: (error, inconclusive) Using iterator to local container 'ints' that may be invalid.\n", errout_str()); check("void f() {\n" @@ -3018,7 +3025,7 @@ class TestStl : public TestFixture { " iter = ints.begin() + 2;\n" " ints.erase(iter);\n" " ints.erase(iter);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:4] -> [test.cpp:5] -> [test.cpp:1] -> [test.cpp:6]: (error) Using iterator to local container 'ints' that may be invalid.\n", "[test.cpp:1] -> [test.cpp:4] -> [test.cpp:5] -> [test.cpp:1] -> [test.cpp:6]: (error, inconclusive) Using iterator to local container 'ints' that may be invalid.\n", errout_str()); } @@ -3337,7 +3344,7 @@ class TestStl : public TestFixture { " std::vector::iterator iter = ints.begin();\n" " ints.insert(iter, 1);\n" " ints.insert(iter, 2);\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5] -> [test.cpp:3] -> [test.cpp:6]: (error) Using iterator to local container 'ints' that may be invalid.\n", "[test.cpp:4] -> [test.cpp:5] -> [test.cpp:3] -> [test.cpp:6]: (error, inconclusive) Using iterator to local container 'ints' that may be invalid.\n", errout_str()); check("void* f(const std::vector& bars) {\n" @@ -3520,7 +3527,7 @@ class TestStl : public TestFixture { "int foo() {\n" " iterator i;\n" " return i.foo();;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("class iterator {\n" @@ -3531,7 +3538,7 @@ class TestStl : public TestFixture { "int foo() {\n" " iterator i;\n" " return i.foo();;\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:8]: (error, inconclusive) Invalid iterator 'i' used.\n", errout_str()); } @@ -3542,7 +3549,7 @@ class TestStl : public TestFixture { " {\n" " }\n" " }\n" - "}", true); + "}", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -3780,7 +3787,7 @@ class TestStl : public TestFixture { "{\n" " if (x.size() == 0) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:7]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3792,7 +3799,7 @@ class TestStl : public TestFixture { "{\n" " if (x.size() == 0) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3804,7 +3811,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (x.size() == 0) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3816,7 +3823,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (0 == x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3828,7 +3835,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (x.size() != 0) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3840,7 +3847,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (0 != x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3852,7 +3859,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (x.size() > 0) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3864,7 +3871,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (0 < x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3876,7 +3883,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (x.size() >= 1) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3888,7 +3895,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (x.size() < 1) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3900,7 +3907,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (1 <= x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3912,7 +3919,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (1 > x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3924,7 +3931,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3936,7 +3943,7 @@ class TestStl : public TestFixture { " std::list x;\n" " if (!x.size()) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3955,7 +3962,7 @@ class TestStl : public TestFixture { " std::list x;\n" " fun(!x.size());\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -3967,7 +3974,7 @@ class TestStl : public TestFixture { " std::list x;\n" " fun(a && x.size());\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout_str()); check(code); ASSERT_EQUALS("", errout_str()); @@ -4004,7 +4011,7 @@ class TestStl : public TestFixture { "{\n" " if (f.x.size() == 0) {}\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = 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 @@ -4027,7 +4034,7 @@ class TestStl : public TestFixture { "int main() {\n" " if (zzz->x.size() > 0) { }\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = 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 @@ -4046,7 +4053,7 @@ class TestStl : public TestFixture { " Zzz * zzz;\n" " if (zzz->x.size() > 0) { }\n" "}"; - check(code, false, Standards::CPP03); + check(code, dinit(CheckOptions, $.cppstandard = 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 @@ -4482,7 +4489,7 @@ class TestStl : public TestFixture { " return (\"\" + m).c_str();\n" " }\n" " std::string m;\n" - "};\n", /*inconclusive*/ true); + "};\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout_str()); check("struct S {\n" // #10493 @@ -5215,7 +5222,7 @@ class TestStl : public TestFixture { " iterator it;\n" " it->m_place = 0;\n" " return it;\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:18]: (error, inconclusive) Invalid iterator 'it' used.\n", errout_str()); } @@ -5224,35 +5231,35 @@ class TestStl : public TestFixture { " for(int& x:v)\n" " x = 1;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::fill algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" " for(int& x:v)\n" " x = x + 1;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo(int a, int b) {\n" " for(int& x:v)\n" " x = a + b;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::fill or std::generate algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" " for(int& x:v)\n" " x += 1;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" " for(int& x:v)\n" " x = f();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::generate algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5261,7 +5268,7 @@ class TestStl : public TestFixture { " x = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" @@ -5270,7 +5277,7 @@ class TestStl : public TestFixture { " f();\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // There should probably be a message for unconditional break @@ -5280,14 +5287,14 @@ class TestStl : public TestFixture { " break;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " for(int& x:v)\n" " x = ++x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -5297,7 +5304,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n += x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::accumulate algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5305,7 +5312,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = n + x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::accumulate algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5313,7 +5320,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n += 1;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::distance algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5321,7 +5328,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = n + 1;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::distance algorithm instead of a raw loop.\n", errout_str()); check("bool f(int);\n" @@ -5330,7 +5337,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " b &= f(x);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool f(int);\n" @@ -5339,7 +5346,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " b |= f(x);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool f(int);\n" @@ -5348,7 +5355,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " b = b && f(x);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool f(int);\n" @@ -5357,7 +5364,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " b = b || f(x);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" @@ -5365,7 +5372,7 @@ class TestStl : public TestFixture { " for(int& x:v)\n" " n = ++x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("std::size_t f(const std::map& m) {\n" // #10412 @@ -5432,7 +5439,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_back(x);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::copy algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5440,7 +5447,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_back(f(x));\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5448,7 +5455,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_back(x + 1);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5456,7 +5463,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_front(x);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::copy algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5464,7 +5471,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_front(f(x));\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5472,7 +5479,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_front(x + 1);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5480,7 +5487,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_back(v);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" @@ -5488,7 +5495,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " c.push_back(0);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -5498,7 +5505,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n++;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::distance algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5506,21 +5513,21 @@ class TestStl : public TestFixture { " for(int x:v)\n" " ++n;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::distance algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" " for(int& x:v)\n" " x++;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" " for(int& x:v)\n" " ++x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); } @@ -5533,7 +5540,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (style) Consider using std::replace_if algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5545,7 +5552,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:6]: (style) Consider using std::accumulate algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5557,7 +5564,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:6]: (style) Consider using std::count_if algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5569,7 +5576,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:6]: (style) Consider using std::count_if algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5580,7 +5587,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (style) Consider using std::transform algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5592,7 +5599,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:6]: (style) Consider using std::copy_if algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5604,7 +5611,7 @@ class TestStl : public TestFixture { " }\n" " return true;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::all_of or std::none_of algorithm instead of a raw loop.\n", errout_str()); @@ -5617,7 +5624,7 @@ class TestStl : public TestFixture { " }\n" " return true;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::any_of algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5630,7 +5637,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (style) Consider using std::any_of algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5643,7 +5650,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (style) Consider using std::find_if algorithm instead of a raw loop.\n", errout_str()); check("bool pred(int x);\n" @@ -5657,7 +5664,7 @@ class TestStl : public TestFixture { " if(b) {}\n" " return true;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool pred(int x);\n" @@ -5670,7 +5677,7 @@ class TestStl : public TestFixture { " }\n" " return true;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool pred(int x);\n" @@ -5683,7 +5690,7 @@ class TestStl : public TestFixture { " }\n" " return true;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool pred(int x);\n" @@ -5695,7 +5702,7 @@ class TestStl : public TestFixture { " return true;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // There is no transform_if @@ -5708,7 +5715,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool pred(int x);\n" @@ -5720,7 +5727,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool pred(int x);\n" @@ -5733,7 +5740,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool g(int);\n" @@ -5809,7 +5816,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = x > n ? x : n;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::max_element algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5817,7 +5824,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = x < n ? x : n;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::min_element algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5825,7 +5832,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = x > n ? n : x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::min_element algorithm instead of a raw loop.\n", errout_str()); check("void foo() {\n" @@ -5833,7 +5840,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = x < n ? n : x;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::max_element algorithm instead of a raw loop.\n", errout_str()); check("void foo(int m) {\n" @@ -5841,7 +5848,7 @@ class TestStl : public TestFixture { " for(int x:v)\n" " n = x > m ? x : n;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::accumulate algorithm instead of a raw loop.\n", errout_str()); check("void f(const std::vector& v) {\n" @@ -5851,7 +5858,7 @@ class TestStl : public TestFixture { " maxY = y;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Consider using std::max_element algorithm instead of a raw loop.\n", "", errout_str()); @@ -5868,7 +5875,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (style) Consider using std::any_of algorithm instead of a raw loop.\n", errout_str()); @@ -5881,7 +5888,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (style) Consider using std::any_of algorithm instead of a raw loop.\n", errout_str()); @@ -5894,7 +5901,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool g(std::vector& v) {\n" @@ -5906,7 +5913,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool g(const std::vector& v, int& j) {\n" @@ -5918,7 +5925,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool g(const std::vector& v, int& j) {\n" @@ -5931,7 +5938,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool g(const std::vector& v, int& j) {\n" @@ -5944,7 +5951,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("bool g(const std::vector& v, int j) {\n" @@ -5957,7 +5964,7 @@ class TestStl : public TestFixture { " }\n" " return false;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2]: (style) Consider using std::all_of or std::none_of algorithm instead of a raw loop.\n", errout_str()); @@ -5975,7 +5982,7 @@ class TestStl : public TestFixture { " QDir(s);\n" " }\n" "};\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -5985,7 +5992,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << *v0 << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:1] -> [test.cpp:4]: (error) Using iterator to local container 'v' that may be invalid.\n", errout_str()); check("std::string e();\n" @@ -5997,7 +6004,7 @@ class TestStl : public TestFixture { " if (d != f.end()) {}\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::vector &v) {\n" @@ -6005,7 +6012,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << (*v0)[0] << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:1] -> [test.cpp:4]: (error) Using pointer to local variable 'v' that may be invalid.\n", errout_str()); check("void f() {\n" @@ -6014,7 +6021,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << v0 << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:3] -> [test.cpp:3] -> [test.cpp:4] -> [test.cpp:5]: (error) Reference to v that may be invalid.\n", errout_str()); @@ -6025,7 +6032,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << v0 << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4] -> [test.cpp:5]: (error) Reference to v that may be invalid.\n", errout_str()); @@ -6034,7 +6041,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << v0 << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:2] -> [test.cpp:1] -> [test.cpp:3] -> [test.cpp:4]: (error) Reference to v that may be invalid.\n", errout_str()); @@ -6044,7 +6051,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << v0 << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:1] -> [test.cpp:3] -> [test.cpp:4]: (error) Reference to v that may be invalid.\n", errout_str()); @@ -6054,7 +6061,7 @@ class TestStl : public TestFixture { " v.push_back(123);\n" " std::cout << (*v0)[0] << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("const std::vector * g(int);\n" @@ -6070,7 +6077,7 @@ class TestStl : public TestFixture { " if (m == 0) {}\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("std::vector g();\n" @@ -6080,7 +6087,7 @@ class TestStl : public TestFixture { " std::string z;\n" " z += \"\";\n" " z += y;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::vector v)\n" @@ -6092,14 +6099,14 @@ class TestStl : public TestFixture { " cur = v.data();\n" " end = cur + v.size();\n" " }\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9598 check("void f(std::vector v) {\n" " for (auto it = v.begin(); it != v.end(); it = v.erase(it))\n" " *it;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9714 @@ -6108,7 +6115,7 @@ class TestStl : public TestFixture { " std::string x;\n" " v.push_back(x.insert(0, \"x\"));\n" " v.push_back(\"y\");\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9783 @@ -6117,7 +6124,7 @@ class TestStl : public TestFixture { "void foo() {\n" " CString sAppID = GetTaskIDPerUUID(123).c_str();\n" " InitializeJumpList(sAppID);\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9796 check("struct A {};\n" @@ -6128,7 +6135,7 @@ class TestStl : public TestFixture { " A *b = v.back();\n" " v.pop_back();\n" " delete b;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct A {};\n" @@ -6139,7 +6146,7 @@ class TestStl : public TestFixture { " A *b = v.back();\n" " v.pop_back();\n" " delete b;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct A {};\n" @@ -6150,7 +6157,7 @@ class TestStl : public TestFixture { " std::shared_ptr b = v.back();\n" " v.pop_back();\n" " delete b;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9780 @@ -6160,7 +6167,7 @@ class TestStl : public TestFixture { " info.vect = &vect;\n" " vect.push_back(1);\n" " return info.ret;\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9133 @@ -6177,7 +6184,7 @@ class TestStl : public TestFixture { "void Fred::bar() {\n" " v.push_back(0);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:7] -> [test.cpp:8] -> [test.cpp:12] -> [test.cpp:9]: (error) Using iterator to member container 'v' that may be invalid.\n", errout_str()); @@ -6190,7 +6197,7 @@ class TestStl : public TestFixture { "void bar(std::vector& v) {\n" " v.push_back(0);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:1] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:7] -> [test.cpp:1] -> [test.cpp:4]: (error) Using iterator to local container 'v' that may be invalid.\n", errout_str()); @@ -6203,7 +6210,7 @@ class TestStl : public TestFixture { " I i = { &x };\n" " x.clear();\n" " Parse(i);\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -6219,7 +6226,7 @@ class TestStl : public TestFixture { " I cb[1];\n" " for (long i = 0; i < 1; ++i)\n" " cb[i] = b[i];\n" - "}\n",true); + "}\n",dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9836 @@ -6229,7 +6236,7 @@ class TestStl : public TestFixture { " v.clear();\n" " std::cout << *p << std::endl;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:3] -> [test.cpp:3] -> [test.cpp:3] -> [test.cpp:4] -> [test.cpp:2] -> [test.cpp:5]: (error) Using pointer to local variable 'v' that may be invalid.\n", errout_str()); @@ -6246,7 +6253,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " return a.i->front();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct A {\n" @@ -6262,7 +6269,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " g(a);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #10984 @@ -6272,7 +6279,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " g();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::vector v) {\n" @@ -6281,7 +6288,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " g();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:3] -> [test.cpp:4] -> [test.cpp:1] -> [test.cpp:5]: (error) Using iterator to local container 'v' that may be invalid.\n", errout_str()); @@ -6292,7 +6299,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " g();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:4] -> [test.cpp:1] -> [test.cpp:5]: (error) Using iterator to local container 'v' that may be invalid.\n", errout_str()); @@ -6307,7 +6314,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " a.g();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:7] -> [test.cpp:8] -> [test.cpp:5] -> [test.cpp:9]: (error) Using object that points to local variable 'v' that may be invalid.\n", errout_str()); @@ -6322,7 +6329,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " a.g();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:6] -> [test.cpp:7] -> [test.cpp:8] -> [test.cpp:5] -> [test.cpp:9]: (error) Using object that points to local variable 'v' that may be invalid.\n", errout_str()); @@ -6333,7 +6340,7 @@ class TestStl : public TestFixture { " c.erase(c.begin());\n" " d.push_back(0);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #11147 @@ -6343,7 +6350,7 @@ class TestStl : public TestFixture { " s = s.substr(it - s.begin());\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:4]: (performance) Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead.\n", errout_str()); @@ -6354,7 +6361,7 @@ class TestStl : public TestFixture { " args.push_back(\"-h\");\n" " args.front();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(std::vector& v) {\n" // #13108 @@ -6363,7 +6370,7 @@ class TestStl : public TestFixture { " v.erase(it, v.end());\n" " for (const auto& i : w) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #13410 @@ -6372,7 +6379,7 @@ class TestStl : public TestFixture { " v.push_back(1);\n" " return *i;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:1] -> [test.cpp:2] -> [test.cpp:1] -> [test.cpp:2] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:1] -> [test.cpp:4]: (error) Using pointer to local variable 'v' that may be invalid.\n", errout_str()); @@ -6386,7 +6393,7 @@ class TestStl : public TestFixture { " v.push_back(i * 2);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Calling 'push_back' while iterating the container is invalid.\n", errout_str()); // #9713 @@ -6399,7 +6406,7 @@ class TestStl : public TestFixture { " }\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::any_of algorithm instead of a raw loop.\n", errout_str()); check("struct A {\n" @@ -6412,7 +6419,7 @@ class TestStl : public TestFixture { " add(i);\n" " }\n" "};\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS( "[test.cpp:4] -> [test.cpp:7] -> [test.cpp:8]: (error) Calling 'add' while iterating the container is invalid.\n", errout_str()); @@ -6424,7 +6431,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f2(std::map& m, unsigned x) {\n" @@ -6434,7 +6441,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f3(std::map& m, unsigned x) {\n" @@ -6442,7 +6449,7 @@ class TestStl : public TestFixture { " m.emplace(x, 1);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f4(std::set& s, unsigned x) {\n" @@ -6450,7 +6457,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f5(std::map& m, unsigned x) {\n" @@ -6460,7 +6467,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f6(std::map& m, unsigned x) {\n" @@ -6468,7 +6475,7 @@ class TestStl : public TestFixture { " m.emplace(x, 1);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f1(std::unordered_set& s, unsigned x) {\n" @@ -6476,7 +6483,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f2(std::unordered_map& m, unsigned x) {\n" @@ -6486,7 +6493,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f3(std::unordered_map& m, unsigned x) {\n" @@ -6494,7 +6501,7 @@ class TestStl : public TestFixture { " m.emplace(x, 1);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f4(std::unordered_set& s, unsigned x) {\n" @@ -6502,7 +6509,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f5(std::unordered_map& m, unsigned x) {\n" @@ -6512,7 +6519,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void f6(std::unordered_map& m, unsigned x) {\n" @@ -6520,7 +6527,7 @@ class TestStl : public TestFixture { " m.emplace(x, 1);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); check("void g1(std::map& m, unsigned x) {\n" @@ -6530,7 +6537,7 @@ class TestStl : public TestFixture { " m[x] = 2;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void g1(std::map& m, unsigned x) {\n" @@ -6540,7 +6547,7 @@ class TestStl : public TestFixture { " m[x] = 2;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f1(QSet& s, unsigned x) {\n" @@ -6548,7 +6555,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f1(std::multiset& s, unsigned x) {\n" @@ -6556,7 +6563,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f2(std::multimap& m, unsigned x) {\n" @@ -6566,7 +6573,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f3(std::multimap& m, unsigned x) {\n" @@ -6574,7 +6581,7 @@ class TestStl : public TestFixture { " m.emplace(x, 1);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f4(std::multiset& s, unsigned x) {\n" @@ -6582,7 +6589,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f5(std::multimap& m, unsigned x) {\n" @@ -6592,7 +6599,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f1(std::unordered_multiset& s, unsigned x) {\n" @@ -6600,7 +6607,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f2(std::unordered_multimap& m, unsigned x) {\n" @@ -6610,7 +6617,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f3(std::unordered_multimap& m, unsigned x) {\n" @@ -6618,7 +6625,7 @@ class TestStl : public TestFixture { " m.emplace(x, 1);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f4(std::unordered_multiset& s, unsigned x) {\n" @@ -6626,7 +6633,7 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f5(std::unordered_multimap& m, unsigned x) {\n" @@ -6636,7 +6643,7 @@ class TestStl : public TestFixture { " m[x] = 1;\n" " }\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); // #9218 - not small type => do not warn if cpp standard is < c++17 @@ -6646,11 +6653,11 @@ class TestStl : public TestFixture { " s.insert(x);\n" " }\n" "}\n"; - check(code, true, Standards::CPP11); + check(code, dinit(CheckOptions, $.inconclusive = true, $.cppstandard = Standards::CPP11)); ASSERT_EQUALS("", errout_str()); - check(code, true, Standards::CPP14); + check(code, dinit(CheckOptions, $.inconclusive = true, $.cppstandard = Standards::CPP14)); ASSERT_EQUALS("", errout_str()); - check(code, true, Standards::CPP17); + check(code, dinit(CheckOptions, $.inconclusive = true, $.cppstandard = Standards::CPP17)); ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout_str()); } @@ -6663,7 +6670,7 @@ class TestStl : public TestFixture { " if(x.find(5) == x.end())\n" " x[5] = data;\n" " }\n" - "}", false, Standards::CPP03); + "}", dinit(CheckOptions, $.cppstandard = Standards::CPP03)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" @@ -6674,7 +6681,7 @@ class TestStl : public TestFixture { " if(x.find(5) == x.end())\n" " x[5] = data;\n" " }\n" - "}", false, Standards::CPP11); + "}", dinit(CheckOptions, $.cppstandard = Standards::CPP11)); ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.emplace(5, data);'.\n", errout_str()); check("void foo() {\n" @@ -6695,35 +6702,35 @@ class TestStl : public TestFixture { " std::vector v;\n" " for(auto x:v) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Iterating over container 'v' that is always empty.\n", errout_str()); check("void f(std::vector v) {\n" " v.clear();\n" " for(auto x:v) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Iterating over container 'v' that is always empty.\n", errout_str()); check("void f(std::vector v) {\n" " if (!v.empty()) { return; }\n" " for(auto x:v) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Iterating over container 'v' that is always empty.\n", errout_str()); check("void f(std::vector v) {\n" " if (v.empty()) { return; }\n" " for(auto x:v) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " std::vector v;\n" " std::sort(v.begin(), v.end());\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (style) Using sort with iterator 'v.begin()' that is always empty.\n", errout_str()); check("void f() {\n" // #1201 @@ -6731,14 +6738,14 @@ class TestStl : public TestFixture { " std::vector v2;\n" " std::copy(v1.begin(), v1.end(), v2.begin());\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (style) Using copy with iterator 'v2.begin()' that is always empty.\n", errout_str()); check("void f() {\n" " std::vector v;\n" " v.insert(v.end(), 1);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct A {\n" @@ -6750,7 +6757,7 @@ class TestStl : public TestFixture { " for(auto&& x:v) {}\n" " return a;\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("static void f1(std::list& parameters) {\n" @@ -6764,7 +6771,7 @@ class TestStl : public TestFixture { " int res = ::f2(parameters);\n" " for (auto param : parameters) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("namespace ns {\n" @@ -6775,7 +6782,7 @@ class TestStl : public TestFixture { " const ArrayType arr;\n" " for (const auto &a : arr) {}\n" "}", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:7]: (style) Iterating over container 'arr' that is always empty.\n", errout_str()); check("struct S {\n" @@ -6786,7 +6793,7 @@ class TestStl : public TestFixture { " bar(s);\n" " std::sort(s.v.begin(), s.v.end());\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector& v, int e) {\n" @@ -6798,7 +6805,7 @@ class TestStl : public TestFixture { " }\n" " for (auto i : v) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -6807,7 +6814,7 @@ class TestStl : public TestFixture { " rv.push_back(42);\n" " for (auto i : v) {}\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("extern void f(std::string&&);\n" @@ -6816,7 +6823,7 @@ class TestStl : public TestFixture { " const std::string& s_ref = s;\n" " f(std::move(s));\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #12757 @@ -6831,7 +6838,7 @@ class TestStl : public TestFixture { " for (const auto& i : m) {}\n" " return {};\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f(bool b) {\n" // #13121 @@ -6839,7 +6846,7 @@ class TestStl : public TestFixture { " for (auto c : s) {}\n" " if (b)\n" " s += \'a\';\n" - "}\n", true); + "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); } @@ -6848,14 +6855,14 @@ class TestStl : public TestFixture { " static std::mutex m;\n" " static std::lock_guard g(m);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning) Lock guard is defined globally. Lock guards are intended to be local. A global lock guard could lead to a deadlock since it won't unlock until the end of the program.\n", errout_str()); check("void f() {\n" " static std::mutex m;\n" " std::lock_guard g(m);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" @@ -6863,7 +6870,7 @@ class TestStl : public TestFixture { " static std::unique_lock g(m, std::defer_lock);\n" " static std::lock(g);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning) Lock guard is defined globally. Lock guards are intended to be local. A global lock guard could lead to a deadlock since it won't unlock until the end of the program.\n", errout_str()); check("void f() {\n" @@ -6871,21 +6878,21 @@ class TestStl : public TestFixture { " std::unique_lock g(m, std::defer_lock);\n" " std::lock(g);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " std::mutex m;\n" " std::lock_guard g(m);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout_str()); check("void f() {\n" " std::mutex m;\n" " std::unique_lock g(m);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout_str()); check("void f() {\n" @@ -6893,7 +6900,7 @@ class TestStl : public TestFixture { " std::unique_lock g(m, std::defer_lock);\n" " std::lock(g);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:3]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout_str()); check("void g();\n" @@ -6903,7 +6910,7 @@ class TestStl : public TestFixture { " g();\n" " m.unlock();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void g();\n" @@ -6913,7 +6920,7 @@ class TestStl : public TestFixture { " g();\n" " m.unlock();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout_str()); check("class A {\n" @@ -6922,7 +6929,7 @@ class TestStl : public TestFixture { " std::lock_guard g(m);\n" " }\n" "};\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("class A {\n" @@ -6934,7 +6941,7 @@ class TestStl : public TestFixture { " m.unlock();\n" " }\n" "};\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("class A {\n" @@ -6943,7 +6950,7 @@ class TestStl : public TestFixture { " static std::lock_guard g(m);\n" " }\n" "};\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning) Lock guard is defined globally. Lock guards are intended to be local. A global lock guard could lead to a deadlock since it won't unlock until the end of the program.\n", errout_str()); check("std::mutex& h();\n" @@ -6951,7 +6958,7 @@ class TestStl : public TestFixture { " auto& m = h();\n" " std::lock_guard g(m);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void g();\n" @@ -6962,7 +6969,7 @@ class TestStl : public TestFixture { " g();\n" " m.unlock();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("std::mutex& h();\n" @@ -6970,7 +6977,7 @@ class TestStl : public TestFixture { " auto m = h();\n" " std::lock_guard g(m);\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:4]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout_str()); check("void g();\n" @@ -6981,7 +6988,7 @@ class TestStl : public TestFixture { " g();\n" " m.unlock();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("[test.cpp:5]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout_str()); check("void foo();\n" @@ -6997,7 +7004,7 @@ class TestStl : public TestFixture { " bar();\n" " m.unlock();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo();\n" @@ -7011,7 +7018,7 @@ class TestStl : public TestFixture { " std::unique_lock g{m};\n" " bar();\n" "}\n", - true); + dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); check("void foo() { int f = 0; auto g(f); g = g; }"); diff --git a/test/teststring.cpp b/test/teststring.cpp index f43d34b1105..1eb6ac03a65 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -61,9 +61,15 @@ class TestString : public TestFixture { TEST_CASE(deadStrcmp); } + struct CheckOptions + { + CheckOptions() = default; + const char* filename = "test.cpp"; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], const char filename[] = "test.cpp") { - std::vector files(1, filename); + void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) { + std::vector files(1, options.filename); Tokenizer tokenizer(settings, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -326,22 +332,22 @@ class TestString : public TestFixture { check("bool foo(char* c) {\n" " return \"x\" == c+foo;\n" - "}", "test.cpp"); + "}"); ASSERT_EQUALS("", errout_str()); check("bool foo(char* c) {\n" " return \"x\" == c+foo;\n" - "}", "test.c"); + "}", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:2]: (warning) String literal compared with variable 'c+foo'. Did you intend to use strcmp() instead?\n", errout_str()); check("bool foo(Foo c) {\n" " return \"x\" == c.foo;\n" - "}", "test.cpp"); + "}"); ASSERT_EQUALS("", errout_str()); check("bool foo(Foo c) {\n" " return \"x\" == c.foo;\n" - "}", "test.c"); + "}", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:2]: (warning) String literal compared with variable 'c.foo'. Did you intend to use strcmp() instead?\n", errout_str()); check("bool foo(const std::string& c) {\n" @@ -357,7 +363,7 @@ class TestString : public TestFixture { // Ticket #4257 check("bool foo() {\n" "MyString *str=Getter();\n" - "return *str==\"bug\"; }\n", "test.c"); + "return *str==\"bug\"; }\n", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable '*str'. Did you intend to use strcmp() instead?\n", errout_str()); // Ticket #4257 @@ -369,27 +375,27 @@ class TestString : public TestFixture { // Ticket #4257 check("bool foo() {\n" "MyString **str=OtherGetter();\n" - "return *str==\"bug\"; }", "test.c"); + "return *str==\"bug\"; }", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable '*str'. Did you intend to use strcmp() instead?\n", errout_str()); // Ticket #4257 check("bool foo() {\n" "MyString str=OtherGetter2();\n" - "return &str==\"bug\"; }", "test.c"); + "return &str==\"bug\"; }", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable '&str'. Did you intend to use strcmp() instead?\n", errout_str()); // Ticket #5734 check("int foo(char c) {\n" - "return c == '4';}", "test.cpp"); + "return c == '4';}"); ASSERT_EQUALS("", errout_str()); check("int foo(char c) {\n" - "return c == '4';}", "test.c"); + "return c == '4';}", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("", errout_str()); check("int foo(char c) {\n" - "return c == \"42\"[0];}", "test.cpp"); + "return c == \"42\"[0];}"); ASSERT_EQUALS("", errout_str()); check("int foo(char c) {\n" - "return c == \"42\"[0];}", "test.c"); + "return c == \"42\"[0];}", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("", errout_str()); // 5639 String literal compared with char buffer in a struct @@ -399,7 +405,7 @@ class TestString : public TestFixture { "void foo() {\n" " struct Example example;\n" " if (example.buffer == \"test\") ;\n" - "}\n", "test.cpp"); + "}\n"); ASSERT_EQUALS("[test.cpp:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout_str()); check("struct Example {\n" " char buffer[200];\n" @@ -407,7 +413,7 @@ class TestString : public TestFixture { "void foo() {\n" " struct Example example;\n" " if (example.buffer == \"test\") ;\n" - "}\n", "test.c"); + "}\n", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout_str()); // #9726 @@ -461,7 +467,7 @@ class TestString : public TestFixture { check("bool foo(char* c) {\n" " return *c == 0;\n" - "}", "test.c"); + "}", dinit(CheckOptions, $.filename = "test.c")); ASSERT_EQUALS("", errout_str()); check("bool foo(char* c) {\n" diff --git a/test/testsummaries.cpp b/test/testsummaries.cpp index 8939731f95b..1aff5097a5c 100644 --- a/test/testsummaries.cpp +++ b/test/testsummaries.cpp @@ -37,10 +37,10 @@ class TestSummaries : public TestFixture { #define createSummaries(...) createSummaries_(__FILE__, __LINE__, __VA_ARGS__) template - std::string createSummaries_(const char* file, int line, const char (&code)[size], bool cpp = true) { + std::string createSummaries_(const char* file, int line, const char (&code)[size]) { // tokenize.. SimpleTokenizer tokenizer(settingsDefault, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code), file, line); return Summaries::create(tokenizer, ""); } diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index f5a774ee821..7de2f2e3727 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -2617,7 +2617,15 @@ class TestSymbolDatabase : public TestFixture { ASSERT_EQUALS("char", arg1->typeStartToken()->str()); ASSERT_EQUALS("char", arg1->typeEndToken()->str()); } - +/* + struct CheckOptions + { + CheckOptions() = default; + bool debug = true; + bool cpp = true; + const Settings* s = nullptr; + }; + */ #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template void check_(const char* file, int line, const char (&code)[size], bool debug = true, bool cpp = true, const Settings* pSettings = nullptr) { diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 917b89900bf..df9c738efe9 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -139,13 +139,13 @@ class TestToken : public TestFixture { } #define MatchCheck(...) MatchCheck_(__FILE__, __LINE__, __VA_ARGS__) - bool MatchCheck_(const char* file, int line, const std::string& code, const std::string& pattern, unsigned int varid = 0) { + bool MatchCheck_(const char* file, int line, const std::string& code, const std::string& pattern) { SimpleTokenizer tokenizer(settingsDefault, *this); const std::string code2 = ";" + code + ";"; try { ASSERT_LOC(tokenizer.tokenize(code2), file, line); } catch (...) {} - return Token::Match(tokenizer.tokens()->next(), pattern.c_str(), varid); + return Token::Match(tokenizer.tokens()->next(), pattern.c_str()); } void multiCompare() const { diff --git a/test/testtype.cpp b/test/testtype.cpp index bc422af4ab9..f831a54e387 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -47,37 +47,49 @@ class TestType : public TestFixture { TEST_CASE(shiftTooManyBits); // #11496 } + struct CheckOptions + { + CheckOptions() = default; + Standards::cppstd_t standard = Standards::cppstd_t::CPP11; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) 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(); + void check_(const char* file, int line, const char (&code)[size], const Settings& settings, const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).cpp(options.standard).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code), 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(); + void check_(const char* file, int line, const std::string& code, const Settings& settings, const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).cpp(options.standard).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. runChecks(tokenizer, this); } + struct CheckPOptions + { + CheckPOptions() = default; + const char* filename = "test.cpp"; + }; + #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) template - void checkP_(const char* file, int line, const char (&code)[size], const Settings& settings, const char filename[] = "test.cpp") { + void checkP_(const char* file, int line, const char (&code)[size], const Settings& settings, const CheckPOptions& options = make_default_obj()) { const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).build(); - std::vector files(1, filename); + std::vector files(1, options.filename); Tokenizer tokenizer(settings1, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -122,9 +134,9 @@ class TestType : public TestFixture { ASSERT_EQUALS("", errout_str()); // c++14 - check(type + " foo(" + type + " x) { return x << 31; }", settings, true, Standards::cppstd_t::CPP14); + check(type + " foo(" + type + " x) { return x << 31; }", settings, dinit(CheckOptions, $.standard = Standards::CPP14)); ASSERT_EQUALS("[test.cpp:1]: (portability) Shifting signed 32-bit value by 31 bits is implementation-defined behaviour\n", errout_str()); - check(type + " f(int x) { return (x = (" + type + ")x << 32); }", settings, true, Standards::cppstd_t::CPP14); + check(type + " f(int x) { return (x = (" + type + ")x << 32); }", settings, dinit(CheckOptions, $.standard = Standards::CPP14)); ASSERT_EQUALS("[test.cpp:1]: (error) Shifting 32-bit value by 32 bits is undefined behaviour\n", errout_str()); } } @@ -157,11 +169,11 @@ class TestType : public TestFixture { ASSERT_EQUALS("", errout_str()); // c++14 - check("signed long long foo(signed long long x) { return x << 64; }",settings, true, Standards::cppstd_t::CPP14); + check("signed long long foo(signed long long x) { return x << 64; }",settings, dinit(CheckOptions, $.standard = Standards::CPP14)); ASSERT_EQUALS("[test.cpp:1]: (error) Shifting 64-bit value by 64 bits is undefined behaviour\n", errout_str()); - check("signed long long f(long long x) { return (x = (signed long long)x << 64); }",settings, true, Standards::cppstd_t::CPP14); + check("signed long long f(long long x) { return (x = (signed long long)x << 64); }",settings, dinit(CheckOptions, $.standard = Standards::CPP14)); ASSERT_EQUALS("[test.cpp:1]: (error) Shifting 64-bit value by 64 bits is undefined behaviour\n", errout_str()); - check("signed long long f(signed long long x) { return x << 63; }",settings, true, Standards::cppstd_t::CPP14); + check("signed long long f(signed long long x) { return x << 63; }",settings, dinit(CheckOptions, $.standard = Standards::CPP14)); ASSERT_EQUALS("[test.cpp:1]: (portability) Shifting signed 64-bit value by 63 bits is implementation-defined behaviour\n", errout_str()); check("signed long long f(signed long long x) { return x << 62; }",settings); ASSERT_EQUALS("", errout_str()); @@ -554,14 +566,14 @@ class TestType : public TestFixture { "void f()\n" "{\n" " unsigned short u = TEST(true, 75000.0);\n" - "}\n", settingsDefault, "test.c"); + "}\n", settingsDefault, dinit(CheckPOptions, $.filename = "test.c")); ASSERT_EQUALS("", errout_str()); checkP("#define TEST(b, f) b ? 5000 : (unsigned short)f\n" "void f()\n" "{\n" " unsigned short u = TEST(false, 75000.0);\n" - "}\n", settingsDefault, "test.c"); + "}\n", settingsDefault, dinit(CheckPOptions, $.filename = "test.c")); ASSERT_EQUALS("[test.c:4]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout_str())); } @@ -579,14 +591,14 @@ class TestType : public TestFixture { "int f()\n" "{\n" " fun(INT_MIN);\n" - "}", s, "test.cpp"); + "}", s); ASSERT_EQUALS("[test.cpp:3]: (error) Signed integer overflow for expression '-x'.\n", errout_str()); checkP("void f() {\n" // #8399 " int32_t i = INT32_MAX;\n" " i << 1;\n" " i << 2;\n" - "}", s, "test.cpp"); + "}", s); ASSERT_EQUALS("[test.cpp:4]: (error) Signed integer overflow for expression 'i<<2'.\n", errout_str()); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 783dc04d51b..e1046c7eb5e 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -104,13 +104,21 @@ class TestUninitVar : public TestFixture { TEST_CASE(ctuTest); } + struct CheckUninitVarOptions + { + CheckUninitVarOptions() = default; + bool cpp = true; + bool debugwarnings = false; + const Settings *s = nullptr; + }; + #define checkUninitVar(...) checkUninitVar_(__FILE__, __LINE__, __VA_ARGS__) - void checkUninitVar_(const char* file, int line, const char code[], bool cpp = true, bool debugwarnings = false, const Settings *s = nullptr) { - const Settings settings1 = settingsBuilder(s ? *s : settings).debugwarnings(debugwarnings).build(); + void checkUninitVar_(const char* file, int line, const char code[], const CheckUninitVarOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).debugwarnings(options.debugwarnings).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); // Check for redundant code.. CheckUninitVar checkuninitvar(&tokenizer, &settings1, this); @@ -320,7 +328,7 @@ class TestUninitVar : public TestFixture { " int x, y;\n" " x = (y = 10);\n" " int z = y * 2;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("static void foo() {\n" @@ -340,7 +348,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("int f() {\n" " int a,b,c;\n" " a = b = c;\n" - "}", true, /*debugwarnings=*/ false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout_str()); checkUninitVar("static void foo()\n" @@ -372,7 +380,7 @@ class TestUninitVar : public TestFixture { " A ret;\n" " return ret;\n" "}\n", - false); + dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ret\n", errout_str()); } // extracttests.enable @@ -382,7 +390,7 @@ class TestUninitVar : public TestFixture { " union lf { long l; float f; } u_lf;\n" " float hx = (u_lf.f = (x), u_lf.l);\n" "}", - false, false); + dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("void a()\n" @@ -398,7 +406,7 @@ class TestUninitVar : public TestFixture { " int *y = &x;\n" " *y = 0;\n" " x++;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void a()\n" @@ -407,7 +415,7 @@ class TestUninitVar : public TestFixture { " char *z = x;\n" " memset(z, 0, sizeof(x));\n" " memcpy(y, x, sizeof(x));\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); // Handling >> and << @@ -442,7 +450,7 @@ class TestUninitVar : public TestFixture { " int ret;\n" " int a = value >> ret;\n" "}\n", - false); + dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout_str()); checkUninitVar("void foo() {\n" // #3707 @@ -463,7 +471,7 @@ class TestUninitVar : public TestFixture { " int ret;\n" " a = value << ret;\n" "}\n", - false); + dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout_str()); // #4320 says this is a FP. << is overloaded. @@ -662,7 +670,7 @@ class TestUninitVar : public TestFixture { "\n" "found:\n" " int a = b;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("int foo()\n" @@ -697,7 +705,7 @@ class TestUninitVar : public TestFixture { " goto exit;\n" " i++;\n" "exit:\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("int foo() {\n" @@ -707,7 +715,7 @@ class TestUninitVar : public TestFixture { " x = a;\n" " y = 1;\n" " goto again;\n" - "}", false, false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); // Ticket #3873 (false positive) @@ -732,7 +740,7 @@ class TestUninitVar : public TestFixture { " iter = x;\n" " }\n" " return 1 + iter;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); // C++11 style initialization @@ -794,11 +802,11 @@ class TestUninitVar : public TestFixture { "}"; // Assume dfs is a non POD type if file is C++ - checkUninitVar(code, true); + checkUninitVar(code); ASSERT_EQUALS("", errout_str()); // Assume dfs is a POD type if file is C - checkUninitVar(code, false); + checkUninitVar(code, dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: a\n", errout_str()); const char code2[] = "struct AB { int a,b; };\n" @@ -806,10 +814,10 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " return ab;\n" "}"; - checkUninitVar(code2, true); + checkUninitVar(code2); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n" "[test.cpp:4]: (error) Uninitialized struct member: ab.b\n", errout_str()); - checkUninitVar(code2, false); + checkUninitVar(code2, dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab\n", errout_str()); // Ticket #3890 - False positive for std::map @@ -823,7 +831,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" " std::vector *x = NULL;\n" " return x;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); { @@ -836,7 +844,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" " Fred _tm;\n" " _tm.dostuff();\n" - "}", true, false, &s); + "}", dinit(CheckUninitVarOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -845,7 +853,7 @@ class TestUninitVar : public TestFixture { " A a,b;\n" " b[0] = 0;" " return a;\n" - "}", false, false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -970,7 +978,7 @@ class TestUninitVar : public TestFixture { " i = 33;\n" " }\n" " return i;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("static void foo(int x)\n" @@ -1214,7 +1222,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" " X var;\n" " memset(var, 0, sizeof(var));\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("int f() {\n" // #8692 @@ -1576,7 +1584,7 @@ class TestUninitVar : public TestFixture { " break;\n" " };\n" " })\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); } @@ -1769,7 +1777,7 @@ class TestUninitVar : public TestFixture { " char *buffer=(char*)malloc(128*sizeof(char));\n" " strcpy(strMsg,buffer);\n" " free(buffer);\n" - "}", true, false); + "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout_str()); checkUninitVar("void f(){\n" @@ -1777,7 +1785,7 @@ class TestUninitVar : public TestFixture { " char *buffer=static_cast(malloc(128*sizeof(char)));\n" " strcpy(strMsg,buffer);\n" " free(buffer);\n" - "}", true, false); + "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout_str()); // #3845 @@ -1832,7 +1840,7 @@ class TestUninitVar : public TestFixture { "void f() {\n" " struct Fred fred[10];\n" " fred[1].x = 0;\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("char f() {\n" @@ -2072,7 +2080,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("int main() {\n" " char * pBuf = new(10);\n" " a = *pBuf;\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("class A {};\n" // #10698 @@ -2146,7 +2154,7 @@ class TestUninitVar : public TestFixture { " int a,b;\n" " int get_a() { return a; }" " } x = { 0, 0 };\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f()\n" @@ -2159,7 +2167,7 @@ class TestUninitVar : public TestFixture { " i = 0;\n" " }\n" " return i;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f(int x) {\n" @@ -2188,7 +2196,7 @@ class TestUninitVar : public TestFixture { " int &b = a;\n" " b = 0;\n" " int x = a;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f(struct blame_entry *ent)\n" @@ -2247,7 +2255,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("static int foo() {\n" " int ret;\n" " return cin >> ret;\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout_str()); } @@ -2385,7 +2393,7 @@ class TestUninitVar : public TestFixture { " char a[100];\n" " strncpy(a, \"hello\", sizeof(a));\n" " strncat(a, \"world\", 20);\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); // #3245 - false positive @@ -2418,7 +2426,7 @@ class TestUninitVar : public TestFixture { " const char* source = \"You\";\n" " strncpy(dst, source, sizeof(dst));\n" " char value = dst[2];\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); } @@ -2484,7 +2492,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" " int *n = ({ typeof(*n) z; (typeof(*n)*)z; })\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); } @@ -2581,7 +2589,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" // #3926 - weird cast. " int x;\n" " *(((char *)&x) + 0) = 0;\n" - "}", false, false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f() {\n" // #4737 - weird cast. @@ -2593,7 +2601,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" " int x;\n" " char *p = (char*)&x + 1;\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f() {\n" @@ -2642,10 +2650,10 @@ class TestUninitVar : public TestFixture { " int x;\n" " if (i >> x) { }\n" "}"; - checkUninitVar(code, true); + checkUninitVar(code); ASSERT_EQUALS("", errout_str()); - checkUninitVar(code, false); + checkUninitVar(code, dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: x\n", errout_str()); } @@ -2724,7 +2732,7 @@ class TestUninitVar : public TestFixture { "out1:\n" "out2:\n" " return ret;\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f() {\n" @@ -2786,7 +2794,7 @@ class TestUninitVar : public TestFixture { " int i;\n" " ({ if (0); });\n" " for_each(i) { }\n" - "}", false, false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); // if, if checkUninitVar("void f(int a) {\n" @@ -2833,7 +2841,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("static void f(int x, int y) {\n" " int a;\n" " if (x == 0 && (a == 1)) { }\n" - "}", true, false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout_str()); checkUninitVar("void f() {\n" @@ -2856,7 +2864,7 @@ class TestUninitVar : public TestFixture { " if (x) ab = getAB();\n" " else ab.a = 0;\n" " if (ab.a == 1) b = ab.b;\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("int f(void) {\n" @@ -3040,7 +3048,7 @@ class TestUninitVar : public TestFixture { checkUninitVar("void f() {\n" // #4717 - ({}) " int a = ({ long b = (long)(123); 2 + b; });\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -3099,7 +3107,7 @@ class TestUninitVar : public TestFixture { " int syncpos, len;\n" " Sync(syncpos, type, len);\n" "}"; - checkUninitVar(code, true); + checkUninitVar(code); ASSERT_EQUALS("", errout_str()); } @@ -3108,7 +3116,7 @@ class TestUninitVar : public TestFixture { "void f1() { char *p; *p = 0; }\n" "class Ns::C* p;\n" "void f2() { char *p; *p = 0; }"; - checkUninitVar(code, true); + checkUninitVar(code); ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: p\n" "[test.cpp:4]: (error) Uninitialized variable: p\n", errout_str()); } @@ -3128,7 +3136,7 @@ class TestUninitVar : public TestFixture { " int bar = 1;\n" " return bar;\n" "}"; - checkUninitVar(code, true); + checkUninitVar(code); ASSERT_EQUALS("", errout_str()); } @@ -3138,7 +3146,7 @@ class TestUninitVar : public TestFixture { " int x;\n" " x = get(x) && x;\n" "}"; - checkUninitVar(code, true); + checkUninitVar(code); ASSERT_EQUALS("", errout_str()); } @@ -3424,19 +3432,19 @@ class TestUninitVar : public TestFixture { checkUninitVar("void write_packet() {\n" " time_t now0;\n" " time(&now0);\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("void write_packet() {\n" " time_t* now0;\n" " time(now0);\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: now0\n", errout_str()); checkUninitVar("void write_packet() {\n" " char now0;\n" " strcmp(&now0, sth);\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: now0\n", errout_str()); // #2775 - uninitialized struct pointer in subfunction @@ -3478,7 +3486,7 @@ class TestUninitVar : public TestFixture { " if (y != 0) return;\n" " i++;\n" " }\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f() {\n" @@ -3488,7 +3496,7 @@ class TestUninitVar : public TestFixture { " if (y != 0) return;\n" " i++;\n" " }\n" - "}", true, false); + "}"); ASSERT_EQUALS("", errout_str()); checkUninitVar("void f() {\n" @@ -4417,7 +4425,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " ab.a = 0;\n" " do_something(ab);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:6]: (error) Uninitialized struct member: ab.b\n", errout_str()); checkUninitVar("struct AB { int a; int b; };\n" // #4760 @@ -4425,7 +4433,7 @@ class TestUninitVar : public TestFixture { "void f(void) {\n" " struct AB ab;\n" " do_something(ab.a);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout_str()); checkUninitVar("struct AB { int a; int b; };\n" @@ -4441,7 +4449,7 @@ class TestUninitVar : public TestFixture { "void f(void) {\n" " struct AB ab;\n" " int a = ab.a;\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout_str()); checkUninitVar("struct AB { int a; int b; };\n" @@ -4455,7 +4463,7 @@ class TestUninitVar : public TestFixture { "void f(void) {\n" " struct AB ab;\n" " buf[ab.a] = 0;\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout_str()); checkUninitVar("struct AB { int a; int b; };\n" @@ -4463,7 +4471,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " ab.a = 1;\n" " x = ab;\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout_str()); checkUninitVar("struct AB { int a; int b; };\n" @@ -4471,7 +4479,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " ab.a = 1;\n" " x = *(&ab);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout_str()); checkUninitVar("void f(void) {\n" @@ -4479,7 +4487,7 @@ class TestUninitVar : public TestFixture { " int x;\n" " ab.a = (addr)&x;\n" " dostuff(&ab,0);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct Element {\n" @@ -4550,7 +4558,7 @@ class TestUninitVar : public TestFixture { "void f(void) {\n" " struct AB ab;\n" " assign(&ab.a, 0);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct Cstring { char *text; int size, alloc; };\n" @@ -4579,7 +4587,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " uninitvar_funcArgInTest(&ab);\n" " x = ab;\n" - "}\n", false, false, &s); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false, $.s = &s)); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout_str()); checkUninitVar("struct AB { int a; };\n" @@ -4587,7 +4595,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " uninitvar_funcArgOutTest(&ab);\n" " x = ab;\n" - "}\n", false, false, &s); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -4598,7 +4606,7 @@ class TestUninitVar : public TestFixture { " ab.a = 0;\n" " ab.b = 0;\n" " do_something(ab);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); { @@ -4606,28 +4614,28 @@ class TestUninitVar : public TestFixture { "void f(void) {\n" " struct AB ab;\n" " strcpy(ab.a, STR);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct AB { unsigned char a[10]; };\n" // #8999 - cast "void f(void) {\n" " struct AB ab;\n" " strcpy((char *)ab.a, STR);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct AB { char a[10]; };\n" "void f(void) {\n" " struct AB ab;\n" " strcpy(x, ab.a);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); TODO_ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab.a\n", "", errout_str()); checkUninitVar("struct AB { int a; };\n" "void f(void) {\n" " struct AB ab;\n" " dosomething(ab.a);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -4637,7 +4645,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " ab = getAB();\n" " do_something(ab);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); { @@ -4675,7 +4683,7 @@ class TestUninitVar : public TestFixture { " ab.s.b = 2;\n" " ab.s.c = 3;\n" " do_something(ab);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct conf {\n" @@ -4688,7 +4696,7 @@ class TestUninitVar : public TestFixture { " struct conf c;\n" " initdata(&c);\n" " do_something(c);\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct PIXEL {\n" @@ -4766,7 +4774,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " ab.a = 0;\n" " return ab.b;\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout_str()); checkUninitVar("struct AB { int a; int b; };\n" @@ -4774,7 +4782,7 @@ class TestUninitVar : public TestFixture { " struct AB ab;\n" " ab.a = 0;\n" " return ab.a;\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct S { int a; int b; };\n" // #8299 @@ -4802,7 +4810,7 @@ class TestUninitVar : public TestFixture { " struct FRED fred;\n" " fred.a = do_something();\n" " if (fred.a == 0) { }\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("struct FRED {\n" @@ -4814,21 +4822,21 @@ class TestUninitVar : public TestFixture { " struct FRED fred;\n" " fred.a = do_something();\n" " if (fred.b == 0) { }\n" - "}\n", false, false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:9]: (error) Uninitialized struct member: fred.b\n", errout_str()); checkUninitVar("struct Fred { int a; };\n" "void f() {\n" " struct Fred fred;\n" " if (fred.a==1) {}\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: fred.a\n", errout_str()); checkUninitVar("struct S { int n; int m; };\n" "void f(void) {\n" " struct S s;\n" " for (s.n = 0; s.n <= 10; s.n++) { }\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); checkUninitVar("void test2() {\n" @@ -4905,7 +4913,7 @@ class TestUninitVar : public TestFixture { "void foo() {\n" " A a;\n" " x = a.m;\n" - "}", true); + "}"); ASSERT_EQUALS("", errout_str()); // Unknown type (C) @@ -4916,7 +4924,7 @@ class TestUninitVar : public TestFixture { "void foo() {\n" " A a;\n" " x = a.m;\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:7]: (error) Uninitialized struct member: a.m\n", errout_str()); // Type with constructor @@ -5098,7 +5106,7 @@ class TestUninitVar : public TestFixture { " int a = 1+ab.a;\n" " do_something(a);\n" " }\n" - "}\n", false); + "}\n", dinit(CheckUninitVarOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout_str()); checkUninitVar("void f(int i) {\n" // #4569 fp @@ -5397,7 +5405,7 @@ class TestUninitVar : public TestFixture { " i = 0;\n" " return i;\n" " } while (0);\n" - "}\n", true, false, &s); + "}\n", dinit(CheckUninitVarOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -5445,7 +5453,7 @@ class TestUninitVar : public TestFixture { " char *dp;\n" " dp=(char *)d;\n" " init(dp);\n" - "}", false); + "}", dinit(CheckUninitVarOptions, $.cpp = false)); // Unknown type TODO_ASSERT_EQUALS("", "[test.c:4]: (error) Uninitialized variable: d\n", errout_str()); } diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index b0b6a631bb3..ef8a871c11b 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -91,14 +91,22 @@ class TestUnusedFunctions : public TestFixture { TEST_CASE(staticFunction); } + struct CheckOptions + { + CheckOptions() = default; + Platform::Type platform = Platform::Type::Native; + const Settings* s = nullptr; + bool cpp = true; + }; + #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char* file, int line, const char (&code)[size], Platform::Type platform = Platform::Type::Native, const Settings *s = nullptr, bool cpp = true) { - const Settings settings1 = settingsBuilder(s ? *s : settings).platform(platform).build(); + void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).platform(options.platform).build(); // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); // Check for unused functions.. CheckUnusedFunctions checkUnusedFunctions; @@ -692,10 +700,10 @@ class TestUnusedFunctions : public TestFixture { const Settings s = settingsBuilder(settings).library("windows.cfg").build(); - check("int WinMain() { }", Platform::Type::Native, &s); + check("int WinMain() { }", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); - check("int _tmain() { }", Platform::Type::Native, &s); + check("int _tmain() { }", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -708,10 +716,10 @@ class TestUnusedFunctions : public TestFixture { const Settings s = settingsBuilder(settings).library("windows.cfg").build(); - check("int wWinMain() { }", Platform::Type::Native, &s); + check("int wWinMain() { }", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); - check("int _tmain() { }", Platform::Type::Native, &s); + check("int _tmain() { }", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -724,7 +732,7 @@ class TestUnusedFunctions : public TestFixture { const Settings s = settingsBuilder(settings).library("gnu.cfg").build(); check("int _init() { }\n" - "int _fini() { }\n", Platform::Type::Native, &s); + "int _fini() { }\n", dinit(CheckOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -812,10 +820,10 @@ class TestUnusedFunctions : public TestFixture { void attributeMaybeUnused() { - check("[[__maybe_unused__]] void f() {}\n", Platform::Type::Native, nullptr, false); + check("[[__maybe_unused__]] void f() {}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); - check("[[maybe_unused]] void f() {}\n", Platform::Type::Native, nullptr, false); + check("[[maybe_unused]] void f() {}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("[[maybe_unused]] void f() {}\n"); @@ -833,7 +841,7 @@ class TestUnusedFunctions : public TestFixture { check("void f(void) {}\n" "int main() {\n" " f();\n" - "}\n", Platform::Type::Native, nullptr, false); + "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:1]: (style) The function 'f' should have static linkage since it is not used outside of its translation unit.\n", errout_str()); } }; diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index 179c21af7b9..1069bc4b3f1 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -86,9 +86,15 @@ class TestUnusedPrivateFunction : public TestFixture { TEST_CASE(trailingReturn); } + struct CheckOptions + { + CheckOptions() = default; + Platform::Type platform = Platform::Type::Native; + }; + #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 settings1 = settingsBuilder(settings).platform(platform).build(); + void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) { + const Settings settings1 = settingsBuilder(settings).platform(options.platform).build(); std::vector files(1, "test.cpp"); Tokenizer tokenizer(settings1, *this); @@ -603,7 +609,7 @@ class TestUnusedPrivateFunction : public TestFixture { "public:\n" " Foo() { }\n" " __property int x = {read=getx}\n" - "};", Platform::Type::Win32A); + "};", dinit(CheckOptions, $.platform = Platform::Type::Win32A)); ASSERT_EQUALS("", errout_str()); } @@ -616,7 +622,7 @@ class TestUnusedPrivateFunction : public TestFixture { " }\n" "public:\n" " Foo() { }\n" - "};", Platform::Type::Win32A); + "};", dinit(CheckOptions, $.platform = Platform::Type::Win32A)); ASSERT_EQUALS("", errout_str()); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 95624048651..875052f858e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -261,15 +261,38 @@ class TestUnusedVar : public TestFixture { TEST_CASE(globalData); } + struct FunctionVariableUsageOptions + { + FunctionVariableUsageOptions() = default; + bool cpp = true; + }; + #define functionVariableUsage(...) functionVariableUsage_(__FILE__, __LINE__, __VA_ARGS__) + void functionVariableUsage_(const char* file, int line, const char code[], const FunctionVariableUsageOptions& options = make_default_obj()) { + // Tokenize.. + SimpleTokenizer tokenizer(settings, *this); + ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line); + + // Check for unused variables.. + CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this); + checkUnusedVar.checkFunctionVariableUsage(); + } + + struct CheckStructMemberUsageOptions + { + CheckStructMemberUsageOptions() = default; + const std::list* directives = nullptr; + const Settings *s = nullptr; + }; + #define checkStructMemberUsage(...) checkStructMemberUsage_(__FILE__, __LINE__, __VA_ARGS__) - void checkStructMemberUsage_(const char* file, int line, const char code[], const std::list* directives = nullptr, const Settings *s = nullptr) { - const Settings *settings1 = s ? s : &settings; + void checkStructMemberUsage_(const char* file, int line, const char code[], const CheckStructMemberUsageOptions& options = make_default_obj()) { + const Settings *settings1 = options.s ? options.s : &settings; // Tokenize.. SimpleTokenizer tokenizer(*settings1, *this); - if (directives) - tokenizer.setDirectives(*directives); + if (options.directives) + tokenizer.setDirectives(*options.directives); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check for unused variables.. @@ -292,8 +315,8 @@ class TestUnusedVar : public TestFixture { } #define checkFunctionVariableUsageP(...) checkFunctionVariableUsageP_(__FILE__, __LINE__, __VA_ARGS__) - void checkFunctionVariableUsageP_(const char* file, int line, const char code[], const char* filename = "test.cpp") { - std::vector files(1, filename); + void checkFunctionVariableUsageP_(const char* file, int line, const char code[]) { + std::vector files(1, "test.cpp"); Tokenizer tokenizer(settings, *this); PreprocessorHelper::preprocess(code, files, tokenizer, *this); @@ -1592,7 +1615,7 @@ class TestUnusedVar : public TestFixture { void structmember15() { // #3088 std::list directives; directives.emplace_back("test.cpp", 1, "#pragma pack(1)"); - checkStructMemberUsage("\nstruct Foo { int x; int y; };", &directives); + checkStructMemberUsage("\nstruct Foo { int x; int y; };", dinit(CheckStructMemberUsageOptions, $.directives = &directives)); ASSERT_EQUALS("", errout_str()); } @@ -1823,7 +1846,7 @@ class TestUnusedVar : public TestFixture { "void f() {\n" " struct B* pb = &a.b;\n" " pb->x = 1;\n" - "}\n", nullptr, &s); + "}\n", dinit(CheckStructMemberUsageOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); checkStructMemberUsage("union U {\n" @@ -1841,7 +1864,7 @@ class TestUnusedVar : public TestFixture { " pb->x = 1;\n" " struct C* pc = &u.c;\n" " pc->s[0] = 1;\n" - "}\n", nullptr, &s); + "}\n", dinit(CheckStructMemberUsageOptions, $.s = &s)); ASSERT_EQUALS("", errout_str()); } @@ -2058,16 +2081,6 @@ class TestUnusedVar : public TestFixture { ASSERT_EQUALS("", errout_str()); } - void functionVariableUsage_(const char* file, int line, const char code[], bool cpp = true) { - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this); - ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); - - // Check for unused variables.. - CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this); - checkUnusedVar.checkFunctionVariableUsage(); - } - void localvar1() { // extracttests.disable functionVariableUsage("void foo()\n" @@ -2183,7 +2196,7 @@ class TestUnusedVar : public TestFixture { "{\n" " undefined i = 0;\n" "}\n", - false); + dinit(FunctionVariableUsageOptions, $.cpp = false)); ASSERT_EQUALS( "[test.c:3]: (style) Variable 'i' is assigned a value that is never used.\n" "[test.c:3]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate @@ -2579,7 +2592,7 @@ class TestUnusedVar : public TestFixture { " undefined i;\n" " return i;\n" "}\n", - false); + dinit(FunctionVariableUsageOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (style) Variable 'i' is not assigned a value.\n", errout_str()); functionVariableUsage("undefined *foo()\n" @@ -3346,7 +3359,7 @@ class TestUnusedVar : public TestFixture { functionVariableUsage("void f(int x) {\n" " C c;\n" " if (c >>= x) {}\n" - "}", false); + "}", dinit(FunctionVariableUsageOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:3]: (style) Variable 'c' is assigned a value that is never used.\n", errout_str()); functionVariableUsage("void f() {\n" @@ -5133,7 +5146,7 @@ class TestUnusedVar : public TestFixture { " int x;\n" " unknown_type p = &x;\n" " *p = 9;\n" - "}", false); + "}", dinit(FunctionVariableUsageOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); } @@ -5288,7 +5301,7 @@ class TestUnusedVar : public TestFixture { " A a;\n" " return 0;\n" "}\n", - false); + dinit(FunctionVariableUsageOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:2]: (style) Unused variable: a\n", errout_str()); // extracttests.enable @@ -7113,7 +7126,7 @@ class TestUnusedVar : public TestFixture { "void fun(Date result) {" " result.x = 12;\n" "}", - false + dinit(FunctionVariableUsageOptions, $.cpp = false) ); ASSERT_EQUALS("[test.c:1]: (style) Variable 'result.x' is assigned a value that is never used.\n", errout_str()); diff --git a/test/testvaarg.cpp b/test/testvaarg.cpp index f4a42a4a111..5e5abed3810 100644 --- a/test/testvaarg.cpp +++ b/test/testvaarg.cpp @@ -31,9 +31,9 @@ class TestVaarg : public TestFixture { private: const Settings settings = settingsBuilder().severity(Severity::warning).build(); -#define check(code) check_(code, __FILE__, __LINE__) +#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template - void check_(const char (&code)[size], const char* file, int line) { + 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/testvarid.cpp b/test/testvarid.cpp index 2b486946750..d47dd9662fd 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -257,18 +257,25 @@ class TestVarID : public TestFixture { TEST_CASE(structuredBindings); } + struct TokenizeOptions + { + TokenizeOptions() = default; + bool cpp = true; + const Settings *s = nullptr; + }; + #define tokenize(...) tokenize_(__FILE__, __LINE__, __VA_ARGS__) 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; + std::string tokenize_(const char* file, int line, const char (&code)[size], const TokenizeOptions& options = make_default_obj()) { + const Settings *settings1 = options.s ? options.s : &settings; SimpleTokenizer tokenizer(*settings1, *this); - ASSERT_LOC((tokenizer.tokenize)(code, cpp), file, line); + ASSERT_LOC((tokenizer.tokenize)(code, options.cpp), file, line); // result.. - Token::stringifyOptions options = Token::stringifyOptions::forDebugVarId(); - options.files = false; - return tokenizer.tokens()->stringifyList(options); + Token::stringifyOptions str_options = Token::stringifyOptions::forDebugVarId(); + str_options.files = false; + return tokenizer.tokens()->stringifyList(str_options); } #define tokenizeHeader(...) tokenizeHeader_(__FILE__, __LINE__, __VA_ARGS__) @@ -329,7 +336,7 @@ class TestVarID : public TestFixture { " for (int i = 0; i < 10; ++i)\n" " i = 3;\n" " i = 4;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: static int i@1 = 1 ;\n" "2: void f ( )\n" @@ -354,7 +361,7 @@ class TestVarID : public TestFixture { " i = 3;\n" " }\n" " i = 4;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: static int i@1 = 1 ;\n" "2: void f ( )\n" @@ -378,7 +385,7 @@ class TestVarID : public TestFixture { " struct ABC abc;\n" " abc.a = 3;\n" " i = abc.a;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void f ( )\n" "2: {\n" @@ -397,7 +404,7 @@ class TestVarID : public TestFixture { "{\n" " char str[10];\n" " str[0] = 0;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: static char str@1 [ 4 ] ;\n" "2: void f ( )\n" @@ -414,7 +421,7 @@ class TestVarID : public TestFixture { "void f(const unsigned int a[])\n" "{\n" " int i = *(a+10);\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void f ( const unsigned int a@1 [ ] )\n" "2: {\n" @@ -429,7 +436,7 @@ class TestVarID : public TestFixture { "void f()\n" "{\n" " int a,b;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void f ( )\n" "2: {\n" @@ -445,7 +452,7 @@ class TestVarID : public TestFixture { "int f(int a, int b)\n" "{\n" " return a+b;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: int f ( int a@1 , int b@2 )\n" "2: {\n" @@ -463,7 +470,7 @@ class TestVarID : public TestFixture { " {\n" " char b[256] = \"test\";\n" " }\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void func ( ) {\n" "2: char a@1 [ 256 ] = \"test\" ;\n" @@ -481,7 +488,7 @@ class TestVarID : public TestFixture { "{\n" " int a;\n" " return a;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: int f ( )\n" "2: {\n" @@ -498,7 +505,7 @@ class TestVarID : public TestFixture { "{\n" " unsigned long mask = (1UL << size_) - 1;\n" " return (abits_val_ & mask);\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void foo ( )\n" "2: {\n" @@ -528,7 +535,7 @@ class TestVarID : public TestFixture { void varid9() { const std::string actual = tokenize( - "typedef int INT32;\n", false); + "typedef int INT32;\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: ;\n"; @@ -541,7 +548,7 @@ class TestVarID : public TestFixture { "{\n" " int abc;\n" " struct abc abc1;\n" - "}", false); + "}", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void foo ( )\n" "2: {\n" @@ -581,7 +588,7 @@ class TestVarID : public TestFixture { "{\n" " int a; int b;\n" " a = a;\n" - "}\n", false); + "}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void f ( )\n" "2: {\n" @@ -600,7 +607,7 @@ class TestVarID : public TestFixture { "A a;\n" "B b;\n" "b * a;\n" - "}", false); + "}", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void foo ( )\n" "2: {\n" @@ -618,7 +625,7 @@ class TestVarID : public TestFixture { "struct S {\n" " struct T {\n" " } t;\n" - "} s;", false); + "} s;", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: struct S {\n" "2: struct T {\n" @@ -633,7 +640,7 @@ class TestVarID : public TestFixture { "struct S {\n" " struct T {\n" " } t;\n" - "};", false); + "};", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: struct S {\n" "2: struct T {\n" @@ -657,7 +664,7 @@ class TestVarID : public TestFixture { "4: y = z * x@1 ;\n" "5: }\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid17() { // ticket #1810 @@ -673,7 +680,7 @@ class TestVarID : public TestFixture { "4: return c@1 ;\n" "5: }\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid18() { @@ -797,7 +804,7 @@ class TestVarID : public TestFixture { "3: EventPtr event@3 ; event@3 = * eventP@1 ;\n" "4: * actionsP@2 = & event@3 . actions@4 ;\n" "5: }\n"; - ASSERT_EQUALS(expected1, tokenize(code1, false)); + ASSERT_EQUALS(expected1, tokenize(code1, dinit(TokenizeOptions, $.cpp = false))); const char code2[] = "void f(int b, int c) {\n" " x(a*b*c,10);\n" @@ -805,7 +812,7 @@ class TestVarID : public TestFixture { const char expected2[] = "1: void f ( int b@1 , int c@2 ) {\n" "2: x ( a * b@1 * c@2 , 10 ) ;\n" "3: }\n"; - ASSERT_EQUALS(expected2, tokenize(code2, false)); + ASSERT_EQUALS(expected2, tokenize(code2, dinit(TokenizeOptions, $.cpp = false))); const char code3[] = "class Nullpointer : public ExecutionPath\n" " {\n" @@ -916,7 +923,7 @@ class TestVarID : public TestFixture { { const char code[] = "static int const SZ = 22;\n"; ASSERT_EQUALS("1: static const int SZ@1 = 22 ;\n", - tokenize(code, false)); + tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } } @@ -930,12 +937,12 @@ class TestVarID : public TestFixture { const char code1[] = "union evt; void f(const evt & event);"; ASSERT_EQUALS("1: union evt ; void f ( const evt & event@1 ) ;\n", tokenize(code1)); - ASSERT_THROW_INTERNAL(tokenize(code1, false), SYNTAX); + ASSERT_THROW_INTERNAL(tokenize(code1, dinit(TokenizeOptions, $.cpp = false)), SYNTAX); const char code2[] = "struct evt; void f(const evt & event);"; ASSERT_EQUALS("1: struct evt ; void f ( const evt & event@1 ) ;\n", tokenize(code2)); - ASSERT_THROW_INTERNAL(tokenize(code2, false), SYNTAX); + ASSERT_THROW_INTERNAL(tokenize(code2, dinit(TokenizeOptions, $.cpp = false)), SYNTAX); } void varid42() { @@ -955,7 +962,7 @@ class TestVarID : public TestFixture { void varid43() { const char code[] ="int main(int flag) { if(a & flag) { return 1; } }"; ASSERT_EQUALS("1: int main ( int flag@1 ) { if ( a & flag@1 ) { return 1 ; } }\n", - tokenize(code, false)); + tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid44() { @@ -973,7 +980,7 @@ class TestVarID : public TestFixture { void varid46() { // #3756 const char code[] ="void foo() { int t; x = (struct t *)malloc(); f(t); }"; ASSERT_EQUALS("1: void foo ( ) { int t@1 ; x = ( struct t * ) malloc ( ) ; f ( t@1 ) ; }\n", - tokenize(code, false)); + tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid47() { // function parameters @@ -981,7 +988,7 @@ class TestVarID : public TestFixture { { const char code[] ="void f(std::string &string, std::string &len) {}"; ASSERT_EQUALS("1: void f ( std :: string & string@1 , std :: string & len@2 ) { }\n", - tokenize(code, true)); + tokenize(code)); } // #4729 @@ -1005,19 +1012,19 @@ class TestVarID : public TestFixture { void varid49() { // #3799 - void f(std::vector) const char code[] ="void f(std::vector)"; ASSERT_EQUALS("1: void f ( std :: vector < int > )\n", - tokenize(code, true)); + tokenize(code)); } void varid50() { // #3760 - explicit const char code[] ="class A { explicit A(const A&); };"; ASSERT_EQUALS("1: class A { explicit A ( const A & ) ; } ;\n", - tokenize(code, true)); + tokenize(code)); } void varid51() { // don't set varid on template function const char code[] ="T t; t.x<0>();"; ASSERT_EQUALS("1: T t@1 ; t@1 . x < 0 > ( ) ;\n", - tokenize(code, true)); + tokenize(code)); } void varid52() { @@ -1027,12 +1034,12 @@ class TestVarID : public TestFixture { ASSERT_EQUALS("1: A < B < C > :: D > e@1 ;\n" "2: B < C < > > b@2 [ 10 ] ;\n" "3: B < C < > > c@3 [ 10 ] ;\n", - tokenize(code, true)); + tokenize(code)); } void varid53() { // #4172 - Template instantiation: T<&functionName> list[4]; ASSERT_EQUALS("1: A < & f > list@1 [ 4 ] ;\n", - tokenize("A<&f> list[4];", true)); + tokenize("A<&f> list[4];")); } void varid54() { // hang @@ -1056,7 +1063,7 @@ class TestVarID : public TestFixture { "void baz2 ( struct foo & foo@4 ) { } " "void bar3 ( struct foo * foo@5 ) { } " "void baz3 ( struct foo * foo@6 ) { }\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid56() { // Ticket #6548 - function with a throw() @@ -1065,42 +1072,42 @@ class TestVarID : public TestFixture { const char expected1[] = "1: " "void fred ( int x@1 ) throw ( ) { } " "void wilma ( ) { x ++ ; }\n"; - ASSERT_EQUALS(expected1, tokenize(code1, true)); + ASSERT_EQUALS(expected1, tokenize(code1)); const char code2[] = "void fred(int x) const throw(EXCEPT) {}" "void wilma() { x++; }"; const char expected2[] = "1: " "void fred ( int x@1 ) const throw ( EXCEPT ) { } " "void wilma ( ) { x ++ ; }\n"; - ASSERT_EQUALS(expected2, tokenize(code2, true)); + ASSERT_EQUALS(expected2, tokenize(code2)); const char code3[] = "void fred(int x) throw() ABCD {}" "void wilma() { x++; }"; const char expected3[] = "1: " "void fred ( int x@1 ) throw ( ) { } " "void wilma ( ) { x ++ ; }\n"; - ASSERT_EQUALS(expected3, tokenize(code3, true)); + ASSERT_EQUALS(expected3, tokenize(code3)); const char code4[] = "void fred(int x) noexcept() {}" "void wilma() { x++; }"; const char expected4[] = "1: " "void fred ( int x@1 ) noexcept ( ) { } " "void wilma ( ) { x ++ ; }\n"; - ASSERT_EQUALS(expected4, tokenize(code4, true)); + ASSERT_EQUALS(expected4, tokenize(code4)); const char code5[] = "void fred(int x) noexcept {}" "void wilma() { x++; }"; const char expected5[] = "1: " "void fred ( int x@1 ) noexcept ( true ) { } " "void wilma ( ) { x ++ ; }\n"; - ASSERT_EQUALS(expected5, tokenize(code5, true)); + ASSERT_EQUALS(expected5, tokenize(code5)); const char code6[] = "void fred(int x) noexcept ( false ) {}" "void wilma() { x++; }"; const char expected6[] = "1: " "void fred ( int x@1 ) noexcept ( false ) { } " "void wilma ( ) { x ++ ; }\n"; - ASSERT_EQUALS(expected6, tokenize(code6, true)); + ASSERT_EQUALS(expected6, tokenize(code6)); } void varid57() { // #6636: new scope by {} @@ -1130,7 +1137,7 @@ class TestVarID : public TestFixture { "11: }\n" "12:\n" "13: }\n"; - ASSERT_EQUALS(expected1, tokenize(code1, true)); + ASSERT_EQUALS(expected1, tokenize(code1)); } void varid58() { // #6638: for loop in for condition @@ -1148,7 +1155,7 @@ class TestVarID : public TestFixture { "5: i@1 ++ ;\n" "6: }\n" "7: }\n"; - ASSERT_EQUALS(expected1, tokenize(code1, true)); + ASSERT_EQUALS(expected1, tokenize(code1)); } void varid59() { // #6696 @@ -1164,7 +1171,7 @@ class TestVarID : public TestFixture { "2: struct B {\n" "3: ~ B ( ) { }\n" "4: } ;\n"; - TODO_ASSERT_EQUALS(wanted, expected, tokenize(code, true)); + TODO_ASSERT_EQUALS(wanted, expected, tokenize(code)); } void varid60() { // #7267 - cast @@ -1253,7 +1260,7 @@ class TestVarID : public TestFixture { "_Generic(*x, int: foo, default: bar)();"; const char expected1[] = "1: int * x@1 ;\n" "2: _Generic ( * x@1 , int : foo , default : bar ) ( ) ;\n"; - ASSERT_EQUALS(expected1, tokenize(code1, false)); + ASSERT_EQUALS(expected1, tokenize(code1, dinit(TokenizeOptions, $.cpp = false))); } void varid68() { // #11740 @@ -1267,8 +1274,8 @@ class TestVarID : public TestFixture { "3: void f ( struct S strOut@2 ) {\n" "4: switch ( str_chars ( & strOut@2 ) [ 0 ] ) { }\n" "5: }\n"; - ASSERT_EQUALS(expected1, tokenize(code1, false)); - ASSERT_EQUALS(expected1, tokenize(code1, true)); + ASSERT_EQUALS(expected1, tokenize(code1, dinit(TokenizeOptions, $.cpp = false))); + ASSERT_EQUALS(expected1, tokenize(code1)); } void varid69() { @@ -1278,7 +1285,7 @@ class TestVarID : public TestFixture { const char expected1[] = "1: void f ( ) {\n" "2: auto g@1 ; g@1 = [ ] ( int & , int & r@2 , int i@3 ) { } ;\n" "3: }\n"; - ASSERT_EQUALS(expected1, tokenize(code1, true)); + ASSERT_EQUALS(expected1, tokenize(code1)); } void varid70() { @@ -1289,7 +1296,7 @@ class TestVarID : public TestFixture { const char expected1[] = "1: int x@1 ; x@1 = 1 ? ( 1 << 0 ) : 0 ;\n" "2: void foo ( bool init@2 ) ;\n" "3: void init ( ) ;\n"; - ASSERT_EQUALS(expected1, tokenize(code1, true)); + ASSERT_EQUALS(expected1, tokenize(code1)); const char code2[] = "int x = 1 ? f(1 << 0) : 0;\n" "void foo(bool init);\n" @@ -1297,15 +1304,15 @@ class TestVarID : public TestFixture { const char expected2[] = "1: int x@1 ; x@1 = 1 ? f ( 1 << 0 ) : 0 ;\n" "2: void foo ( bool init@2 ) ;\n" "3: void init ( ) ;\n"; - ASSERT_EQUALS(expected2, tokenize(code2, true)); + ASSERT_EQUALS(expected2, tokenize(code2)); const char code3[] = "extern void (*arr[10])(uint32_t some);\n"; const char expected3[] = "1: extern void ( * arr@1 [ 10 ] ) ( uint32_t some@2 ) ;\n"; - ASSERT_EQUALS(expected3, tokenize(code3, true)); + ASSERT_EQUALS(expected3, tokenize(code3)); const char code4[] = "_Static_assert(sizeof((struct S){0}.i) == 4);\n"; // #12729 const char expected4[] = "1: _Static_assert ( sizeof ( ( struct S ) { 0 } . i ) == 4 ) ;\n"; - ASSERT_EQUALS(expected4, tokenize(code4, false)); + ASSERT_EQUALS(expected4, tokenize(code4, dinit(TokenizeOptions, $.cpp = false))); } void varid71() { @@ -1371,7 +1378,7 @@ class TestVarID : public TestFixture { "20: myspace :: CounterTest :: CounterTest ( const myspace :: CounterTest < T2 > & p@9 ) : obj@6 ( 0 ) {\n" "21: count@7 = p@9 . count@10 ;\n" "22: }\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid_for_1() { @@ -1405,7 +1412,7 @@ class TestVarID : public TestFixture { "3: throw t@2 ;\n" "4: }\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid_cpp_keywords_in_c_code2() { // #5373 @@ -1418,13 +1425,13 @@ class TestVarID : public TestFixture { " return clear_extent_bit(tree, start, end, EXTENT_DIRTY | EXTENT_DELALLOC | " " EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);\n" "}"; - ASSERT_NO_THROW(tokenize(code, false)); + ASSERT_NO_THROW(tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid_cpp_keywords_in_c_code3() { // #12120 const char code[] = "const struct class *p;"; const char expected[] = "1: const struct class * p@1 ;\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varidFunctionCall1() { @@ -1436,7 +1443,7 @@ class TestVarID : public TestFixture { "2: int x@1 ;\n" "3: x@1 = a ( y * x@1 , 10 ) ;\n" "4: }\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varidFunctionCall2() { @@ -1448,7 +1455,7 @@ class TestVarID : public TestFixture { "2: x ( a * b"); const std::string expected2(" , 10 ) ;\n" "3: }\n"); - ASSERT_EQUALS(expected1+"@1"+expected2, tokenize(code, false)); + ASSERT_EQUALS(expected1+"@1"+expected2, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varidFunctionCall3() { @@ -1470,16 +1477,16 @@ class TestVarID : public TestFixture { // Ticket #3280 const char code1[] = "void f() { int x; fun(a,b*x); }"; ASSERT_EQUALS("1: void f ( ) { int x@1 ; fun ( a , b * x@1 ) ; }\n", - tokenize(code1, false)); + tokenize(code1, dinit(TokenizeOptions, $.cpp = false))); const char code2[] = "void f(int a) { int x; fun(a,b*x); }"; ASSERT_EQUALS("1: void f ( int a@1 ) { int x@2 ; fun ( a@1 , b * x@2 ) ; }\n", - tokenize(code2, false)); + tokenize(code2, dinit(TokenizeOptions, $.cpp = false))); } void varidFunctionCall5() { const char code[] = "void foo() { (f(x[2]))(x[2]); }"; ASSERT_EQUALS("1: void foo ( ) { f ( x [ 2 ] ) ( x [ 2 ] ) ; }\n", - tokenize(code, false)); + tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varidStl() { @@ -1541,7 +1548,7 @@ class TestVarID : public TestFixture { { const std::string actual = tokenize( "void f();\n" - "void f(){}\n", false); + "void f(){}\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void f ( ) ;\n" "2: void f ( ) { }\n"; @@ -1554,7 +1561,7 @@ class TestVarID : public TestFixture { "A f(3);\n" "A f2(true);\n" "A g();\n" - "A e(int c);\n", false); + "A e(int c);\n", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: A f@1 ( 3 ) ;\n" "2: A f2@2 ( true ) ;\n" @@ -1596,13 +1603,13 @@ class TestVarID : public TestFixture { } { - const std::string actual = tokenize("void f(struct foobar);", false); + const std::string actual = tokenize("void f(struct foobar);", dinit(TokenizeOptions, $.cpp = false)); const char expected[] = "1: void f ( struct foobar ) ;\n"; ASSERT_EQUALS(expected, actual); } { - const std::string actual = tokenize("bool f(X x, int=3);", true); + const std::string actual = tokenize("bool f(X x, int=3);"); const char expected[] = "1: bool f ( X x@1 , int = 3 ) ;\n"; ASSERT_EQUALS(expected, actual); } @@ -1613,7 +1620,7 @@ class TestVarID : public TestFixture { " extern void f(int a[2]);\n" " f(a);\n" " a[0] = 0;\n" - "}\n", true); + "}\n"); const char expected[] = "1: int main ( ) {\n" "2: int a@1 [ 2 ] ;\n" "3: extern void f ( int a [ 2 ] ) ;\n" @@ -1628,7 +1635,7 @@ class TestVarID : public TestFixture { " int n1;\n" " void g(int is, int n1);\n" " n1 = n - 1;\n" - "}\n", true); + "}\n"); const char expected[] = "1: void f ( int n@1 ) {\n" "2: int n1@2 ;\n" "3: void g ( int is , int n1 ) ;\n" @@ -1641,7 +1648,7 @@ class TestVarID : public TestFixture { void varid_sizeof() { const char code[] = "x = sizeof(a*b);"; const char expected[] = "1: x = sizeof ( a * b ) ;\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid_reference_to_containers() { @@ -1955,15 +1962,15 @@ class TestVarID : public TestFixture { void varid_in_class13() { const char code1[] = "struct a { char typename; };"; ASSERT_EQUALS("1: struct a { char typename@1 ; } ;\n", - tokenize(code1, false)); + tokenize(code1, dinit(TokenizeOptions, $.cpp = false))); ASSERT_EQUALS("1: struct a { char typename ; } ;\n", // not valid C++ code - tokenize(code1, true)); + tokenize(code1)); const char code2[] = "struct a { char typename[2]; };"; ASSERT_EQUALS("1: struct a { char typename@1 [ 2 ] ; } ;\n", - tokenize(code2, false)); + tokenize(code2, dinit(TokenizeOptions, $.cpp = false))); ASSERT_EQUALS("1: struct a { char typename [ 2 ] ; } ;\n", // not valid C++ code - tokenize(code2, true)); + tokenize(code2)); } void varid_in_class14() { @@ -1980,7 +1987,7 @@ class TestVarID : public TestFixture { "4: std :: list < int > x@2 ;\n" "5: list@1 . do_something ( ) ;\n" "6: Tokenizer :: list@1 . do_something ( ) ;\n" - "7: }\n", tokenize(code, true)); + "7: }\n", tokenize(code)); } void varid_in_class15() { // #5533 - functions @@ -1991,7 +1998,7 @@ class TestVarID : public TestFixture { ASSERT_EQUALS("1: class Fred {\n" "2: void x ( int a@1 ) const ;\n" "3: void y ( ) { a = 0 ; }\n" - "4: }\n", tokenize(code, true)); + "4: }\n", tokenize(code)); } void varid_in_class16() { // Set varId for inline member functions @@ -2003,7 +2010,7 @@ class TestVarID : public TestFixture { ASSERT_EQUALS("1: class Fred {\n" "2: int x@1 ;\n" "3: void foo ( int x@2 ) { this . x@1 = x@2 ; }\n" - "4: } ;\n", tokenize(code, true)); + "4: } ;\n", tokenize(code)); } { const char code[] = "class Fred {\n" @@ -2013,7 +2020,7 @@ class TestVarID : public TestFixture { ASSERT_EQUALS("1: class Fred {\n" "2: void foo ( int x@1 ) { this . x@2 = x@1 ; }\n" "3: int x@2 ;\n" - "4: } ;\n", tokenize(code, true)); + "4: } ;\n", tokenize(code)); } { const char code[] = "class Fred {\n" @@ -2023,7 +2030,7 @@ class TestVarID : public TestFixture { ASSERT_EQUALS("1: class Fred {\n" "2: void foo ( int x@1 ) { ( * this ) . x@2 = x@1 ; }\n" "3: int x@2 ;\n" - "4: } ;\n", tokenize(code, true)); + "4: } ;\n", tokenize(code)); } } @@ -2051,7 +2058,7 @@ class TestVarID : public TestFixture { "9: FOO Set ( BAR ) ;\n" "10: int method_with_class ( B < B > b@3 ) ;\n" "11: bool function ( std :: map < int , int , MYless > & m@4 ) ;\n" - "12: } ;\n", tokenize(code1, true)); + "12: } ;\n", tokenize(code1)); const char code2[] = "int i;\n" "SomeType someVar1(i, i);\n" @@ -2062,7 +2069,7 @@ class TestVarID : public TestFixture { "2: SomeType someVar1@2 ( i@1 , i@1 ) ;\n" "3: SomeType someVar2 ( j , j ) ;\n" // This one could be a function "4: SomeType someVar3@3 ( j , 1 ) ;\n" - "5: SomeType someVar4@4 ( new bar ) ;\n", tokenize(code2, true)); + "5: SomeType someVar4@4 ( new bar ) ;\n", tokenize(code2)); } void varid_in_class18() { @@ -2085,7 +2092,7 @@ class TestVarID : public TestFixture { "7: } ;\n" "8: A :: B :: B ( ) :\n" "9: i@1 ( 0 )\n" - "10: { }\n", tokenize(code, true)); + "10: { }\n", tokenize(code)); } void varid_in_class19() { @@ -2102,7 +2109,7 @@ class TestVarID : public TestFixture { "4: } ;\n" "5: Fred :: ~ Fred ( ) {\n" "6: free ( str1@1 ) ;\n" - "7: }\n", tokenize(code, true)); + "7: }\n", tokenize(code)); } void varid_in_class20() { @@ -2122,7 +2129,7 @@ class TestVarID : public TestFixture { "5: cacheEntry ( ) ;\n" "6: } ;\n" "7:\n" - "8: template < class C > cacheEntry < C > :: cacheEntry ( ) : m_key@1 ( ) { }\n", tokenize(code, true)); + "8: template < class C > cacheEntry < C > :: cacheEntry ( ) : m_key@1 ( ) { }\n", tokenize(code)); } void varid_in_class21() { @@ -2144,7 +2151,7 @@ class TestVarID : public TestFixture { "7: template < typename t1 , typename t2 >\n" "8: A :: B < t1 , t2 > :: B ( ) : x@1 ( 9 ) { }\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid_in_class22() { @@ -2168,7 +2175,7 @@ class TestVarID : public TestFixture { "8: for ( std :: vector < data > :: const_iterator i@3 = std@1 . begin ( ) ; i@3 != end@2 ; ++ i@3 ) { }\n" "9: }\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid_in_class23() { // #11293 @@ -2190,7 +2197,7 @@ class TestVarID : public TestFixture { "7: void f ( ) { b@1 = false ; }\n" "8: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid_in_class24() { @@ -2207,7 +2214,7 @@ class TestVarID : public TestFixture { "3: public:\n" "4:\n" "5: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } { @@ -2219,7 +2226,7 @@ class TestVarID : public TestFixture { "2: Q_OBJECT\n" "3:\n" "4: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } } @@ -2240,7 +2247,7 @@ class TestVarID : public TestFixture { "4: if ( v@2 . front ( ) . i@3 ) { }\n" "5: }\n" "6: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true, &s)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.s = &s))); } { @@ -2258,7 +2265,7 @@ class TestVarID : public TestFixture { "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)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.s = &s))); } } @@ -2284,7 +2291,7 @@ class TestVarID : public TestFixture { "8: void S :: f ( ) {\n" "9: u8@1 [ 0 ] = 0 ;\n" "10: }\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid_in_class27() { @@ -2296,7 +2303,7 @@ class TestVarID : public TestFixture { "2: int * * pp@1 ;\n" "3: void f ( ) { int x@2 ( * pp@1 [ 0 ] ) ; }\n" "4: } ;\n"; - ASSERT_EQUALS(expected, tokenize(code, true)); + ASSERT_EQUALS(expected, tokenize(code)); } void varid_namespace_1() { // #7272 @@ -2313,7 +2320,7 @@ class TestVarID : public TestFixture { "4: int x@2 ;\n" "5: union { char y@3 ; } ;\n" "6: } ;\n" - "7: }\n", tokenize(code, true)); + "7: }\n", tokenize(code)); } void varid_namespace_2() { // #7000 @@ -2330,7 +2337,7 @@ class TestVarID : public TestFixture { " X = 0;\n" // X@2 "}"; - const std::string actual = tokenize(code, true); + const std::string actual = tokenize(code); ASSERT(actual.find("X@2 = 0") != std::string::npos); } @@ -2361,7 +2368,7 @@ class TestVarID : public TestFixture { "}\n" "}"; - const std::string actual = tokenize(code, true); + const std::string actual = tokenize(code); ASSERT_EQUALS("5: int type@2 ;", getLine(actual,5)); ASSERT_EQUALS("11: type@2 = 0 ;", getLine(actual,11)); @@ -2381,7 +2388,7 @@ class TestVarID : public TestFixture { "4: void dostuff ( ) ;\n" "5: } ;\n" "6: void bar :: dostuff ( ) { int x2@2 ; x2@2 = x@1 * 2 ; }\n" - "7: }\n", tokenize(code, true)); + "7: }\n", tokenize(code)); } void varid_namespace_5() { @@ -2402,7 +2409,7 @@ class TestVarID : public TestFixture { "6: } ;\n" "7: void bar :: dostuff ( ) { int x2@2 ; x2@2 = x@1 * 2 ; }\n" "8: }\n" - "9: }\n", tokenize(code, true)); + "9: }\n", tokenize(code)); } void varid_namespace_6() { @@ -2425,7 +2432,7 @@ class TestVarID : public TestFixture { "7: std :: map < Vec2i , int > :: iterator iter@2 ;\n" "8: }\n" "9: }\n" - "10: }\n", tokenize(code, true)); + "10: }\n", tokenize(code)); } void varid_initList() { @@ -2717,7 +2724,7 @@ class TestVarID : public TestFixture { "3: AAA\n" "4: a@1 [ 0 ] = 0 ;\n" "5: }\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varid_using() { @@ -3091,7 +3098,7 @@ class TestVarID : public TestFixture { " delta = 1;\n" " break;\n" " }\n" - "}", false)); + "}", dinit(TokenizeOptions, $.cpp = false))); ASSERT_EQUALS("1: int * f ( ) {\n" // #11838 "2: int * label@1 ; label@1 = 0 ;\n" @@ -3369,7 +3376,7 @@ class TestVarID : public TestFixture { tokenize("const char *f(int*);\n" "void g(int i) {\n" " if (f(&i)[0] == 'm') {}\n" - "}\n", false)); + "}\n", dinit(TokenizeOptions, $.cpp = false))); } void varid_globalScope() { @@ -3423,7 +3430,7 @@ class TestVarID : public TestFixture { "alignas(16) int x;"; const char expected[] = "1: extern alignas ( 16 ) int x@1 ;\n" "2: alignas ( 16 ) int x@2 ;\n"; - ASSERT_EQUALS(expected, tokenize(code, false)); + ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } void varidclass1() {