|
| 1 | +//===- unittests/Interpreter/OutOfProcessInterpreterTest.cpp --- Interpreter |
| 2 | +// tests when Out-of-Process ----===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// Unit tests for Clang's Interpreter library. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "InterpreterTestFixture.h" |
| 15 | + |
| 16 | +#include "clang/AST/Decl.h" |
| 17 | +#include "clang/AST/DeclGroup.h" |
| 18 | +#include "clang/AST/Mangle.h" |
| 19 | +#include "clang/Basic/Version.h" |
| 20 | +#include "clang/Config/config.h" |
| 21 | +#include "clang/Frontend/CompilerInstance.h" |
| 22 | +#include "clang/Frontend/TextDiagnosticPrinter.h" |
| 23 | +#include "clang/Interpreter/Interpreter.h" |
| 24 | +#include "clang/Interpreter/RemoteJITUtils.h" |
| 25 | +#include "clang/Interpreter/Value.h" |
| 26 | +#include "clang/Sema/Lookup.h" |
| 27 | +#include "clang/Sema/Sema.h" |
| 28 | +#include "llvm/Support/Error.h" |
| 29 | +#include "llvm/TargetParser/Host.h" |
| 30 | + |
| 31 | +#include "llvm/TargetParser/Host.h" |
| 32 | + |
| 33 | +#include "gmock/gmock.h" |
| 34 | +#include "gtest/gtest.h" |
| 35 | + |
| 36 | +using namespace clang; |
| 37 | + |
| 38 | +llvm::ExitOnError ExitOnError; |
| 39 | + |
| 40 | +#ifdef _WIN32 |
| 41 | +#define STDIN_FILENO 0 |
| 42 | +#define STDOUT_FILENO 1 |
| 43 | +#define STDERR_FILENO 2 |
| 44 | +#endif |
| 45 | + |
| 46 | +namespace { |
| 47 | + |
| 48 | +using Args = std::vector<const char *>; |
| 49 | + |
| 50 | +static void removePathComponent(unsigned N, llvm::SmallString<256> &Path) { |
| 51 | + for (unsigned i = 0; i < N; ++i) |
| 52 | + llvm::sys::path::remove_filename(Path); |
| 53 | +} |
| 54 | + |
| 55 | +static std::string getExecutorPath() { |
| 56 | + llvm::SmallString<256> ExecutorPath(llvm::sys::fs::getMainExecutable( |
| 57 | + nullptr, reinterpret_cast<void *>(&getExecutorPath))); |
| 58 | + removePathComponent(5, ExecutorPath); |
| 59 | + llvm::sys::path::append(ExecutorPath, "bin", "llvm-jitlink-executor"); |
| 60 | + return ExecutorPath.str().str(); |
| 61 | +} |
| 62 | + |
| 63 | +static std::string getOrcRuntimePath() { |
| 64 | + llvm::SmallString<256> RuntimePath(llvm::sys::fs::getMainExecutable( |
| 65 | + nullptr, reinterpret_cast<void *>(&getOrcRuntimePath))); |
| 66 | + removePathComponent(5, RuntimePath); |
| 67 | + llvm::sys::path::append(RuntimePath, CLANG_INSTALL_LIBDIR_BASENAME, "clang", |
| 68 | + CLANG_VERSION_MAJOR_STRING, "lib"); |
| 69 | + |
| 70 | + llvm::Triple SystemTriple(llvm::sys::getProcessTriple()); |
| 71 | + if (SystemTriple.isOSBinFormatMachO()) { |
| 72 | + llvm::sys::path::append(RuntimePath, "darwin", "liborc_rt_osx.a"); |
| 73 | + } else if (SystemTriple.isOSBinFormatELF()) { |
| 74 | + llvm::sys::path::append(RuntimePath, "x86_64-unknown-linux-gnu", |
| 75 | + "liborc_rt.a"); |
| 76 | + } |
| 77 | + |
| 78 | + return RuntimePath.str().str(); |
| 79 | +} |
| 80 | + |
| 81 | +static std::unique_ptr<Interpreter> |
| 82 | +createInterpreterWithRemoteExecution(const Args &ExtraArgs = {}, |
| 83 | + DiagnosticConsumer *Client = nullptr) { |
| 84 | + Args ClangArgs = {"-Xclang", "-emit-llvm-only"}; |
| 85 | + llvm::append_range(ClangArgs, ExtraArgs); |
| 86 | + auto CB = clang::IncrementalCompilerBuilder(); |
| 87 | + CB.SetCompilerArgs(ClangArgs); |
| 88 | + auto CI = cantFail(CB.CreateCpp()); |
| 89 | + if (Client) |
| 90 | + CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false); |
| 91 | + |
| 92 | + std::unique_ptr<llvm::orc::LLJITBuilder> JB; |
| 93 | + |
| 94 | + llvm::Triple SystemTriple(llvm::sys::getProcessTriple()); |
| 95 | + |
| 96 | + if ((SystemTriple.isOSBinFormatELF() || SystemTriple.isOSBinFormatMachO())) { |
| 97 | + std::string OOPExecutor = getExecutorPath(); |
| 98 | + std::string OrcRuntimePath = getOrcRuntimePath(); |
| 99 | + bool UseSharedMemory = false; |
| 100 | + std::string SlabAllocateSizeString = ""; |
| 101 | + std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC; |
| 102 | + EPC = ExitOnError(launchExecutor(OOPExecutor, UseSharedMemory, |
| 103 | + SlabAllocateSizeString, |
| 104 | + [=] { // Lambda defined inline |
| 105 | + auto redirect = [](int from, int to) { |
| 106 | + if (from != to) { |
| 107 | + dup2(from, to); |
| 108 | + close(from); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + redirect(0, STDIN_FILENO); |
| 113 | + redirect(1, STDOUT_FILENO); |
| 114 | + redirect(2, STDERR_FILENO); |
| 115 | + |
| 116 | + setvbuf(stdout, nullptr, _IONBF, 0); |
| 117 | + setvbuf(stderr, nullptr, _IONBF, 0); |
| 118 | + })); |
| 119 | + if (EPC) { |
| 120 | + CB.SetTargetTriple(EPC->getTargetTriple().getTriple()); |
| 121 | + JB = ExitOnError(clang::Interpreter::createLLJITBuilder(std::move(EPC), |
| 122 | + OrcRuntimePath)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return cantFail(clang::Interpreter::create(std::move(CI), std::move(JB))); |
| 127 | +} |
| 128 | + |
| 129 | +static size_t DeclsSize(TranslationUnitDecl *PTUDecl) { |
| 130 | + return std::distance(PTUDecl->decls().begin(), PTUDecl->decls().end()); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(InterpreterTestBase, SanityWithRemoteExecution) { |
| 134 | + if (!HostSupportsJIT()) |
| 135 | + GTEST_SKIP(); |
| 136 | + |
| 137 | + std::unique_ptr<Interpreter> Interp = createInterpreterWithRemoteExecution(); |
| 138 | + |
| 139 | + using PTU = PartialTranslationUnit; |
| 140 | + |
| 141 | + PTU &R1(cantFail(Interp->Parse("void g(); void g() {}"))); |
| 142 | + EXPECT_EQ(2U, DeclsSize(R1.TUPart)); |
| 143 | + |
| 144 | + PTU &R2(cantFail(Interp->Parse("int i;"))); |
| 145 | + EXPECT_EQ(1U, DeclsSize(R2.TUPart)); |
| 146 | +} |
| 147 | + |
| 148 | +} // end anonymous namespace |
0 commit comments