Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

programmemory.cpp: avoid repeated iteration over values in Executor::executeImpl() #6701

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,18 +1411,34 @@ namespace {

ValueFlow::Value executeImpl(const Token* expr)
{
const ValueFlow::Value* value = nullptr;
if (!expr)
return unknown();
if (expr->hasKnownIntValue() && !expr->isAssignmentOp() && expr->str() != ",")
return expr->values().front();
if ((value = expr->getKnownValue(ValueFlow::Value::ValueType::FLOAT)) ||
(value = expr->getKnownValue(ValueFlow::Value::ValueType::TOK)) ||
(value = expr->getKnownValue(ValueFlow::Value::ValueType::ITERATOR_START)) ||
(value = expr->getKnownValue(ValueFlow::Value::ValueType::ITERATOR_END)) ||
(value = expr->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE))) {
return *value;
const ValueFlow::Value* value = nullptr;
for (const auto& val : expr->values())
{
if (!val.isKnown())
continue;
switch (val.valueType) {
case ValueFlow::Value::ValueType::INT: {
if (!expr->isAssignmentOp() && expr->str() != ",")
return expr->values().front();
break;
}
case ValueFlow::Value::ValueType::FLOAT:
case ValueFlow::Value::ValueType::TOK:
case ValueFlow::Value::ValueType::ITERATOR_START:
case ValueFlow::Value::ValueType::ITERATOR_END:
case ValueFlow::Value::ValueType::CONTAINER_SIZE: {
if (!value)
value = &val;
break;
}
default:
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to make this a function that returns a pointer to the value. Then we can just write if((const ValueFlow::Value* value = findKnownValue(expr->values()))) here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

But then we would need to exclude the INT handling since that currently has priority and a different handling.

}
if (value)
return *value;
if (expr->isNumber()) {
if (MathLib::isFloat(expr->str()))
return unknown();
Expand Down
Loading