From 00fd3d92c6e31a0498527af83f2f510f529dbd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 7 Mar 2023 12:26:17 +0100 Subject: [PATCH] made `ForwardTraversal::mSettings` and `ReverseTraversal::mSettings` references / adjusted some calling code to use references as well (#4858) --- lib/forwardanalyzer.cpp | 20 +++++++++--------- lib/forwardanalyzer.h | 4 ++-- lib/reverseanalyzer.cpp | 10 ++++----- lib/reverseanalyzer.h | 4 ++-- lib/valueflow.cpp | 46 ++++++++++++++++++++--------------------- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 9e6f3a016..10bfb04fa 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -50,11 +50,11 @@ struct OnExit { struct ForwardTraversal { enum class Progress { Continue, Break, Skip }; enum class Terminate { None, Bail, Escape, Modified, Inconclusive, Conditional }; - ForwardTraversal(const ValuePtr& analyzer, const Settings* settings) + ForwardTraversal(const ValuePtr& analyzer, const Settings& settings) : analyzer(analyzer), settings(settings), actions(Analyzer::Action::None), analyzeOnly(false), analyzeTerminate(false) {} ValuePtr analyzer; - const Settings* settings; + const Settings& settings; Analyzer::Action actions; bool analyzeOnly; bool analyzeTerminate; @@ -138,7 +138,7 @@ struct ForwardTraversal { traverseRecursive(tok->astOperand2(), f, traverseUnknown); traverseRecursive(tok->astOperand1(), f, traverseUnknown); return Break(Analyzer::Terminate::Escape); - } else if (Token::Match(tok, "%name% (") && isEscapeFunction(tok, &settings->library)) { + } else if (Token::Match(tok, "%name% (") && isEscapeFunction(tok, &settings.library)) { // Traverse the parameters of the function before escaping traverseRecursive(tok->next()->astOperand2(), f, traverseUnknown); return Break(Analyzer::Terminate::Escape); @@ -315,7 +315,7 @@ struct ForwardTraversal { 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; } @@ -325,7 +325,7 @@ struct ForwardTraversal { 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; @@ -388,13 +388,13 @@ struct ForwardTraversal { if (stepTok) { std::pair exprToks = stepTok->findExpressionStartEndTokens(); if (exprToks.first != nullptr && exprToks.second != nullptr) - stepChangesCond |= isExpressionChanged(condTok, exprToks.first, exprToks.second->next(), settings, true); + stepChangesCond |= isExpressionChanged(condTok, exprToks.first, exprToks.second->next(), &settings, true); } - const bool bodyChangesCond = isExpressionChanged(condTok, endBlock->link(), endBlock, settings, true); + const bool bodyChangesCond = isExpressionChanged(condTok, endBlock->link(), endBlock, &settings, true); // Check for mutation in the condition const bool condChanged = nullptr != findAstNode(condTok, [&](const Token* tok) { - return isVariableChanged(tok, 0, settings, true); + return isVariableChanged(tok, 0, &settings, true); }); const bool changed = stepChangesCond || bodyChangesCond || condChanged; if (!changed) @@ -896,14 +896,14 @@ struct ForwardTraversal { } }; -Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const ValuePtr& a, const Settings* settings) +Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const ValuePtr& a, const Settings& settings) { ForwardTraversal ft{a, settings}; ft.updateRange(start, end); return Analyzer::Result{ ft.actions, ft.terminate }; } -Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr& a, const Settings* settings) +Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr& a, const Settings& settings) { if (Settings::terminated()) throw TerminateException(); diff --git a/lib/forwardanalyzer.h b/lib/forwardanalyzer.h index a23c8a638..4296d6f1f 100644 --- a/lib/forwardanalyzer.h +++ b/lib/forwardanalyzer.h @@ -28,8 +28,8 @@ template class ValuePtr; Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const ValuePtr& a, - const Settings* settings); + const Settings& settings); -Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr& a, const Settings* settings); +Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr& a, const Settings& settings); #endif diff --git a/lib/reverseanalyzer.cpp b/lib/reverseanalyzer.cpp index f54ea02f5..516151050 100644 --- a/lib/reverseanalyzer.cpp +++ b/lib/reverseanalyzer.cpp @@ -36,11 +36,11 @@ #include struct ReverseTraversal { - ReverseTraversal(const ValuePtr& analyzer, const Settings* settings) + ReverseTraversal(const ValuePtr& analyzer, const Settings& settings) : analyzer(analyzer), settings(settings) {} ValuePtr analyzer; - const Settings* settings; + const Settings& settings; std::pair evalCond(const Token* tok) const { std::vector result = analyzer->evaluate(tok); @@ -244,7 +244,7 @@ struct ReverseTraversal { // Assignment to } else if (lhsAction.matches() && !assignTok->astOperand2()->hasKnownIntValue() && assignTok->astOperand2()->exprId() > 0 && - isConstExpression(assignTok->astOperand2(), settings->library, true)) { + isConstExpression(assignTok->astOperand2(), settings.library, true)) { const std::string info = "Assignment to '" + assignTok->expressionString() + "'"; ValuePtr a = analyzer->reanalyze(assignTok->astOperand2(), info); if (a) { @@ -388,13 +388,13 @@ struct ReverseTraversal { } }; -void valueFlowGenericReverse(Token* start, const ValuePtr& a, const Settings* settings) +void valueFlowGenericReverse(Token* start, const ValuePtr& a, const Settings& settings) { ReverseTraversal rt{a, settings}; rt.traverse(start); } -void valueFlowGenericReverse(Token* start, const Token* end, const ValuePtr& a, const Settings* settings) +void valueFlowGenericReverse(Token* start, const Token* end, const ValuePtr& a, const Settings& settings) { ReverseTraversal rt{a, settings}; rt.traverse(start, end); diff --git a/lib/reverseanalyzer.h b/lib/reverseanalyzer.h index f8e075bd6..f42e0852a 100644 --- a/lib/reverseanalyzer.h +++ b/lib/reverseanalyzer.h @@ -25,7 +25,7 @@ class Token; template class ValuePtr; -void valueFlowGenericReverse(Token* start, const ValuePtr& a, const Settings* settings); -void valueFlowGenericReverse(Token* start, const Token* end, const ValuePtr& a, const Settings* settings); +void valueFlowGenericReverse(Token* start, const ValuePtr& a, const Settings& settings); +void valueFlowGenericReverse(Token* start, const Token* end, const ValuePtr& a, const Settings& settings); #endif diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 30373f53c..d3ef05007 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2000,7 +2000,7 @@ static Analyzer::Result valueFlowForward(Token* startToken, return valueFlowGenericForward(startToken, endToken, makeAnalyzer(exprTok, std::move(value), tokenlist, settings), - settings); + *settings); } static Analyzer::Result valueFlowForward(Token* startToken, @@ -2045,7 +2045,7 @@ static Analyzer::Result valueFlowForwardRecursive(Token* top, if (settings->debugnormal) setSourceLocation(v, loc, top); result.update( - valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), tokenlist, settings), settings)); + valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), tokenlist, settings), *settings)); } return result; } @@ -2061,7 +2061,7 @@ static void valueFlowReverse(Token* tok, for (ValueFlow::Value& v : values) { if (settings->debugnormal) setSourceLocation(v, loc, tok); - valueFlowGenericReverse(tok, endToken, makeReverseAnalyzer(varToken, std::move(v), tokenlist, settings), settings); + valueFlowGenericReverse(tok, endToken, makeReverseAnalyzer(varToken, std::move(v), tokenlist, settings), *settings); } } @@ -5109,12 +5109,12 @@ static const Scope* getLoopScope(const Token* tok) } // -static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings) +static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings &settings) { for (const Scope * scope : symboldatabase->functionScopes) { if (const Token* incompleteTok = findIncompleteVar(scope->bodyStart, scope->bodyEnd)) { if (incompleteTok->isIncompleteVar()) { - if (settings->debugwarnings) + if (settings.debugwarnings) bailoutIncompleteVar(tokenlist, errorLogger, incompleteTok, "Skipping function due to incomplete variable " + incompleteTok->str()); break; } @@ -5132,7 +5132,7 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* continue; if (condTok->hasKnownIntValue()) continue; - if (!isConstExpression(condTok, settings->library, tokenlist->isCPP())) + if (!isConstExpression(condTok, settings.library, tokenlist->isCPP())) continue; const bool is1 = (condTok->isComparisonOp() || condTok->tokType() == Token::eLogicalOp || astIsBool(condTok)); @@ -5142,11 +5142,11 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* for (const Token* condTok2 : getConditions(condTok, "&&")) { if (is1) { const bool isBool = astIsBool(condTok2) || Token::Match(condTok2, "%comp%|%oror%|&&"); - SameExpressionAnalyzer a1(condTok2, makeConditionValue(1, condTok2, /*assume*/ true, !isBool), tokenlist, settings); // don't set '1' for non-boolean expressions + SameExpressionAnalyzer a1(condTok2, makeConditionValue(1, condTok2, /*assume*/ true, !isBool), tokenlist, &settings); // don't set '1' for non-boolean expressions valueFlowGenericForward(startTok, startTok->link(), a1, settings); } - OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(0, condTok2, true), tokenlist, settings); + OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(0, condTok2, true), tokenlist, &settings); valueFlowGenericForward(startTok, startTok->link(), a2, settings); } } @@ -5157,18 +5157,18 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* if (Token::simpleMatch(startTok->link(), "} else {")) { startTok = startTok->link()->tokAt(2); for (const Token* condTok2:conds) { - SameExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist, settings); + SameExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist, &settings); valueFlowGenericForward(startTok, startTok->link(), a1, settings); if (is1) { - OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist, settings); + OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist, &settings); valueFlowGenericForward(startTok, startTok->link(), a2, settings); } } } // Check if the block terminates early - if (isEscapeScope(blockTok, tokenlist, settings)) { + if (isEscapeScope(blockTok, tokenlist, &settings)) { const Scope* scope2 = scope; // If escaping a loop then only use the loop scope if (isBreakOrContinueScope(blockTok->link())) { @@ -5177,11 +5177,11 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* continue; } for (const Token* condTok2:conds) { - SameExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist, settings); + SameExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist, &settings); valueFlowGenericForward(startTok->link()->next(), scope2->bodyEnd, a1, settings); if (is1) { - OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist, settings); + OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist, &settings); valueFlowGenericForward(startTok->link()->next(), scope2->bodyEnd, a2, settings); } } @@ -7210,12 +7210,12 @@ bool productParams(const std::unordered_map>& v static void valueFlowInjectParameter(TokenList* tokenlist, SymbolDatabase* symboldatabase, ErrorLogger* errorLogger, - const Settings* settings, + const Settings& settings, const Scope* functionScope, const std::unordered_map>& vars) { const bool r = productParams(vars, [&](const std::unordered_map& arg) { - MultiValueFlowAnalyzer a(arg, tokenlist, settings, symboldatabase); + MultiValueFlowAnalyzer a(arg, tokenlist, &settings, symboldatabase); valueFlowGenericForward(const_cast(functionScope->bodyStart), functionScope->bodyEnd, a, settings); }); if (!r) { @@ -7223,7 +7223,7 @@ static void valueFlowInjectParameter(TokenList* tokenlist, Function* f = functionScope->function; if (f) fname = f->name(); - if (settings->debugwarnings) + if (settings.debugwarnings) bailout(tokenlist, errorLogger, functionScope->bodyStart, "Too many argument passed to " + fname); } } @@ -7387,7 +7387,7 @@ IteratorRange MakeIteratorRange(Iterator start, Iterator last) return {start, last}; } -static void valueFlowSubFunction(TokenList* tokenlist, SymbolDatabase* symboldatabase, ErrorLogger* errorLogger, const Settings* settings) +static void valueFlowSubFunction(TokenList* tokenlist, SymbolDatabase* symboldatabase, ErrorLogger* errorLogger, const Settings& settings) { int id = 0; for (const Scope* scope : MakeIteratorRange(symboldatabase->functionScopes.crbegin(), symboldatabase->functionScopes.crend())) { @@ -7401,9 +7401,9 @@ static void valueFlowSubFunction(TokenList* tokenlist, SymbolDatabase* symboldat const Function * const calledFunction = tok->function(); if (!calledFunction) { // library function? - const std::string& returnValue(settings->library.returnValue(tok)); + const std::string& returnValue(settings.library.returnValue(tok)); if (!returnValue.empty()) - valueFlowLibraryFunction(tok->next(), returnValue, settings); + valueFlowLibraryFunction(tok->next(), returnValue, &settings); continue; } @@ -7433,7 +7433,7 @@ static void valueFlowSubFunction(TokenList* tokenlist, SymbolDatabase* symboldat }); // Remove uninit values if argument is passed by value if (argtok->variable() && !argtok->variable()->isPointer() && argvalues.size() == 1 && argvalues.front().isUninitValue()) { - if (CheckUninitVar::isVariableUsage(tokenlist->isCPP(), argtok, settings->library, false, CheckUninitVar::Alloc::NO_ALLOC, 0)) + if (CheckUninitVar::isVariableUsage(tokenlist->isCPP(), argtok, settings.library, false, CheckUninitVar::Alloc::NO_ALLOC, 0)) continue; } @@ -7665,7 +7665,7 @@ static void valueFlowUninit(TokenList* tokenlist, SymbolDatabase* /*symbolDataba continue; } MemberExpressionAnalyzer analyzer(memVar.nameToken()->str(), tok, uninitValue, tokenlist, settings); - valueFlowGenericForward(tok->next(), tok->scope()->bodyEnd, analyzer, settings); + valueFlowGenericForward(tok->next(), tok->scope()->bodyEnd, analyzer, *settings); for (auto&& p : *analyzer.partialReads) { Token* tok2 = p.first; @@ -8895,7 +8895,7 @@ void ValueFlow::setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase, valueFlowSymbolic(tokenlist, symboldatabase, settings); valueFlowBitAnd(tokenlist, settings); valueFlowSameExpressions(tokenlist, settings); - valueFlowConditionExpressions(tokenlist, symboldatabase, errorLogger, settings); + valueFlowConditionExpressions(tokenlist, symboldatabase, errorLogger, *settings); const std::uint64_t stopTime = getValueFlowStopTime(settings); @@ -8931,7 +8931,7 @@ void ValueFlow::setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase, if (std::time(nullptr) < stopTime) valueFlowForLoop(tokenlist, symboldatabase, errorLogger, settings); if (std::time(nullptr) < stopTime) - valueFlowSubFunction(tokenlist, symboldatabase, errorLogger, settings); + valueFlowSubFunction(tokenlist, symboldatabase, errorLogger, *settings); if (std::time(nullptr) < stopTime) valueFlowFunctionReturn(tokenlist, errorLogger, settings); if (std::time(nullptr) < stopTime)