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,25 +3079,37 @@ 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();}")); {
ASSERT_EQUALS("void f ( ) { " + *it + " ; }", tok("void f() { " + *it + "; foo();}"));
ASSERT_EQUALS("void f ( int n ) { if ( n ) { " + *it + " ; } foo ( ) ; }",tok("void f(int n) { if (n) { " + *it + "; } foo();}"));
ASSERT_EQUALS("int f ( int n ) { switch ( n ) { case 0 : ; " + *it + " ; default : ; " + *it + " ; } " + *it + " ; }",
tok("int f(int n) { switch (n) {case 0: " + *it + "; n*=2; default: " + *it + "; n*=6;} " + *it + "; foo();}"));
//ticket #3132 //ticket #3132
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 i ) { goto label ; { label : ; " + *it + " ; } }",tok("void f (int i) { goto label; switch(i) { label: " + *it + "; } }"));
//ticket #3148 //ticket #3148
ASSERT_EQUALS("void f ( ) { MACRO ( return 0 ) }",tok("void f() { MACRO(return NULL) }")); ASSERT_EQUALS("void f ( ) { MACRO ( " + *it + " ) }",tok("void f() { MACRO(" + *it + ") }"));
ASSERT_EQUALS("void f ( ) { MACRO ( return ; , 0 ) }",tok("void f() { MACRO(return;, NULL) }")); ASSERT_EQUALS("void f ( ) { MACRO ( " + *it + " ; , 0 ) }",tok("void f() { MACRO(" + *it + ";, NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , return 0 ) }",tok("void f() { MACRO(bar1, return NULL) }")); ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , " + *it + " ) }",tok("void f() { MACRO(bar1, " + *it + ") }"));
ASSERT_EQUALS("void f ( ) { MACRO ( return ; bar2 , foo ) }",tok("void f() { MACRO(return; bar2, foo) }")); ASSERT_EQUALS("void f ( ) { MACRO ( " + *it + " ; bar2 , foo ) }",tok("void f() { MACRO(" + *it + "; bar2, foo) }"));
} }
void return2() { {
const char code[] = "void f(){ " std::string code = "void f(){ "
" if (k>0) goto label; " " if (k>0) goto label; "
" return; " " " + *it + "; "
" if (tnt) " " if (tnt) "
" { " " { "
" { " " { "
@ -3118,12 +3120,12 @@ private:
" bar(); " " bar(); "
" } " " } "
"}"; "}";
ASSERT_EQUALS("void f ( ) { if ( 0 < k ) { goto label ; } return ; { label : ; bar ( ) ; } }",tok(code)); ASSERT_EQUALS("void f ( ) { if ( 0 < k ) { goto label ; } " + *it + " ; { label : ; bar ( ) ; } }",tok(code));
} }
void return3() { {
const char code[] = "void foo () {" std::string code = "void foo () {"
" return;" " " + *it + ";"
" {" " {"
" boo();" " boo();"
" while (n) { --n; }" " while (n) { --n; }"
@ -3133,108 +3135,44 @@ private:
" }" " }"
" }" " }"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { return ; { { label : ; ok ( ) ; } } }", tok(code)); ASSERT_EQUALS("void foo ( ) { " + *it + " ; { { label : ; ok ( ) ; } } }", tok(code));
} }
void return4() { {
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: return 2; { random(); } tack(); " "case 2: " + *it + "; { random(); } tack(); "
"switch(y) { case 1: return 0; case 2: return 7; } " "switch(y) { case 1: " + *it + "; case 2: " + *it + "; } "
"return 2; } return 3; }"; "" + *it + "; } " + *it + "; }";
ASSERT_EQUALS("int f ( ) { switch ( x ) { case 1 : ; return 1 ; case 2 : ; return 2 ; } return 3 ; }",tok(code)); ASSERT_EQUALS("int f ( ) { switch ( x ) { case 1 : ; " + *it + " ; case 2 : ; " + *it + " ; } " + *it + " ; }",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: switch(y) { case 1: " + *it + "; bar2(); foo(); case 2: " + *it + "; }"
"return 2; } return 3; }"; "" + *it + "; } " + *it + "; }";
const char expected[] = "int f ( ) {" std::string expected = "int f ( ) {"
" switch ( x ) { case 1 : ; return 1 ;" " switch ( x ) { case 1 : ; " + *it + " ;"
" case 2 : ; switch ( y ) { case 1 : ; return 0 ; case 2 : ; return 7 ; }" " case 2 : ; switch ( y ) { case 1 : ; " + *it + " ; case 2 : ; " + *it + " ; }"
" return 2 ; } return 3 ; }"; " " + *it + " ; } " + *it + " ; }";
ASSERT_EQUALS(expected,tok(code)); ASSERT_EQUALS(expected,tok(code));
} }
void return6() { {
const char code[] = "void foo () {" std::string code = "void foo () {"
" switch (i) { case 0: switch (j) { case 0: return -1; }" " switch (i) { case 0: switch (j) { case 0: " + *it + "; }"
" case 1: switch (j) { case -1: return -1; }" " case 1: switch (j) { case -1: " + *it + "; }"
" case 2: switch (j) { case -2: return -1; }" " case 2: switch (j) { case -2: " + *it + "; }"
" case 3: if (blah6) return -1; break; } }"; " case 3: if (blah6) {" + *it + ";} break; } }";
const char expected[] = "void foo ( ) {" std::string expected = "void foo ( ) {"
" switch ( i ) { case 0 : ; switch ( j ) { case 0 : ; return -1 ; }" " switch ( i ) { case 0 : ; switch ( j ) { case 0 : ; " + *it + " ; }"
" case 1 : ; switch ( j ) { case -1 : ; return -1 ; }" " case 1 : ; switch ( j ) { case -1 : ; " + *it + " ; }"
" case 2 : ; switch ( j ) { case -2 : ; return -1 ; }" " case 2 : ; switch ( j ) { case -2 : ; " + *it + " ; }"
" case 3 : ; if ( blah6 ) { return -1 ; } break ; } }"; " case 3 : ; if ( blah6 ) { " + *it + " ; } break ; } }";
ASSERT_EQUALS(expected, tok(code)); 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(); } }"));
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();}}"));
ASSERT_EQUALS("int f ( int n ) { switch ( n ) { case 0 : ; break ; default : ; break ; } }",
tok("int f(int n) { switch (n) {case 0: break; n*=2; default: break; n*=6;}}"));
ASSERT_EQUALS("void f ( ) { MACRO ( break ) }",tok("void f() { MACRO(break) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( break ; , 0 ) }",tok("void f() { MACRO(break;, NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , break ) }",tok("void f() { MACRO(bar1, break) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( break ; bar2 , foo ) }",tok("void f() { MACRO(break; bar2, foo) }"));
} }
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() {