From 6955e719cf121fdda359bfa37dd39d574d19f30f Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 16 Oct 2014 10:59:46 +0200 Subject: [PATCH] Collected garbage code tests and moved them to testgarbage.cpp --- lib/tokenize.cpp | 3 +- test/testbufferoverrun.cpp | 22 +------ test/testexceptionsafety.cpp | 6 -- test/testgarbage.cpp | 112 ++++++++++++++++++++++++++++++++++- test/testmemleak.cpp | 8 --- test/testsymboldatabase.cpp | 23 ------- test/testtokenize.cpp | 7 --- test/testuninitvar.cpp | 15 ----- test/testunusedvar.cpp | 32 ++++------ test/testvalueflow.cpp | 19 ------ 10 files changed, 126 insertions(+), 121 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a17040fbc..451defe7c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8398,7 +8398,8 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end) break; } tok->deleteNext(); - } else { //no need to keep the other strings, remove them. + } else { + // no need to keep the other strings, remove them. if (tok->strAt(1) == "while") { if (tok->str() == "}" && tok->link()->strAt(-1) == "do") tok->link()->previous()->deleteThis(); diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index d7f00a488..5bdec0bf0 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -288,12 +288,8 @@ private: TEST_CASE(recursive_long_time); TEST_CASE(crash1); // Ticket #1587 - crash - TEST_CASE(crash2); // Ticket #2607 - crash - TEST_CASE(crash3); // Ticket #3034 - crash - TEST_CASE(crash4); // Ticket #5426 - crash - TEST_CASE(crash5); // TIcket #5595 - crash - - TEST_CASE(garbage1); // Ticket #5203 + TEST_CASE(crash2); // Ticket #3034 - crash + TEST_CASE(crash3); // Ticket #5426 - crash TEST_CASE(executionPaths1); TEST_CASE(executionPaths2); @@ -3749,10 +3745,6 @@ private: } void crash2() { - check("struct C {} {} x"); - } - - void crash3() { check("void a(char *p) {\n" " f( { if(finally_arg); } );\n" "}\n" @@ -3763,19 +3755,11 @@ private: "}"); } - void crash4() { + void crash3() { check("struct b { unknown v[0]; };\n" "void d() { struct b *f; f = malloc(108); }"); } - void crash5() { - check("static f() { int i; int source[1] = { 1 }; for (i = 0; i < 4; i++) (u, if (y u.x e)) }", true, "test.cpp", false); // Garbage code - } - - void garbage1() { // Ticket #5203 - check("int f ( int* r ) { { int s[2] ; f ( s ) ; if ( ) } }"); - } - void epcheck(const char code[], const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index 3ce134027..b4f09d59f 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -49,8 +49,6 @@ private: TEST_CASE(nothrowAttributeThrow); TEST_CASE(nothrowAttributeThrow2); // #5703 TEST_CASE(nothrowDeclspecThrow); - - TEST_CASE(garbage); } void check(const char code[], bool inconclusive = false) { @@ -413,10 +411,6 @@ private: check("const char *func() __attribute((nothrow)); void func1() { return 0; }\n"); ASSERT_EQUALS("", errout.str()); } - - void garbage() { - check("{ } A() { delete }"); // #6080 - } }; REGISTER_TEST(TestExceptionSafety) diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 8723ef3a0..ce7c67a6e 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -53,8 +53,21 @@ private: TEST_CASE(garbageCode10); // #6127 TEST_CASE(garbageCode11); TEST_CASE(garbageCode12); + TEST_CASE(garbageCode13); // Ticket #2607 - crash + TEST_CASE(garbageCode14); // TIcket #5595 - crash + TEST_CASE(garbageCode15); // Ticket #5203 + TEST_CASE(garbageCode16); + TEST_CASE(garbageCode17); + TEST_CASE(garbageCode18); + TEST_CASE(garbageCode19); + TEST_CASE(garbageCode20); + TEST_CASE(garbageCode21); + TEST_CASE(garbageCode22); + TEST_CASE(garbageCode23); - TEST_CASE(astGarbage); + TEST_CASE(garbageValueFlow); + TEST_CASE(garbageSymbolDatabase); + TEST_CASE(garbageAST); } std::string checkCode(const char code[], const char filename[] = "test.cpp") { @@ -258,7 +271,102 @@ private: checkCode("{ g; S (void) { struct } { } int &g; }"); } - void astGarbage() { + void garbageCode13() { + checkCode("struct C {} {} x"); + } + + void garbageCode14() { + checkCode("static f() { int i; int source[1] = { 1 }; for (i = 0; i < 4; i++) (u, if (y u.x e)) }"); // Garbage code + } + + void garbageCode15() { // Ticket #5203 + checkCode("int f ( int* r ) { { int s[2] ; f ( s ) ; if ( ) } }"); + } + + void garbageCode16() { + checkCode("{ } A() { delete }"); // #6080 + } + + void garbageCode17() { + ASSERT_THROW(checkCode("void h(int l) {\n" + " while\n" // Don't crash (#3870) + "}"), InternalError); + } + + void garbageCode18() { + ASSERT_THROW(checkCode("switch(){case}"), InternalError); + } + + void garbageCode19() { + // ticket #3512 - Don't crash on garbage code + ASSERT_EQUALS("p = const", checkCode("1 *p = const")); + } + + void garbageCode20() { + // #3953 (valgrind errors on garbage code) + ASSERT_EQUALS("void f ( 0 * ) ;", checkCode("void f ( 0 * ) ;")); + } + + void garbageCode21() { + // Ticket #3486 - Don't crash garbage code + checkCode("void f()\n" + "{\n" + " (\n" + " x;\n" + " int a, a2, a2*x; if () ;\n" + " )\n" + "}"); + } + + void garbageCode22() { + // Ticket #3480 - Don't crash garbage code + ASSERT_THROW(checkCode("int f()\n" + "{\n" + " return if\n" + "}"), InternalError); + } + + void garbageCode23() { + //garbage code : don't crash (#3481) + checkCode("{\n" + " if (1) = x\n" + " else abort s[2]\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + + void garbageValueFlow() { + // #6089 + const char* code = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n" + "{\n" + " (foo(s, , 2, , , 5, , 7)) abort()\n" + "}\n"; + ASSERT_THROW(checkCode(code), InternalError); + + // #6106 + code = " f { int i ; b2 , [ ] ( for ( i = 0 ; ; ) ) }"; + checkCode(code); + + // 6122 survive garbage code + code = "; { int i ; for ( i = 0 ; = 123 ; ) - ; }"; + checkCode(code); + } + + void garbageSymbolDatabase() { + checkCode("void f( { u = 1 ; } ) { }"); + + checkCode("{ }; void namespace A::f; { g() { int } }"); + + ASSERT_THROW(checkCode("class Foo {}; class Bar : public Foo"), InternalError); + + ASSERT_THROW(checkCode("YY_DECL { switch (yy_act) {\n" + " case 65: YY_BREAK\n" + " case YY_STATE_EOF(block):\n" + " yyterminate(); \n" + "} }"), InternalError); // #5663 + } + + void garbageAST() { checkCode("--"); // don't crash checkCode("N 1024 float a[N], b[N + 3], c[N]; void N; (void) i;\n" diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index e5f4cd5a1..975acec43 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -361,8 +361,6 @@ private: // #1879 non regression test case TEST_CASE(trac1879); - TEST_CASE(garbageCode); - TEST_CASE(ptrptr); // test that the cfg files are configured correctly @@ -4213,12 +4211,6 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str()); } - void garbageCode() { - ASSERT_THROW(check("void h(int l) {\n" - " while\n" // Don't crash (#3870) - "}"), InternalError); - } - void ptrptr() { check("void f() {\n" " char *p;\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 2902a715e..5c8a4a67e 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -231,8 +231,6 @@ private: TEST_CASE(isImplicitlyVirtual); - TEST_CASE(garbage); - TEST_CASE(isFunction); // UNKNOWN_MACRO(a,b) { .. } TEST_CASE(findFunction1); @@ -2119,27 +2117,6 @@ private: } } - void garbage() { - { - GET_SYMBOL_DB("void f( { u = 1 ; } ) { }"); - (void)db; - } - { - GET_SYMBOL_DB("{ }; void namespace A::f; { g() { int } }"); - (void)db; - } - { - ASSERT_THROW(createSymbolDatabase("class Foo {}; class Bar : public Foo"), InternalError); - } - { - ASSERT_THROW(createSymbolDatabase("YY_DECL { switch (yy_act) {\n" - " case 65: YY_BREAK\n" - " case YY_STATE_EOF(block):\n" - " yyterminate(); \n" - "} }"), InternalError); // #5663 - } - } - void isFunction() { // #5602 - UNKNOWN_MACRO(a,b) { .. } GET_SYMBOL_DB("TEST(a,b) {\n" " std::vector messages;\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7b888d1d3..0f3cde7b1 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5203,8 +5203,6 @@ private: //with unhandled MACRO() code ASSERT_EQUALS("void f(){ MACRO( ab: b=0;, foo)}", labels_("void f() { MACRO(ab: b=0;, foo)}")); ASSERT_EQUALS("void f(){ MACRO( bar, ab:{&(* b. x)=0;})}", labels_("void f() { MACRO(bar, ab: {&(*b.x)=0;})}")); - //don't crash with garbage code - ASSERT_THROW(labels_("switch(){case}"), InternalError); } void simplifyInitVar() { @@ -5946,8 +5944,6 @@ private: tokenizeAndStringify("int f(int a) { return 0 * a; }", true)); ASSERT_EQUALS("bool f ( int i ) { switch ( i ) { case 15 : ; return true ; } }", tokenizeAndStringify("bool f(int i) { switch (i) { case 10 + 5: return true; } }", true)); - // ticket #3512 - Don't crash on garbage code - ASSERT_EQUALS("p = const", tokenizeAndStringify("1 *p = const", true)); // ticket #3576 - False positives in boolean expressions ASSERT_EQUALS("int foo ( ) { return 1 ; }", @@ -5962,9 +5958,6 @@ private: // ticket #3964 - simplify numeric calculations in tokenization ASSERT_EQUALS("char a [ 10 ] ;", tokenizeAndStringify("char a[9+1];")); - - // #3953 (valgrind errors on garbage code) - ASSERT_EQUALS("void f ( 0 * ) ;", tokenizeAndStringify("void f ( 0 * ) ;")); } void simplifyCompoundAssignment() { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6e77f7df6..0493ab59d 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -585,12 +585,6 @@ private: "}"); ASSERT_EQUALS("", errout.str()); - // Ticket #3480 - Don't crash garbage code - ASSERT_THROW(checkUninitVar("int f()\n" - "{\n" - " return if\n" - "}"), InternalError); - // Ticket #3873 (false positive) checkUninitVar("MachineLoopRange *MachineLoopRanges::getLoopRange(const MachineLoop *Loop) {\n" " MachineLoopRange *&Range = Cache[Loop];\n" @@ -2536,15 +2530,6 @@ private: "}"); ASSERT_EQUALS("", errout.str()); - // Ticket #3486 - Don't crash garbage code - checkUninitVar2("void f()\n" - "{\n" - " (\n" - " x;\n" - " int a, a2, a2*x; if () ;\n" - " )\n" - "}"); - // Ticket #3890 - False positive for std::map checkUninitVar2("void f() {\n" " std::map x;\n" diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 59e072d19..2b8ee0a7a 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -90,13 +90,12 @@ private: TEST_CASE(localvar38); TEST_CASE(localvar39); // ticket #3454 TEST_CASE(localvar40); // ticket #3473 - TEST_CASE(localvar41); // ticket #3481 - TEST_CASE(localvar42); // ticket #3603 - TEST_CASE(localvar43); // ticket #3742 - TEST_CASE(localvar44); // ticket #3602 - TEST_CASE(localvar45); // ticket #4020 - TEST_CASE(localvar46); // ticket #4899 - TEST_CASE(localvar47); // ticket #5491 (C++11 style initialization) + TEST_CASE(localvar41); // ticket #3603 + TEST_CASE(localvar42); // ticket #3742 + TEST_CASE(localvar43); // ticket #3602 + TEST_CASE(localvar44); // ticket #4020 + TEST_CASE(localvar45); // ticket #4899 + TEST_CASE(localvar46); // ticket #5491 (C++11 style initialization) TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias3); // ticket #1639 @@ -1786,15 +1785,6 @@ private: } void localvar41() { - //garbage code : don't crash - functionVariableUsage("{\n" - " if (1) = x\n" - " else abort s[2]\n" - "}"); - ASSERT_EQUALS("", errout.str()); - } - - void localvar42() { // #3603 - false positive 'x is assigned a value that is never used' functionVariableUsage("int f() {\n" " int x = 1;\n" @@ -1804,7 +1794,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void localvar43() { // #3742 + void localvar42() { // #3742 functionVariableUsage("float g_float = 1;\n" "extern void SomeTestFunc(float);\n" "void MyFuncError()\n" @@ -1836,7 +1826,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void localvar44() { // ticket #3602 (false positive) + void localvar43() { // ticket #3602 (false positive) functionVariableUsage("void bar()\n" "{\n" " int * piArray = NULL;\n" @@ -1862,7 +1852,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void localvar45() { // #4020 - FP + void localvar44() { // #4020 - FP functionVariableUsage("void func() {\n" " int *sp_mem[2] = { 0x00, 0x00 };\n" " int src = 1, dst = 2;\n" @@ -1871,7 +1861,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void localvar46() { // #4899 - FP + void localvar45() { // #4899 - FP functionVariableUsage("int func() {\n" " int a = 123;\n" " int b = (short)-a;;\n" @@ -1880,7 +1870,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void localvar47() { // #5491/#5494 + void localvar46() { // #5491/#5494 functionVariableUsage("int func() {\n" " int i = 0;\n" " int j{i};\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 50b6149ad..b09f891bc 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -62,8 +62,6 @@ private: TEST_CASE(valueFlowForLoop); TEST_CASE(valueFlowSubFunction); TEST_CASE(valueFlowFunctionReturn); - - TEST_CASE(garbage); } bool testValueOfX(const char code[], unsigned int linenr, int value) { @@ -1324,23 +1322,6 @@ private: "}"; ASSERT_EQUALS(15, valueOfTok(code, "*").intvalue); } - - void garbage() { - // #6089 - const char* code = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n" - "{\n" - " (foo(s, , 2, , , 5, , 7)) abort()\n" - "}\n"; - ASSERT_THROW(valueOfTok(code, "*"), InternalError); - - // #6106 - code = " f { int i ; b2 , [ ] ( for ( i = 0 ; ; ) ) }"; - valueOfTok(code, "*"); - - // 6122 survive garbage code - code = "; { int i ; for ( i = 0 ; = 123 ; ) - ; }"; - valueOfTok(code, "*"); - } }; REGISTER_TEST(TestValueFlow)