Remove redundant simplifyDeadCode call inside the Tokenizer::tokenize and move the relative tests in the file 'testsimplifytokens.cpp'.

This commit is contained in:
Edoardo Prezioso 2011-10-13 12:26:27 +02:00
parent 82366918ff
commit 16506f0a8c
3 changed files with 81 additions and 82 deletions

View File

@ -2773,8 +2773,6 @@ bool Tokenizer::tokenize(std::istream &code,
}
}
simplifyDeadCode();
_tokens->assignProgressValues();
removeRedundantSemicolons();

View File

@ -157,6 +157,13 @@ private:
TEST_CASE(goto1);
TEST_CASE(goto2);
//remove redundant code after the 'return ;' statement
TEST_CASE(return1);
TEST_CASE(return2);
TEST_CASE(return3);
TEST_CASE(return4);
TEST_CASE(return5);
// Simplify nested strcat() calls
TEST_CASE(strcat1);
TEST_CASE(strcat2);
@ -2984,6 +2991,78 @@ private:
ASSERT_EQUALS(code, tok(code));
}
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();}"));
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 ; switch ( i ) { 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) }"));
}
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));
}
void return3()
{
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));
}
void return4()
{
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));
}
void return5()
{
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));
}
void strcat1()
{
const char code[] = "; strcat(strcat(strcat(strcat(strcat(strcat(dst, \"this \"), \"\"), \"is \"), \"a \"), \"test\"), \".\");";

View File

@ -358,13 +358,6 @@ private:
TEST_CASE(simplifyIfAddBraces); // ticket # 2739 (segmentation fault)
//remove redundant code after the 'return ;' statement
TEST_CASE(simplifyDeadCodereturn1);
TEST_CASE(simplifyDeadCodereturn2);
TEST_CASE(simplifyDeadCodereturn3);
TEST_CASE(simplifyDeadCodereturn4);
TEST_CASE(simplifyDeadCodereturn5);
TEST_CASE(platformWin32);
TEST_CASE(platformWin32A);
TEST_CASE(platformWin32W);
@ -1587,7 +1580,8 @@ private:
"{"
" int i ;"
" for ( i = 0 ; i < 10 ; ++ i ) { }"
" return ; "
" return ;"
" str [ i ] = 0 ; "
"}",
simplifyKnownVariables(code));
}
@ -5969,78 +5963,6 @@ private:
}
}
void simplifyDeadCodereturn1()
{
ASSERT_EQUALS("void f ( ) { return ; }", tokenizeAndStringify("void f() { return; foo();}"));
ASSERT_EQUALS("void f ( int n ) { if ( n ) { return ; } foo ( ) ; }",tokenizeAndStringify("void f(int n) { if (n) return; foo();}"));
ASSERT_EQUALS("int f ( int n ) { switch ( n ) { case 0 : return 0 ; default : ; return n ; } return -1 ; }",
tokenizeAndStringify("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 ; switch ( i ) { label : ; return ; } }",tokenizeAndStringify("void f (int i) { goto label; switch(i) { label: return; } }"));
//ticket #3148
ASSERT_EQUALS("void f ( ) { MACRO ( return 0 ) }",tokenizeAndStringify("void f() { MACRO(return NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( return ; , 0 ) }",tokenizeAndStringify("void f() { MACRO(return;, NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( bar1 , return 0 ) }",tokenizeAndStringify("void f() { MACRO(bar1, return NULL) }"));
ASSERT_EQUALS("void f ( ) { MACRO ( return ; bar2 , foo ) }",tokenizeAndStringify("void f() { MACRO(return; bar2, foo) }"));
}
void simplifyDeadCodereturn2()
{
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 ( ) ; } }",simplifyKnownVariables(code));
}
void simplifyDeadCodereturn3()
{
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 ; }",simplifyKnownVariables(code));
}
void simplifyDeadCodereturn4()
{
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,simplifyKnownVariables(code));
}
void simplifyDeadCodereturn5()
{
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, simplifyKnownVariables(code));
}
void platformWin32()
{
const char code[] = "unsigned int sizeof_short = sizeof(short);"