Refactoring: unify same dead code tests with a list.

This commit is contained in:
Edoardo Prezioso 2011-11-08 14:07:22 +01:00
parent aaa77c4709
commit 3abece027e
1 changed files with 91 additions and 153 deletions

View File

@ -24,6 +24,7 @@
#include "settings.h" #include "settings.h"
#include <sstream> #include <sstream>
#include <list>
extern std::ostringstream errout; extern std::ostringstream errout;
@ -158,19 +159,8 @@ private:
// ticket #3138 // ticket #3138
TEST_CASE(goto3); TEST_CASE(goto3);
//remove redundant code after flow control statements //remove dead code after flow control statements
TEST_CASE(return1); TEST_CASE(flowControl);
TEST_CASE(return2);
TEST_CASE(return3);
TEST_CASE(return4);
TEST_CASE(return5);
TEST_CASE(return6);
TEST_CASE(break1);
TEST_CASE(break2);
TEST_CASE(continue1);
TEST_CASE(continue2);
// Simplify nested strcat() calls // Simplify nested strcat() calls
TEST_CASE(strcat1); TEST_CASE(strcat1);
@ -408,7 +398,7 @@ private:
TEST_CASE(removeRedundantFor); TEST_CASE(removeRedundantFor);
} }
std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified) { std::string tok(std::string code, bool simplify = true, Settings::PlatformType type = Settings::Unspecified) {
errout.str(""); errout.str("");
Settings settings; Settings settings;
@ -3089,152 +3079,100 @@ private:
} }
} }
void return1() { void flowControl() {
ASSERT_EQUALS("void f ( ) { return ; }", tok("void f() { return; foo();}")); std::list<std::string> beforedead;
ASSERT_EQUALS("void f ( int n ) { if ( n ) { return ; } foo ( ) ; }",tok("void f(int n) { if (n) return; foo();}")); beforedead.push_back("return");
beforedead.push_back("return 0");
beforedead.push_back("return -1");
beforedead.push_back("goto labels");
beforedead.push_back("break");
beforedead.push_back("continue");
beforedead.push_back("break ; break");
beforedead.push_back("continue ; continue");
ASSERT_EQUALS("int f ( int n ) { switch ( n ) { case 0 : ; return 0 ; default : ; return n ; } return -1 ; }", for (std::list<std::string>::iterator it = beforedead.begin(); it != beforedead.end(); ++it) {
tok("int f(int n) { switch (n) {case 0: return 0; n*=2; default: return n; n*=6;} return -1; foo();}")); {
//ticket #3132 ASSERT_EQUALS("void f ( ) { " + *it + " ; }", tok("void f() { " + *it + "; foo();}"));
ASSERT_EQUALS("void f ( int i ) { goto label ; { label : ; return ; } }",tok("void f (int i) { goto label; switch(i) { label: return; } }")); ASSERT_EQUALS("void f ( int n ) { if ( n ) { " + *it + " ; } foo ( ) ; }",tok("void f(int n) { if (n) { " + *it + "; } foo();}"));
//ticket #3148
ASSERT_EQUALS("void f ( ) { MACRO ( return 0 ) }",tok("void f() { MACRO(return NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( return ; , 0 ) }",tok("void f() { MACRO(return;, NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , return 0 ) }",tok("void f() { MACRO(bar1, return NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( return ; bar2 , foo ) }",tok("void f() { MACRO(return; bar2, foo) }"));
}
void return2() { ASSERT_EQUALS("int f ( int n ) { switch ( n ) { case 0 : ; " + *it + " ; default : ; " + *it + " ; } " + *it + " ; }",
const char code[] = "void f(){ " tok("int f(int n) { switch (n) {case 0: " + *it + "; n*=2; default: " + *it + "; n*=6;} " + *it + "; foo();}"));
" if (k>0) goto label; " //ticket #3132
" return; " ASSERT_EQUALS("void f ( int i ) { goto label ; { label : ; " + *it + " ; } }",tok("void f (int i) { goto label; switch(i) { label: " + *it + "; } }"));
" if (tnt) " //ticket #3148
" { " ASSERT_EQUALS("void f ( ) { MACRO ( " + *it + " ) }",tok("void f() { MACRO(" + *it + ") }"));
" { " ASSERT_EQUALS("void f ( ) { MACRO ( " + *it + " ; , 0 ) }",tok("void f() { MACRO(" + *it + ";, NULL) }"));
" check(); " ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , " + *it + " ) }",tok("void f() { MACRO(bar1, " + *it + ") }"));
" k=0; " ASSERT_EQUALS("void f ( ) { MACRO ( " + *it + " ; bar2 , foo ) }",tok("void f() { MACRO(" + *it + "; bar2, foo) }"));
" } " }
" label: "
" bar(); "
" } "
"}";
ASSERT_EQUALS("void f ( ) { if ( 0 < k ) { goto label ; } return ; { label : ; bar ( ) ; } }",tok(code));
}
void return3() { {
const char code[] = "void foo () {" std::string code = "void f(){ "
" return;" " if (k>0) goto label; "
" {" " " + *it + "; "
" boo();" " if (tnt) "
" while (n) { --n; }" " { "
" {" " { "
" label:" " check(); "
" ok();" " k=0; "
" }" " } "
" }" " label: "
"}"; " bar(); "
ASSERT_EQUALS("void foo ( ) { return ; { { label : ; ok ( ) ; } } }", tok(code)); " } "
} "}";
ASSERT_EQUALS("void f ( ) { if ( 0 < k ) { goto label ; } " + *it + " ; { label : ; bar ( ) ; } }",tok(code));
}
void return4() { {
const char code[] = "int f() { " std::string code = "void foo () {"
"switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return; " " " + *it + ";"
"case 2: return 2; { random(); } tack(); " " {"
"switch(y) { case 1: return 0; case 2: return 7; } " " boo();"
"return 2; } return 3; }"; " while (n) { --n; }"
ASSERT_EQUALS("int f ( ) { switch ( x ) { case 1 : ; return 1 ; case 2 : ; return 2 ; } return 3 ; }",tok(code)); " {"
} " label:"
" ok();"
" }"
" }"
"}";
ASSERT_EQUALS("void foo ( ) { " + *it + " ; { { label : ; ok ( ) ; } } }", tok(code));
}
void return5() { {
const char code[] = "int f() {" std::string code = "int f() { "
"switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return;" "switch (x) { case 1: " + *it + "; bar(); tack; { ticak(); " + *it + " } " + *it + " "
"case 2: switch(y) { case 1: return 0; bar2(); foo(); case 2: return 7; }" "case 2: " + *it + "; { random(); } tack(); "
"return 2; } return 3; }"; "switch(y) { case 1: " + *it + "; case 2: " + *it + "; } "
const char expected[] = "int f ( ) {" "" + *it + "; } " + *it + "; }";
" switch ( x ) { case 1 : ; return 1 ;" ASSERT_EQUALS("int f ( ) { switch ( x ) { case 1 : ; " + *it + " ; case 2 : ; " + *it + " ; } " + *it + " ; }",tok(code));
" case 2 : ; switch ( y ) { case 1 : ; return 0 ; case 2 : ; return 7 ; }" }
" return 2 ; } return 3 ; }";
ASSERT_EQUALS(expected,tok(code));
}
void return6() { {
const char code[] = "void foo () {" std::string code = "int f() {"
" switch (i) { case 0: switch (j) { case 0: return -1; }" "switch (x) { case 1: " + *it + "; bar(); tack; { ticak(); " + *it + "; } " + *it + ";"
" case 1: switch (j) { case -1: return -1; }" "case 2: switch(y) { case 1: " + *it + "; bar2(); foo(); case 2: " + *it + "; }"
" case 2: switch (j) { case -2: return -1; }" "" + *it + "; } " + *it + "; }";
" case 3: if (blah6) return -1; break; } }"; std::string expected = "int f ( ) {"
const char expected[] = "void foo ( ) {" " switch ( x ) { case 1 : ; " + *it + " ;"
" switch ( i ) { case 0 : ; switch ( j ) { case 0 : ; return -1 ; }" " case 2 : ; switch ( y ) { case 1 : ; " + *it + " ; case 2 : ; " + *it + " ; }"
" case 1 : ; switch ( j ) { case -1 : ; return -1 ; }" " " + *it + " ; } " + *it + " ; }";
" case 2 : ; switch ( j ) { case -2 : ; return -1 ; }" ASSERT_EQUALS(expected,tok(code));
" case 3 : ; if ( blah6 ) { return -1 ; } break ; } }"; }
ASSERT_EQUALS(expected, tok(code));
}
void break1() { {
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 10 ; i ++ ) { foo ( i ) ; break ; } }", tok("void f() { int i; for (i=0; i<10; i++) { foo(i); break; bar1(); } }")); std::string code = "void foo () {"
ASSERT_EQUALS("void f ( int n ) { int i ; for ( i = 0 ; i < 10 ; i ++ ) { if ( n ) { break ; } foo ( ) ; } }",tok("void f(int n) { int i; for(i=0; i<10; i++) { if (n) break; foo();}}")); " switch (i) { case 0: switch (j) { case 0: " + *it + "; }"
" case 1: switch (j) { case -1: " + *it + "; }"
ASSERT_EQUALS("int f ( int n ) { switch ( n ) { case 0 : ; break ; default : ; break ; } }", " case 2: switch (j) { case -2: " + *it + "; }"
tok("int f(int n) { switch (n) {case 0: break; n*=2; default: break; n*=6;}}")); " case 3: if (blah6) {" + *it + ";} break; } }";
std::string expected = "void foo ( ) {"
ASSERT_EQUALS("void f ( ) { MACRO ( break ) }",tok("void f() { MACRO(break) }")); " switch ( i ) { case 0 : ; switch ( j ) { case 0 : ; " + *it + " ; }"
ASSERT_EQUALS("void f ( ) { MACRO ( break ; , 0 ) }",tok("void f() { MACRO(break;, NULL) }")); " case 1 : ; switch ( j ) { case -1 : ; " + *it + " ; }"
ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , break ) }",tok("void f() { MACRO(bar1, break) }")); " case 2 : ; switch ( j ) { case -2 : ; " + *it + " ; }"
ASSERT_EQUALS("void f ( ) { MACRO ( break ; bar2 , foo ) }",tok("void f() { MACRO(break; bar2, foo) }")); " case 3 : ; if ( blah6 ) { " + *it + " ; } break ; } }";
} ASSERT_EQUALS(expected, tok(code));
}
void break2() { }
const char code[] = "void f()"
"{ "
" while(tnt) "
" { "
" --tnt; "
" if (k>0) goto label; "
" break; "
" if (tnt) "
" { "
" { "
" check(); "
" k=0; "
" } "
" label: "
" bar(); "
" } "
" } "
"}";
ASSERT_EQUALS("void f ( ) { while ( tnt ) { -- tnt ; if ( 0 < k ) { goto label ; } break ; { label : ; bar ( ) ; } } }",tok(code));
}
void continue1() {
ASSERT_EQUALS("void f ( int n ) { int i ; for ( i = 0 ; i < 10 ; i ++ ) { if ( n ) { continue ; } foo ( ) ; } }",tok("void f(int n) { int i; for(i=0; i<10; i++) { if (n) continue; foo();}}"));
ASSERT_EQUALS("void f ( ) { MACRO ( continue ) }",tok("void f() { MACRO(continue) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( continue ; , 0 ) }",tok("void f() { MACRO(continue;, NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , continue ) }",tok("void f() { MACRO(bar1, continue) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( continue ; bar2 , foo ) }",tok("void f() { MACRO(continue; bar2, foo) }"));
}
void continue2() {
const char code[] = "void f()"
"{ "
" while(tnt) "
" { "
" --tnt; "
" if (k>0) goto label; "
" continue; "
" if (tnt) "
" { "
" { "
" check(); "
" k=0; "
" } "
" label: "
" bar(); "
" } "
" } "
"}";
ASSERT_EQUALS("void f ( ) { while ( tnt ) { -- tnt ; if ( 0 < k ) { goto label ; } continue ; { label : ; bar ( ) ; } } }",tok(code));
} }
void strcat1() { void strcat1() {