[clang] [clang][repl] fix `new` on Mac M1 (PR #69072)

Maksim Levental via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 22:30:41 PDT 2023


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/69072

>From 940da2de0fc92e8e0da64fd35ad08effb6093447 Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Sat, 14 Oct 2023 12:46:42 -0500
Subject: [PATCH] [clang][repl] fix `new`

---
 clang/lib/Interpreter/Interpreter.cpp         | 24 ++++++++++++-------
 .../unittests/Interpreter/InterpreterTest.cpp | 21 +++++++++++++---
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 7968c62cbd3e7b3..57514f2d0cc424d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -248,7 +248,6 @@ Interpreter::~Interpreter() {
 // can't find the precise resource directory in unittests so we have to hard
 // code them.
 const char *const Runtimes = R"(
-    void* operator new(__SIZE_TYPE__, void* __p) noexcept;
     void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, void*);
@@ -257,14 +256,9 @@ const char *const Runtimes = R"(
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, long double);
     void __clang_Interpreter_SetValueNoAlloc(void*,void*,void*,unsigned long long);
     template <class T, class = T (*)() /*disable for arrays*/>
-    void __clang_Interpreter_SetValueCopyArr(T* Src, void* Placement, unsigned long Size) {
-      for (auto Idx = 0; Idx < Size; ++Idx)
-        new ((void*)(((T*)Placement) + Idx)) T(Src[Idx]);
-    }
+    void __clang_Interpreter_SetValueCopyArr(T* Src, void* Placement, unsigned long Size);
     template <class T, unsigned long N>
-    void __clang_Interpreter_SetValueCopyArr(const T (*Src)[N], void* Placement, unsigned long Size) {
-      __clang_Interpreter_SetValueCopyArr(Src[0], Placement, Size);
-    }
+    void __clang_Interpreter_SetValueCopyArr(const T (*Src)[N], void* Placement, unsigned long Size);
 )";
 
 llvm::Expected<std::unique_ptr<Interpreter>>
@@ -762,6 +756,20 @@ __clang_Interpreter_SetValueNoAlloc(void *This, void *OutVal,
   VRef = Value(static_cast<Interpreter *>(This), OpaqueType);
 }
 
+template <class T, class>
+REPL_EXTERNAL_VISIBILITY void
+__clang_Interpreter_SetValueCopyArr(T *Src, void *Placement,
+                                    unsigned long Size) {
+  for (unsigned long Idx = 0; Idx < Size; ++Idx)
+    new ((void *)(((T *)Placement) + Idx)) T(Src[Idx]);
+}
+template <class T, unsigned long N>
+REPL_EXTERNAL_VISIBILITY void
+__clang_Interpreter_SetValueCopyArr(const T (*Src)[N], void *Placement,
+                                    unsigned long Size) {
+  __clang_Interpreter_SetValueCopyArr(Src[0], Placement, Size);
+}
+
 static void SetValueDataBasedOnQualType(Value &V, unsigned long long Data) {
   QualType QT = V.getType();
   if (const auto *ET = QT->getAs<EnumType>())
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 5f2911e9a7adad3..4963846dffa6f98 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -24,10 +24,13 @@
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/TargetSelect.h"
+#include "llvm/TargetParser/Host.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
+#include <iostream>
+
 using namespace clang;
 
 #if defined(_AIX)
@@ -45,13 +48,25 @@ static std::unique_ptr<Interpreter>
 createInterpreter(const Args &ExtraArgs = {},
                   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
+  if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) {
+    Args macOsArgs = {"-Xcc", "-isysroot",
+                      "/Applications/Xcode.app/Contents/Developer/Platforms/"
+                      "MacOSX.platform/Developer/SDKs/MacOSX.sdk"};
+    ClangArgs.insert(ClangArgs.end(), macOsArgs.begin(), macOsArgs.end());
+  }
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
   auto CB = clang::IncrementalCompilerBuilder();
   CB.SetCompilerArgs(ClangArgs);
   auto CI = cantFail(CB.CreateCpp());
   if (Client)
     CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
-  return cantFail(clang::Interpreter::create(std::move(CI)));
+  auto interp = cantFail(clang::Interpreter::create(std::move(CI)));
+  if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin())
+    (void)cantFail(interp->Parse("#include <new>"));
+  else
+    (void)cantFail(interp->Parse(
+        "void* operator new(__SIZE_TYPE__, void* __p) noexcept;"));
+  return interp;
 }
 
 static size_t DeclsSize(TranslationUnitDecl *PTUDecl) {
@@ -148,12 +163,12 @@ TEST(InterpreterTest, UndoCommand) {
   auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get());
 
   // Fail to undo.
-  auto Err1 = Interp->Undo();
+  auto Err1 = Interp->Undo(2);
   EXPECT_EQ("Operation failed. Too many undos",
             llvm::toString(std::move(Err1)));
   auto Err2 = Interp->Parse("int foo = 42;");
   EXPECT_TRUE(!!Err2);
-  auto Err3 = Interp->Undo(2);
+  auto Err3 = Interp->Undo(3);
   EXPECT_EQ("Operation failed. Too many undos",
             llvm::toString(std::move(Err3)));
 



More information about the cfe-commits mailing list