Skip to content

C++: Ignore destructor calls on temporaries in IR translation classes - #22754

Merged
MathiasVP merged 3 commits into
github:mainfrom
MathiasVP:ignore-path-dependent-destructors-in-translation-classes
Oct 6, 2026
Merged

MathiasVP merged 3 commits into
github:mainfrom
MathiasVP:ignore-path-dependent-destructors-in-translation-classes

Conversation

@MathiasVP

@MathiasVP MathiasVP commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Consider this snippet (which I also added as a test in this PR):

struct A { int a[2]; ~A(); };

void f(bool b) {
  const int &t = b ? A().a[0] : A().a[1];
  return;
}

Currently, the extractor incorrectly places each temporary's destructor call inside the conditional branches. However, an upcoming extractor fix will move those destructor calls to the return statement. This revealed a bug in the IR's placement of destructors.

IR construction only generates destructors for non-conditional destruction. We do this by filtering out those expressions here. However, we forgot to filter out the calls to conditional destructors inside the translation classes. For example here where we just return any destructor without checking if that destructor is conditional.

Unfortunately, I was unable to craft a testcase that fails on the current version of the extractor. So it's a bit hard to judge the impact of this PR. I did, however, check that this fixes the issue when running on a database extracted with CodeQL running the fixed version of the extractor.

@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Oct 5, 2026
@MathiasVP MathiasVP changed the title C++: Ignore destructor calls on temporaries in translation classes C++: Ignore destructor calls on temporaries in IR translation classes Oct 5, 2026
@MathiasVP MathiasVP added the C++ label Oct 5, 2026
@MathiasVP
MathiasVP marked this pull request as ready for review October 5, 2026 20:20
@MathiasVP
MathiasVP requested a review from a team as a code owner October 5, 2026 20:20
Copilot AI balanced review requested due to automatic review settings October 5, 2026 20:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Updates the C++ IR translation logic to ignore implicit destructor calls for temporaries created within conditional evaluation contexts, and adds a regression test covering conditional-operator temporaries with non-trivial destructors.

Changes:

  • Add temp_test13 demonstrating temporaries with destructors inside a ternary that returns an lvalue subobject.
  • Filter implicit destructor calls in IR translation to exclude “conditional temporary destructor calls”.
  • Update IR/AST expected outputs to match the new filtering behavior.
File Description
cpp/​ql/​test/​library-tests/​ir/​ir/​destructors_for_temps.cpp Adds a new test case and a struct with array + destructor to exercise conditional temporary destructor handling.
cpp/​ql/​test/​library-tests/​ir/​ir/​raw_ir.expected Updates the raw IR snapshot to include temp_test13 and its new control-flow/aliasing behavior.
cpp/​ql/​test/​library-tests/​ir/​ir/​aliased_ir.expected Updates aliased IR snapshot consistent with the new implicit-dtor filtering.
cpp/​ql/​test/​library-tests/​ir/​ir/​PrintAST.expected Updates AST snapshot showing the implicit destructor calls attached to the conditional expression.
cpp/​ql/​lib/​semmle/​code/​cpp/​ir/​implementation/​raw/​internal/​TranslatedStmt.qll Introduces filtered indexing for implicit destructor calls and uses it across statement child traversal.
cpp/​ql/​lib/​semmle/​code/​cpp/​ir/​implementation/​raw/​internal/​TranslatedExpr.qll Applies the same filtered indexing for expression-attached implicit destructor calls.
cpp/​ql/​lib/​semmle/​code/​cpp/​ir/​implementation/​raw/​internal/​TranslatedElement.qll Changes visibility of isConditionalTemporaryDestructorCall to support reuse in other internal modules.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cpp/ql/test/library-tests/ir/ir/destructors_for_temps.cpp

@jketema jketema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MathiasVP
MathiasVP merged commit 47d2033 into github:main Oct 6, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants