From e89c03da92fa1be78722b77c5a88b4d312983913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 27 May 2009 20:06:19 +0200 Subject: [PATCH] Fix ticket 308 (cppcheck msg:: invalid number of ((). Cant process file) --- src/tokenize.cpp | 14 +++++++++++++- test/testtokenize.cpp | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 90abd554b..3315835a5 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1776,8 +1776,20 @@ bool Tokenizer::simplifyCasts() else if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <")) { - while (tok->next() && tok->next()->str() != ">") + unsigned int level = 0; + while (tok->next()) + { + const Token *next = tok->next(); + if (next->str() == "<") + ++level; + else if (next->str() == ">") + { + --level; + if (level == 0) + break; + } tok->deleteNext(); + } tok->deleteNext(); tok->deleteNext(); Token *tok2 = tok; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index f7d1c0210..bea96f1a3 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -66,6 +66,7 @@ private: TEST_CASE(longtok); TEST_CASE(removeCast1); + TEST_CASE(removeCast2); TEST_CASE(inlineasm); @@ -228,6 +229,24 @@ private: ASSERT_EQUALS(std::string(" int * f ( int * ) ;"), ostr.str()); } + // remove static_cast.. + void removeCast2() + { + const char code[] = "t = (static_cast *>(&p));\n"; + + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyCasts(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(std::string(" t = ( & p ) ;"), ostr.str()); + } + void inlineasm() {