[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 13:24:17 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:

Thanks again for catching these - they both cause crashes.

For the `__extension__` example, we have a `StringLiteral` wrapped in a `UnaryOperator`

```
|-VarDecl 0x561d5f1ba190 </usr/local/google/home/ayzhao/src/tests/extension.cc:1:1, col:42> col:7 x 'char *' cinit
| `-CXXNewExpr 0x561d5f1d8580 <col:11, col:42> 'char *' array Function 0x561d5f1ba990 'operator new[]' 'void *(unsigned long)'
|   |-IntegerLiteral 0x561d5f1d8530 <col:15> 'unsigned long' 5
|   `-UnaryOperator 0x561d5f1ba2f0 <col:22, col:36> 'char[5]' prefix '__extension__' cannot overflow
|     `-StringLiteral 0x561d5f1ba2d0 <col:36> 'char[5]' "asdf"
```

and for the Objective-C++ example, we have a `ObjCEncodeExpr`:

```
|-VarDecl 0x55b8a4d0a150 </usr/local/google/home/ayzhao/src/tests/encode.mii:1:1, col:34> col:7 x 'char *' cinit
| `-CXXNewExpr 0x55b8a4d2a560 <col:11, col:34> 'char *' array Function 0x55b8a4d0a940 'operator new[]' 'void *(unsigned long)'
|   |-IntegerLiteral 0x55b8a4d2a510 <col:15> 'unsigned long' 2
|   `-ObjCEncodeExpr 0x55b8a4d0a2a0 <col:22, col:33> 'char[2]' 'int'
```

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


More information about the cfe-commits mailing list