-
Notifications
You must be signed in to change notification settings - Fork 1.5k
assert and fixed some flawed known value usage #7394
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,11 +391,10 @@ namespace ValueFlow | |
|
||
else if (parent->str() == "?" && tok->str() == ":" && tok == parent->astOperand2() && parent->astOperand1()) { | ||
// is condition always true/false? | ||
if (parent->astOperand1()->hasKnownValue()) { | ||
const Value &condvalue = parent->astOperand1()->values().front(); | ||
const bool cond(condvalue.isTokValue() || (condvalue.isIntValue() && condvalue.intvalue != 0)); | ||
if (const Value* condvalue = parent->astOperand1()->getKnownValue()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use In this case, since we prefer intvalue over tokvalue, we could make a const ValueFlow::Value* Token::getKnownValue(std::initializer_list<Value::ValueType> types) const
{
for(auto t:types) {
const ValueFlow::Value* value = getKnownValue(t);
if(value)
return value;
}
return nullptr;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification. This might cause multiple lookups which would be bad in a hot path - see #6701 for such a case. But correctness is obviously first priority. So we could adjust that later on in the other PR. |
||
const bool cond(condvalue->isTokValue() || (condvalue->isIntValue() && condvalue->intvalue != 0)); | ||
if (cond && !tok->astOperand1()) { // true condition, no second operator | ||
setTokenValue(parent, condvalue, settings); | ||
setTokenValue(parent, *condvalue, settings); | ||
} else { | ||
const Token *op = cond ? tok->astOperand1() : tok->astOperand2(); | ||
if (!op) // #7769 segmentation fault at setTokenValue() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code is technically redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this redundant code is still here.
was it kept by intention?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it redundant? It might not be set or it might be empty. That is not the same. Or do you mean that the function as a whole is mostly redundant to the other one.
But it is still WIP. I forgot to mark it as draft again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This:
Is logically the same if the first 2 lines are removed:
if
mImpl->mValues
is empty thenit
will bemImpl->mValues->end()
and that means nullptr is returned.So I suggest to remove the extra code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I get it.
That would not be the same since the lamdba might still be created even though it is never used. I think I saw a similar optimization somewhere recently (but it is possible that was copying data in the capture). Need to have a look at the generated code and/or profile it.