[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)
Mital Ashok via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 15 13:10:23 PST 2024
https://github.com/MitalAshok updated https://github.com/llvm/llvm-project/pull/78201
>From 730f7159c04cbb83fa18f50e8db32f6c5295ef6f Mon Sep 17 00:00:00 2001
From: Mital Ashok <mital at mitalashok.co.uk>
Date: Mon, 15 Jan 2024 18:31:06 +0000
Subject: [PATCH 1/2] [clang] Fix direct-initialization with new expressions
for arrays
Fixes #78183
---
clang/docs/ReleaseNotes.rst | 3 ++
.../clang/Basic/DiagnosticSemaKinds.td | 2 --
clang/lib/Sema/SemaExprCXX.cpp | 29 -------------------
clang/test/SemaCXX/new-delete.cpp | 21 +++++++-------
4 files changed, 14 insertions(+), 41 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7e130304c5c08f8..a2e757defb3c7fd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -937,6 +937,9 @@ Bug Fixes to AST Handling
- Fixed a bug where Template Instantiation failed to handle Lambda Expressions
with certain types of Attributes.
(`#76521 <https://github.com/llvm/llvm-project/issues/76521>`_)
+- Fixed a bug where the parenthesized initialization of arrays in a new
+ expression were rejected even in C++20 mode.
+ (`#78183 <https://github.com/llvm/llvm-project/issues/78183>`_)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 414779a7970ab8e..920edebfb703333 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7776,8 +7776,6 @@ def err_new_array_nonconst : Error<
"only the first dimension of an allocated array may have dynamic size">;
def err_new_array_size_unknown_from_init : Error<
"cannot determine allocated array size from initializer">;
-def err_new_array_init_args : Error<
- "array 'new' cannot have initialization arguments">;
def ext_new_paren_array_nonconst : ExtWarn<
"when type is in parentheses, array cannot have dynamic size">;
def err_placement_new_non_placement_delete : Error<
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 4ae04358d5df7c8..efb917f9bcd5045 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1946,25 +1946,6 @@ Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
Initializer);
}
-static bool isLegalArrayNewInitializer(CXXNewInitializationStyle Style,
- Expr *Init) {
- if (!Init)
- return true;
- if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init))
- return PLE->getNumExprs() == 0;
- if (isa<ImplicitValueInitExpr>(Init))
- return true;
- else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init))
- return !CCE->isListInitialization() &&
- CCE->getConstructor()->isDefaultConstructor();
- else if (Style == CXXNewInitializationStyle::List) {
- assert(isa<InitListExpr>(Init) &&
- "Shouldn't create list CXXConstructExprs for arrays.");
- return true;
- }
- return false;
-}
-
bool
Sema::isUnavailableAlignedAllocationFunction(const FunctionDecl &FD) const {
if (!getLangOpts().AlignedAllocationUnavailable)
@@ -2400,16 +2381,6 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
}
}
- // Array 'new' can't have any initializers except empty parentheses.
- // Initializer lists are also allowed, in C++11. Rely on the parser for the
- // dialect distinction.
- if (ArraySize && !isLegalArrayNewInitializer(InitStyle, Initializer)) {
- SourceRange InitRange(Exprs.front()->getBeginLoc(),
- Exprs.back()->getEndLoc());
- Diag(StartLoc, diag::err_new_array_init_args) << InitRange;
- return ExprError();
- }
-
// If we can perform the initialization, and we've not already done so,
// do it now.
if (!AllocType->isDependentType() &&
diff --git a/clang/test/SemaCXX/new-delete.cpp b/clang/test/SemaCXX/new-delete.cpp
index 0270e42b7389fec..07a8495a35f7561 100644
--- a/clang/test/SemaCXX/new-delete.cpp
+++ b/clang/test/SemaCXX/new-delete.cpp
@@ -1,7 +1,8 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++98
-// RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++11
-// RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++14
-// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null %std_cxx17-
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,until-cxx20 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++98
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,until-cxx20 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++11
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,until-cxx20 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++14
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,until-cxx20,cxx17 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++17
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++20
// FIXME Location is (frontend)
// cxx17-note@*:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
@@ -340,23 +341,23 @@ namespace PR5918 { // Look for template operator new overloads.
namespace Test1 {
void f() {
- (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
+ (void)new int[10](1, 2); // until-cxx20-error {{array initializer must be an initializer list}}
typedef int T[10];
- (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
+ (void)new T(1, 2); // until-cxx20-error {{array initializer must be an initializer list}}
}
template<typename T>
void g(unsigned i) {
- (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}}
+ (void)new T[1](i);
}
template<typename T>
void h(unsigned i) {
- (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}}
+ (void)new T(i); // until-cxx20-error {{array initializer must be an initializer list}}
}
template void h<unsigned>(unsigned);
-template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int[10]>' requested here}}
+template void h<unsigned[10]>(unsigned); // until-cxx20-note {{in instantiation of function template specialization 'Test1::h<unsigned int[10]>' requested here}}
}
@@ -556,7 +557,7 @@ namespace P12023 {
int main()
{
- CopyCounter* f = new CopyCounter[10](CopyCounter()); // expected-error {{cannot have initialization arguments}}
+ CopyCounter* f = new CopyCounter[10](CopyCounter()); // until-cxx20-error {{array initializer must be an initializer list}}
return 0;
}
}
>From 7cc65942b8705dbfeb8dd49bf00de59db3722fe8 Mon Sep 17 00:00:00 2001
From: Mital Ashok <mital.vaja at googlemail.com>
Date: Mon, 15 Jan 2024 21:10:16 +0000
Subject: [PATCH 2/2] Update clang/docs/ReleaseNotes.rst
Co-authored-by: cor3ntin <corentinjabot at gmail.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a2e757defb3c7fd..4fa6040602e5b96 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -938,7 +938,7 @@ Bug Fixes to AST Handling
with certain types of Attributes.
(`#76521 <https://github.com/llvm/llvm-project/issues/76521>`_)
- Fixed a bug where the parenthesized initialization of arrays in a new
- expression were rejected even in C++20 mode.
+ expression were incorrectly rejected in C++20 mode.
(`#78183 <https://github.com/llvm/llvm-project/issues/78183>`_)
Miscellaneous Bug Fixes
More information about the cfe-commits
mailing list