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