C++: Reduce duplication from crement operations #14867
Draft
+132
∄1�728
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.
This PR fixes a dataflow duplication issue caused by having multiple dataflow nodes return the same expression when the expression has tricky semantics that's difficult to model using just
Exprs.For example, consider the
f(x++)operation. For this expression we generate IR equivalent to:and the question is then: which dataflow node's
asExpr()should return thex++expression. Should it be the one corresponding tox = x + 1(that is, the incremented value), or should it betmpinf(tmp)(that is, the original value)?The C/C++ standard would say that it should be the latter, but when writing queries people are less interested in what the standard demands, and more interesting in whatever fits their query logic, and often that ends up being the former 😂.
So C/C++ currently does both. So both the node corresponding to
x = x + 1, and the node corresponding totmpinf(tmp), returnx++. This causes duplication when writing a query such as this (where we track flow from whatsource()points to, to what the argument ofsinkpoints to):This PR fixes this duplication by adding another dataflow node predicate called
Node.asDefinition(andNode.asIndirectDefinitionfor the indirect case). In the context of thef(x++)example above, theNode.asDefinitionpredicate will have a result for the dataflow node corresponding tox = x + 1, andNode.asExprwill have a result only for the node corresponding totmp.This means that the query from before will only have 1 result:
