Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MathiasVP
Copy link
Contributor

The C++ IR represents an aggregate literal x = {.a = 1, .b = 2} as a sequence of field writes:

x.a = 1;
x.b = 2;

and as such there is no instruction for which getUnconvertedResultExpression() gives the aggregate literal. And since Node.asExpr() piggybacks on getUnconvertedResultExpression() for most nodes node.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.

@github-actions github-actions bot added the C++ label May 15, 2025
@MathiasVP MathiasVP marked this pull request as ready for review May 15, 2025 19:08
@Copilot Copilot AI review requested due to automatic review settings May 15, 2025 19:08
@MathiasVP MathiasVP requested a review from a team as a code owner May 15, 2025 19:08
Copy link
Contributor

@Copilot Copilot AI left a 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 in ExprNodes.qll to link post-update nodes to ClassAggregateLiteral.
  • 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:
Copy link
Preview

Copilot AI May 15, 2025

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'.

Suggested change
* 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 {
Copy link
Preview

Copilot AI May 15, 2025

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.

Suggested change
private class PostUpdateExprNode extends ExprNodeBase instanceof PostUpdateNode {
private class PostUpdateExprNode extends ExprNodeBase, PostUpdateNode {

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant