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

Maksim Levental via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 14 21:44:14 PDT 2023


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

>From d7c57d94d7e75ed1e0df87ebf3c4464e7b951453 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 b3c62d0d295129ad94f94fc641e789038c78f03a 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 cfb0771a5040d48f3a41108e541ed6f467438f4e 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 | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 30348e1c03f8c76..dcb214a88806916 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -274,7 +274,14 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
   if (Err)
     return std::move(Err);
 
-  auto PTU = Interp->Parse(Runtimes);
+  std::string runtimes = "";
+  if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin())
+    runtimes += "#include <new>\n";
+  else
+    runtimes += "void* operator new(__SIZE_TYPE__, void* __p) noexcept;\n";
+  runtimes += Runtimes;
+
+  auto PTU = Interp->Parse(runtimes);
   if (!PTU)
     return PTU.takeError();
 



More information about the cfe-commits mailing list