From 9d8af2078f6560ac876ab8a7987b8b70501d3f3a Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 31 Aug 2024 16:15:41 +0200 Subject: [PATCH 01/11] valueflow.cpp: avoid unnecessary copies in `valueFlowContainerSize()` --- lib/valueflow.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index fdf7394aa3a..d073975c148 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6663,8 +6663,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist, continue; } - for (const ValueFlow::Value& value : values) { - valueFlowForward(nameToken->next(), var->nameToken(), value, tokenlist, errorLogger, settings); + for (ValueFlow::Value& value : values) { + valueFlowForward(nameToken->next(), var->nameToken(), std::move(value), tokenlist, errorLogger, settings); } } @@ -6710,8 +6710,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist, const Token* constructorArgs = tok; values = getContainerSizeFromConstructor(constructorArgs, tok->valueType(), settings, true); } - for (const ValueFlow::Value& value : values) - setTokenValue(tok, value, settings); + for (ValueFlow::Value& value : values) + setTokenValue(tok, std::move(value), settings); } else if (Token::Match(tok->previous(), ",|(") && (Token::Match(tok, "{|%str%") || settings.library.detectContainer(tok))) { if (Token* argTok = tok->previous()->astOperand2()) { From 7e5de6025e117dc96fd1425538255e553a89faa3 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 31 Aug 2024 16:19:40 +0200 Subject: [PATCH 02/11] valueflow.cpp: avoid unnecessary copies in `valueFlowFunctionReturn()` --- lib/valueflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d073975c148..d20ee351334 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5844,10 +5844,10 @@ static void valueFlowFunctionReturn(TokenList& tokenlist, ErrorLogger& errorLogg if (programMemory.empty() && !arguments.empty()) continue; std::vector values = execute(function->functionScope, programMemory, settings); - for (const ValueFlow::Value& v : values) { + for (ValueFlow::Value& v : values) { if (v.isUninitValue()) continue; - setFunctionReturnValue(function, tok, v, settings); + setFunctionReturnValue(function, tok, std::move(v), settings); } } } From 249826b8a79de41e70c894468f2f9d6286005e18 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 31 Aug 2024 16:25:39 +0200 Subject: [PATCH 03/11] valueflow.cpp: avoid unnecessary copy in `valueFlowLifetime()` --- lib/valueflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d20ee351334..0c4ecabfa47 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3006,10 +3006,10 @@ static void valueFlowLifetime(TokenList &tokenlist, ErrorLogger &errorLogger, co if (tok2 && tok2 != op1 && (!tok2->variable() || !tok2->variable()->isArray()) && !(tok2->valueType() && tok2->valueType()->container)) continue; } - for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) { + for (ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) { if (!settings.certainty.isEnabled(Certainty::inconclusive) && lt.inconclusive) continue; - ErrorPath errorPath = lt.errorPath; + ErrorPath& errorPath = lt.errorPath; errorPath.emplace_back(tok, "Address of variable taken here."); ValueFlow::Value value; From d12fe8147f0ddb70e9cafceb90510bbdc77475a9 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:48:57 +0100 Subject: [PATCH 04/11] valueflow.cpp: avoid unnecessary copy in `valueFlowAfterAssign()` --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0c4ecabfa47..43fbc21d70e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4209,7 +4209,7 @@ static void valueFlowAfterAssign(TokenList &tokenlist, continue; ids.insert(value.tokvalue->exprId()); } - for (ValueFlow::Value value : values) { + for (ValueFlow::Value& value : values) { if (!value.isSymbolicValue()) continue; const Token* expr = value.tokvalue; From ce371bee4ba3ed78677ff452d9cd28264a77e481 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:49:23 +0100 Subject: [PATCH 05/11] valueflow.cpp: avoid unnecessary copies in `valueFlowInferCondition()` --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 43fbc21d70e..f838b94685f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5052,7 +5052,7 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin for (const ValuePtr& model : iteratorModels) { std::vector result = infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values()); - for (ValueFlow::Value value : result) { + for (ValueFlow::Value& value : result) { value.valueType = ValueFlow::Value::ValueType::INT; setTokenValue(tok, std::move(value), settings); } From b2427a6170c0eef7284eb627a49c62d3cc5f1cc8 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:51:13 +0100 Subject: [PATCH 06/11] valueflow.cpp: avoid unnecessary copy in `valueFlowContainerSize()` --- lib/valueflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f838b94685f..0103242b434 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6748,8 +6748,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist, Token* rhs = tok->tokAt(2)->astOperand2(); std::vector values = getInitListSize(rhs, containerTok->valueType(), settings); valueFlowContainerSetTokValue(tokenlist, errorLogger, settings, containerTok, rhs); - for (const ValueFlow::Value& value : values) - valueFlowForward(containerTok->next(), containerTok, value, tokenlist, errorLogger, settings); + for (ValueFlow::Value& value : values) + valueFlowForward(containerTok->next(), containerTok, std::move(value), tokenlist, errorLogger, settings); } } else if (Token::Match(tok, ". %name% (") && tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->container) { From 68d24d19d448978418e9d18c2b4b5f4d3620f588 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:51:31 +0100 Subject: [PATCH 07/11] valueflow.cpp: avoid unnecessary copy in `valueFlowSafeFunctions()` --- lib/valueflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0103242b434..f643003e410 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7014,8 +7014,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab argValues.back().valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming " + arg.name() + " size is 1000000"); argValues.back().safe = true; - for (const ValueFlow::Value &value : argValues) - valueFlowForward(const_cast(functionScope->bodyStart), arg.nameToken(), value, tokenlist, errorLogger, settings); + for (ValueFlow::Value &value : argValues) + valueFlowForward(const_cast(functionScope->bodyStart), arg.nameToken(), std::move(value), tokenlist, errorLogger, settings); continue; } From ba28899a1369e288782e7235562301d066c320eb Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 8 Aug 2024 00:13:38 +0200 Subject: [PATCH 08/11] programmemory.cpp: avoid unnecessary copy in `programMemoryParseCondition()` --- lib/programmemory.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 750fd6653e0..65435bb47b9 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -364,11 +364,13 @@ static void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, con if (endTok && findExpressionChanged(vartok, tok->next(), endTok, settings)) return; const bool impossible = (tok->str() == "==" && !then) || (tok->str() == "!=" && then); - const ValueFlow::Value& v = then ? truevalue : falsevalue; - pm.setValue(vartok, impossible ? asImpossible(v) : v); + ValueFlow::Value& v = then ? truevalue : falsevalue; + const auto iv = v.intvalue; + // cppcheck-suppress accessMoved - FP #13628 + pm.setValue(vartok, impossible ? asImpossible(std::move(v)) : v); const Token* containerTok = settings.library.getContainerFromYield(vartok, Library::Container::Yield::SIZE); if (containerTok) - pm.setContainerSizeValue(containerTok, v.intvalue, !impossible); + pm.setContainerSizeValue(containerTok, iv, !impossible); } else if (Token::simpleMatch(tok, "!")) { programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, !then); } else if (then && Token::simpleMatch(tok, "&&")) { From ade5c87c1e8f9f058982ab30d23b64e8ed1aacc6 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 10 Jun 2025 12:03:09 +0200 Subject: [PATCH 09/11] valueflow.cpp: avoid unnecessary copies in `valueFlowFunctionReturn()` [skip ci] --- lib/valueflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f643003e410..a306e7fae58 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5815,10 +5815,10 @@ static void valueFlowFunctionReturn(TokenList& tokenlist, ErrorLogger& errorLogg bool hasKnownValue = false; - for (const ValueFlow::Value& v : getCommonValuesFromTokens(returns)) { - setFunctionReturnValue(function, tok, v, settings, false); + for (ValueFlow::Value& v : getCommonValuesFromTokens(returns)) { if (v.isKnown()) hasKnownValue = true; + setFunctionReturnValue(function, tok, std::move(v), settings, false); } if (hasKnownValue) From 7cd6bfb3f08260ef9c27c3c9785a0f641e49122e Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 9 Jan 2025 01:00:00 +0100 Subject: [PATCH 10/11] vf_settokenvalue.cpp: avoid unnecessary copy in `setTokenValue()` [skip ci] --- lib/vf_settokenvalue.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index b2647bf46ec..19643f3629b 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -258,8 +258,9 @@ namespace ValueFlow } if (Token::simpleMatch(parent, "=") && astIsRHS(tok)) { - setTokenValue(parent, value, settings); - if (!value.isUninitValue()) + const bool isUninit = value.isUninitValue(); + setTokenValue(parent, std::move(value), settings); + if (!isUninit) return; } From 8e0b759e1fbcc82119017187522d29ee018a204a Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:47:27 +0100 Subject: [PATCH 11/11] valueflow.cpp: avoid unnecessary copies with `valueFlowForwardConst()` [skip ci] --- lib/valueflow.cpp | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a306e7fae58..b7b860066d9 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3795,16 +3795,15 @@ template static void valueFlowForwardConst(Token* start, const Token* end, const Variable* var, - const ContainerOfValue& values, - const Settings& settings, - int /*unused*/ = 0) + ContainerOfValue values, + const Settings& settings) { if (!precedes(start, end)) throw InternalError(var->nameToken(), "valueFlowForwardConst: start token does not precede the end token."); for (Token* tok = start; tok != end; tok = tok->next()) { if (tok->varId() == var->declarationId()) { - for (const ValueFlow::Value& value : values) - setTokenValue(tok, value, settings); + for (ValueFlow::Value& value : values) + setTokenValue(tok, std::move(value), settings); } else { [&] { // Follow references @@ -3813,7 +3812,7 @@ static void valueFlowForwardConst(Token* start, return ref.token->varId() == var->declarationId(); }); if (it != refs.end()) { - for (ValueFlow::Value value : values) { + for (ValueFlow::Value& value : values) { if (refs.size() > 1) value.setInconclusive(); value.errorPath.insert(value.errorPath.end(), it->errors.cbegin(), it->errors.cend()); @@ -3829,7 +3828,7 @@ static void valueFlowForwardConst(Token* start, continue; if (v.tokvalue->varId() != var->declarationId()) continue; - for (ValueFlow::Value value : values) { + for (ValueFlow::Value& value : values) { if (!v.isKnown() && value.isImpossible()) continue; if (v.intvalue != 0) { @@ -3849,15 +3848,6 @@ static void valueFlowForwardConst(Token* start, } } -static void valueFlowForwardConst(Token* start, - const Token* end, - const Variable* var, - const std::initializer_list& values, - const Settings& settings) -{ - valueFlowForwardConst(start, end, var, values, settings, 0); -} - static ValueFlow::Value::Bound findVarBound(const Variable* var, const Token* start, const Token* end, @@ -3985,7 +3975,7 @@ static void valueFlowForwardAssign(Token* const tok, }); std::list constValues; constValues.splice(constValues.end(), values, it, values.end()); - valueFlowForwardConst(nextExpression, endOfVarScope, expr->variable(), constValues, settings); + valueFlowForwardConst(nextExpression, endOfVarScope, expr->variable(), std::move(constValues), settings); } if (isInitialVarAssign(expr)) { // Check if variable is only incremented or decremented @@ -4001,7 +3991,7 @@ static void valueFlowForwardAssign(Token* const tok, value.bound = b; value.invertRange(); value.setImpossible(); - valueFlowForwardConst(nextExpression, endOfVarScope, expr->variable(), {std::move(value)}, settings); + valueFlowForwardConst(nextExpression, endOfVarScope, expr->variable(), std::list{std::move(value)}, settings); } } } @@ -6579,7 +6569,7 @@ static void valueFlowContainerSetTokValue(const TokenList& tokenlist, ErrorLogge value.setKnown(); Token* start = initList->link() ? initList->link() : initList->next(); if (tok->variable() && tok->variable()->isConst()) { - valueFlowForwardConst(start, tok->variable()->scope()->bodyEnd, tok->variable(), {std::move(value)}, settings); + valueFlowForwardConst(start, tok->variable()->scope()->bodyEnd, tok->variable(), std::list{std::move(value)}, settings); } else { valueFlowForward(start, tok, std::move(value), tokenlist, errorLogger, settings); }