From 087fd7911a5c40a3a887daa7ce2d4a363b45d0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 15 Apr 2024 20:21:32 +0200 Subject: [PATCH] avoid even more unchecked pointer dereferences / changed some pointers to references (#6296) --- gui/mainwindow.cpp | 22 +++++++++---------- gui/mainwindow.h | 4 ++-- lib/astutils.cpp | 33 +++++++++++++--------------- lib/astutils.h | 6 +++--- lib/checkautovariables.cpp | 2 +- lib/checkother.cpp | 4 ++-- lib/checkstl.cpp | 2 +- lib/findtoken.h | 10 ++++----- lib/forwardanalyzer.cpp | 4 ++-- lib/fwdanalysis.cpp | 2 +- lib/programmemory.cpp | 18 ++++++++-------- lib/programmemory.h | 8 +++---- lib/symboldatabase.cpp | 10 ++++----- lib/valueflow.cpp | 44 +++++++++++++++++++------------------- test/testastutils.cpp | 2 +- 15 files changed, 83 insertions(+), 88 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 5d79794784c..eb2905d3475 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -823,24 +823,24 @@ void MainWindow::addIncludeDirs(const QStringList &includeDirs, Settings &result } } -Library::Error MainWindow::loadLibrary(Library *library, const QString &filename) +Library::Error MainWindow::loadLibrary(Library &library, const QString &filename) { Library::Error ret; // Try to load the library from the project folder.. if (mProjectFile) { QString path = QFileInfo(mProjectFile->getFilename()).canonicalPath(); - ret = library->load(nullptr, (path+"/"+filename).toLatin1()); + ret = library.load(nullptr, (path+"/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; } // Try to load the library from the application folder.. const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); - ret = library->load(nullptr, (appPath+"/"+filename).toLatin1()); + ret = library.load(nullptr, (appPath+"/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; - ret = library->load(nullptr, (appPath+"/cfg/"+filename).toLatin1()); + ret = library.load(nullptr, (appPath+"/cfg/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; @@ -848,10 +848,10 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename // Try to load the library from FILESDIR/cfg.. const QString filesdir = FILESDIR; if (!filesdir.isEmpty()) { - ret = library->load(nullptr, (filesdir+"/cfg/"+filename).toLatin1()); + ret = library.load(nullptr, (filesdir+"/cfg/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; - ret = library->load(nullptr, (filesdir+filename).toLatin1()); + ret = library.load(nullptr, (filesdir+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; } @@ -860,10 +860,10 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename // Try to load the library from the cfg subfolder.. const QString datadir = getDataDir(); if (!datadir.isEmpty()) { - ret = library->load(nullptr, (datadir+"/"+filename).toLatin1()); + ret = library.load(nullptr, (datadir+"/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; - ret = library->load(nullptr, (datadir+"/cfg/"+filename).toLatin1()); + ret = library.load(nullptr, (datadir+"/cfg/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) return ret; } @@ -871,7 +871,7 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename return ret; } -bool MainWindow::tryLoadLibrary(Library *library, const QString& filename) +bool MainWindow::tryLoadLibrary(Library &library, const QString& filename) { const Library::Error error = loadLibrary(library, filename); if (error.errorcode != Library::ErrorCode::OK) { @@ -972,7 +972,7 @@ QPair MainWindow::getCppcheckSettings() // default to --check-level=normal for GUI for now result.setCheckLevel(Settings::CheckLevel::normal); - const bool std = tryLoadLibrary(&result.library, "std.cfg"); + const bool std = tryLoadLibrary(result.library, "std.cfg"); if (!std) { QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir= at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.\n\nAnalysis is aborted.").arg("std.cfg")); return {false, {}}; @@ -1022,7 +1022,7 @@ QPair MainWindow::getCppcheckSettings() for (const QString& library : libraries) { result.libraries.emplace_back(library.toStdString()); const QString filename = library + ".cfg"; - tryLoadLibrary(&result.library, filename); + tryLoadLibrary(result.library, filename); } for (const SuppressionList::Suppression &suppression : mProjectFile->getCheckingSuppressions()) { diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 5214b690ddc..b0721213545 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -392,7 +392,7 @@ private slots: * @param filename filename (no path) * @return error code */ - Library::Error loadLibrary(Library *library, const QString &filename); + Library::Error loadLibrary(Library &library, const QString &filename); /** * @brief Tries to load library file, prints message on error @@ -400,7 +400,7 @@ private slots: * @param filename filename (no path) * @return True if no error */ - bool tryLoadLibrary(Library *library, const QString& filename); + bool tryLoadLibrary(Library &library, const QString& filename); QString loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon); diff --git a/lib/astutils.cpp b/lib/astutils.cpp index bd832a8b852..105d780fd88 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -304,15 +304,12 @@ Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok return tok->valueType()->container->getYield(ftok2->str()); } -Library::Container::Yield astFunctionYield(const Token* tok, const Settings* settings, const Token** ftok) +Library::Container::Yield astFunctionYield(const Token* tok, const Settings& settings, const Token** ftok) { if (!tok) return Library::Container::Yield::NO_YIELD; - if (!settings) - return Library::Container::Yield::NO_YIELD; - - const auto* function = settings->library.getFunction(tok); + const auto* function = settings.library.getFunction(tok); if (!function) return Library::Container::Yield::NO_YIELD; @@ -635,7 +632,7 @@ static std::vector getParentMembers(const Token* tok) return result; } -const Token* getParentLifetime(const Token* tok, const Library* library) +const Token* getParentLifetime(const Token* tok, const Library& library) { std::vector members = getParentMembers(tok); if (members.size() < 2) @@ -647,7 +644,7 @@ const Token* getParentLifetime(const Token* tok, const Library* library) return var->isLocal() || var->isArgument(); if (Token::simpleMatch(tok2, "[")) return true; - return isTemporary(tok2, library); + return isTemporary(tok2, &library); }); if (it == members.rend()) return tok; @@ -2127,18 +2124,18 @@ bool isUniqueExpression(const Token* tok) return isUniqueExpression(tok->astOperand2()); } -static bool isEscaped(const Token* tok, bool functionsScope, const Library* library) +static bool isEscaped(const Token* tok, bool functionsScope, const Library& library) { - if (library && library->isnoreturn(tok)) + if (library.isnoreturn(tok)) return true; if (functionsScope) return Token::simpleMatch(tok, "throw"); return Token::Match(tok, "return|throw"); } -static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library* library) +static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library& library) { - if (library && library->isnoreturn(tok)) + if (library.isnoreturn(tok)) return true; if (functionsScope) return Token::simpleMatch(tok, "throw"); @@ -2162,7 +2159,7 @@ bool isEscapeFunction(const Token* ftok, const Library* library) return false; } -static bool hasNoreturnFunction(const Token* tok, const Library* library, const Token** unknownFunc) +static bool hasNoreturnFunction(const Token* tok, const Library& library, const Token** unknownFunc) { if (!tok) return false; @@ -2176,12 +2173,12 @@ static bool hasNoreturnFunction(const Token* tok, const Library* library, const return true; if (function->isAttributeNoreturn()) return true; - } else if (library && library->isnoreturn(ftok)) { + } else if (library.isnoreturn(ftok)) { return true; } else if (Token::Match(ftok, "exit|abort")) { return true; } - if (unknownFunc && !function && library && library->functions.count(library->getFunctionName(ftok)) == 0) + if (unknownFunc && !function && library.functions.count(library.getFunctionName(ftok)) == 0) *unknownFunc = ftok; return false; } @@ -2192,7 +2189,7 @@ static bool hasNoreturnFunction(const Token* tok, const Library* library, const return false; } -bool isReturnScope(const Token* const endToken, const Library* library, const Token** unknownFunc, bool functionScope) +bool isReturnScope(const Token* const endToken, const Library& library, const Token** unknownFunc, bool functionScope) { if (!endToken || endToken->str() != "}") return false; @@ -3005,9 +3002,9 @@ namespace { }; struct ExpressionChangedSkipDeadCode { - const Library* library; + const Library& library; const std::function(const Token* tok)>* evaluate; - ExpressionChangedSkipDeadCode(const Library* library, + ExpressionChangedSkipDeadCode(const Library& library, const std::function(const Token* tok)>& evaluate) : library(library), evaluate(&evaluate) {} @@ -3036,7 +3033,7 @@ const Token* findExpressionChangedSkipDeadCode(const Token* expr, int depth) { return findExpressionChangedImpl( - expr, start, end, settings, depth, ExpressionChangedSkipDeadCode{&settings->library, evaluate}); + expr, start, end, settings, depth, ExpressionChangedSkipDeadCode{settings->library, evaluate}); } const Token* getArgumentStart(const Token* ftok) diff --git a/lib/astutils.h b/lib/astutils.h index 6c296b4e9ee..6426225253e 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -173,7 +173,7 @@ bool astIsContainerString(const Token* tok); Library::Container::Action astContainerAction(const Token* tok, const Token** ftok = nullptr); Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok = nullptr); -Library::Container::Yield astFunctionYield(const Token* tok, const Settings* settings, const Token** ftok = nullptr); +Library::Container::Yield astFunctionYield(const Token* tok, const Settings& settings, const Token** ftok = nullptr); /** Is given token a range-declaration in a range-based for loop */ bool astIsRangeBasedForDecl(const Token* tok); @@ -209,7 +209,7 @@ const Token* astParentSkipParens(const Token* tok); const Token* getParentMember(const Token * tok); const Token* getParentLifetime(const Token* tok); -const Token* getParentLifetime(const Token* tok, const Library* library); +const Token* getParentLifetime(const Token* tok, const Library& library); std::vector getParentValueTypes(const Token* tok, const Settings* settings = nullptr, @@ -304,7 +304,7 @@ bool isEscapeFunction(const Token* ftok, const Library* library); /** Is scope a return scope (scope will unconditionally return) */ CPPCHECKLIB bool isReturnScope(const Token* const endToken, - const Library* library = nullptr, + const Library& library, const Token** unknownFunc = nullptr, bool functionScope = false); diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 7434fb3be3a..17242326490 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -598,7 +598,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token continue; if (!printInconclusive && val.isInconclusive()) continue; - const Token* parent = getParentLifetime(val.tokvalue, &mSettings->library); + const Token* parent = getParentLifetime(val.tokvalue, mSettings->library); if (!exprs.insert(parent).second) continue; for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, escape || isAssignedToNonLocal(tok))) { diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9823957e525..453495af520 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3802,10 +3802,10 @@ void CheckOther::checkComparePointers() continue; if (var1->isRValueReference() || var2->isRValueReference()) continue; - if (const Token* parent2 = getParentLifetime(v2.tokvalue, &mSettings->library)) + if (const Token* parent2 = getParentLifetime(v2.tokvalue, mSettings->library)) if (var1 == parent2->variable()) continue; - if (const Token* parent1 = getParentLifetime(v1.tokvalue, &mSettings->library)) + if (const Token* parent1 = getParentLifetime(v1.tokvalue, mSettings->library)) if (var2 == parent1->variable()) continue; comparePointersError(tok, &v1, &v2); diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 35218e43a06..e581a36fc47 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1121,7 +1121,7 @@ void CheckStl::invalidContainer() const Scope* s = tok2->scope(); if (!s) continue; - if (isReturnScope(s->bodyEnd, &mSettings->library)) + if (isReturnScope(s->bodyEnd, mSettings->library)) continue; invalidContainerLoopError(r.ftok, tok, r.errorPath); bail = true; diff --git a/lib/findtoken.h b/lib/findtoken.h index d3404758b16..f75845fd813 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -79,7 +79,7 @@ template )> -bool findTokensSkipDeadCodeImpl(const Library* library, +bool findTokensSkipDeadCodeImpl(const Library& library, T* start, const Token* end, const Predicate& pred, @@ -169,7 +169,7 @@ bool findTokensSkipDeadCodeImpl(const Library* library, } template )> -std::vector findTokensSkipDeadCode(const Library* library, +std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred, @@ -190,13 +190,13 @@ std::vector findTokensSkipDeadCode(const Library* library, } template )> -std::vector findTokensSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred) +std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred) { return findTokensSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } template )> -T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate) +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate) { T* result = nullptr; (void)findTokensSkipDeadCodeImpl( @@ -213,7 +213,7 @@ T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, con } template )> -T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred) +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred) { return findTokenSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index c5cb8eaf3ee..2572ccc0613 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -311,7 +311,7 @@ namespace { for (const Token* tok=start; tok != end; tok = tok->previous()) { if (Token::simpleMatch(tok, "}")) { const Token* ftok = nullptr; - const bool r = isReturnScope(tok, &settings.library, &ftok); + const bool r = isReturnScope(tok, settings.library, &ftok); if (r) return true; } @@ -321,7 +321,7 @@ namespace { bool isEscapeScope(const Token* endBlock, bool& unknown) const { const Token* ftok = nullptr; - const bool r = isReturnScope(endBlock, &settings.library, &ftok); + const bool r = isReturnScope(endBlock, settings.library, &ftok); if (!r && ftok) unknown = true; return r; diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index 264153d1fd0..c261b39ea96 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -236,7 +236,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token * } } tok = bodyStart->link(); - if (isReturnScope(tok, &mLibrary)) + if (isReturnScope(tok, mLibrary)) return Result(Result::Type::BAILOUT); if (Token::simpleMatch(tok, "} else {")) tok = tok->linkAt(2); diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 62a1c2b2a2b..49b11310104 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -271,7 +271,7 @@ static bool isBasicForLoop(const Token* tok) return true; } -void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Token* endTok, const Settings* settings, bool then) +static void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Token* endTok, const Settings* settings, bool then) { auto eval = [&](const Token* t) -> std::vector { if (!t) @@ -427,14 +427,14 @@ static void removeModifiedVars(ProgramMemory& pm, const Token* tok, const Token* static ProgramMemory getInitialProgramState(const Token* tok, const Token* origin, - const Settings* settings, + const Settings& settings, const ProgramMemory::Map& vars = ProgramMemory::Map {}) { ProgramMemory pm; if (origin) { fillProgramMemoryFromConditions(pm, origin, nullptr); const ProgramMemory state = pm; - fillProgramMemoryFromAssignments(pm, tok, settings, state, vars); + fillProgramMemoryFromAssignments(pm, tok, &settings, state, vars); removeModifiedVars(pm, tok, origin); } return pm; @@ -534,15 +534,15 @@ ProgramMemory ProgramMemoryState::get(const Token* tok, const Token* ctx, const return local.state; } -ProgramMemory getProgramMemory(const Token* tok, const Token* expr, const ValueFlow::Value& value, const Settings* settings) +ProgramMemory getProgramMemory(const Token* tok, const Token* expr, const ValueFlow::Value& value, const Settings& settings) { ProgramMemory programMemory; programMemory.replace(getInitialProgramState(tok, value.tokvalue, settings)); programMemory.replace(getInitialProgramState(tok, value.condition, settings)); - fillProgramMemoryFromConditions(programMemory, tok, settings); + fillProgramMemoryFromConditions(programMemory, tok, &settings); programMemory.setValue(expr, value); const ProgramMemory state = programMemory; - fillProgramMemoryFromAssignments(programMemory, tok, settings, state, {{expr, value}}); + fillProgramMemoryFromAssignments(programMemory, tok, &settings, state, {{expr, value}}); return programMemory; } @@ -1257,7 +1257,7 @@ namespace { int fdepth = 4; int depth = 10; - explicit Executor(ProgramMemory* pm = nullptr, const Settings* settings = nullptr) : pm(pm), settings(settings) {} + Executor(ProgramMemory* pm, const Settings* settings) : pm(pm), settings(settings) {} static ValueFlow::Value unknown() { return ValueFlow::Value::unknown(); @@ -1748,9 +1748,9 @@ static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Sett return ex.execute(expr); } -std::vector execute(const Scope* scope, ProgramMemory& pm, const Settings* settings) +std::vector execute(const Scope* scope, ProgramMemory& pm, const Settings& settings) { - Executor ex{&pm, settings}; + Executor ex{&pm, &settings}; return ex.execute(scope); } diff --git a/lib/programmemory.h b/lib/programmemory.h index b746e2e8dd7..85703455b5f 100644 --- a/lib/programmemory.h +++ b/lib/programmemory.h @@ -161,8 +161,6 @@ struct ProgramMemory { Map mValues; }; -void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Token* endTok, const Settings* settings, bool then); - struct ProgramMemoryState { ProgramMemory state; std::map origins; @@ -182,13 +180,13 @@ struct ProgramMemoryState { ProgramMemory get(const Token* tok, const Token* ctx, const ProgramMemory::Map& vars) const; }; -std::vector execute(const Scope* scope, ProgramMemory& pm, const Settings* settings); +std::vector execute(const Scope* scope, ProgramMemory& pm, const Settings& settings); void execute(const Token* expr, ProgramMemory& programMemory, MathLib::bigint* result, bool* error, - const Settings* settings = nullptr); + const Settings* settings); /** * Is condition always false when variable has given value? @@ -207,7 +205,7 @@ bool conditionIsTrue(const Token* condition, ProgramMemory pm, const Settings* s /** * Get program memory by looking backwards from given token. */ -ProgramMemory getProgramMemory(const Token* tok, const Token* expr, const ValueFlow::Value& value, const Settings* settings); +ProgramMemory getProgramMemory(const Token* tok, const Token* expr, const ValueFlow::Value& value, const Settings& settings); ValueFlow::Value evaluateLibraryFunction(const std::unordered_map& args, const std::string& returnValue, diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 4a883977ee2..b2d3605f409 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1522,7 +1522,7 @@ void SymbolDatabase::createSymbolDatabaseEscapeFunctions() continue; if (Token::findsimplematch(scope.bodyStart, "return", scope.bodyEnd)) continue; - function->isEscapeFunction(isReturnScope(scope.bodyEnd, &mSettings.library, nullptr, true)); + function->isEscapeFunction(isReturnScope(scope.bodyEnd, mSettings.library, nullptr, true)); } } @@ -2306,7 +2306,7 @@ void Variable::evaluate(const Settings* settings) if (!settings) return; - const Library * const lib = &settings->library; + const Library & lib = settings->library; // TODO: ValueType::parseDecl() is also performing a container lookup bool isContainer = false; @@ -2367,10 +2367,10 @@ void Variable::evaluate(const Settings* settings) std::string strtype = mTypeStartToken->str(); for (const Token *typeToken = mTypeStartToken; Token::Match(typeToken, "%type% :: %type%"); typeToken = typeToken->tokAt(2)) strtype += "::" + typeToken->strAt(2); - setFlag(fIsClass, !lib->podtype(strtype) && !mTypeStartToken->isStandardType() && !isEnumType() && !isPointer() && strtype != "..."); + setFlag(fIsClass, !lib.podtype(strtype) && !mTypeStartToken->isStandardType() && !isEnumType() && !isPointer() && strtype != "..."); setFlag(fIsStlType, Token::simpleMatch(mTypeStartToken, "std ::")); setFlag(fIsStlString, ::isStlStringType(mTypeStartToken)); - setFlag(fIsSmartPointer, mTypeStartToken->isCpp() && lib->isSmartPointer(mTypeStartToken)); + setFlag(fIsSmartPointer, mTypeStartToken->isCpp() && lib.isSmartPointer(mTypeStartToken)); } if (mAccess == AccessControl::Argument) { tok = mNameToken; @@ -7599,7 +7599,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to continue; } - const auto yield = astFunctionYield(tok->previous(), &mSettings); + const auto yield = astFunctionYield(tok->previous(), mSettings); if (yield == Library::Container::Yield::START_ITERATOR || yield == Library::Container::Yield::END_ITERATOR || yield == Library::Container::Yield::ITERATOR) { diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index dc826e60290..6e7e79c4c1b 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4040,7 +4040,7 @@ static void valueFlowForwardLifetime(Token * tok, TokenList &tokenlist, ErrorLog // TODO: handle `[` if (Token::simpleMatch(parent->astOperand1(), ".")) { const Token* parentLifetime = - getParentLifetime(parent->astOperand1()->astOperand2(), &settings.library); + getParentLifetime(parent->astOperand1()->astOperand2(), settings.library); if (parentLifetime && parentLifetime->exprId() > 0) { valueFlowForward(nextExpression, endOfVarScope, parentLifetime, std::move(values), tokenlist, errorLogger, settings); } @@ -5049,7 +5049,7 @@ static void valueFlowLifetime(TokenList &tokenlist, ErrorLogger &errorLogger, co // Skip if its a free function that doesnt yield an iterator to the container if (Token::Match(parent->previous(), "%name% (") && !contains({Library::Container::Yield::START_ITERATOR, Library::Container::Yield::END_ITERATOR}, - astFunctionYield(parent->previous(), &settings))) + astFunctionYield(parent->previous(), settings))) continue; ValueFlow::Value master; @@ -5061,7 +5061,7 @@ static void valueFlowLifetime(TokenList &tokenlist, ErrorLogger &errorLogger, co master.lifetimeKind = ValueFlow::Value::LifetimeKind::Iterator; } else if (astIsIterator(parent) && Token::Match(parent->previous(), "%name% (") && contains({Library::Container::Yield::START_ITERATOR, Library::Container::Yield::END_ITERATOR}, - astFunctionYield(parent->previous(), &settings))) { + astFunctionYield(parent->previous(), settings))) { master.errorPath.emplace_back(parent, "Iterator to container is created here."); master.lifetimeKind = ValueFlow::Value::LifetimeKind::Iterator; } else if ((astIsPointer(parent->tokAt(2)) && @@ -5820,12 +5820,12 @@ static void valueFlowForwardConst(Token* start, static ValueFlow::Value::Bound findVarBound(const Variable* var, const Token* start, const Token* end, - const Settings* settings) + const Settings& settings) { ValueFlow::Value::Bound result = ValueFlow::Value::Bound::Point; const Token* next = start; while ((next = findExpressionChangedSkipDeadCode( - var->nameToken(), next->next(), end, settings, &evaluateKnownValues))) { + var->nameToken(), next->next(), end, &settings, &evaluateKnownValues))) { ValueFlow::Value::Bound b = ValueFlow::Value::Bound::Point; if (next->varId() != var->declarationId()) return ValueFlow::Value::Bound::Point; @@ -5948,7 +5948,7 @@ static void valueFlowForwardAssign(Token* const tok, } if (isInitialVarAssign(expr)) { // Check if variable is only incremented or decremented - ValueFlow::Value::Bound b = findVarBound(expr->variable(), nextExpression, endOfVarScope, &settings); + ValueFlow::Value::Bound b = findVarBound(expr->variable(), nextExpression, endOfVarScope, settings); if (b != ValueFlow::Value::Bound::Point) { auto knownValueIt = std::find_if(values.begin(), values.end(), [](const ValueFlow::Value& value) { if (!value.isKnown()) @@ -6713,9 +6713,9 @@ struct ConditionHandler { ProgramMemory pm; fillFromPath(pm, initTok, path); fillFromPath(pm, condTok, path); - execute(initTok, pm, nullptr, nullptr); + execute(initTok, pm, nullptr, nullptr, nullptr); MathLib::bigint result = 1; - execute(condTok, pm, &result, nullptr); + execute(condTok, pm, &result, nullptr, nullptr); if (result == 0) return; // Remove condition since for condition is not redundant @@ -6783,7 +6783,7 @@ struct ConditionHandler { if (condTok->astParent() && Token::Match(top->previous(), "while|for (")) dead_if = !isBreakScope(after); else if (!dead_if) - dead_if = isReturnScope(after, &settings.library, &unknownFunction); + dead_if = isReturnScope(after, settings.library, &unknownFunction); if (!dead_if && unknownFunction) { if (settings.debugwarnings) @@ -6795,7 +6795,7 @@ struct ConditionHandler { after = after->linkAt(2); unknownFunction = nullptr; if (!dead_else) - dead_else = isReturnScope(after, &settings.library, &unknownFunction); + dead_else = isReturnScope(after, settings.library, &unknownFunction); if (!dead_else && unknownFunction) { if (settings.debugwarnings) bailout(tokenlist, errorLogger, unknownFunction, "possible noreturn scope"); @@ -7132,10 +7132,10 @@ static bool valueFlowForLoop2(const Token *tok, ProgramMemory programMemory; MathLib::bigint result(0); bool error = false; - execute(firstExpression, programMemory, &result, &error); + execute(firstExpression, programMemory, &result, &error, nullptr); if (error) return false; - execute(secondExpression, programMemory, &result, &error); + execute(secondExpression, programMemory, &result, &error, nullptr); if (result == 0) // 2nd expression is false => no looping return false; if (error) { @@ -7158,9 +7158,9 @@ static bool valueFlowForLoop2(const Token *tok, int maxcount = 10000; while (result != 0 && !error && --maxcount > 0) { endMemory = programMemory; - execute(thirdExpression, programMemory, &result, &error); + execute(thirdExpression, programMemory, &result, &error, nullptr); if (!error) - execute(secondExpression, programMemory, &result, &error); + execute(secondExpression, programMemory, &result, &error, nullptr); } if (memory1) @@ -7217,7 +7217,7 @@ static void valueFlowForLoopSimplify(Token* const bodyStart, } if (Token::Match(tok2, "%oror%|&&")) { - const ProgramMemory programMemory(getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), &settings)); + const ProgramMemory programMemory(getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), settings)); if ((tok2->str() == "&&" && !conditionIsTrue(tok2->astOperand1(), programMemory, &settings)) || (tok2->str() == "||" && !conditionIsFalse(tok2->astOperand1(), programMemory, &settings))) { // Skip second expression.. @@ -7242,11 +7242,11 @@ static void valueFlowForLoopSimplify(Token* const bodyStart, if ((tok2->str() == "&&" && conditionIsFalse(tok2->astOperand1(), - getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), &settings), + getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), settings), &settings)) || (tok2->str() == "||" && conditionIsTrue(tok2->astOperand1(), - getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), &settings), + getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), settings), &settings))) break; @@ -7965,7 +7965,7 @@ static void valueFlowFunctionReturn(TokenList &tokenlist, ErrorLogger &errorLogg } if (programMemory.empty() && !arguments.empty()) continue; - std::vector values = execute(function->functionScope, programMemory, &settings); + std::vector values = execute(function->functionScope, programMemory, settings); for (const ValueFlow::Value& v : values) { if (v.isUninitValue()) continue; @@ -8020,7 +8020,7 @@ static void addToErrorPath(ValueFlow::Value& value, const ValueFlow::Value& from static std::vector findAllUsages(const Variable* var, Token* start, // cppcheck-suppress constParameterPointer // FP - const Library* library) + const Library& library) { // std::vector result; const Scope* scope = var->scope(); @@ -8031,7 +8031,7 @@ static std::vector findAllUsages(const Variable* var, }); } -static Token* findStartToken(const Variable* var, Token* start, const Library* library) +static Token* findStartToken(const Variable* var, Token* start, const Library& library) { std::vector uses = findAllUsages(var, start, library); if (uses.empty()) @@ -8111,7 +8111,7 @@ static void valueFlowUninit(TokenList& tokenlist, ErrorLogger& errorLogger, cons bool partial = false; - Token* start = findStartToken(var, tok->next(), &settings.library); + Token* start = findStartToken(var, tok->next(), settings.library); std::map partialReads; if (const Scope* scope = var->typeScope()) { @@ -8568,7 +8568,7 @@ static Library::Container::Yield findIteratorYield(Token* tok, const Token** fto return yield; //begin/end free functions - return astFunctionYield(tok->astParent()->previous(), &settings, ftok); + return astFunctionYield(tok->astParent()->previous(), settings, ftok); } static void valueFlowIterators(TokenList &tokenlist, const Settings &settings) diff --git a/test/testastutils.cpp b/test/testastutils.cpp index baa8357d09c..869e991ed54 100644 --- a/test/testastutils.cpp +++ b/test/testastutils.cpp @@ -145,7 +145,7 @@ class TestAstUtils : public TestFixture { const Token * const tok = (offset < 0) ? tokenizer.list.back()->tokAt(1+offset) : tokenizer.tokens()->tokAt(offset); - return (isReturnScope)(tok); + return (isReturnScope)(tok, settingsDefault.library); } void isReturnScopeTest() {