Related to ticket #3560 (conditional pointer user): remove also dead code in the lower scope if the actual scope isn't special.

This commit is contained in:
Edoardo Prezioso 2012-01-30 19:04:35 +01:00
parent 11e724df46
commit 0fd7504295
3 changed files with 40 additions and 7 deletions

View File

@ -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();
@ -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)
@ -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;
}
}
}

View File

@ -3207,13 +3207,13 @@ private:
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) {
{
@ -3395,6 +3395,28 @@ private:
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() {

View File

@ -2064,7 +2064,7 @@ private:
" }"
" 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));
}