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

Maksim Levental via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 20:38:46 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 9a071b39c2b31aa9bb1c2dfdd5de139666a06c1d 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/ClangReplPrologue.h    | 26 +++++++++++++++++
 clang/test/Interpreter/code-undo.cpp          |  5 +++-
 clang/test/Interpreter/const.cpp              |  6 ++--
 .../Interpreter/disambiguate-decl-stmt.cpp    |  6 ++--
 clang/test/Interpreter/dynamic-library.cpp    |  4 ++-
 clang/test/Interpreter/execute-stmts.cpp      |  5 ++--
 clang/test/Interpreter/execute-weak.cpp       |  4 ++-
 clang/test/Interpreter/execute.cpp            |  7 +++--
 clang/test/Interpreter/fail.cpp               |  5 +++-
 clang/test/Interpreter/global-dtor.cpp        |  4 ++-
 clang/test/Interpreter/inline-virtual.cpp     |  6 ++--
 clang/test/Interpreter/lambda.cpp             |  7 +++--
 clang/test/Interpreter/multiline.cpp          |  4 ++-
 clang/test/Interpreter/plugins.cpp            |  4 ++-
 clang/test/Interpreter/simple-exception.cpp   |  4 ++-
 clang/test/Interpreter/stmt-serialization.cpp |  4 ++-
 17 files changed, 81 insertions(+), 48 deletions(-)
 create mode 100644 clang/test/Interpreter/ClangReplPrologue.h

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/ClangReplPrologue.h b/clang/test/Interpreter/ClangReplPrologue.h
new file mode 100644
index 000000000000000..4ae820cd35e7da8
--- /dev/null
+++ b/clang/test/Interpreter/ClangReplPrologue.h
@@ -0,0 +1,26 @@
+//
+// Created by maksim on 10/19/23.
+//
+
+#ifndef LLVM_CLANGREPLPROLOGUE_H
+#define LLVM_CLANGREPLPROLOGUE_H
+
+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);
+}
+
+#endif //LLVM_CLANGREPLPROLOGUE_H
diff --git a/clang/test/Interpreter/code-undo.cpp b/clang/test/Interpreter/code-undo.cpp
index 83ade0ec9158ba3..6aa9c7fd104a5ed 100644
--- a/clang/test/Interpreter/code-undo.cpp
+++ b/clang/test/Interpreter/code-undo.cpp
@@ -1,5 +1,8 @@
 // UNSUPPORTED: system-aix
-// RUN: cat %s | clang-repl | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
+
 extern "C" int printf(const char *, ...);
 int x1 = 0;
 int x2 = 42;
diff --git a/clang/test/Interpreter/const.cpp b/clang/test/Interpreter/const.cpp
index 4b6ce65e3643e64..91db774a4492da8 100644
--- a/clang/test/Interpreter/const.cpp
+++ b/clang/test/Interpreter/const.cpp
@@ -2,8 +2,10 @@
 // see https://github.com/llvm/llvm-project/issues/68092
 // XFAIL: system-windows
 
-// RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -O2 -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char*, ...);
 
diff --git a/clang/test/Interpreter/disambiguate-decl-stmt.cpp b/clang/test/Interpreter/disambiguate-decl-stmt.cpp
index a49d7013c540ac2..8355cf7d6465b2d 100644
--- a/clang/test/Interpreter/disambiguate-decl-stmt.cpp
+++ b/clang/test/Interpreter/disambiguate-decl-stmt.cpp
@@ -1,5 +1,7 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -fincremental-extensions -std=c++20 %s
-// RUN: %clang_cc1 -fsyntax-only -DMS -fms-extensions -verify -fincremental-extensions -std=c++20 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fincremental-extensions -std=c++20 -I %S %s
+// RUN: %clang_cc1 -fsyntax-only -DMS -fms-extensions -verify -fincremental-extensions -std=c++20 -I %S %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char*,...);
 
