[clang] c592866 - [clang][Sema] Avoid assert when diagnosing address-space qualified new/delete (#178424)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 13 04:39:56 PST 2026


Author: Ayush Kumar Gaur
Date: 2026-02-13T20:39:51+08:00
New Revision: c592866bbad0b63155612e8b9d8977e6e9db1070

URL: https://github.com/llvm/llvm-project/commit/c592866bbad0b63155612e8b9d8977e6e9db1070
DIFF: https://github.com/llvm/llvm-project/commit/c592866bbad0b63155612e8b9d8977e6e9db1070.diff

LOG: [clang][Sema] Avoid assert when diagnosing address-space qualified new/delete (#178424)

### Whats the error

Clang could assert when diagnosing new or delete on types in
language-specific address spaces (e.g. OpenCL __local), instead of
emitting a normal error.

### Why it happened

The diagnostics used getAddressSpaceAttributePrintValue(), which assumes
target-specific address spaces and asserts for language-defined ones
like
OpenCL.

### Whats the Fix

Explicitly check for language defined address spaces in new/delete
diagnostics and emit the error directly, avoiding the crashing path, Add
a regression test.

Fixes #178319

Added: 
    clang/test/SemaCXX/address-space-new-delete.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaExprCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 83cd562c6f49b..dcb27c25d7110 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -290,6 +290,7 @@ Miscellaneous Clang Crashes Fixed
 - Fixed a crash when using loop hint with a value dependent argument inside a
   generic lambda. (#GH172289)
 - Fixed a crash in C++ overload resolution with ``_Atomic``-qualified argument types. (#GH170433)
+- Fixed an assertion when diagnosing address-space qualified ``new``/``delete`` in language-defined address spaces such as OpenCL ``__local``. (#GH178319)
 
 OpenACC Specific Changes
 ------------------------

diff  --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 91967a7a9ff97..366491fb4b2fc 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2676,8 +2676,9 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
   else if (AllocType.getAddressSpace() != LangAS::Default &&
            !getLangOpts().OpenCLCPlusPlus)
     return Diag(Loc, diag::err_address_space_qualified_new)
-      << AllocType.getUnqualifiedType()
-      << AllocType.getQualifiers().getAddressSpaceAttributePrintValue();
+           << AllocType.getUnqualifiedType()
+           << Qualifiers::getAddrSpaceAsString(AllocType.getAddressSpace());
+
   else if (getLangOpts().ObjCAutoRefCount) {
     if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
       QualType BaseAllocType = Context.getBaseElementType(AT);
@@ -4069,7 +4070,7 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
       return Diag(Ex.get()->getBeginLoc(),
                   diag::err_address_space_qualified_delete)
              << Pointee.getUnqualifiedType()
-             << Pointee.getQualifiers().getAddressSpaceAttributePrintValue();
+             << Qualifiers::getAddrSpaceAsString(Pointee.getAddressSpace());
 
     CXXRecordDecl *PointeeRD = nullptr;
     if (Pointee->isVoidType() && !isSFINAEContext()) {

diff  --git a/clang/test/SemaCXX/address-space-new-delete.cpp b/clang/test/SemaCXX/address-space-new-delete.cpp
new file mode 100644
index 0000000000000..56ce7c8bf567a
--- /dev/null
+++ b/clang/test/SemaCXX/address-space-new-delete.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++17 %s -verify -Wno-unknown-attributes
+
+typedef int LocalInt __attribute__((opencl_local));
+
+void test_new() {
+  int *p = new LocalInt[1]; // expected-error {{'new' cannot allocate objects of type 'int' in address space '__local'}}
+}
+
+void test_delete(LocalInt *p) {
+  delete p; // expected-error {{'delete' cannot delete objects of type 'int' in address space '__local'}}
+}


        


More information about the cfe-commits mailing list