tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik, 
sepavloff.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is split-out from https://reviews.llvm.org/D134859 as it showed up there.

When the opcode argument is not trivially copyable, we can't just memcpy it 
into our code vector. Use placement new to copy it instead.

This is currently dead code without https://reviews.llvm.org/D134859.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138554

Files:
  clang/lib/AST/Interp/ByteCodeEmitter.cpp


Index: clang/lib/AST/Interp/ByteCodeEmitter.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -163,8 +163,15 @@
   }
 
   if constexpr (!std::is_pointer_v<T>) {
-    const char *Data = reinterpret_cast<const char *>(&Val);
-    Code.insert(Code.end(), Data, Data + Size);
+    if constexpr (std::is_trivially_copyable_v<T>) {
+      const char *Data = reinterpret_cast<const char *>(&Val);
+      Code.insert(Code.end(), Data, Data + Size);
+    } else {
+      // Construct the value directly into our storage vector.
+      size_t ValPos = Code.size();
+      Code.resize(Code.size() + Size);
+      new (Code.data() + ValPos) T(Val);
+    }
   } else {
     uint32_t ID = P.getOrCreateNativePointer(Val);
     const char *Data = reinterpret_cast<const char *>(&ID);


Index: clang/lib/AST/Interp/ByteCodeEmitter.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -163,8 +163,15 @@
   }
 
   if constexpr (!std::is_pointer_v<T>) {
-    const char *Data = reinterpret_cast<const char *>(&Val);
-    Code.insert(Code.end(), Data, Data + Size);
+    if constexpr (std::is_trivially_copyable_v<T>) {
+      const char *Data = reinterpret_cast<const char *>(&Val);
+      Code.insert(Code.end(), Data, Data + Size);
+    } else {
+      // Construct the value directly into our storage vector.
+      size_t ValPos = Code.size();
+      Code.resize(Code.size() + Size);
+      new (Code.data() + ValPos) T(Val);
+    }
   } else {
     uint32_t ID = P.getOrCreateNativePointer(Val);
     const char *Data = reinterpret_cast<const char *>(&ID);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to