[clang] [clang][bytecode] Check for errors in CXXDeleteExprs (PR #185189)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 7 05:22:17 PST 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/185189

e.g. `getOperatorDelete()` may return null for those.

>From 12f7561add34971c2c761bb207a2feb575f6f988 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 7 Mar 2026 14:19:29 +0100
Subject: [PATCH] [clang][bytecode] Check for errors in CXXDeleteExprs

e.g. `getOperatorDelete()` may return null for those.
---
 clang/lib/AST/ByteCode/Compiler.cpp    | 6 +++---
 clang/test/AST/ByteCode/new-delete.cpp | 9 +++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 93ad8eb26f29e..6d8b84a83a4ad 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4136,15 +4136,15 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
 
 template <class Emitter>
 bool Compiler<Emitter>::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
-  const Expr *Arg = E->getArgument();
-
+  if (E->containsErrors())
+    return false;
   const FunctionDecl *OperatorDelete = E->getOperatorDelete();
 
   if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
     return this->emitInvalidNewDeleteExpr(E, E);
 
   // Arg must be an lvalue.
-  if (!this->visit(Arg))
+  if (!this->visit(E->getArgument()))
     return false;
 
   return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp
index 4e2af79a24811..bf892e79a67ac 100644
--- a/clang/test/AST/ByteCode/new-delete.cpp
+++ b/clang/test/AST/ByteCode/new-delete.cpp
@@ -1135,6 +1135,15 @@ namespace NonLiteralType {
   }
 }
 
+namespace BrokenDelete {
+  constexpr void foo() {
+    F *f = /* missing */; // both-error {{unknown type name 'F'}} \
+                          // both-error {{expected expression}}
+
+    delete f;
+  }
+}
+
 #else
 /// Make sure we reject this prior to C++20
 constexpr int a() { // both-error {{never produces a constant expression}}



More information about the cfe-commits mailing list