Fix #760 (Tokenizer: Goto not simplified)

http://sourceforge.net/apps/trac/cppcheck/ticket/760
This commit is contained in:
Reijo Tomperi 2009-09-30 14:30:53 +03:00
parent 500dedb8a3
commit bb1a9a07e4
2 changed files with 55 additions and 5 deletions

View File

@ -3388,15 +3388,24 @@ void Tokenizer::simplifyGoto()
{
// Is this label at the end..
bool end = false;
int lev = 0;
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
{
if (tok2->str() == "}")
{
end = true;
break;
--lev;
if (lev < 0)
{
end = true;
break;
}
}
else if (tok2->str() == "{")
{
++lev;
}
if (tok2->str() == "{" || Token::Match(tok2, "%var% :"))
if (Token::Match(tok2, "%var% :"))
{
break;
}
@ -3435,11 +3444,23 @@ void Tokenizer::simplifyGoto()
bool ret = false;
std::list<Token*> links;
int lev = 0;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "}")
break;
if (tok2->str() == "return")
{
--lev;
if (lev < 0)
break;
continue;
}
if (tok2->str() == "{")
{
++lev;
continue;
}
else if (tok2->str() == "return")
ret = true;
token->insertToken(tok2->str().c_str());
token = token->next();

View File

@ -1506,6 +1506,35 @@ private:
ASSERT_EQUALS(expect, tok(code));
}
{
const char code[] = "void foo()\n"
"{\n"
" if (a())\n"
" goto out;\n"
" b();\n"
"out:\n"
" if (c())\n"
" {\n"
" d();\n"
" }\n"
"}";
const char expect[] = "void foo ( ) "
"{ "
"if ( a ( ) ) "
"{ "
"if ( c ( ) ) "
"{ d ( ) ; } "
"return ; "
"} "
"b ( ) ; "
"if ( c ( ) ) "
"{ d ( ) ; } "
"}";
ASSERT_EQUALS(expect, tok(code));
}
{
const char code[] = "class NoLabels { bool varOne : 1 ; bool varTwo : 1 ; } ;";
ASSERT_EQUALS(code, tok(code));