Fix #760 (Tokenizer: Goto not simplified)
http://sourceforge.net/apps/trac/cppcheck/ticket/760
This commit is contained in:
parent
500dedb8a3
commit
bb1a9a07e4
|
@ -3388,15 +3388,24 @@ void Tokenizer::simplifyGoto()
|
||||||
{
|
{
|
||||||
// Is this label at the end..
|
// Is this label at the end..
|
||||||
bool end = false;
|
bool end = false;
|
||||||
|
int lev = 0;
|
||||||
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
if (tok2->str() == "}")
|
if (tok2->str() == "}")
|
||||||
{
|
{
|
||||||
end = true;
|
--lev;
|
||||||
break;
|
if (lev < 0)
|
||||||
|
{
|
||||||
|
end = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (tok2->str() == "{")
|
||||||
|
{
|
||||||
|
++lev;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tok2->str() == "{" || Token::Match(tok2, "%var% :"))
|
if (Token::Match(tok2, "%var% :"))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -3435,11 +3444,23 @@ void Tokenizer::simplifyGoto()
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
std::list<Token*> links;
|
std::list<Token*> links;
|
||||||
|
int lev = 0;
|
||||||
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
if (tok2->str() == "}")
|
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;
|
ret = true;
|
||||||
token->insertToken(tok2->str().c_str());
|
token->insertToken(tok2->str().c_str());
|
||||||
token = token->next();
|
token = token->next();
|
||||||
|
|
|
@ -1506,6 +1506,35 @@ private:
|
||||||
ASSERT_EQUALS(expect, tok(code));
|
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 ; } ;";
|
const char code[] = "class NoLabels { bool varOne : 1 ; bool varTwo : 1 ; } ;";
|
||||||
ASSERT_EQUALS(code, tok(code));
|
ASSERT_EQUALS(code, tok(code));
|
||||||
|
|
Loading…
Reference in New Issue