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/TargetInfo.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 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ExecutionEngine/Orc/LLJIT.h" 26 #include "llvm/Support/ManagedStatic.h" 27 #include "llvm/Support/TargetSelect.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 CB = clang::IncrementalCompilerBuilder(); 42 CB.SetCompilerArgs(ClangArgs); 43 auto CI = cantFail(CB.CreateCpp()); 44 if (Client) 45 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false); 46 return cantFail(clang::Interpreter::create(std::move(CI))); 47 } 48 49 TEST(InterpreterTest, CatchException) { 50 llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 51 llvm::InitializeNativeTarget(); 52 llvm::InitializeNativeTargetAsmPrinter(); 53 54 { 55 auto J = llvm::orc::LLJITBuilder().create(); 56 if (!J) { 57 // The platform does not support JITs. 58 // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we 59 // can avoid that by going via the C interface. 60 LLVMConsumeError(llvm::wrap(J.takeError())); 61 GTEST_SKIP(); 62 } 63 } 64 65 #define Stringify(s) Stringifyx(s) 66 #define Stringifyx(s) #s 67 68 // We define a custom exception to avoid #include-ing the <exception> header 69 // which would require this test to know about the libstdc++ location. 70 // its own header file. 71 #define CUSTOM_EXCEPTION \ 72 struct custom_exception { \ 73 custom_exception(const char *Msg) : Message(Msg) {} \ 74 const char *Message; \ 75 }; 76 77 CUSTOM_EXCEPTION; 78 79 std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION); 80 ExceptionCode += 81 R"( 82 extern "C" int printf(const char*, ...); 83 static void ThrowerAnError(const char* Name) { 84 throw custom_exception(Name); 85 } 86 87 extern "C" int throw_exception() { 88 try { 89 ThrowerAnError("To be caught in JIT"); 90 } catch (const custom_exception& E) { 91 printf("Caught: '%s'\n", E.Message); 92 } catch (...) { 93 printf("Unknown exception\n"); 94 } 95 ThrowerAnError("To be caught in binary"); 96 return 0; 97 } 98 )"; 99 std::unique_ptr<Interpreter> Interp = createInterpreter(); 100 // FIXME: Re-enable the excluded target triples. 101 const clang::CompilerInstance *CI = Interp->getCompilerInstance(); 102 const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple(); 103 104 // AIX is unsupported. 105 if (Triple.isOSAIX()) 106 GTEST_SKIP(); 107 108 // FIXME: ARM fails due to `Not implemented relocation type!` 109 if (Triple.isARM()) 110 GTEST_SKIP(); 111 112 // FIXME: libunwind on darwin is broken, see PR49692. 113 if (Triple.isOSDarwin() && (Triple.getArch() == llvm::Triple::aarch64 || 114 Triple.getArch() == llvm::Triple::aarch64_32)) 115 GTEST_SKIP(); 116 117 llvm::cantFail(Interp->ParseAndExecute(ExceptionCode)); 118 testing::internal::CaptureStdout(); 119 auto ThrowException = 120 llvm::cantFail(Interp->getSymbolAddress("throw_exception")) 121 .toPtr<int (*)()>(); 122 EXPECT_ANY_THROW(ThrowException()); 123 std::string CapturedStdOut = testing::internal::GetCapturedStdout(); 124 EXPECT_EQ(CapturedStdOut, "Caught: 'To be caught in JIT'\n"); 125 } 126 127 } // end anonymous namespace 128