diff --git a/clang/test/Interpreter/dynamic-library.cpp b/clang/test/Interpreter/dynamic-library.cpp
index 6c4621f729c1c88..c7c74fd281d966e 100644
--- a/clang/test/Interpreter/dynamic-library.cpp
+++ b/clang/test/Interpreter/dynamic-library.cpp
@@ -14,7 +14,9 @@
 //
 // }
 
-// RUN: cat %s | env LD_LIBRARY_PATH=%S/Inputs:$LD_LIBRARY_PATH clang-repl | FileCheck %s
+// RUN: cat %s | env LD_LIBRARY_PATH=%S/Inputs:$LD_LIBRARY_PATH clang-repl -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char* format, ...);
 
diff --git a/clang/test/Interpreter/execute-stmts.cpp b/clang/test/Interpreter/execute-stmts.cpp
index 2d4c17e0c91e661..33ea246ee802e05 100644
--- a/clang/test/Interpreter/execute-stmts.cpp
+++ b/clang/test/Interpreter/execute-stmts.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
-// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc  -verify | FileCheck %s
-// RUN: %clang_cc1 -verify -fincremental-extensions -emit-llvm -o -  %s \
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc  -verify -Xcc -I %S | FileCheck %s
+// RUN: %clang_cc1 -verify -fincremental-extensions -emit-llvm -I %S -o -  %s \
 // RUN:           | FileCheck --check-prefix=CODEGEN-CHECK %s
 
 // expected-no-diagnostics
@@ -9,6 +9,7 @@
 //CODEGEN-CHECK-COUNT-2: define internal void @__stmts__
 //CODEGEN-CHECK-NOT: define internal void @__stmts__
 
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char*,...);
 
diff --git a/clang/test/Interpreter/execute-weak.cpp b/clang/test/Interpreter/execute-weak.cpp
index 85fa5d276f5f468..67187a3c2a9a83a 100644
--- a/clang/test/Interpreter/execute-weak.cpp
+++ b/clang/test/Interpreter/execute-weak.cpp
@@ -1,5 +1,7 @@
 // UNSUPPORTED: system-aix, system-windows
-// RUN: cat %s | clang-repl | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char *, ...);
 int __attribute__((weak)) bar() { return 42; }
diff --git a/clang/test/Interpreter/execute.cpp b/clang/test/Interpreter/execute.cpp
index d54ab99749c1bac..968e498e43e1338 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -5,8 +5,11 @@
 // RUN:            'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
 // CHECK-DRIVER: i = 10
 
-// RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -O2 -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
+
 extern "C" int printf(const char *, ...);
 int i = 42;
 auto r1 = printf("i = %d\n", i);
diff --git a/clang/test/Interpreter/fail.cpp b/clang/test/Interpreter/fail.cpp
index 4e301f37548f1f9..dbf36be75976f82 100644
--- a/clang/test/Interpreter/fail.cpp
+++ b/clang/test/Interpreter/fail.cpp
@@ -5,7 +5,10 @@
 // RUN: clang-repl "int x = 10;" "int y=7; err;" "int y = 10;"
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
-// RUN: cat %s | not clang-repl | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
+
 BOOM!
 extern "C" int printf(const char *, ...);
 int i = 42;
diff --git a/clang/test/Interpreter/global-dtor.cpp b/clang/test/Interpreter/global-dtor.cpp
index 1f241d9f19317e6..728816a8650e162 100644
--- a/clang/test/Interpreter/global-dtor.cpp
+++ b/clang/test/Interpreter/global-dtor.cpp
@@ -3,7 +3,9 @@
 //
 // Tests that a global destructor is ran on platforms with gnu exception support.
 //
-// RUN: cat %s | clang-repl | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char *, ...);
 
diff --git a/clang/test/Interpreter/inline-virtual.cpp b/clang/test/Interpreter/inline-virtual.cpp
index 79ab8ed337ffeaf..3695fbac99a7bf4 100644
--- a/clang/test/Interpreter/inline-virtual.cpp
+++ b/clang/test/Interpreter/inline-virtual.cpp
@@ -3,8 +3,10 @@
 //
 // We disable RTTI to avoid problems on Windows for non-RTTI builds of LLVM
 // where the JIT cannot find ??_7type_info@@6B at .
