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 TEST(InterpreterTest, CatchException) { 48 llvm::InitializeNativeTarget(); 49 llvm::InitializeNativeTargetAsmPrinter(); 50 51 { 52 auto J = llvm::orc::LLJITBuilder().create(); 53 if (!J) { 54 // The platform does not support JITs. 55 // We can't use llvm::consumeError as it needs typeinfo for ErrorInfoBase. 56 auto E = J.takeError(); 57 (void)E; 58 return; 59 } 60 } 61 62 #define Stringify(s) Stringifyx(s) 63 #define Stringifyx(s) #s 64 65 // We define a custom exception to avoid #include-ing the <exception> header 66 // which would require this test to know about the libstdc++ location. 67 // its own header file. 68 #define CUSTOM_EXCEPTION \ 69 struct custom_exception { \ 70 custom_exception(const char* Msg) : Message(Msg) {} \ 71 const char* Message; \ 72 }; 73 74 CUSTOM_EXCEPTION; 75 76 std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION); 77 ExceptionCode += 78 R"( 79 extern "C" int printf(const char*, ...); 80 static void ThrowerAnError(const char* Name) { 81 throw custom_exception(Name); 82 } 83 84 extern "C" int throw_exception() { 85 try { 86 ThrowerAnError("In JIT"); 87 } catch (const custom_exception& E) { 88 printf("Caught: '%s'\n", E.Message); 89 } catch (...) { 90 printf("Unknown exception\n"); 91 } 92 ThrowerAnError("From JIT"); 93 return 0; 94 } 95 )"; 96 std::unique_ptr<Interpreter> Interp = createInterpreter(); 97 // FIXME: Re-enable the excluded target triples. 98 const clang::CompilerInstance *CI = Interp->getCompilerInstance(); 99 const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple(); 100 // FIXME: PPC fails due to `Symbols not found: [DW.ref.__gxx_personality_v0]` 101 // The current understanding is that the JIT should emit this symbol if it was 102 // not (eg. the way passing clang -fPIC does it). 103 if (Triple.isPPC()) 104 return; 105 106 // FIXME: ARM fails due to `Not implemented relocation type!` 107 if (Triple.isARM()) 108 return; 109 110 // FIXME: Hexagon fails due to `No available targets are compatible with 111 // triple "x86_64-unknown-linux-gnu"` 112 if (Triple.getArch() == llvm::Triple::hexagon) 113 return; 114 115 // Adjust the resource-dir 116 llvm::cantFail(Interp->ParseAndExecute(ExceptionCode)); 117 testing::internal::CaptureStdout(); 118 auto ThrowException = 119 (int (*)())llvm::cantFail(Interp->getSymbolAddress("throw_exception")); 120 EXPECT_ANY_THROW(ThrowException()); 121 std::string CapturedStdOut = testing::internal::GetCapturedStdout(); 122 EXPECT_EQ(CapturedStdOut, "Caught: 'In JIT'\n"); 123 124 llvm::llvm_shutdown(); 125 } 126 127 } // end anonymous namespace 128