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

Maksim Levental via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 21:37:39 PDT 2023


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

>From f2d35a0f0356d5ea570019bc02558bd5fc143afb 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 1/3] [clang][repl] fix `new`

---
 clang/lib/Interpreter/Interpreter.cpp | 2 +-
 clang/test/Interpreter/execute.cpp    | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 7968c62cbd3e7b3..ddfbc9ac01c6743 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -248,7 +248,7 @@ 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;
+    #include <new>
     void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, void*);
diff --git a/clang/test/Interpreter/execute.cpp b/clang/test/Interpreter/execute.cpp
index 6e73ed3927e8155..d54ab99749c1bac 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -20,4 +20,6 @@ auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, reinterpret_cast<unsigned long long
 inline int foo() { return 42; }
 int r3 = foo();
 
+#include <memory>
+
 %quit

>From a9799463a5d2a4bb2d788f871b454d5d3fa0add8 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental at gmail.com>
Date: Sat, 14 Oct 2023 19:26:04 -0500
Subject: [PATCH 2/3] Update clang/lib/Interpreter/Interpreter.cpp

Remove include new

Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
 clang/lib/Interpreter/Interpreter.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index ddfbc9ac01c6743..30348e1c03f8c76 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"(
-    #include <new>
     void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, void*);

>From d5e4a70a01054e667285f6b3ca77141ae63a71cb Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Sat, 14 Oct 2023 23:44:01 -0500
Subject: [PATCH 3/3] branch on isdarwin

---
 clang/lib/Interpreter/Interpreter.cpp         | 28 +------------------
 clang/test/Interpreter/execute.cpp            |  2 --
 .../unittests/Interpreter/InterpreterTest.cpp | 25 ++++++++++++++++-
 3 files changed, 25 insertions(+), 30 deletions(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 30348e1c03f8c76..a249a12b3a1aaf9 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -244,28 +244,6 @@ Interpreter::~Interpreter() {
   }
 }
 
-// These better to put in a runtime header but we can't. This is because we
-// can't find the precise resource directory in unittests so we have to hard
-// code them.
-const char *const Runtimes = R"(
-    void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*);
-    void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*);
-    void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, void*);
-    void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, float);
-    void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, double);
-    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]);
-    }
-    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);
-    }
-)";
-
 llvm::Expected<std::unique_ptr<Interpreter>>
 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
   llvm::Error Err = llvm::Error::success();
@@ -274,10 +252,6 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
   if (Err)
     return std::move(Err);
 
-  auto PTU = Interp->Parse(Runtimes);
-  if (!PTU)
-    return PTU.takeError();
-
   Interp->ValuePrintingInfo.resize(3);
   // FIXME: This is a ugly hack. Undo command checks its availability by looking
   // at the size of the PTU list. However we have parsed something in the
@@ -351,7 +325,7 @@ Interpreter::Parse(llvm::StringRef Code) {
       return std::move(E);
   }
 
-  // Tell the interpreter sliently ignore unused expressions since value
+  // Tell the interpreter silently ignore unused expressions since value
   // printing could cause it.
   getCompilerInstance()->getDiagnostics().setSeverity(
       clang::diag::warn_unused_expr, diag::Severity::Ignored, SourceLocation());
diff --git a/clang/test/Interpreter/execute.cpp b/clang/test/Interpreter/execute.cpp
index d54ab99749c1bac..6e73ed3927e8155 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -20,6 +20,4 @@ auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, reinterpret_cast<unsigned long long
 inline int foo() { return 42; }
 int r3 = foo();
 
-#include <memory>
-
 %quit
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 5f2911e9a7adad3..de2fba87ddd3b63 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -51,7 +51,30 @@ createInterpreter(const Args &ExtraArgs = {},
   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)));
+  (void)cantFail(interp->Parse(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 *);
+void __clang_Interpreter_SetValueNoAlloc(void *, void *, void *, float);
+void __clang_Interpreter_SetValueNoAlloc(void *, void *, void *, double);
+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]);
+}
+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);
+}
+)"));
+  return interp;
 }
 
 static size_t DeclsSize(TranslationUnitDecl *PTUDecl) {



More information about the cfe-commits mailing list