================
@@ -960,9 +964,36 @@ CIRGenModule::getConstantArrayFromStringLiteral(const 
StringLiteral *e) {
     return builder.getString(str, eltTy, finalSize);
   }
 
-  errorNYI(e->getSourceRange(),
-           "getConstantArrayFromStringLiteral: wide characters");
-  return mlir::Attribute();
+  auto arrayTy = mlir::dyn_cast<cir::ArrayType>(convertType(e->getType()));
+  assert(arrayTy && "string literals must be emitted as an array type");
+
+  auto arrayEltTy = mlir::dyn_cast<cir::IntType>(arrayTy.getElementType());
+  assert(arrayEltTy &&
+         "string literal elements must be emitted as integral type");
+
+  auto arraySize = arrayTy.getSize();
+  auto literalSize = e->getLength();
+
+  // Collect the code units.
+  SmallVector<uint32_t, 32> elementValues;
+  elementValues.reserve(arraySize);
+  for (unsigned i = 0; i < literalSize; ++i)
+    elementValues.push_back(e->getCodeUnit(i));
+  elementValues.resize(arraySize);
+
+  // If the string is full of null bytes, emit a #cir.zero instead.
+  if (std::all_of(elementValues.begin(), elementValues.end(),
+                  [](uint32_t x) { return x == 0; }))
+    return cir::ZeroAttr::get(arrayTy);
+
+  // Otherwise emit a constant array holding the characters.
+  SmallVector<mlir::Attribute, 32> elements;
+  elements.reserve(arraySize);
+  for (uint64_t i = 0; i < arraySize; ++i)
+    elements.push_back(cir::IntAttr::get(arrayEltTy, elementValues[i]));
----------------
xlauko wrote:

```suggestion
  for (uint32_t elementValue : elementValues)
    elements.push_back(cir::IntAttr::get(arrayEltTy, elementValues));
```

https://github.com/llvm/llvm-project/pull/171541
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to