From 176b3925b3d3e672ae54e24114a78ca39b3417e5 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 25 Jul 2015 14:17:55 +0200 Subject: [PATCH] Removed "verify" code in testrunner. Fixing its messages reduces the accuracy of the test suite. --- test/testautovariables.cpp | 4 -- test/testboost.cpp | 4 -- test/testbufferoverrun.cpp | 33 ++++------ test/testclass.cpp | 10 +-- test/testcondition.cpp | 52 ++++++--------- test/testnullpointer.cpp | 128 ++++++++++++++++++------------------- test/testother.cpp | 36 +++++------ test/testsuite.cpp | 12 ---- test/testsuite.h | 2 - test/testuninitvar.cpp | 12 ++-- 10 files changed, 114 insertions(+), 179 deletions(-) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 985c2b01f..51311afd5 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -49,11 +49,7 @@ private: checkAutoVariables.assignFunctionArg(); if (runSimpleChecks) { - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); - if (str1 != str2) - warnUnsimplified(str1, str2); // Check auto variables checkAutoVariables.autoVariables(); diff --git a/test/testboost.cpp b/test/testboost.cpp index 983d9e7e9..64d306c7e 100644 --- a/test/testboost.cpp +++ b/test/testboost.cpp @@ -43,11 +43,7 @@ private: Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); - if (str1 != str2) - warnUnsimplified(str1, str2); // Check.. CheckBoost checkBoost; diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 1e4cda696..a8f044e0a 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -31,7 +31,7 @@ public: private: - void check(const char code[], bool experimental = true, const char filename[] = "test.cpp", bool verify = true) { + void check(const char code[], bool experimental = true, const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); @@ -46,19 +46,8 @@ private: Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, filename); - - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); - - // Assign variable ids tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); - - // Ensure that the test case is not bad. - if (verify && str1 != str2) { - warnUnsimplified(str1, str2); - } - // Check for buffer overruns.. CheckBufferOverrun checkBufferOverrun; checkBufferOverrun.runSimplifiedChecks(&tokenizer, &settings, this); @@ -1845,7 +1834,7 @@ private: " }\n" " a[i - 1] = 0;\n" " }\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); check("void f() {\n" @@ -2049,21 +2038,21 @@ private: check("void f() {\n" " char str[3];\n" " str[((unsigned char)3) - 1] = 0;\n" - "}", false, "test.cpp", false); + "}", false, "test.cpp"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" // #5416 FP " char *str[3];\n" " do_something(&str[0][5]);\n" - "}", false, "test.cpp", false); + "}", false, "test.cpp"); ASSERT_EQUALS("", errout.str()); check("class X { static const int x[100]; };\n" // #6070 - "const int X::x[100] = {0};", false, "test.cpp", false); + "const int X::x[100] = {0};", false, "test.cpp"); ASSERT_EQUALS("", errout.str()); check("namespace { class X { static const int x[100]; };\n" // #6232 - "const int X::x[100] = {0}; }", false, "test.cpp", false); + "const int X::x[100] = {0}; }", false, "test.cpp"); ASSERT_EQUALS("", errout.str()); } @@ -2647,7 +2636,7 @@ private: 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, "6350.c", false); + "}", false, "6350.c"); ASSERT_EQUALS("", errout.str()); } @@ -2856,7 +2845,7 @@ private: check("void f() {\n" " int *tab4 = malloc(20 * sizeof(int));\n" " tab4[20] = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds.\n", errout.str()); // ticket #1134 @@ -2864,7 +2853,7 @@ private: " int *x, i;\n" " x = malloc(10 * sizeof(int));\n" " x[10] = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Array 'x[10]' accessed at index 10, which is out of bounds.\n", errout.str()); check("void f() {\n" @@ -2874,7 +2863,7 @@ private: " tab4 = malloc(21 * sizeof(int));\n" " tab4[20] = 0;\n" " free(tab4);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" @@ -2883,7 +2872,7 @@ private: " tab4 = realloc(tab4,21 * sizeof(int));\n" " tab4[20] = 0;\n" " free(tab4);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testclass.cpp b/test/testclass.cpp index 9c4172d9a..62aa97f53 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -2744,7 +2744,7 @@ private: "[test.cpp:3]: (warning) Suspicious pointer subtraction. Did you intend to write '->'?\n", errout.str()); } - void checkConst(const char code[], const Settings *s = 0, bool inconclusive = true, bool verify = true) { + void checkConst(const char code[], const Settings *s = 0, bool inconclusive = true) { // Clear the error log errout.str(""); @@ -2760,12 +2760,7 @@ private: Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); - if (verify && str1 != str2) - warnUnsimplified(str1, str2); CheckClass checkClass(&tokenizer, &settings, this); checkClass.checkConst(); @@ -3962,8 +3957,7 @@ private: " if( m_d != 0 )\n" " return m_iRealVal / m_d;\n" " return dRet;\n" - "};", nullptr, true, false - ); + "};", nullptr, true); ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (style, inconclusive) Technically the member function 'A::dGetValue' can be const.\n", errout.str()); } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 9074bba01..1311b7814 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -66,7 +66,7 @@ private: TEST_CASE(alwaysTrue); } - void check(const char code[], bool validate=true, const char* filename = "test.cpp") { + void check(const char code[], const char* filename = "test.cpp") { // Clear the error buffer.. errout.str(""); @@ -81,15 +81,8 @@ private: std::istringstream istr(code); tokenizer.tokenize(istr, filename); checkCondition.runChecks(&tokenizer, &settings, this); - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); checkCondition.runSimplifiedChecks(&tokenizer, &settings, this); - - // Ensure that the test case is not bad. - if (validate && str1 != str2) { - warnUnsimplified(str1, str2); - } } void assignAndCompare() { @@ -380,15 +373,8 @@ private: CheckCondition checkCondition; checkCondition.runChecks(&tokenizer, &settings, this); - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); checkCondition.runSimplifiedChecks(&tokenizer, &settings, this); - - // Ensure that the test case is not bad. - if (str1 != str2) { - warnUnsimplified(str1, str2); - } } void duplicateIf() { check("void f(int a, int &b) {\n" @@ -468,19 +454,19 @@ private: check("void f(WIDGET *widget) {\n" " if (dynamic_cast(widget)){}\n" " else if (dynamic_cast(widget)){}\n" - "}",false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int x) {\n" // #6482 " if (x & 1) {}\n" " else if (x == 0) {}\n" - "}",false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int x) {\n" " if (x & 15) {}\n" " else if (x == 40) {}\n" - "}",false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (style) Expression is always false because 'else if' condition matches previous condition at line 2.\n", errout.str()); } @@ -899,7 +885,7 @@ private: void incorrectLogicOperator4() { check("void f(int x) {\n" " if (x && x != $0) {}\n" - "}", false); + "}"); ASSERT_EQUALS("", errout.str()); } @@ -1329,7 +1315,7 @@ private: void clarifyCondition1() { check("void f() {\n" " if (x = b() < 0) {}\n" // don't simplify and verify this code - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.\n", errout.str()); check("void f(int i) {\n" @@ -1344,61 +1330,61 @@ private: check("void f() {\n" " if (x = b < 0 ? 1 : 2) {}\n" // don't simplify and verify this code - "}", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" " int y = rand(), z = rand();\n" " if (y || (!y && z));\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str()); check("void f() {\n" " int y = rand(), z = rand();\n" " if (y || !y && z);\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str()); check("void f() {\n" " if (!a || a && b) {}\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: a. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str()); check("void f() {\n" " if (!tok->next()->function() || \n" " (tok->next()->function() && tok->next()->function()->isConstructor()));\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next().function(). 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str()); check("void f() {\n" " if (!tok->next()->function() || \n" " (!tok->next()->function() && tok->next()->function()->isConstructor()));\n" - "}", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" " if (!tok->next()->function() || \n" " (!tok2->next()->function() && tok->next()->function()->isConstructor()));\n" - "}", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" " if (!tok->next(1)->function(1) || \n" " (tok->next(1)->function(1) && tok->next(1)->function(1)->isConstructor()));\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next(1).function(1). 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str()); check("void f() {\n" " if (!tok->next()->function(1) || \n" " (tok->next()->function(2) && tok->next()->function()->isConstructor()));\n" - "}", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" " int y = rand(), z = rand();\n" " if (y==0 || y!=0 && z);\n" - "}", false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: y!=0. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str()); check("void f() {\n" @@ -1438,15 +1424,15 @@ private: 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;", true, "test.cpp"); + check("class A;", "test.cpp"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" " if (result != (char *)&inline_result) { }\n" // don't simplify and verify cast - "}", false); + "}"); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 539f9152b..06a695899 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -94,7 +94,7 @@ private: TEST_CASE(nullpointer_internal_error); // #5080 } - void check(const char code[], bool inconclusive = false, const char filename[] = "test.cpp", bool verify=true) { + void check(const char code[], bool inconclusive = false, const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); @@ -111,11 +111,7 @@ private: CheckNullPointer checkNullPointer(&tokenizer, &settings, this); checkNullPointer.nullPointer(); - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); - if (verify && str1 != str2) - warnUnsimplified(str1, str2); checkNullPointer.nullConstantDereference(); } @@ -236,7 +232,7 @@ private: " }\n" " else if (a->x == 2) { }\n" " if (a) { }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // ticket #2134 - sizeof doesn't dereference @@ -246,7 +242,7 @@ private: " sizeof(*list);\n" " if (!list)\n" " ;\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); // ticket #2245 - sizeof doesn't dereference @@ -307,7 +303,7 @@ private: " return;\n" " }\n" " if (!abc);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (warning) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str()); // TODO: False negative if member of member is dereferenced @@ -322,7 +318,7 @@ private: " abc->a = 0;\n" " if (abc && abc->b == 0)\n" " ;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str()); // ok dereferencing in a condition @@ -352,7 +348,7 @@ private: check("void f(struct ABC *abc) {\n" " abc = (ABC *)(abc->_next);\n" " if (abc) { }" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); // reassign struct.. @@ -385,7 +381,7 @@ private: "out:\n" " if (!abc)\n" " ;\n" - "}",false,"test.cpp",false); + "}"); ASSERT_EQUALS("", errout.str()); // loops.. @@ -420,7 +416,7 @@ private: " int a = abc->a;\n" " if (!dynamic_cast(abc))\n" " ;\n" - "}",false,"test.cpp",false); + "}"); ASSERT_EQUALS("", errout.str()); // #2641 - global pointer, function call @@ -559,7 +555,7 @@ private: "{\n" " if (*p == 0) { }\n" " if (!p) { }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); // no error @@ -596,7 +592,7 @@ private: " int a = 2 * x;" " if (x == 0)\n" " ;\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); check("void foo(int *p)\n" @@ -649,7 +645,7 @@ private: " int * a=0;\n" " if (!a) {};\n" " int c = a ? 0 : 1;\n" - "}\n",true,"test.cpp",false); + "}\n",true); ASSERT_EQUALS("", errout.str()); // #3686 @@ -657,14 +653,14 @@ private: " int * a=0;\n" " if (!a) {};\n" " int c = a ? b : b+1;\n" - "}\n",true,"test.cpp",false); + "}\n",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,"test.cpp",false); + "}\n",true); ASSERT_EQUALS("", errout.str()); check("void foo(P *p)\n" @@ -808,26 +804,26 @@ private: " int c = sizeof(test[0]);\n" " if (!test)\n" " ;\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); check("void f(type* p) {\n" // #4983 " x(sizeof p[0]);\n" " if (!p)\n" " ;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // #3023 - checked deref check("void f(struct ABC *abc) {\n" " WARN_ON(!abc || abc->x == 0);\n" " if (!abc) { }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(struct ABC *abc) {\n" " WARN_ON(!abc || abc->x == 7);\n" " if (!abc) { }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // #3425 - false positives when there are macros @@ -1116,7 +1112,7 @@ private: "{\n" " std::string * x = 0;\n" " *x = \"test\";\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: x\n" "[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); } @@ -1147,11 +1143,11 @@ private: " return *i;\n" "}\n"; - check(code, false, "test.cpp", false); // C++ file => nullptr means NULL + check(code, false, "test.cpp"); // C++ file => nullptr means NULL ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: i\n" "[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); - check(code, false, "test.c", false); // C file => nullptr does not mean NULL + check(code, false, "test.c"); // C file => nullptr does not mean NULL ASSERT_EQUALS("", errout.str()); } @@ -1198,7 +1194,7 @@ private: " {\n" " i++;\n" " };\n" - "}\n", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: str\n" "[test.cpp:5]: (error) Null pointer dereference\n", errout.str()); } @@ -1291,7 +1287,7 @@ private: " pointer_=NULL;\n" " *pointer_=0;\n" " return *this;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: pointer_\n" "[test.cpp:8]: (error) Null pointer dereference\n", errout.str()); } @@ -1321,7 +1317,7 @@ private: " break;\n" " }\n" " return p;\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("[test.cpp:7]: (error) Possible null pointer dereference: p\n" "[test.cpp:7]: (error) Null pointer dereference\n", errout.str()); } @@ -1357,34 +1353,34 @@ private: " if (NULL == p) {\n" " }\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); check("void foo(char *p) {\n" " if (p == NULL) {\n" " }\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); check("void foo(char *p) {\n" " if (p == NULL) {\n" " }\n" " printf(\"%c\", *p);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); check("void foo(char *p) {\n" " if (p && *p == 0) {\n" " }\n" " printf(\"%c\", *p);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); check("void foo(char *p) {\n" " if (p && *p == 0) {\n" " } else { *p = 0; }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); check("void foo(char *p) {\n" @@ -1486,7 +1482,7 @@ private: " if (!dynamic_cast(foo)) {\n" " *foo = 0;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // ticket: #2300 - calling unknown function that may initialize the pointer @@ -1514,7 +1510,7 @@ private: " MACRO;\n" " }\n" " fred->a();\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // #2493 - switch @@ -1527,7 +1523,7 @@ private: " fred->a();\n" " break;\n" " };\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // #4118 - second if @@ -1563,7 +1559,7 @@ private: " else {\n" " int b = *i;\n" " }\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); // #2696 - false positives nr 1 @@ -1576,7 +1572,7 @@ private: "\n" " if (pFoo)\n" " bar();\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); // #2696 - false positives nr 2 @@ -1625,7 +1621,7 @@ private: " if (p == 0 && (p = malloc(10)) != 0) {\n" " *p = 0;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // check, assign and use @@ -1634,7 +1630,7 @@ private: " if (p == 0 && (p = malloc(10)) != a && (*p = a)) {\n" " *p = 0;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // check, and use @@ -1643,7 +1639,7 @@ private: " if (p == 0 && (*p = 0)) {\n" " return;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); // check, and use @@ -1652,7 +1648,7 @@ private: " if (p == 0 && p->x == 10) {\n" " return;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); // check, and use @@ -1661,7 +1657,7 @@ private: " if (p == 0 || p->x == 10) {\n" " return;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // check, and use @@ -1670,7 +1666,7 @@ private: " if (p == NULL && (*p = a)) {\n" " return;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); // check, and use @@ -1678,7 +1674,7 @@ private: " if (!p && x==1 || p && p->x==0) {\n" " return;\n" " }\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); { @@ -1687,17 +1683,17 @@ private: " fred->x();\n" "}"; - check(code, false, "test.cpp", false); // non-inconclusive + check(code, false); // non-inconclusive ASSERT_EQUALS("", errout.str()); - check(code, true, "test.cpp", false); // inconclusive + check(code, true); // inconclusive ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning, inconclusive) Possible null pointer dereference: fred - otherwise it is redundant to check it against null.\n", errout.str()); } check("void f(char *s) {\n" // #3358 " if (s==0);\n" " strcpy(a, s?b:c);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); // sizeof @@ -1742,7 +1738,7 @@ private: check("void f() {\n" " int *pointer = NULL;\n" " pointer = func(sizeof pointer[0]);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); } @@ -1751,7 +1747,7 @@ private: check("void f() {\n" " int* p = 0;\n" " return p[4];\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n" "[test.cpp:3]: (error) Null pointer dereference\n", errout.str()); @@ -1805,7 +1801,7 @@ private: check("void f() {\n" " char *s = 0;\n" " printf(\"%s\", s == 0 ? a : s);\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" @@ -1929,20 +1925,20 @@ private: "bool foo() {\n" " PolymorphicA* a = 0;\n" " return typeid(*a) == typeid(*a);\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); check("struct NonPolymorphicA { ~A() {} };\n" "bool foo() {\n" " NonPolymorphicA* a = 0;\n" " return typeid(*a) == typeid(*a);\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); check("bool foo() {\n" " char* c = 0;\n" " return typeid(*c) == typeid(*c);\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); } @@ -2063,7 +2059,7 @@ private: " oss << foo << p;\n" " if(q == 0)\n" " oss << foo << q;\n" - "}", false, "test.cpp", false); + "}", false); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n" "[test.cpp:4]: (error) Possible null pointer dereference: p\n" "[test.cpp:6] -> [test.cpp:5]: (warning) Possible null pointer dereference: q - otherwise it is redundant to check it against null.\n", errout.str()); @@ -2075,7 +2071,7 @@ private: " std::cin >> p;\n" " std::cout << abc << p;\n" " }\n" - "}", false, "test.cpp", false); + "}", false); TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n" "[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n" "[test.cpp:5] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n" @@ -2090,7 +2086,7 @@ private: " char* p2 = 0;\n" " std::cin >> (int)p;\n" // result casted " std::cout << (int)p;\n" - "}", true, "test.cpp", false); + "}", true); ASSERT_EQUALS("", errout.str()); check("void f(const std::string& str) {\n" @@ -2113,7 +2109,7 @@ private: " stringstream out;\n" " out << ((ip >> 0) & 0xFF);\n" " return out.str();\n" - "}n", true, "test.cpp", false); + "}n", true); ASSERT_EQUALS("", errout.str()); // avoid regression from first fix attempt for #5811... check("void deserialize(const std::string &data) {\n" @@ -2121,7 +2117,7 @@ private: "unsigned int len = 0;\n" "if (!(iss >> len))\n" " return;\n" - "}\n", true, "test.cpp", false); + "}\n", true); ASSERT_EQUALS("", errout.str()); } @@ -2322,7 +2318,7 @@ private: check("void f(int *p = 0) {\n" " if (p != 0 && bar())\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int *p) {\n" @@ -2333,7 +2329,7 @@ private: check("void f(int *p = 0) {\n" " if (p != 0)\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int *p = 0) {\n" @@ -2341,19 +2337,19 @@ private: " if (p == 0)\n" " p = &y;\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int *p = 0) {\n" " if (a != 0)\n" " *p = 0;\n" - "}", true, "test.cpp", false); + "}", true); TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Possible null pointer dereference if the default parameter value is used: p\n", "", errout.str()); check("void f(int *p = 0) {\n" " p = a;\n" " *p = 0;\n" // <- don't simplify and verify - "}",false,"test.cpp",false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int *p = 0) {\n" @@ -2367,7 +2363,7 @@ private: " return 0;\n" " }\n" " return *p;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int *p = 0) {\n" @@ -2441,7 +2437,7 @@ private: " init(&p);\n" " }\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void f(int *p = 0) {\n" @@ -2449,7 +2445,7 @@ private: " throw SomeException;\n" " }\n" " *p = 0;\n" - "}", false, "test.cpp", false); + "}"); ASSERT_EQUALS("", errout.str()); check("void foo(int *p = 0) {\n" diff --git a/test/testother.cpp b/test/testother.cpp index 77d889b6c..8a79f2552 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -167,7 +167,7 @@ private: TEST_CASE(test_isSameExpression); } - void check(const char raw_code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool runSimpleChecks=true, Settings* settings = 0, bool verify = true) { + void check(const char raw_code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool runSimpleChecks=true, Settings* settings = 0) { // Clear the error buffer.. errout.str(""); @@ -200,11 +200,7 @@ private: checkOther.runChecks(&tokenizer, settings, this); if (runSimpleChecks) { - const std::string str1(tokenizer.tokens()->stringifyList(0,true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0,true)); - if (verify && str1 != str2) - warnUnsimplified(str1, str2); checkOther.runSimplifiedChecks(&tokenizer, settings, this); } } @@ -415,13 +411,13 @@ private: check("void f(int x) {\n" " int y = 17 / x;\n" " if (x == 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'x==0' is useless or there is division by zero at line 2.\n", errout.str()); check("void f(unsigned int x) {\n" " int y = 17 / x;\n" " if (x != 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'x!=0' is useless or there is division by zero at line 2.\n", errout.str()); // function call @@ -438,7 +434,7 @@ private: " int y = 17 / x;\n" " x = some+calculation;\n" " if (x != 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); { @@ -449,7 +445,7 @@ private: " int y = 17 / x;\n" " do_something();\n" " if (x != 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); // function is called. but don't care, variable is local @@ -459,7 +455,7 @@ private: " int y = 17 / x;\n" " do_something();\n" " if (x != 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'x!=0' is useless or there is division by zero at line 4.\n", errout.str()); } @@ -474,7 +470,7 @@ private: "void f() {\n" " int y = 17 / x;\n" " while (y || x == 0) { x--; }\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); // ticket 5033 segmentation fault (valid code) in CheckOther::checkZeroDivisionOrUselessCondition @@ -497,19 +493,19 @@ private: check("int f(int d) {\n" " int r = (a?b:c) / d;\n" " if (d == 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'd==0' is useless or there is division by zero at line 2.\n", errout.str()); check("int f(int a) {\n" " int r = a ? 1 / a : 0;\n" " if (a == 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); check("int f(int a) {\n" " int r = (a == 0) ? 0 : 1 / a;\n" " if (a == 0) {}\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); } @@ -3220,7 +3216,7 @@ private: " for (i == 0; i < 10; i ++) {\n" " c ++;\n" " }\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead?\n", errout.str()); check("void foo(int c) {\n" @@ -3277,7 +3273,7 @@ private: " for (int i = (x == 0) ? 0 : 5; i < 10; i ++) {\n" " x++;\n" " }\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); check("void foo(int x) {\n" @@ -3359,7 +3355,7 @@ private: check("void f(int x) {\n" " x = (x != 0);" " func(x);\n" - "}", nullptr, false, true, true, nullptr, false); + "}"); ASSERT_EQUALS("", errout.str()); // ticket #3001 - false positive @@ -3759,12 +3755,12 @@ private: void clarifyCalculation() { check("int f(char c) {\n" " return 10 * (c == 0) ? 1 : 2;\n" - "}", nullptr, false, false, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for '*' and '?'.\n", errout.str()); check("void f(char c) {\n" " printf(\"%i\", 10 * (c == 0) ? 1 : 2);\n" - "}", nullptr, false, false, true, nullptr, false); + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for '*' and '?'.\n", errout.str()); check("void f() {\n" @@ -4297,7 +4293,7 @@ private: check("void foo() {\n" " if ((mystrcmp(a, b) == 0) || (mystrcmp(a, b) == 0)) {}\n" - "}", "test.cpp", false, false, true, &settings, false); + "}", "test.cpp", false, false, true, &settings); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'.\n", errout.str()); check("void GetValue() { return rand(); }\n" diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 1ecba779b..594c6add4 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -245,18 +245,6 @@ void TestFixture::run(const std::string &str) run(); } -void TestFixture::warn(const char msg[]) const -{ - warnings << "Warning: " << currentTest << " " << msg << std::endl; -} - -void TestFixture::warnUnsimplified(const std::string& unsimplified, const std::string& simplified) -{ - warn(("Unsimplified code in test case. It looks like this test " - "should either be cleaned up or moved to TestTokenizer or " - "TestSimplifyTokens instead.\nactual=" + unsimplified + "\nexpected=" + simplified).c_str()); -} - void TestFixture::processOptions(const options& args) { quiet_tests = args.quiet(); diff --git a/test/testsuite.h b/test/testsuite.h index 6b56b74ef..3d0654053 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -71,8 +71,6 @@ public: virtual void reportOut(const std::string &outmsg); virtual void reportErr(const ErrorLogger::ErrorMessage &msg); void run(const std::string &str); - void warn(const char msg[]) const; - void warnUnsimplified(const std::string& unsimplified, const std::string& simplified); explicit TestFixture(const std::string &_name); virtual ~TestFixture() { } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6cd6d733b..2adf70115 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -75,7 +75,7 @@ private: TEST_CASE(deadPointer); } - void checkUninitVar(const char code[], const char fname[] = "test.cpp", bool verify = true, bool debugwarnings = false) { + void checkUninitVar(const char code[], const char fname[] = "test.cpp", bool debugwarnings = false) { // Clear the error buffer.. errout.str(""); @@ -86,11 +86,7 @@ private: std::istringstream istr(code); tokenizer.tokenize(istr, fname); - const std::string str1(tokenizer.tokens()->stringifyList(0, true)); tokenizer.simplifyTokenList2(); - const std::string str2(tokenizer.tokens()->stringifyList(0, true)); - if (verify && str1 != str2) - warnUnsimplified(str1, str2); // Check for redundant code.. CheckUninitVar checkuninitvar(&tokenizer, &settings, this); @@ -3439,7 +3435,7 @@ private: checkUninitVar("void f() {\n" // #4911 - bad simplification => don't crash " int a;\n" " do { a=do_something() } while (a);\n" - "}\n", "test.cpp", /*verify=*/true, /*debugwarnings=*/true); + "}\n", "test.cpp", /*debugwarnings=*/true); ASSERT_EQUALS("[test.cpp:3]: (debug) ValueFlow bailout: variable a stopping on }\n", errout.str()); checkUninitVar("void f() {\n" @@ -3758,7 +3754,7 @@ private: " struct flex_array *group;\n" " struct cgroup_taskset tset = { };\n" " do { } while_each_thread(leader, tsk);\n" - "}", "test.cpp", /*verify=*/true, /*debugwarnings=*/false); + "}", "test.cpp", /*debugwarnings=*/false); ASSERT_EQUALS("", errout.str()); // --debug-warnings mode => Debug warning @@ -3768,7 +3764,7 @@ private: " struct flex_array *group;\n" " struct cgroup_taskset tset = { };\n" " do { } while_each_thread(leader, tsk);\n" - "}", "test.cpp", /*verify=*/true, /*debugwarnings=*/true); + "}", "test.cpp", /*debugwarnings=*/true); ASSERT_EQUALS("[test.cpp:6]: (debug) assertion failed '} while ('\n", errout.str()); }