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

View File

@ -592,6 +592,7 @@ private:
} }
void casting() void casting()
{
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
@ -603,6 +604,18 @@ private:
ASSERT_EQUALS(expected, sizeof_(code)); 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));
}
}