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 | // Disable LSan for this test. |
33 | // FIXME: Re-enable once we can assume GCC 13.2 or higher. |
34 | // https://llvm.org/github.com/llvm/llvm-project/issues/67586. |
35 | #if LLVM_ADDRESS_SANITIZER_BUILD || LLVM_HWADDRESS_SANITIZER_BUILD |
36 | #include <sanitizer/lsan_interface.h> |
37 | LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; } |
38 | #endif |
39 | |
40 | using namespace clang; |
41 | |
42 | namespace { |
43 | using Args = std::vector<const char *>; |
44 | static std::unique_ptr<Interpreter> |
45 | createInterpreter(const Args & = {}, |
46 | DiagnosticConsumer *Client = nullptr) { |
47 | Args ClangArgs = {"-Xclang" , "-emit-llvm-only" }; |
48 | ClangArgs.insert(position: ClangArgs.end(), first: ExtraArgs.begin(), last: ExtraArgs.end()); |
49 | auto CB = clang::IncrementalCompilerBuilder(); |
50 | CB.SetCompilerArgs(ClangArgs); |
51 | auto CI = cantFail(ValOrErr: CB.CreateCpp()); |
52 | if (Client) |
53 | CI->getDiagnostics().setClient(client: Client, /*ShouldOwnClient=*/false); |
54 | return cantFail(ValOrErr: clang::Interpreter::create(CI: std::move(CI))); |
55 | } |
56 | |
57 | TEST(InterpreterTest, CatchException) { |
58 | llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
59 | llvm::InitializeNativeTarget(); |
60 | llvm::InitializeNativeTargetAsmPrinter(); |
61 | |
62 | { |
63 | auto J = llvm::orc::LLJITBuilder().create(); |
64 | if (!J) { |
65 | // The platform does not support JITs. |
66 | // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we |
67 | // can avoid that by going via the C interface. |
68 | LLVMConsumeError(Err: llvm::wrap(Err: J.takeError())); |
69 | GTEST_SKIP(); |
70 | } |
71 | } |
72 | |
73 | #define Stringify(s) Stringifyx(s) |
74 | #define Stringifyx(s) #s |
75 | |
76 | // We define a custom exception to avoid #include-ing the <exception> header |
77 | // which would require this test to know about the libstdc++ location. |
78 | // its own header file. |
79 | #define CUSTOM_EXCEPTION \ |
80 | struct custom_exception { \ |
81 | custom_exception(const char *Msg) : Message(Msg) {} \ |
82 | const char *Message; \ |
83 | }; |
84 | |
85 | CUSTOM_EXCEPTION; |
86 | |
87 | std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION); |
88 | ExceptionCode += |
89 | R"( |
90 | extern "C" int printf(const char*, ...); |
91 | static void ThrowerAnError(const char* Name) { |
92 | throw custom_exception(Name); |
93 | } |
94 | |
95 | extern "C" int throw_exception() { |
96 | try { |
97 | ThrowerAnError("To be caught in JIT"); |
98 | } catch (const custom_exception& E) { |
99 | printf("Caught: '%s'\n", E.Message); |
100 | } catch (...) { |
101 | printf("Unknown exception\n"); |
102 | } |
103 | ThrowerAnError("To be caught in binary"); |
104 | return 0; |
105 | } |
106 | )" ; |
107 | std::unique_ptr<Interpreter> Interp = createInterpreter(); |
108 | // FIXME: Re-enable the excluded target triples. |
109 | const clang::CompilerInstance *CI = Interp->getCompilerInstance(); |
110 | const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple(); |
111 | |
112 | // AIX is unsupported. |
113 | if (Triple.isOSAIX()) |
114 | GTEST_SKIP(); |
115 | |
116 | // FIXME: ARM fails due to `Not implemented relocation type!` |
117 | if (Triple.isARM()) |
118 | GTEST_SKIP(); |
119 | |
120 | // FIXME: libunwind on darwin is broken, see PR49692. |
121 | if (Triple.isOSDarwin() && (Triple.getArch() == llvm::Triple::aarch64 || |
122 | Triple.getArch() == llvm::Triple::aarch64_32)) |
123 | GTEST_SKIP(); |
124 | |
125 | llvm::cantFail(Err: Interp->ParseAndExecute(Code: ExceptionCode)); |
126 | testing::internal::CaptureStdout(); |
127 | auto ThrowException = |
128 | llvm::cantFail(ValOrErr: Interp->getSymbolAddress(IRName: "throw_exception" )) |
129 | .toPtr<int (*)()>(); |
130 | EXPECT_ANY_THROW(ThrowException()); |
131 | std::string CapturedStdOut = testing::internal::GetCapturedStdout(); |
132 | EXPECT_EQ(CapturedStdOut, "Caught: 'To be caught in JIT'\n" ); |
133 | } |
134 | |
135 | } // end anonymous namespace |
136 | |