From a16f360a827917e4f95faeb1d4376220eda22519 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 6 Apr 2024 12:02:23 +0200 Subject: [PATCH] Cleanup in createSymbolDatabaseIncompleteVars() (#6238) --- lib/symboldatabase.cpp | 75 +++--------------------------------------- lib/tokenize.cpp | 22 +++++++++---- lib/tokenize.h | 5 +++ test/testgarbage.cpp | 4 +-- 4 files changed, 28 insertions(+), 78 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 65d47954e4f..b4d30fa2591 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1451,67 +1451,6 @@ void SymbolDatabase::createSymbolDatabaseEnums() void SymbolDatabase::createSymbolDatabaseIncompleteVars() { - // TODO: replace with Keywords::getX() - static const std::unordered_set cpp20keywords = { - "alignas", - "alignof", - "axiom", - "co_await", - "co_return", - "co_yield", - "concept", - "synchronized", - "consteval", - "reflexpr", - "requires", - }; - static const std::unordered_set cppkeywords = { - "asm", - "auto", - "catch", - "char", - "class", - "const", - "constexpr", - "decltype", - "default", - "do", - "enum", - "explicit", - "export", - "extern", - "final", - "friend", - "inline", - "mutable", - "namespace", - "new", - "noexcept", - "nullptr", - "override", - "private", - "protected", - "public", - "register", - "sizeof", - "static", - "static_assert", - "struct", - "template", - "this", - "thread_local", - "throw", - "try", - "typedef", - "typeid", - "typename", - "union", - "using", - "virtual", - "void", - "volatile", - "NULL", - }; for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) { const Scope * scope = tok->scope(); if (!scope) @@ -1524,11 +1463,14 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars() tok = tok->link(); continue; } - if (Token::Match(tok, "catch|typeid (")) { + if (tok->isCpp() && (Token::Match(tok, "catch|typeid (") || + Token::Match(tok, "static_cast|dynamic_cast|const_cast|reinterpret_cast"))) { tok = tok->linkAt(1); continue; } - if (!(tok->isNameOnly() || tok->isKeyword())) + if (tok->str() == "NULL") + continue; + if (tok->isKeyword() || !tok->isNameOnly()) continue; if (tok->type()) continue; @@ -1544,16 +1486,9 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars() tok = tok->linkAt(1); continue; } - if (tok->isKeyword()) - continue; // Skip goto labels if (Token::simpleMatch(tok->previous(), "goto")) continue; - // TODO: handle all C/C++ standards - if (cppkeywords.count(tok->str()) > 0) - continue; - if (tok->isCpp() && (mSettings.standards.cpp >= Standards::CPP20) && cpp20keywords.count(tok->str()) > 0) - continue; std::string fstr = tok->str(); const Token* ftok = tok->previous(); while (Token::simpleMatch(ftok, "::")) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5cd1834f9ef..a6389023056 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5414,6 +5414,21 @@ void Tokenizer::createLinks2() } } +void Tokenizer::markCppCasts() +{ + if (isC()) + return; + for (Token* tok = list.front(); tok; tok = tok->next()) { + if (Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast")) { + if (!Token::simpleMatch(tok->next(), "<") || !Token::simpleMatch(tok->linkAt(1), "> (")) + syntaxError(tok); + tok = tok->linkAt(1)->next(); + tok->isCast(true); + } + } + +} + void Tokenizer::sizeofAddParentheses() { for (Token *tok = list.front(); tok; tok = tok->next()) { @@ -5807,12 +5822,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) 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); - } - } + markCppCasts(); // specify array size arraySize(); diff --git a/lib/tokenize.h b/lib/tokenize.h index d6155fefb6b..5ec2a10571b 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -387,6 +387,11 @@ class CPPCHECKLIB Tokenizer { */ void createLinks2(); + /** + * Set isCast() for C++ casts + */ + void markCppCasts(); + public: /** Syntax error */ diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index f3e27620995..36827ffd0a3 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -1744,8 +1744,8 @@ class TestGarbage : public TestFixture { ASSERT_THROW(checkCode("struct{}*"), InternalError); // don't crash } void garbageCode224() { - checkCode("void f(){ auto* b = dynamic_cast