[clang] aaf73ae - [clang][Interp] Use placement new to construct opcode args into vector

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 01:34:06 PST 2022


Author: Timm Bäder
Date: 2022-11-30T10:09:52+01:00
New Revision: aaf73ae266db44fce107a0b73fcb33527bfb52eb

URL: https://github.com/llvm/llvm-project/commit/aaf73ae266db44fce107a0b73fcb33527bfb52eb
DIFF: https://github.com/llvm/llvm-project/commit/aaf73ae266db44fce107a0b73fcb33527bfb52eb.diff

LOG: [clang][Interp] Use placement new to construct opcode args into vector

This way we're invoking the copy constructor, which might be necessary
if the argument is not trivially constructible.

Differential Revision: https://reviews.llvm.org/D138554

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeEmitter.cpp b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
index 9fd830bcc046..1d72a904556e 100644
--- a/clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -161,8 +161,10 @@ static void emit(Program &P, std::vector<char> &Code, const T &Val,
   }
 
   if constexpr (!std::is_pointer_v<T>) {
-    const char *Data = reinterpret_cast<const char *>(&Val);
-    Code.insert(Code.end(), Data, Data + Size);
+    // 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);


        


More information about the cfe-commits mailing list