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

Alan Zhao via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 14:47:29 PST 2024


================
@@ -1038,11 +1038,14 @@ void CodeGenFunction::EmitNewArrayInitializer(
     return true;
   };
 
+  const InitListExpr *ILE = dyn_cast<InitListExpr>(Init);
+  const CXXParenListInitExpr *CPLIE = dyn_cast<CXXParenListInitExpr>(Init);
+  const StringLiteral *SL = dyn_cast<StringLiteral>(Init);
   // If the initializer is an initializer list, first do the explicit elements.
-  if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
+  if (ILE || CPLIE || SL) {
----------------
alanzhao1 wrote:

OK, I've implemented a fix for this locally (but I haven't pushed my branch yet as I haven't written tests).

I'm running into a problem with the example:

```objc
char* x = new char[](@encode(int));
```

Clang produces the error:

```
$ bin/clang++ -c -o - -S -emit-llvm -std=c++20 ~/src/tests/encode.mii
/usr/local/google/home/ayzhao/src/tests/encode.mii:1:28: error: cannot compile this aggregate expression yet
    1 | const char* x = new char[](@encode(int));
      |                            ^~~~~~~~~~~~
1 error generated.
```

Clang emits the same error for a braced initialization-list:

```
$ bin/clang++ -c -o - -S -emit-llvm -std=c++20 ~/src/tests/encode.mii
/usr/local/google/home/ayzhao/src/tests/encode.mii:1:28: error: cannot compile this aggregate expression yet
    1 | const char* x = new char[]{@encode(int)};
      |                            ^~~~~~~~~~~~
1 error generated.
```

while GCC happily compiles both examples: https://godbolt.org/z/494x4ne5o

Given that braced initialization of a `ObjCEncodeExpr` is already broken in clang, I'm leaning towards maintaining the same behavior between clang's braced-initialization and parenthesized initialization here, but if others have any other ideas, I'm happy to hear them.

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


More information about the cfe-commits mailing list