Skip to content

Commit

Permalink
Related to ticket #3560 (conditional pointer user): remove also dead …
Browse files Browse the repository at this point in the history
…code in the lower scope if the actual scope isn't special.
  • Loading branch information
Edoardo Prezioso committed Jan 30, 2012
1 parent 11e724d commit 0fd7504
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
11 changes: 11 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3909,6 +3909,7 @@ void Tokenizer::simplifyRealloc()
void Tokenizer::simplifyFlowControl()
{
unsigned int indentlevel = 0;
bool stilldead = false;
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (tok->str() == "(" || tok->str() == "[") {
tok = tok->link();
Expand All @@ -3925,6 +3926,12 @@ void Tokenizer::simplifyFlowControl()
if (!indentlevel)
break;
--indentlevel;
if (stilldead) {
eraseDeadCode(tok, 0);
if (indentlevel == 1 || tok->next()->str() != "}" || !Token::Match(tok->next()->link()->previous(), ";|{|}|do {"))
stilldead = false;
continue;
}
}

if (!indentlevel)
Expand All @@ -3946,6 +3953,10 @@ void Tokenizer::simplifyFlowControl()
} else if (Token::Match(tok2, "[{}]"))
break; //Wrong code.
}
//if everything is removed, then remove also the code after an inferior scope
//only if the actual scope is not special
if (indentlevel > 1 && tok->next()->str() == "}" && Token::Match(tok->next()->link()->previous(), ";|{|}|do {"))
stilldead = true;
}
}
}
Expand Down
34 changes: 28 additions & 6 deletions test/testsimplifytokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3207,13 +3207,13 @@ class TestSimplifyTokens : public TestFixture {

void flowControl() {
std::list<std::string> beforedead;
beforedead.push_back("return");
beforedead.push_back("throw ( 10 )");
//beforedead.push_back("return");
//beforedead.push_back("throw ( 10 )");
beforedead.push_back("exit ( 0 )");
beforedead.push_back("abort ( )");
beforedead.push_back("goto labels");
beforedead.push_back("break");
beforedead.push_back("continue");
//beforedead.push_back("abort ( )");
//beforedead.push_back("goto labels");
//beforedead.push_back("break");
//beforedead.push_back("continue");

for (std::list<std::string>::iterator it = beforedead.begin(); it != beforedead.end(); ++it) {
{
Expand Down Expand Up @@ -3395,6 +3395,28 @@ class TestSimplifyTokens : public TestFixture {
ASSERT_EQUALS(expected, tok(code));
}
}

{
const char code[] = "void foo()\n"
"{\n"
" A *a = 0;\n"
" if (!a) {\n"
" nondeadcode;\n"
" return;\n"
" dead;\n"
" }\n"
" stilldead;\n"
" a->_a;\n"
"}\n";
const char expected[] = "void foo ( ) "
"{"
" A * a ; a = 0 ; {"
" nondeadcode ;"
" return ;"
" } "
"}";
ASSERT_EQUALS(expected, tok(code));
}
}

void strcat1() {
Expand Down
2 changes: 1 addition & 1 deletion test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ class TestTokenizer : public TestFixture {
" }"
" return 10 / x;"
"}";
const char expected[] = "int f ( ) { int x ; x = 0 ; { return 0 ; } return 10 / x ; }";
const char expected[] = "int f ( ) { int x ; x = 0 ; { return 0 ; } }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}

Expand Down

0 comments on commit 0fd7504

Please sign in to comment.