From 628b8942aa678b93166e3e7b7838481ccbeb5c34 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 16 Jan 2024 14:29:53 +0100 Subject: [PATCH] pass `ErrorLogger` by reference into `SymbolDatabase` --- lib/symboldatabase.cpp | 25 ++++++++++++------------- lib/symboldatabase.h | 4 ++-- lib/tokenize.cpp | 6 ++++-- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index b4d30fa2591..4f86497551c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -52,7 +52,7 @@ #include //--------------------------------------------------------------------------- -SymbolDatabase::SymbolDatabase(Tokenizer& tokenizer, const Settings& settings, ErrorLogger* errorLogger) +SymbolDatabase::SymbolDatabase(Tokenizer& tokenizer, const Settings& settings, ErrorLogger& errorLogger) : mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger) { if (!mTokenizer.tokens()) @@ -169,10 +169,9 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() // find all scopes for (const Token *tok = mTokenizer.tokens(); tok; tok = tok ? tok->next() : nullptr) { // #5593 suggested to add here: - if (mErrorLogger) - mErrorLogger->reportProgress(mTokenizer.list.getSourceFilePath(), - "SymbolDatabase", - tok->progressValue()); + mErrorLogger.reportProgress(mTokenizer.list.getSourceFilePath(), + "SymbolDatabase", + tok->progressValue()); // Locate next class if ((tok->isCpp() && tok->isKeyword() && ((Token::Match(tok, "class|struct|union|namespace ::| %name% final| {|:|::|<") && @@ -2087,14 +2086,14 @@ void SymbolDatabase::validateExecutableScopes() const for (std::size_t i = 0; i < functions; ++i) { const Scope* const scope = functionScopes[i]; const Function* const function = scope->function; - if (mErrorLogger && scope->isExecutable() && !function) { + if (scope->isExecutable() && !function) { const std::list callstack(1, scope->classDef); const std::string msg = std::string("Executable scope '") + scope->classDef->str() + "' with unknown function."; const ErrorMessage errmsg(callstack, &mTokenizer.list, Severity::debug, "symbolDatabaseWarning", msg, Certainty::normal); - mErrorLogger->reportErr(errmsg); + mErrorLogger.reportErr(errmsg); } } } @@ -2152,7 +2151,7 @@ void SymbolDatabase::debugSymbolDatabase() const for (const Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) { if (tok->astParent() && tok->astParent()->getTokenDebug() == tok->getTokenDebug()) continue; - if (mErrorLogger && tok->getTokenDebug() == TokenDebug::ValueType) { + if (tok->getTokenDebug() == TokenDebug::ValueType) { std::string msg = "Value type is "; ErrorPath errorPath; @@ -2164,7 +2163,7 @@ void SymbolDatabase::debugSymbolDatabase() const msg += "missing"; } errorPath.emplace_back(tok, ""); - mErrorLogger->reportErr( + mErrorLogger.reportErr( {errorPath, &mTokenizer.list, Severity::debug, "valueType", msg, CWE{0}, Certainty::normal}); } } @@ -3584,27 +3583,27 @@ std::string Type::name() const void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const { - if (tok && mSettings.debugwarnings && mErrorLogger) { + if (tok && mSettings.debugwarnings) { const std::list locationList(1, tok); const ErrorMessage errmsg(locationList, &mTokenizer.list, Severity::debug, type, msg, Certainty::normal); - mErrorLogger->reportErr(errmsg); + mErrorLogger.reportErr(errmsg); } } void SymbolDatabase::returnImplicitIntError(const Token *tok) const { - if (tok && mSettings.severity.isEnabled(Severity::portability) && (tok->isC() && mSettings.standards.c != Standards::C89) && mErrorLogger) { + if (tok && mSettings.severity.isEnabled(Severity::portability) && (tok->isC() && mSettings.standards.c != Standards::C89)) { const std::list locationList(1, tok); const ErrorMessage errmsg(locationList, &mTokenizer.list, Severity::portability, "returnImplicitInt", "Omitted return type of function '" + tok->str() + "' defaults to int, this is not supported by ISO C99 and later standards.", Certainty::normal); - mErrorLogger->reportErr(errmsg); + mErrorLogger.reportErr(errmsg); } } diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index ffa0777ecf5..901991313d4 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1319,7 +1319,7 @@ class CPPCHECKLIB ValueType { class CPPCHECKLIB SymbolDatabase { friend class TestSymbolDatabase; public: - SymbolDatabase(Tokenizer& tokenizer, const Settings& settings, ErrorLogger* errorLogger); + SymbolDatabase(Tokenizer& tokenizer, const Settings& settings, ErrorLogger& errorLogger); ~SymbolDatabase(); /** @brief Information about all namespaces/classes/structures */ @@ -1467,7 +1467,7 @@ class CPPCHECKLIB SymbolDatabase { Tokenizer& mTokenizer; const Settings &mSettings; - ErrorLogger *mErrorLogger; + ErrorLogger &mErrorLogger; /** variable symbol table */ std::vector mVariableList; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 40b74271b1b..bb83f88d9a5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9987,8 +9987,10 @@ void Tokenizer::simplifyBorland() void Tokenizer::createSymbolDatabase() { - if (!mSymbolDatabase) - mSymbolDatabase = new SymbolDatabase(*this, mSettings, mErrorLogger); + if (!mSymbolDatabase) { + assert(mErrorLogger != nullptr); + mSymbolDatabase = new SymbolDatabase(*this, mSettings, *mErrorLogger); + } mSymbolDatabase->validate(); }