[clang] [clang][Sema] Avoid assert when diagnosing address-space qualified new/delete (PR #178424)
Ayush Kumar Gaur via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 8 11:54:57 PST 2026
https://github.com/Ayush3941 updated https://github.com/llvm/llvm-project/pull/178424
>From 092d180df50f99e8bc81a0328e74dc249434614f Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 28 Jan 2026 08:46:08 -0500
Subject: [PATCH 01/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete
---
clang/lib/Sema/SemaExprCXX.cpp | 21 +++++++++++++++++--
.../test/SemaCXX/address-space-new-delete.cpp | 13 ++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaCXX/address-space-new-delete.cpp
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 91967a7a9ff97..b2a1a4dfa5e9c 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2653,6 +2653,22 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
ResultType, AllocTypeInfo, Range, DirectInitRange);
}
+// Format an address space for diagnostics without assuming it maps to a
+// target-specific value. Language-specific spaces (e.g. OpenCL) are rendered
+// using their spelled qualifiers, while target-specific ones are printed as the
+// numeric attribute value for compatibility with existing messages.
+
+static std::string formatAddressSpaceForDiag(LangAS AS,
+ const LangOptions &LangOpts) {
+ PrintingPolicy PP(LangOpts);
+ Qualifiers Q;
+ Q.setAddressSpace(AS);
+ std::string S;
+ llvm::raw_string_ostream OS(S);
+ Q.print(OS, PP, false);
+ return OS.str();
+}
+
bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
SourceRange R) {
// C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
@@ -2677,7 +2693,8 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
!getLangOpts().OpenCLCPlusPlus)
return Diag(Loc, diag::err_address_space_qualified_new)
<< AllocType.getUnqualifiedType()
- << AllocType.getQualifiers().getAddressSpaceAttributePrintValue();
+ << formatAddressSpaceForDiag(AllocType.getAddressSpace(), getLangOpts());
+
else if (getLangOpts().ObjCAutoRefCount) {
if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
QualType BaseAllocType = Context.getBaseElementType(AT);
@@ -4069,7 +4086,7 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
return Diag(Ex.get()->getBeginLoc(),
diag::err_address_space_qualified_delete)
<< Pointee.getUnqualifiedType()
- << Pointee.getQualifiers().getAddressSpaceAttributePrintValue();
+ << formatAddressSpaceForDiag(Pointee.getAddressSpace(), getLangOpts());
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..2d1d4352b8dd9
--- /dev/null
+++ b/clang/test/SemaCXX/address-space-new-delete.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -std=c++17 %s -verify
+
+typedef int FOO __attribute__((opencl_local));
+
+void test_new() {
+ int *p = new FOO[1];
+ // expected-error at -1 {{'new' cannot allocate objects of type 'int' in address space '__local'}}
+}
+
+void test_delete(FOO *p) {
+ delete p;
+ // expected-error at -1 {{cannot delete objects of type 'int' in address space '__local'}}
+}
>From 342773363ca684fefc55a4f9e168d167f1a0b70f Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 28 Jan 2026 08:49:12 -0500
Subject: [PATCH 02/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format
---
clang/lib/Sema/SemaExprCXX.cpp | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index b2a1a4dfa5e9c..9fd28ca43d8c5 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2692,8 +2692,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()
- << formatAddressSpaceForDiag(AllocType.getAddressSpace(), getLangOpts());
+ << AllocType.getUnqualifiedType()
+ << formatAddressSpaceForDiag(AllocType.getAddressSpace(),
+ getLangOpts());
else if (getLangOpts().ObjCAutoRefCount) {
if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
@@ -4086,7 +4087,8 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
return Diag(Ex.get()->getBeginLoc(),
diag::err_address_space_qualified_delete)
<< Pointee.getUnqualifiedType()
- << formatAddressSpaceForDiag(Pointee.getAddressSpace(), getLangOpts());
+ << formatAddressSpaceForDiag(Pointee.getAddressSpace(),
+ getLangOpts());
CXXRecordDecl *PointeeRD = nullptr;
if (Pointee->isVoidType() && !isSFINAEContext()) {
>From 12c7a0520311ceee0e9eca322ad4d8c8fcc1af87 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 28 Jan 2026 09:17:32 -0500
Subject: [PATCH 03/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v2
---
clang/lib/Sema/SemaExprCXX.cpp | 1 +
clang/test/SemaCXX/address-space-new-delete.cpp | 4 ++--
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 9fd28ca43d8c5..f8b1d11c7e9ac 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -50,6 +50,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TypeSize.h"
+#include "llvm/Support/raw_ostream.h"
#include <optional>
using namespace clang;
using namespace sema;
diff --git a/clang/test/SemaCXX/address-space-new-delete.cpp b/clang/test/SemaCXX/address-space-new-delete.cpp
index 2d1d4352b8dd9..6cbadc4aa784a 100644
--- a/clang/test/SemaCXX/address-space-new-delete.cpp
+++ b/clang/test/SemaCXX/address-space-new-delete.cpp
@@ -4,10 +4,10 @@ typedef int FOO __attribute__((opencl_local));
void test_new() {
int *p = new FOO[1];
- // expected-error at -1 {{'new' cannot allocate objects of type 'int' in address space '__local'}}
+ // expected-error at -1 {{'new' cannot allocate objects of type 'int' in address space}}
}
void test_delete(FOO *p) {
delete p;
- // expected-error at -1 {{cannot delete objects of type 'int' in address space '__local'}}
+ // expected-error at -1 {{cannot delete objects of type 'int' in address space}}
}
>From 8af7abe1e549e6f6f9880f097ba2576c57bb3c73 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 28 Jan 2026 09:47:48 -0500
Subject: [PATCH 04/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v3
---
clang/lib/Sema/SemaExprCXX.cpp | 3 +++
clang/test/SemaCXX/address-space-new-delete.cpp | 4 ++--
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f8b1d11c7e9ac..a51ea28210126 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2661,6 +2661,9 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
static std::string formatAddressSpaceForDiag(LangAS AS,
const LangOptions &LangOpts) {
+ if (isTargetAddressSpace(AS))
+ return llvm::utostr(toTargetAddressSpace(AS));
+
PrintingPolicy PP(LangOpts);
Qualifiers Q;
Q.setAddressSpace(AS);
diff --git a/clang/test/SemaCXX/address-space-new-delete.cpp b/clang/test/SemaCXX/address-space-new-delete.cpp
index 6cbadc4aa784a..3bfb75f11b290 100644
--- a/clang/test/SemaCXX/address-space-new-delete.cpp
+++ b/clang/test/SemaCXX/address-space-new-delete.cpp
@@ -1,10 +1,10 @@
// RUN: %clang_cc1 -std=c++17 %s -verify
-typedef int FOO __attribute__((opencl_local));
+typedef int FOO __attribute__((address_space(1)));
void test_new() {
int *p = new FOO[1];
- // expected-error at -1 {{'new' cannot allocate objects of type 'int' in address space}}
+ // expected-error at -1 {{cannot allocate objects of type 'int' in address space}}
}
void test_delete(FOO *p) {
>From c13bdad10dc68c7bf303205dd611e141461e3feb Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 28 Jan 2026 22:37:05 -0500
Subject: [PATCH 05/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v3
---
clang/lib/Sema/SemaExprCXX.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index a51ea28210126..f141a86d27bea 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -50,7 +50,6 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TypeSize.h"
-#include "llvm/Support/raw_ostream.h"
#include <optional>
using namespace clang;
using namespace sema;
>From ff001e16ebd1f600eb6365fd485d738dca83d591 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Thu, 29 Jan 2026 06:06:52 -0500
Subject: [PATCH 06/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v3 with more relevant test
---
clang/test/SemaCXX/address-space-new-delete.cpp | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/clang/test/SemaCXX/address-space-new-delete.cpp b/clang/test/SemaCXX/address-space-new-delete.cpp
index 3bfb75f11b290..8cae8e017f5ea 100644
--- a/clang/test/SemaCXX/address-space-new-delete.cpp
+++ b/clang/test/SemaCXX/address-space-new-delete.cpp
@@ -1,13 +1,11 @@
-// RUN: %clang_cc1 -std=c++17 %s -verify
+// RUN: %clang_cc1 -std=c++17 %s -verify -Wno-unknown-attributes
-typedef int FOO __attribute__((address_space(1)));
+typedef int LocalInt __attribute__((opencl_local));
void test_new() {
- int *p = new FOO[1];
- // expected-error at -1 {{cannot allocate objects of type 'int' in address space}}
+ int *p = new LocalInt[1]; // expected-error {{cannot allocate objects of type 'int' in address space}}
}
-void test_delete(FOO *p) {
- delete p;
- // expected-error at -1 {{cannot delete objects of type 'int' in address space}}
+void test_delete(LocalInt *p) {
+ delete p; // expected-error {{cannot delete objects of type 'int' in address space}}
}
>From 5db16d8a43781fece81928422f854d143dccd464 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Thu, 29 Jan 2026 21:18:40 -0500
Subject: [PATCH 07/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v4
---
clang/lib/Sema/SemaExprCXX.cpp | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f141a86d27bea..bacb900224a03 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2655,23 +2655,21 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
// Format an address space for diagnostics without assuming it maps to a
// target-specific value. Language-specific spaces (e.g. OpenCL) are rendered
-// using their spelled qualifiers, while target-specific ones are printed as the
-// numeric attribute value for compatibility with existing messages.
+// using their spelled qualifiers, and return whole qual type
-static std::string formatAddressSpaceForDiag(LangAS AS,
+static std::string formatAddressSpaceForDiag(QualType T,
const LangOptions &LangOpts) {
+ LangAS AS = T.getAddressSpace();
if (isTargetAddressSpace(AS))
return llvm::utostr(toTargetAddressSpace(AS));
-
PrintingPolicy PP(LangOpts);
- Qualifiers Q;
- Q.setAddressSpace(AS);
std::string S;
llvm::raw_string_ostream OS(S);
- Q.print(OS, PP, false);
+ T.print(OS, PP);
return OS.str();
}
+
bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
SourceRange R) {
// C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
@@ -2696,7 +2694,7 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
!getLangOpts().OpenCLCPlusPlus)
return Diag(Loc, diag::err_address_space_qualified_new)
<< AllocType.getUnqualifiedType()
- << formatAddressSpaceForDiag(AllocType.getAddressSpace(),
+ << formatAddressSpaceForDiag(AllocType,
getLangOpts());
else if (getLangOpts().ObjCAutoRefCount) {
@@ -4090,7 +4088,7 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
return Diag(Ex.get()->getBeginLoc(),
diag::err_address_space_qualified_delete)
<< Pointee.getUnqualifiedType()
- << formatAddressSpaceForDiag(Pointee.getAddressSpace(),
+ << formatAddressSpaceForDiag(Pointee,
getLangOpts());
CXXRecordDecl *PointeeRD = nullptr;
>From cf8daa36c1ead2f369d9edfee4568ff93732596b Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Thu, 29 Jan 2026 21:20:05 -0500
Subject: [PATCH 08/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v4
---
clang/lib/Sema/SemaExprCXX.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index bacb900224a03..ee2b3989e3b57 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2669,7 +2669,6 @@ static std::string formatAddressSpaceForDiag(QualType T,
return OS.str();
}
-
bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
SourceRange R) {
// C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
@@ -2694,8 +2693,7 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
!getLangOpts().OpenCLCPlusPlus)
return Diag(Loc, diag::err_address_space_qualified_new)
<< AllocType.getUnqualifiedType()
- << formatAddressSpaceForDiag(AllocType,
- getLangOpts());
+ << formatAddressSpaceForDiag(AllocType, getLangOpts());
else if (getLangOpts().ObjCAutoRefCount) {
if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
@@ -4088,8 +4086,7 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
return Diag(Ex.get()->getBeginLoc(),
diag::err_address_space_qualified_delete)
<< Pointee.getUnqualifiedType()
- << formatAddressSpaceForDiag(Pointee,
- getLangOpts());
+ << formatAddressSpaceForDiag(Pointee, getLangOpts());
CXXRecordDecl *PointeeRD = nullptr;
if (Pointee->isVoidType() && !isSFINAEContext()) {
>From aeb3dcd565e44703d71a88fda8b976b28a628bd6 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Thu, 29 Jan 2026 21:54:19 -0500
Subject: [PATCH 09/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format v5
---
clang/lib/Sema/SemaExprCXX.cpp | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index ee2b3989e3b57..8844fdf532bc0 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2658,15 +2658,25 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
// using their spelled qualifiers, and return whole qual type
static std::string formatAddressSpaceForDiag(QualType T,
- const LangOptions &LangOpts) {
+ const LangOptions &LangOpts,
+ const ASTContext &Ctx) {
LangAS AS = T.getAddressSpace();
if (isTargetAddressSpace(AS))
return llvm::utostr(toTargetAddressSpace(AS));
+
PrintingPolicy PP(LangOpts);
- std::string S;
- llvm::raw_string_ostream OS(S);
+
+ std::string Sugared, Desugared;
+ llvm::raw_string_ostream OS(Sugared);
+ llvm::raw_string_ostream KO(Desugared);
+
+ // Sugared type (e.g. LocalInt / FOO)
T.print(OS, PP);
- return OS.str();
+
+ // Desugared type (e.g. __local int)
+ T.getDesugaredType(Ctx).print(KO, PP);
+
+ return OS.str() + " (" + KO.str() + ")";
}
bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
@@ -2693,7 +2703,7 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
!getLangOpts().OpenCLCPlusPlus)
return Diag(Loc, diag::err_address_space_qualified_new)
<< AllocType.getUnqualifiedType()
- << formatAddressSpaceForDiag(AllocType, getLangOpts());
+ << formatAddressSpaceForDiag(AllocType, getLangOpts(), Context);
else if (getLangOpts().ObjCAutoRefCount) {
if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
@@ -4086,7 +4096,7 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
return Diag(Ex.get()->getBeginLoc(),
diag::err_address_space_qualified_delete)
<< Pointee.getUnqualifiedType()
- << formatAddressSpaceForDiag(Pointee, getLangOpts());
+ << formatAddressSpaceForDiag(Pointee, getLangOpts(), Context);
CXXRecordDecl *PointeeRD = nullptr;
if (Pointee->isVoidType() && !isSFINAEContext()) {
>From 26226928d7769610290be1263f963b6d676ae632 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Thu, 29 Jan 2026 21:55:54 -0500
Subject: [PATCH 10/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format printing full qual type
---
clang/lib/Sema/SemaExprCXX.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 8844fdf532bc0..9649dc121f572 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2670,10 +2670,10 @@ static std::string formatAddressSpaceForDiag(QualType T,
llvm::raw_string_ostream OS(Sugared);
llvm::raw_string_ostream KO(Desugared);
- // Sugared type (e.g. LocalInt / FOO)
+
T.print(OS, PP);
- // Desugared type (e.g. __local int)
+
T.getDesugaredType(Ctx).print(KO, PP);
return OS.str() + " (" + KO.str() + ")";
>From c580e1e4fc0625e57a8f1734a5c5d99b6331ad98 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Thu, 29 Jan 2026 21:56:26 -0500
Subject: [PATCH 11/14] [clang][Sema] Fix crash when diagnosing address-space
qualified new/delete with fixed format code
---
clang/lib/Sema/SemaExprCXX.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 9649dc121f572..7d28d8743edcc 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2670,10 +2670,8 @@ static std::string formatAddressSpaceForDiag(QualType T,
llvm::raw_string_ostream OS(Sugared);
llvm::raw_string_ostream KO(Desugared);
-
T.print(OS, PP);
-
T.getDesugaredType(Ctx).print(KO, PP);
return OS.str() + " (" + KO.str() + ")";
>From a5399263bbb7c6a9ff7b1466314178ee6db01a49 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Tue, 3 Feb 2026 07:21:56 -0500
Subject: [PATCH 12/14] adding release notes
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaExprCXX.cpp | 7 +------
2 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e22e7e0fce05c..8f972587c3277 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -223,6 +223,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 7d28d8743edcc..52f6c0b56704d 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2662,18 +2662,13 @@ static std::string formatAddressSpaceForDiag(QualType T,
const ASTContext &Ctx) {
LangAS AS = T.getAddressSpace();
if (isTargetAddressSpace(AS))
- return llvm::utostr(toTargetAddressSpace(AS));
-
+ return Qualifiers::getAddrSpaceAsString(AS);
PrintingPolicy PP(LangOpts);
-
std::string Sugared, Desugared;
llvm::raw_string_ostream OS(Sugared);
llvm::raw_string_ostream KO(Desugared);
-
T.print(OS, PP);
-
T.getDesugaredType(Ctx).print(KO, PP);
-
return OS.str() + " (" + KO.str() + ")";
}
>From f105ebe6d0b67ceea7d9d76ce1fde05bc5151d16 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Sun, 8 Feb 2026 09:13:16 -0500
Subject: [PATCH 13/14] Reverting back to address space only
---
clang/lib/Sema/SemaExprCXX.cpp | 23 ++---------------------
1 file changed, 2 insertions(+), 21 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 52f6c0b56704d..366491fb4b2fc 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2653,25 +2653,6 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
ResultType, AllocTypeInfo, Range, DirectInitRange);
}
-// Format an address space for diagnostics without assuming it maps to a
-// target-specific value. Language-specific spaces (e.g. OpenCL) are rendered
-// using their spelled qualifiers, and return whole qual type
-
-static std::string formatAddressSpaceForDiag(QualType T,
- const LangOptions &LangOpts,
- const ASTContext &Ctx) {
- LangAS AS = T.getAddressSpace();
- if (isTargetAddressSpace(AS))
- return Qualifiers::getAddrSpaceAsString(AS);
- PrintingPolicy PP(LangOpts);
- std::string Sugared, Desugared;
- llvm::raw_string_ostream OS(Sugared);
- llvm::raw_string_ostream KO(Desugared);
- T.print(OS, PP);
- T.getDesugaredType(Ctx).print(KO, PP);
- return OS.str() + " (" + KO.str() + ")";
-}
-
bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
SourceRange R) {
// C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
@@ -2696,7 +2677,7 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
!getLangOpts().OpenCLCPlusPlus)
return Diag(Loc, diag::err_address_space_qualified_new)
<< AllocType.getUnqualifiedType()
- << formatAddressSpaceForDiag(AllocType, getLangOpts(), Context);
+ << Qualifiers::getAddrSpaceAsString(AllocType.getAddressSpace());
else if (getLangOpts().ObjCAutoRefCount) {
if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
@@ -4089,7 +4070,7 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
return Diag(Ex.get()->getBeginLoc(),
diag::err_address_space_qualified_delete)
<< Pointee.getUnqualifiedType()
- << formatAddressSpaceForDiag(Pointee, getLangOpts(), Context);
+ << Qualifiers::getAddrSpaceAsString(Pointee.getAddressSpace());
CXXRecordDecl *PointeeRD = nullptr;
if (Pointee->isVoidType() && !isSFINAEContext()) {
>From 404b7a3884b178c0f077c9d52bc36ca9f5270b0c Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Sun, 8 Feb 2026 14:53:45 -0500
Subject: [PATCH 14/14] Veryfied full message in test
---
clang/test/SemaCXX/address-space-new-delete.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/test/SemaCXX/address-space-new-delete.cpp b/clang/test/SemaCXX/address-space-new-delete.cpp
index 8cae8e017f5ea..56ce7c8bf567a 100644
--- a/clang/test/SemaCXX/address-space-new-delete.cpp
+++ b/clang/test/SemaCXX/address-space-new-delete.cpp
@@ -3,9 +3,9 @@
typedef int LocalInt __attribute__((opencl_local));
void test_new() {
- int *p = new LocalInt[1]; // expected-error {{cannot allocate objects of type 'int' in address space}}
+ 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 {{cannot delete objects of type 'int' in address space}}
+ delete p; // expected-error {{'delete' cannot delete objects of type 'int' in address space '__local'}}
}
More information about the cfe-commits
mailing list