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,36 +1939,39 @@ 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();
}
tok->deleteNext();
tok->deleteNext();
Token *tok2 = tok;
int parlevel = 0;
while (tok2->next() && parlevel >= 0)
{
tok2 = tok2->next();
if (Token::simpleMatch(tok2->next(), "("))
++parlevel;
else if (Token::simpleMatch(tok2->next(), ")"))
--parlevel;
}
if (tok2->next())
tok2->deleteNext();
ret = true;
if (Token::simpleMatch(tok2, "> ("))
{
Token::eraseTokens(tok, tok2->tokAt(2));
tok2 = tok;
int parlevel = 0;
while (tok2->next() && parlevel >= 0)
{
tok2 = tok2->next();
if (Token::simpleMatch(tok2->next(), "("))
++parlevel;
else if (Token::simpleMatch(tok2->next(), ")"))
--parlevel;
}
if (tok2->next())
tok2->deleteNext();
ret = true;
}
}
}

View File

@ -593,14 +593,27 @@ private:
void casting()
{
const char code[] = "void f()\n"
"{\n"
"for (int i = 0; i < static_cast<int>(3); ++i) {}\n"
"}\n";
{
const char code[] = "void f()\n"
"{\n"
"for (int i = 0; i < static_cast<int>(3); ++i) {}\n"
"}\n";
const std::string expected(" void f ( ) { for ( int i = 0 ; i < 3 ; ++ i ) { } }");
const std::string expected(" void f ( ) { for ( int i = 0 ; i < 3 ; ++ i ) { } }");
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));
}
}