Collected garbage code tests and moved them to testgarbage.cpp

This commit is contained in:
PKEuS 2014-10-16 10:59:46 +02:00
parent 9dd3bce98b
commit 6955e719cf
10 changed files with 126 additions and 121 deletions

View File

@ -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();

View File

@ -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("");

View File

@ -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)

View File

@ -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"

View File

@ -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"

View File

@ -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<int> messages;\n"

View File

@ -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() {

View File

@ -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<int,bool> x;\n"

View File

@ -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"

View File

@ -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)