Tokenizer: Fixed problem when simplifying casts

This commit is contained in:
Daniel Marjamäki 2009-06-19 19:25:56 +02:00
parent 5f7d88b36c
commit 03944f1b96
2 changed files with 41 additions and 25 deletions

View File

@ -1939,23 +1939,25 @@ bool Tokenizer::simplifyCasts()
else if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
{
Token *tok2 = tok->next();
unsigned int level = 0;
while (tok->next())
while (tok2)
{
const Token *next = tok->next();
if (next->str() == "<")
if (tok2->str() == "<")
++level;
else if (next->str() == ">")
else if (tok2->str() == ">")
{
--level;
if (level == 0)
break;
}
tok->deleteNext();
tok2 = tok2->next();
}
tok->deleteNext();
tok->deleteNext();
Token *tok2 = tok;
if (Token::simpleMatch(tok2, "> ("))
{
Token::eraseTokens(tok, tok2->tokAt(2));
tok2 = tok;
int parlevel = 0;
while (tok2->next() && parlevel >= 0)
{
@ -1971,6 +1973,7 @@ bool Tokenizer::simplifyCasts()
ret = true;
}
}
}
return ret;
}

View File

@ -592,6 +592,7 @@ private:
}
void casting()
{
{
const char code[] = "void f()\n"
"{\n"
@ -603,6 +604,18 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
{
const char code[] = "void f()\n"
"{\n"
" p = const_cast<char *> qtu ();\n"
"}\n";
const std::string expected(" void f ( ) { p = const_cast < char * > qtu ( ) ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}
}