[clang] [clang] Fix parenthesized list initialization of arrays not working with `new` (PR #76976)

Mital Ashok via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 08:38:44 PST 2024


================
@@ -1561,16 +1577,21 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
   // 1. Build a call to the allocation function.
   FunctionDecl *allocator = E->getOperatorNew();
 
-  // If there is a brace-initializer, cannot allocate fewer elements than inits.
+  // If there is a brace-initializer or C++20 parenthesized initializer, cannot
+  // allocate fewer elements than inits.
   unsigned minElements = 0;
   if (E->isArray() && E->hasInitializer()) {
-    const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer());
-    if (ILE && ILE->isStringLiteralInit())
+    const Expr *Init = E->getInitializer();
+    const InitListExpr *ILE = dyn_cast<InitListExpr>(Init);
+    const CXXParenListInitExpr *CPLIE = dyn_cast<CXXParenListInitExpr>(Init);
+    if ((ILE && ILE->isStringLiteralInit()) || isa<StringLiteral>(Init)) {
----------------
MitalAshok wrote:

You forget to `IgnoreParenImpCasts` or check for `ObjCEncodeExpr`. Currently:

```c++
// seg fault
int n = 0; char* p = new char[n](("a"));

// throws bad_alloc (expected)
int n = 0; char* p = new char[n](("a"));
```

https://github.com/llvm/llvm-project/pull/76976


More information about the cfe-commits mailing list