Improve isSameExpression for literals#2514
Merged
danmar merged 1 commit intoFeb 1, 2020
Merged
Conversation
Improve isSameExpression() for literals with same value but different
representation, for example the following different ways of
representing 9 as double: 9.0, 0.9e1 and 0x1.2p3.
With this change, cppcheck can (for example) correctly detect that the
else if statements are always false in the following example:
void f(double x) {
if (x < 9.0) {}
else if (x < 0x1.2p3) {}
else if (x < 0.9e1) {}
}
Collaborator
|
I'll merge it now.. let's see how it works. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve isSameExpression() for literals with same value but different
representation, for example the following different ways of
representing 9 as double: 9.0, 0.9e1 and 0x1.2p3.
With this change, cppcheck can (for example) correctly detect that the
else if statements are always false in the following example:
test-my-prshows no problems with this, except... negative floating point zero, which seems to be optional in the standard (but IEEE floating point standard mandates it, and at least on my computer (linux), gcc (and glibc) treats them slightly different). From my understanding0.0and-0.0are equal in most aspects, (for example0.0 == -0.0is true,0.0 > -0.0is false). But! Math functions might treat them separately. For exampleatan2(0.0, -0.0)equals3.1415..., whileatan2(-0.0, -0.0)equals-3.1415....With this change,
0.0and-0.0are considered equal (previously, they were not).This allows cppcheck to detect duplicate conditions like the following
which is good, but it also gives false positives on the following
(you could argue that there are alread FPs in the following code:
Since x might be negative zero, and the assignment assures that x is positive zero.
This is not a huge problem. I ran
test-my-prwith 1000 packages and counted 6 FP duplicateConditionalAssign, 1 TP multiCondition.Is it worth adding a flag to
isSameExpression()so that checkers can decided for themselves whether or not they want to treat positive and negative zero as same expression?