From 8bb812548c609218d4e4fd38a07fecd5ee39d613 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 5 Oct 2009 00:12:12 +0300 Subject: [PATCH] Fix #777 (Tokenizer: double casting simplified incorrectly) http://sourceforge.net/apps/trac/cppcheck/ticket/777 --- src/tokenize.cpp | 21 +++++++++++---------- test/testsimplifytokens.cpp | 4 ++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 78eac509f..870430060 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2437,23 +2437,24 @@ void Tokenizer::simplifyCasts() { 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") - continue; - - // 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; + break; // Remove cast.. 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(); unsigned int level = 0; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index ee6be8f62..d0bbe8140 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -165,6 +165,10 @@ private: 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)")); }