From d184f77db3ab3d17c06b2cf6a5bc61de63da3e16 Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 16 Jun 2024 02:45:54 +0200 Subject: [PATCH 1/7] vf_settokenvalue.cpp: avoid unnecessary copies with `setTokenValueCast()` --- lib/vf_settokenvalue.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index 4d39d35daae..40a8f716698 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -126,32 +126,31 @@ namespace ValueFlow return value.isIntValue() || value.isFloatValue(); } - static void setTokenValueCast(Token *parent, const ValueType &valueType, const Value &value, const Settings &settings) + static void setTokenValueCast(Token *parent, const ValueType &valueType, Value value, const Settings &settings) { if (valueType.pointer || value.isImpossible()) - setTokenValue(parent,value,settings); + setTokenValue(parent,std::move(value),settings); else if (valueType.type == ValueType::Type::CHAR) - setTokenValue(parent, castValue(value, valueType.sign, settings.platform.char_bit), settings); + setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.char_bit), settings); else if (valueType.type == ValueType::Type::SHORT) - setTokenValue(parent, castValue(value, valueType.sign, settings.platform.short_bit), settings); + setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.short_bit), settings); else if (valueType.type == ValueType::Type::INT) - setTokenValue(parent, castValue(value, valueType.sign, settings.platform.int_bit), settings); + setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.int_bit), settings); else if (valueType.type == ValueType::Type::LONG) - setTokenValue(parent, castValue(value, valueType.sign, settings.platform.long_bit), settings); + setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.long_bit), settings); else if (valueType.type == ValueType::Type::LONGLONG) - setTokenValue(parent, castValue(value, valueType.sign, settings.platform.long_long_bit), settings); + setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.long_long_bit), settings); else if (valueType.isFloat() && isNumeric(value)) { - Value floatValue = value; - floatValue.valueType = Value::ValueType::FLOAT; if (value.isIntValue()) - floatValue.floatValue = static_cast(value.intvalue); - setTokenValue(parent, std::move(floatValue), settings); + value.floatValue = static_cast(value.intvalue); + value.valueType = Value::ValueType::FLOAT; + setTokenValue(parent, std::move(value), settings); } else if (value.isIntValue()) { const long long charMax = settings.platform.signedCharMax(); const long long charMin = settings.platform.signedCharMin(); if (charMin <= value.intvalue && value.intvalue <= charMax) { // unknown type, but value is small so there should be no truncation etc - setTokenValue(parent,value,settings); + setTokenValue(parent,std::move(value),settings); } } } @@ -382,7 +381,7 @@ namespace ValueFlow && tok->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer) >= valueType.getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer)) return; - setTokenValueCast(parent, valueType, value, settings); + setTokenValueCast(parent, valueType, std::move(value), settings); } else if (parent->str() == ":") { From eb4a12ffa6cce6a9f676d1d1b05957342c7b1d87 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 31 Aug 2024 16:18:35 +0200 Subject: [PATCH 2/7] vf_settokenvalue.cpp: avoid unnecessary objects in `setTokenValue()` --- lib/vf_settokenvalue.cpp | 46 +++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index 40a8f716698..71c509de971 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -306,22 +306,23 @@ namespace ValueFlow value.valueType = Value::ValueType::INT; setTokenValue(next, std::move(value), settings); } else if (yields == Library::Container::Yield::EMPTY) { - Value v(value); - v.valueType = Value::ValueType::INT; - v.bound = Value::Bound::Point; + const Value::Bound bound = value.bound; + const long long intvalue = value.intvalue; + value.valueType = Value::ValueType::INT; + value.bound = Value::Bound::Point; if (value.isImpossible()) { - if (value.intvalue == 0) - v.setKnown(); - else if ((value.bound == Value::Bound::Upper && value.intvalue > 0) || - (value.bound == Value::Bound::Lower && value.intvalue < 0)) { - v.intvalue = 0; - v.setKnown(); + if (intvalue == 0) + value.setKnown(); + else if ((bound == Value::Bound::Upper && intvalue > 0) || + (bound == Value::Bound::Lower && intvalue < 0)) { + value.intvalue = 0; + value.setKnown(); } else - v.setPossible(); + value.setPossible(); } else { - v.intvalue = !v.intvalue; + value.intvalue = !value.intvalue; } - setTokenValue(next, std::move(v), settings); + setTokenValue(next, std::move(value), settings); } return; } @@ -420,11 +421,10 @@ namespace ValueFlow if (ret) return; - Value v(std::move(value)); - v.conditional = true; - v.changeKnownToPossible(); + value.conditional = true; + value.changeKnownToPossible(); - setTokenValue(parent, std::move(v), settings); + setTokenValue(parent, std::move(value), settings); } } @@ -712,15 +712,13 @@ namespace ValueFlow std::vector args = getArguments(value.tokvalue); if (const Library::Function* f = settings.library.getFunction(parent->previous())) { if (f->containerYield == Library::Container::Yield::SIZE) { - Value v(std::move(value)); - v.valueType = Value::ValueType::INT; - v.intvalue = args.size(); - setTokenValue(parent, std::move(v), settings); + value.valueType = Value::ValueType::INT; + value.intvalue = args.size(); + setTokenValue(parent, std::move(value), settings); } else if (f->containerYield == Library::Container::Yield::EMPTY) { - Value v(std::move(value)); - v.intvalue = args.empty(); - v.valueType = Value::ValueType::INT; - setTokenValue(parent, std::move(v), settings); + value.intvalue = args.empty(); + value.valueType = Value::ValueType::INT; + setTokenValue(parent, std::move(value), settings); } } } From 4d81d6d53f2dd89757c774d63fee37d3a5329181 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 30 Sep 2024 01:53:35 +0200 Subject: [PATCH 3/7] valueflow.cpp: avoid unnecessary copy in `valueFlowSymbolicOperators()` --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 86b49c0b137..ab274f1778c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3652,7 +3652,7 @@ static void valueFlowSymbolicOperators(const SymbolDatabase& symboldatabase, con continue; ValueFlow::Value v = makeSymbolic(arg); - v.errorPath = c.errorPath; + v.errorPath = std::move(c.errorPath); v.errorPath.emplace_back(tok, "Passed to " + tok->str()); if (c.intvalue == 0) v.setImpossible(); From 0edd83e5f88c377fc7869ea5be1bef92cdb20191 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:28:19 +0100 Subject: [PATCH 4/7] ValueFlow: avoid unnecessary copy in `getLifetimeObjValue()` --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ab274f1778c..f0cf0a863db 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1511,7 +1511,7 @@ ValueFlow::Value ValueFlow::getLifetimeObjValue(const Token *tok, bool inconclus // There should only be one lifetime if (values.size() != 1) return ValueFlow::Value{}; - return values.front(); + return std::move(values.front()); } template From be58ea7799735a7a5cd4fdd2e0578939a6e592c2 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Dec 2024 16:49:23 +0100 Subject: [PATCH 5/7] valueflow.cpp: avoid unnecessary copies in `valueFlowInferCondition()` --- lib/valueflow.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f0cf0a863db..15a16f6c81d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5070,8 +5070,7 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin std::vector result = infer(makeIntegralInferModel(), "!=", tok->values(), 0); if (result.size() != 1) continue; - ValueFlow::Value value = result.front(); - setTokenValue(tok, std::move(value), settings); + setTokenValue(tok, std::move(result.front()), settings); } } } From 3e0e95ab46ec684da0be72da18a94e7304ed4a1b Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 9 Jan 2025 00:34:22 +0100 Subject: [PATCH 6/7] valueflow.cpp: avoid unnecessary copies with `valueFlowInjectParameter()` --- lib/valueflow.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 15a16f6c81d..7aa0ce4aac6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5489,7 +5489,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist, const Settings& settings, const Variable* arg, const Scope* functionScope, - const std::list& argvalues) + std::list argvalues) { // Is argument passed by value or const reference, and is it a known non-class type? if (arg->isReference() && !arg->isConst() && !arg->isClass()) @@ -5503,7 +5503,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist, valueFlowForward(const_cast(functionScope->bodyStart->next()), functionScope->bodyEnd, arg->nameToken(), - argvalues, + std::move(argvalues), tokenlist, errorLogger, settings); @@ -5730,7 +5730,7 @@ static void valueFlowFunctionDefaultParameter(const TokenList& tokenlist, const argvalues.push_back(std::move(v)); } if (!argvalues.empty()) - valueFlowInjectParameter(tokenlist, errorLogger, settings, var, scope, argvalues); + valueFlowInjectParameter(tokenlist, errorLogger, settings, var, scope, std::move(argvalues)); } } } From 47870d1c5dad28602d92ad2fefc26a822e1c402b Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 10 Jun 2025 11:49:31 +0200 Subject: [PATCH 7/7] vf_settokenvalue.cpp: void unnecessary copy in `setTokenValue()` --- lib/vf_settokenvalue.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index 71c509de971..b2647bf46ec 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -346,27 +346,26 @@ namespace ValueFlow setTokenValue(parent, std::move(value), settings); return; } - Value pvalue = value; if (!value.subexpressions.empty() && Token::Match(parent, ". %var%")) { if (contains(value.subexpressions, parent->strAt(1))) - pvalue.subexpressions.clear(); + value.subexpressions.clear(); else return; } if (parent->isUnaryOp("&")) { - pvalue.indirect++; - setTokenValue(parent, std::move(pvalue), settings); + value.indirect++; + setTokenValue(parent, std::move(value), settings); } else if (Token::Match(parent, ". %var%") && parent->astOperand1() == tok && parent->astOperand2()) { - if (parent->originalName() == "->" && pvalue.indirect > 0) - pvalue.indirect--; - setTokenValue(parent->astOperand2(), std::move(pvalue), settings); + if (parent->originalName() == "->" && value.indirect > 0) + value.indirect--; + setTokenValue(parent->astOperand2(), std::move(value), settings); } else if (Token::Match(parent->astParent(), ". %var%") && parent->astParent()->astOperand1() == parent) { - if (parent->astParent()->originalName() == "->" && pvalue.indirect > 0) - pvalue.indirect--; - setTokenValue(parent->astParent()->astOperand2(), std::move(pvalue), settings); - } else if (parent->isUnaryOp("*") && pvalue.indirect > 0) { - pvalue.indirect--; - setTokenValue(parent, std::move(pvalue), settings); + if (parent->astParent()->originalName() == "->" && value.indirect > 0) + value.indirect--; + setTokenValue(parent->astParent()->astOperand2(), std::move(value), settings); + } else if (parent->isUnaryOp("*") && value.indirect > 0) { + value.indirect--; + setTokenValue(parent, std::move(value), settings); } return; }