[clang] [Clang][Sema] placement new initializes typedef array with correct size (PR #83124)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 16 00:51:04 PDT 2024
https://github.com/mahtohappy updated https://github.com/llvm/llvm-project/pull/83124
>From 517134e20bafba3509531cdd884d617df4d8d86f Mon Sep 17 00:00:00 2001
From: mahtohappy <Happy.Kumar at windriver.com>
Date: Tue, 27 Feb 2024 03:13:51 -0800
Subject: [PATCH] [Clang][Sema] placement new initializes typedef array with
correct size
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/TreeTransform.h | 14 ++++++++++++-
.../instantiate-new-placement-size.cpp | 20 +++++++++++++++++++
3 files changed, 35 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/instantiate-new-placement-size.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 515dffa28df186..1e4a35ab432d1d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -285,6 +285,8 @@ Bug Fixes to C++ Support
templates when determining the primary template of an explicit specialization.
- Fixed a crash in Microsoft compatibility mode where unqualified dependent base class
lookup searches the bases of an incomplete class.
+- placement new initializes typedef array with correct size
+ (`#41441 <https://github.com/llvm/llvm-project/issues/41441>`_)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 7389a48fe56fcc..0cda78650fcc83 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -12669,6 +12669,19 @@ TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
ArraySize = NewArraySize.get();
}
+ // Per C++0x [expr.new]p5, the type being constructed may be a
+ // typedef of an array type.
+ QualType AllocType = AllocTypeInfo->getType();
+ if (ArraySize) {
+ if (const ConstantArrayType *Array =
+ SemaRef.Context.getAsConstantArrayType(AllocType)) {
+ ArraySize = IntegerLiteral::Create(SemaRef.Context, Array->getSize(),
+ SemaRef.Context.getSizeType(),
+ E->getBeginLoc());
+ AllocType = Array->getElementType();
+ }
+ }
+
// Transform the placement arguments (if any).
bool ArgumentChanged = false;
SmallVector<Expr*, 8> PlacementArgs;
@@ -12730,7 +12743,6 @@ TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
return E;
}
- QualType AllocType = AllocTypeInfo->getType();
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
diff --git a/clang/test/SemaCXX/instantiate-new-placement-size.cpp b/clang/test/SemaCXX/instantiate-new-placement-size.cpp
new file mode 100644
index 00000000000000..7a29d3dee8491e
--- /dev/null
+++ b/clang/test/SemaCXX/instantiate-new-placement-size.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang -S -fno-discard-value-names -emit-llvm -o - %s | FileCheck %s
+// Issue no: 41441
+#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>();
+}
More information about the cfe-commits
mailing list