-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C++: Make node.asExpr() instanceof ClassAggregateLiteral
satisfiable
#19501
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?
Conversation
… instanceof ClassAggregateLiteral' holds for this new node subclass.
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.
Pull Request Overview
This PR enables node.asExpr() instanceof ClassAggregateLiteral
by mapping the final post-update node of a class aggregate initialization back to its aggregate literal, and updates tests and change notes accordingly.
- Introduces
ClassAggregateInitializerPostUpdateNode
and related predicates inExprNodes.qll
to link post-update nodes toClassAggregateLiteral
. - Updates existing IR-based dataflow tests to expect
{...}
nodes for class aggregate literals. - Adds new test cases in
asExpr/test.cpp
for class aggregate literal expressions and documents missing support for array aggregates.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
cpp/ql/test/library-tests/dataflow/fields/ir-path-flow.expected | Swapped out post-update labels for {...} to reflect the class aggregate literal. |
cpp/ql/test/library-tests/dataflow/dataflow-tests/localFlow-ir.expected | Inserted {...} expectation for class aggregate literal in local flow tests. |
cpp/ql/test/library-tests/dataflow/asExpr/test.cpp | Added asExpr={...} checks for struct aggregate literals; marked missing for arrays. |
cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ExprNodes.qll | Implemented post-update mapping for ClassAggregateLiteral and extended expr nodes. |
cpp/ql/lib/change-notes/2025-05-15-class-aggregate-literals.md | Documented the fix for asExpr() on class aggregate literals. |
* ``` | ||
* i.getUnconvertedResultExpression() instanceof ClassAggregateLiteral | ||
* ``` | ||
* and thus we don't automaticallt get a dataflow node for which: |
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.
Typo in comment: change 'automaticallt' to 'automatically'.
* and thus we don't automaticallt get a dataflow node for which: | |
* and thus we don't automatically get a dataflow node for which: |
Copilot uses AI. Check for mistakes.
@@ -442,6 +478,12 @@ private module Cached { | |||
final override Expr getConvertedExpr(int n) { exprNodeShouldBeIndirectOperand(this, result, n) } | |||
} | |||
|
|||
private class PostUpdateExprNode extends ExprNodeBase instanceof PostUpdateNode { |
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.
QL class declarations list superclasses and interfaces separated by commas. Replace 'extends ExprNodeBase instanceof PostUpdateNode' with 'extends ExprNodeBase, PostUpdateNode' to correctly inherit both.
private class PostUpdateExprNode extends ExprNodeBase instanceof PostUpdateNode { | |
private class PostUpdateExprNode extends ExprNodeBase, PostUpdateNode { |
Copilot uses AI. Check for mistakes.
The C++ IR represents an aggregate literal
x = {.a = 1, .b = 2}
as a sequence of field writes:and as such there is no instruction for which
getUnconvertedResultExpression()
gives the aggregate literal. And sinceNode.asExpr()
piggybacks ongetUnconvertedResultExpression()
for most nodesnode.asExpr() instanceof AggregateLiteral
also never holds. This gotcha has bitten users several times.This PR solves the problem for class aggregate literals. Since class aggregate literals are represented as a sequence of field writes (see above), we can pick the last field write's post-update node as the node to represent the aggregate literal expression. Semantically this makes sense since the last post-update node represents the state of the object after the last field has been initialized.
Sadly, since we don't model array writes using post-update nodes we can't use the same trick for
ArrayAggregateLiterals
. However, I think we should do that eventually. Once we do that we can apply a similar trick to that case. For now I think this solution at least solves half of the problems that's been reported.