[clang] [wip][Clang-Repl] Add custom function as lambda in launchExecutor and fetch PID of launched executor (PR #147478)

Vipul Cariappa via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 29 06:52:53 PDT 2025


================
@@ -54,6 +67,93 @@ createInterpreter(const Args &ExtraArgs = {},
   return cantFail(clang::Interpreter::create(std::move(CI)));
 }
 
+static std::string getExecutorPath() {
+  llvm::SmallString<256> ExecutorPath(llvm::sys::fs::getMainExecutable(
+      nullptr, reinterpret_cast<void *>(&getExecutorPath)));
+  llvm::sys::path::remove_filename(ExecutorPath);
+
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "Interpreter"
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "unittests"
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "clang"
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "tools"
+
+  llvm::sys::path::append(ExecutorPath, "bin", "llvm-jitlink-executor");
+  return ExecutorPath.str().str();
+}
+
+static std::string getOrcRuntimePath() {
+  llvm::SmallString<256> RuntimePath(llvm::sys::fs::getMainExecutable(
+      nullptr, reinterpret_cast<void *>(&getOrcRuntimePath)));
+
+  llvm::sys::path::remove_filename(RuntimePath);
+
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "Interpreter"
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "unittests"
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "clang"
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "tools"
+
+  llvm::sys::path::append(RuntimePath, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
+                          CLANG_VERSION_MAJOR_STRING, "lib");
+
+  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
+  if (SystemTriple.isOSBinFormatMachO()) {
+    llvm::sys::path::append(RuntimePath, "darwin", "liborc_rt_osx.a");
+  } else if (SystemTriple.isOSBinFormatELF()) {
+    llvm::sys::path::append(RuntimePath, "x86_64-unknown-linux-gnu",
+                            "liborc_rt.a");
+  }
+
+  return RuntimePath.str().str();
+}
+
+static std::unique_ptr<Interpreter>
+createInterpreterWithRemoteExecution(const Args &ExtraArgs = {},
+                                     DiagnosticConsumer *Client = nullptr) {
+  Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
+  llvm::append_range(ClangArgs, ExtraArgs);
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
+  if (Client)
+    CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
+
+  std::unique_ptr<llvm::orc::LLJITBuilder> JB;
+
+  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
+
+  if ((SystemTriple.isOSBinFormatELF() || SystemTriple.isOSBinFormatMachO())) {
+    std::string OOPExecutor = getExecutorPath();
+    std::string OrcRuntimePath = getOrcRuntimePath();
+    bool UseSharedMemory = false;
+    std::string SlabAllocateSizeString = "";
+    std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC;
+    EPC = ExitOnError(launchExecutor(OOPExecutor, UseSharedMemory,
+                                     SlabAllocateSizeString,
+                                     [=] { // Lambda defined inline
+                                       auto redirect = [](int from, int to) {
+                                         if (from != to) {
+                                           dup2(from, to);
+                                           close(from);
+                                         }
+                                       };
+
+                                       redirect(0, STDIN_FILENO);
+                                       redirect(1, STDOUT_FILENO);
+                                       redirect(2, STDERR_FILENO);
+
+                                       setvbuf(stdout, nullptr, _IONBF, 0);
+                                       setvbuf(stderr, nullptr, _IONBF, 0);
+                                     }));
----------------
Vipul-Cariappa wrote:

I guess this could be `nullptr`.
Because we just redirect 0 to 0, 1 to 1, and 2 to 2 here.
When libclang is used, the user of libclang can set it to whatever they want.

---

Ok. This is in tests, so that should be fine.

https://github.com/llvm/llvm-project/pull/147478


More information about the cfe-commits mailing list