Fix #777 (Tokenizer: double casting simplified incorrectly)

http://sourceforge.net/apps/trac/cppcheck/ticket/777
This commit is contained in:
Reijo Tomperi 2009-10-05 00:12:12 +03:00
parent 498beb7d9a
commit 8bb812548c
2 changed files with 15 additions and 10 deletions

View File

@ -2437,23 +2437,24 @@ void Tokenizer::simplifyCasts()
{ {
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (Token::Match(tok->next(), "( %type% *| )") || Token::Match(tok->next(), "( %type% %type% *| )")) while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") ||
Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%"))
{ {
if (tok->isName() && tok->str() != "return") if (tok->isName() && tok->str() != "return")
continue; break;
// Is it a cast of some variable?
const Token *tok2 = tok->tokAt(3);
while (tok2 && tok2->str() != ")")
tok2 = tok2->next();
if (!Token::Match(tok2, ") %var%"))
continue;
// Remove cast.. // Remove cast..
Token::eraseTokens(tok, tok->next()->link()->next()); Token::eraseTokens(tok, tok->next()->link()->next());
if (tok->str() == ")" && tok->link()->previous())
{
// If there was another cast before this, go back
// there to check it also. e.g. "(int)(char)x"
tok = tok->link()->previous();
}
} }
else if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <")) if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
{ {
Token *tok2 = tok->next(); Token *tok2 = tok->next();
unsigned int level = 0; unsigned int level = 0;

View File

@ -165,6 +165,10 @@ private:
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected, tok(code));
} }
ASSERT_EQUALS("if ( * a )", tok("if ((char)*a)"));
ASSERT_EQUALS("if ( & a )", tok("if ((int)&a)"));
ASSERT_EQUALS("if ( * a )", tok("if ((unsigned int)(unsigned char)*a)"));
} }