-// RUN: cat %s | clang-repl -Xcc -fno-rtti | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -fno-rtti -Xcc -O2 | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -fno-rtti -Xcc -I %S | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -fno-rtti -Xcc -O2 -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char *, ...);
 
diff --git a/clang/test/Interpreter/lambda.cpp b/clang/test/Interpreter/lambda.cpp
index df75274a050b29a..78e96e391a9ae54 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,10 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
-// RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -O2 -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
diff --git a/clang/test/Interpreter/multiline.cpp b/clang/test/Interpreter/multiline.cpp
index 054e61a7e3d62ec..db0d6885e1268da 100644
--- a/clang/test/Interpreter/multiline.cpp
+++ b/clang/test/Interpreter/multiline.cpp
@@ -1,6 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
-// RUN: cat %s | clang-repl | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
+
+#include "ClangReplPrologue.h"
 
 extern "C" int printf(const char*,...);
 int i = \
diff --git a/clang/test/Interpreter/plugins.cpp b/clang/test/Interpreter/plugins.cpp
index 59e4f2f29240980..8b72bcdc54e53b2 100644
--- a/clang/test/Interpreter/plugins.cpp
+++ b/clang/test/Interpreter/plugins.cpp
@@ -1,8 +1,10 @@
 // RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -load -Xcc -Xclang \
 // RUN:            -Xcc %llvmshlibdir/PrintFunctionNames%pluginext -Xcc -Xclang\
-// RUN:            -Xcc -add-plugin -Xcc -Xclang -Xcc print-fns 2>&1 | FileCheck %s
+// RUN:            -Xcc -add-plugin -Xcc -Xclang -Xcc print-fns -Xcc -I %S 2>&1 | FileCheck %s
 // REQUIRES: plugins, examples
 
+#include "ClangReplPrologue.h"
+
 int i = 10;
 extern "C" int printf(const char*,...);
 auto r1 = printf("i = %d\n", i);
diff --git a/clang/test/Interpreter/simple-exception.cpp b/clang/test/Interpreter/simple-exception.cpp
index 8741886a0a621cc..fabda06cb78c72a 100644
--- a/clang/test/Interpreter/simple-exception.cpp
+++ b/clang/test/Interpreter/simple-exception.cpp
@@ -2,12 +2,14 @@
 // UNSUPPORTED: system-aix
 // XFAIL for arm, arm64, riscv, or running on Windows.
 // XFAIL: target={{(arm|riscv).*}}, system-windows
-// RUN: cat %s | clang-repl | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -I %S | FileCheck %s
 
 // Incompatible with msan. It passes with -O3 but fail -Oz. Interpreter
 // generates non-instrumented code, which may call back to instrumented.
 // UNSUPPORTED: msan
 
+#include "ClangReplPrologue.h"
+
 extern "C" int printf(const char *, ...);
 
 int f() { throw "Simple exception"; return 0; }
diff --git a/clang/test/Interpreter/stmt-serialization.cpp b/clang/test/Interpreter/stmt-serialization.cpp
index 4d97a3dc5c64c34..d88a4e09a2ed026 100644
--- a/clang/test/Interpreter/stmt-serialization.cpp
+++ b/clang/test/Interpreter/stmt-serialization.cpp
@@ -1,8 +1,10 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -std=c++20 -fincremental-extensions -fmodules-cache-path=%t \
-// RUN:            -x c++ %s -verify
+// RUN:            -I %S -x c++ %s -verify
 // expected-no-diagnostics
 
+#include "ClangReplPrologue.h"
+
 #pragma clang module build TopLevelStmt
 module TopLevelStmt { module Statements {} }
 #pragma clang module contents



More information about the cfe-commits mailing list