From 8583fcf96e654d7aa33f8bf4be0e71b59d981c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Fri, 24 Feb 2023 21:22:08 +0100 Subject: [PATCH] avoid more cases of returning non-const pointers from `const` objects (#4821) --- gui/mainwindow.cpp | 14 +++++++------- gui/resultsview.cpp | 4 ++-- gui/resultsview.h | 4 ++-- lib/symboldatabase.h | 4 ++-- lib/templatesimplifier.h | 1 + lib/tokenize.cpp | 7 ++++++- lib/tokenize.h | 3 ++- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index eb35e79fb..d68be0bfd 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -335,13 +335,13 @@ void MainWindow::loadSettings() mSettings->value(SETTINGS_WINDOW_HEIGHT, 600).toInt()); } - ShowTypes *types = mUI->mResults->getShowTypes(); - mUI->mActionShowStyle->setChecked(types->isShown(ShowTypes::ShowStyle)); - mUI->mActionShowErrors->setChecked(types->isShown(ShowTypes::ShowErrors)); - mUI->mActionShowWarnings->setChecked(types->isShown(ShowTypes::ShowWarnings)); - mUI->mActionShowPortability->setChecked(types->isShown(ShowTypes::ShowPortability)); - mUI->mActionShowPerformance->setChecked(types->isShown(ShowTypes::ShowPerformance)); - mUI->mActionShowInformation->setChecked(types->isShown(ShowTypes::ShowInformation)); + const ShowTypes &types = mUI->mResults->getShowTypes(); + mUI->mActionShowStyle->setChecked(types.isShown(ShowTypes::ShowStyle)); + mUI->mActionShowErrors->setChecked(types.isShown(ShowTypes::ShowErrors)); + mUI->mActionShowWarnings->setChecked(types.isShown(ShowTypes::ShowWarnings)); + mUI->mActionShowPortability->setChecked(types.isShown(ShowTypes::ShowPortability)); + mUI->mActionShowPerformance->setChecked(types.isShown(ShowTypes::ShowPerformance)); + mUI->mActionShowInformation->setChecked(types.isShown(ShowTypes::ShowInformation)); mUI->mActionShowCppcheck->setChecked(true); mUI->mActionShowClang->setChecked(true); diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index 2ff2e8a46..168ccd33e 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -113,9 +113,9 @@ void ResultsView::clearRecheckFile(const QString &filename) mUI->mTree->clearRecheckFile(filename); } -ShowTypes * ResultsView::getShowTypes() const +const ShowTypes & ResultsView::getShowTypes() const { - return &mUI->mTree->mShowSeverities; + return mUI->mTree->mShowSeverities; } void ResultsView::progress(int value, const QString& description) diff --git a/gui/resultsview.h b/gui/resultsview.h index eaaf4e6e2..0669d351b 100644 --- a/gui/resultsview.h +++ b/gui/resultsview.h @@ -189,7 +189,7 @@ public: * @brief Return checking statistics. * @return Pointer to checking statistics. */ - CheckStatistics *getStatistics() const { + const CheckStatistics *getStatistics() const { return mStatistics; } @@ -197,7 +197,7 @@ public: * @brief Return Showtypes. * @return Pointer to Showtypes. */ - ShowTypes * getShowTypes() const; + const ShowTypes & getShowTypes() const; signals: diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 7b7543c83..b583ba402 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1399,12 +1399,12 @@ public: const Scope *findScopeByName(const std::string& name) const; const Type* findType(const Token *startTok, const Scope *startScope, bool lookOutside = false) const; - Type* findType(const Token *startTok, Scope *startScope, bool lookOutside = false) const { + Type* findType(const Token *startTok, Scope *startScope, bool lookOutside = false) { return const_cast(this->findType(startTok, const_cast(startScope), lookOutside)); } const Scope *findScope(const Token *tok, const Scope *startScope) const; - Scope *findScope(const Token *tok, Scope *startScope) const { + Scope *findScope(const Token *tok, Scope *startScope) { return const_cast(this->findScope(tok, const_cast(startScope))); } diff --git a/lib/templatesimplifier.h b/lib/templatesimplifier.h index d9eb6f1fc..75fa05bc4 100644 --- a/lib/templatesimplifier.h +++ b/lib/templatesimplifier.h @@ -156,6 +156,7 @@ public: mNameToken == rhs.mNameToken && mParamEnd == rhs.mParamEnd && mFlags == rhs.mFlags; } + // TODO: do not return non-const pointer from const object Token * token() const { return mToken; } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4c8105ec8..470a2f0c5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -510,7 +510,7 @@ static Token *splitDefinitionFromTypedef(Token *tok, nonneg int *unnamedCount) * code that generated it deals in some way with functions, then this * function will probably need to be extended to handle a new function * related pattern */ -Token *Tokenizer::processFunc(Token *tok2, bool inOperator) const +const Token *Tokenizer::processFunc(const Token *tok2, bool inOperator) const { if (tok2->next() && tok2->next()->str() != ")" && tok2->next()->str() != ",") { @@ -570,6 +570,11 @@ Token *Tokenizer::processFunc(Token *tok2, bool inOperator) const return tok2; } +Token *Tokenizer::processFunc(Token *tok2, bool inOperator) +{ + return const_cast(processFunc(const_cast(tok2), inOperator)); +} + void Tokenizer::simplifyUsingToTypedef() { if (!isCPP() || mSettings->standards.cpp < Standards::CPP11) diff --git a/lib/tokenize.h b/lib/tokenize.h index 20068a013..0ff6c356c 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -693,7 +693,8 @@ public: Tokenizer &operator=(const Tokenizer &) = delete; private: - Token *processFunc(Token *tok2, bool inOperator) const; + const Token *processFunc(const Token *tok2, bool inOperator) const; + Token *processFunc(Token *tok2, bool inOperator); /** * Get new variable id.