Really fixed #4024. Now the simplification won't be done on non-executive scopes.

This commit is contained in:
Edoardo Prezioso 2012-08-10 17:43:09 +02:00
parent 70de691551
commit 1d8240356b
2 changed files with 85 additions and 44 deletions

View File

@ -3615,9 +3615,22 @@ void Tokenizer::simplifyRealloc()
void Tokenizer::simplifyFlowControl() void Tokenizer::simplifyFlowControl()
{ {
for (Token *begin = list.front(); begin; begin = begin->next()) {
if (begin->str() == "(" || begin->str() == "[" ||
(begin->str() == "{" && begin->previous() && begin->strAt(-1) == "="))
begin = begin->link();
//function scope
if (!Token::simpleMatch(begin, ") {") && !Token::Match(begin, ") %var% {"))
continue;
Token *end = begin->linkAt(1+(begin->next()->str() == "{" ? 0 : 1));
unsigned int indentlevel = 0; unsigned int indentlevel = 0;
bool stilldead = false; bool stilldead = false;
for (Token *tok = list.front(); tok; tok = tok->next()) {
for (Token *tok = begin; tok != end; tok = tok->next()) {
if (tok->str() == "(" || tok->str() == "[") { if (tok->str() == "(" || tok->str() == "[") {
tok = tok->link(); tok = tok->link();
continue; continue;
@ -3649,8 +3662,9 @@ void Tokenizer::simplifyFlowControl()
eraseDeadCode(tok, 0); eraseDeadCode(tok, 0);
} else if (Token::Match(tok,"return|goto") || } else if (Token::Match(tok,"return|goto") ||
(Token::Match(tok,"exit|abort") && !Token::Match(tok->previous(),"%type%")) || (Token::Match(tok,"exit|abort")) ||
(tok->str() == "throw" && !isC())) { (tok->str() == "throw" && !isC())) {
//TODO: ensure that we exclude user-defined 'exit|abort|throw', except for 'noreturn'
//catch the first ';' //catch the first ';'
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(" || tok2->str() == "[") { if (tok2->str() == "(" || tok2->str() == "[") {
@ -3668,6 +3682,8 @@ void Tokenizer::simplifyFlowControl()
stilldead = true; stilldead = true;
} }
} }
begin = end;
}
} }

View File

@ -3491,6 +3491,31 @@ private:
"} ;"; "} ;";
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected, tok(code));
} }
{
const char code[] = "class abort { };\n"
"\n"
"class Fred\n"
"{\n"
" public:\n"
" bool foo() const { return f; }\n"
" abort exit();\n"
"\n"
" private:\n"
"bool f;\n"
"};\n";
const char expected[] = "class abort { } ; "
"class Fred "
"{"
" public:"
" bool foo ( ) const { return f ; }"
" abort exit ( ) ;"
""
" private:"
" bool f ; "
"} ;";
ASSERT_EQUALS(expected, tok(code));
}
} }
void strcat1() { void strcat1() {