1 //===- unittests/Interpreter/InterpreterExceptionTest.cpp -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Unit tests for Clang's Interpreter library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Interpreter/Interpreter.h" 14 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclGroup.h" 18 #include "clang/Basic/Version.h" 19 #include "clang/Basic/TargetInfo.h" 20 #include "clang/Config/config.h" 21 #include "clang/Frontend/CompilerInstance.h" 22 #include "clang/Frontend/TextDiagnosticPrinter.h" 23 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ExecutionEngine/Orc/LLJIT.h" 26 #include "llvm/Support/TargetSelect.h" 27 #include "llvm/Support/ManagedStatic.h" 28 29 #include "gmock/gmock.h" 30 #include "gtest/gtest.h" 31 32 using namespace clang; 33 34 namespace { 35 using Args = std::vector<const char *>; 36 static std::unique_ptr<Interpreter> 37 createInterpreter(const Args &ExtraArgs = {}, 38 DiagnosticConsumer *Client = nullptr) { 39 Args ClangArgs = {"-Xclang", "-emit-llvm-only"}; 40 ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); 41 auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs)); 42 if (Client) 43 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false); 44 return cantFail(clang::Interpreter::create(std::move(CI))); 45 } 46 47 // This function isn't referenced outside its translation unit, but it 48 // can't use the "static" keyword because its address is used for 49 // GetMainExecutable (since some platforms don't support taking the 50 // address of main, and some platforms can't implement GetMainExecutable 51 // without being given the address of a function in the main executable). 52 std::string GetExecutablePath(const char *Argv0, void *MainAddr) { 53 return llvm::sys::fs::getMainExecutable(Argv0, MainAddr); 54 } 55 56 static std::string MakeResourcesPath() { 57 // Dir is bin/ or lib/, depending on where BinaryPath is. 58 void *MainAddr = (void *)(intptr_t)GetExecutablePath; 59 std::string BinaryPath = GetExecutablePath(/*Argv0=*/nullptr, MainAddr); 60 61 // build/tools/clang/unittests/Interpreter/Executable -> build/ 62 llvm::StringRef Dir = llvm::sys::path::parent_path(BinaryPath); 63 64 Dir = llvm::sys::path::parent_path(Dir); 65 Dir = llvm::sys::path::parent_path(Dir); 66 Dir = llvm::sys::path::parent_path(Dir); 67 Dir = llvm::sys::path::parent_path(Dir); 68 Dir = llvm::sys::path::parent_path(Dir); 69 SmallString<128> P(Dir); 70 llvm::sys::path::append(P, Twine("lib") + CLANG_LIBDIR_SUFFIX, "clang", 71 CLANG_VERSION_STRING); 72 73 return std::string(P.str()); 74 } 75 76 TEST(InterpreterTest, CatchException) { 77 llvm::InitializeNativeTarget(); 78 llvm::InitializeNativeTargetAsmPrinter(); 79 80 { 81 auto J = llvm::orc::LLJITBuilder().create(); 82 if (!J) { 83 // The platform does not support JITs. 84 // We can't use llvm::consumeError as it needs typeinfo for ErrorInfoBase. 85 auto E = J.takeError(); 86 (void)E; 87 return; 88 } 89 } 90 const char ExceptionCode[] = 91 R"( 92 #include <stdexcept> 93 #include <stdio.h> 94 95 static void ThrowerAnError(const char* Name) { 96 throw std::runtime_error(Name); 97 } 98 99 extern "C" int throw_exception() { 100 try { 101 ThrowerAnError("In JIT"); 102 } catch (const std::exception& E) { 103 printf("Caught: '%s'\n", E.what()); 104 } catch (...) { 105 printf("Unknown exception\n"); 106 } 107 ThrowerAnError("From JIT"); 108 return 0; 109 } 110 )"; 111 std::string ResourceDir = MakeResourcesPath(); 112 std::unique_ptr<Interpreter> Interp = 113 createInterpreter({"-resource-dir", ResourceDir.c_str()}); 114 // FIXME: Re-enable the excluded target triples. 115 const clang::CompilerInstance *CI = Interp->getCompilerInstance(); 116 const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple(); 117 // FIXME: PPC fails due to `Symbols not found: [DW.ref.__gxx_personality_v0]` 118 // The current understanding is that the JIT should emit this symbol if it was 119 // not (eg. the way passing clang -fPIC does it). 120 if (Triple.isPPC()) 121 return; 122 123 // FIXME: ARM fails due to `Not implemented relocation type!` 124 if (Triple.isARM()) 125 return; 126 127 // FIXME: Hexagon fails due to `No available targets are compatible with 128 // triple "x86_64-unknown-linux-gnu"` 129 if (Triple.getArch() == llvm::Triple::hexagon) 130 return; 131 132 // Adjust the resource-dir 133 llvm::cantFail(Interp->ParseAndExecute(ExceptionCode)); 134 testing::internal::CaptureStdout(); 135 auto ThrowException = 136 (int (*)())llvm::cantFail(Interp->getSymbolAddress("throw_exception")); 137 EXPECT_THROW(ThrowException(), std::exception); 138 std::string CapturedStdOut = testing::internal::GetCapturedStdout(); 139 EXPECT_EQ(CapturedStdOut, "Caught: 'In JIT'\n"); 140 141 llvm::llvm_shutdown(); 142 } 143 144 } // end anonymous namespace 145