diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6c2101628..e187937b4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4638,6 +4638,14 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) // Link < with > createLinks2(); + // Mark C++ casts + for (Token *tok = list.front(); tok; tok = tok->next()) { + if (Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <") && Token::simpleMatch(tok->linkAt(1), "> (")) { + tok = tok->linkAt(1)->next(); + tok->isCast(true); + } + } + // specify array size arraySize(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7e8b90fa7..d83824152 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -487,6 +487,8 @@ private: TEST_CASE(unknownType); // #8952 TEST_CASE(unknownMacroBeforeReturn); + + TEST_CASE(cppcast); } std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) { @@ -8335,6 +8337,22 @@ private: void unknownMacroBeforeReturn() { ASSERT_THROW(tokenizeAndStringify("int f() { X return 0; }"), InternalError); } + + void cppcast() { + const char code[] = "a = const_cast(x);\n" + "a = dynamic_cast(x);\n" + "a = reinterpret_cast(x);\n" + "a = static_cast(x);\n"; + + Settings settings; + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { + ASSERT_EQUALS(tok->str() == "(", tok->isCast()); + } + } }; REGISTER_TEST(TestTokenizer)