[clang] [Clang][Sema] placement new initializes typedef array with correct size (PR #83124)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 27 03:25:51 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (mahtohappy)

<details>
<summary>Changes</summary>

When in-place new-ing a local variable of an array of trivial type, the generated code calls 'memset' with the correct size of the array. 
#fixes 41441

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


2 Files Affected:

- (modified) clang/lib/Sema/TreeTransform.h (+1-1) 
- (added) clang/test/CodeGen/instantiate-new-placement-size.cpp (+19) 


``````````diff
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 7389a48fe56fcc..0a46deda194826 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -12731,7 +12731,7 @@ TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
   }
 
   QualType AllocType = AllocTypeInfo->getType();
-  if (!ArraySize) {
+  if (ArraySize) {
     // If no array size was specified, but the new expression was
     // instantiated with an array type (e.g., "new T" where T is
     // instantiated with "int[4]"), extract the outer bound from the
diff --git a/clang/test/CodeGen/instantiate-new-placement-size.cpp b/clang/test/CodeGen/instantiate-new-placement-size.cpp
new file mode 100644
index 00000000000000..eaf013a57a6176
--- /dev/null
+++ b/clang/test/CodeGen/instantiate-new-placement-size.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang -S -emit-llvm -o - %s | FileCheck %s
+#include <new>
+
+// CHECK: call void @llvm.memset.p0.i64(ptr align 1 %x, i8 0, i64 8, i1 false)
+// CHECK: call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 32, i1 false)
+template <typename TYPE>
+void f()
+{
+    typedef TYPE TArray[8];
+
+    TArray x;
+    new(&x) TArray();
+}
+
+int main()
+{
+    f<char>();
+    f<int>();
+}

``````````

</details>


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


More information about the cfe-commits mailing list