[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-16 Thread Mital Ashok via cfe-commits

https://github.com/MitalAshok closed 
https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-16 Thread Mital Ashok via cfe-commits

MitalAshok wrote:

Yeah #76976 supersedes this.

I would still recommend removing `diag::err_new_array_init_args` if only so:

```c++
int x[2](1, 2);
int* y = new int[2](1, 2);
```

have similar error messages.

https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-16 Thread Mital Ashok via cfe-commits

https://github.com/MitalAshok converted_to_draft 
https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread Alan Zhao via cfe-commits

alanzhao1 wrote:

> I believe the approach here is not sufficient and we have a current PR in 
> progress: #76976
> 
> we also need codegen work to implement this.

Correct - here are some examples that will cause this to fail:

```cpp
void foo(int n) {
  new int[n](1, 2);
}
```

```cpp
void bar() {
  new char[]("abcd");
}
```


https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik commented:

I believe the approach here is not sufficient and we have a current PR in 
progress: https://github.com/llvm/llvm-project/pull/76976

we also need codegen work to implement this.

https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread Mital Ashok via cfe-commits

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 
Date: Mon, 15 Jan 2024 18:31:06 +
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 `_)
+- Fixed a bug where the parenthesized initialization of arrays in a new
+  expression were rejected even in C++20 mode.
+  (`#78183 `_)
 
 Miscellaneous Bug Fixes
 ^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 414779a7970ab8e..920edebfb70 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(Init))
-return PLE->getNumExprs() == 0;
-  if (isa(Init))
-return true;
-  else if (CXXConstructExpr *CCE = dyn_cast(Init))
-return !CCE->isListInitialization() &&
-   CCE->getConstructor()->isDefaultConstructor();
-  else if (Style == CXXNewInitializationStyle::List) {
-assert(isa(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 -st

[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread via cfe-commits


@@ -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 `_)
+- Fixed a bug where the parenthesized initialization of arrays in a new
+  expression were rejected even in C++20 mode.

cor3ntin wrote:

```suggestion
  expression were incorrectly rejected in C++20 mode.
```

https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

Nice simplification, thanks.
I validated that this does not impact c++98, which just fails in parsing

https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread via cfe-commits

https://github.com/cor3ntin edited 
https://github.com/llvm/llvm-project/pull/78201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Mital Ashok (MitalAshok)


Changes

Fixes #78183

Just removed the check entirely. The diagnostic issued for trying to initialize 
an array (`array initializer must be an initializer list`) is much clearer 
(than `array 'new' cannot have initialization arguments`).


---
Full diff: https://github.com/llvm/llvm-project/pull/78201.diff


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+3) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (-2) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (-29) 
- (modified) clang/test/SemaCXX/new-delete.cpp (+11-10) 


``diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7e130304c5c08f..a2e757defb3c7f 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 `_)
+- Fixed a bug where the parenthesized initialization of arrays in a new
+  expression were rejected even in C++20 mode.
+  (`#78183 `_)
 
 Miscellaneous Bug Fixes
 ^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 414779a7970ab8..920edebfb70333 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 4ae04358d5df7c..efb917f9bcd504 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(Init))
-return PLE->getNumExprs() == 0;
-  if (isa(Init))
-return true;
-  else if (CXXConstructExpr *CCE = dyn_cast(Init))
-return !CCE->isListInitialization() &&
-   CCE->getConstructor()->isDefaultConstructor();
-  else if (Style == CXXNewInitializationStyle::List) {
-assert(isa(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 0270e42b7389fe..07a8495a35f756 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-n

[clang] [clang] Fix direct-initialization with new expressions for arrays (PR #78201)

2024-01-15 Thread Mital Ashok via cfe-commits

https://github.com/MitalAshok created 
https://github.com/llvm/llvm-project/pull/78201

Fixes #78183

Just removed the check entirely. The diagnostic issued for trying to initialize 
an array (`array initializer must be an initializer list`) is much clearer 
(than `array 'new' cannot have initialization arguments`).


>From 730f7159c04cbb83fa18f50e8db32f6c5295ef6f Mon Sep 17 00:00:00 2001
From: Mital Ashok 
Date: Mon, 15 Jan 2024 18:31:06 +
Subject: [PATCH] [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 7e130304c5c08f..a2e757defb3c7f 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 `_)
+- Fixed a bug where the parenthesized initialization of arrays in a new
+  expression were rejected even in C++20 mode.
+  (`#78183 `_)
 
 Miscellaneous Bug Fixes
 ^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 414779a7970ab8..920edebfb70333 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 4ae04358d5df7c..efb917f9bcd504 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(Init))
-return PLE->getNumExprs() == 0;
-  if (isa(Init))
-return true;
-  else if (CXXConstructExpr *CCE = dyn_cast(Init))
-return !CCE->isListInitialization() &&
-   CCE->getConstructor()->isDefaultConstructor();
-  else if (Style == CXXNewInitializationStyle::List) {
-assert(isa(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 0270e42b7389fe..07a8495a35f756 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-o