Debian Codegraph @

llvm-project/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp

raw
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 #include "llvm-c/Error.h"
29
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32
33 using namespace clang;
34
35 namespace {
36 using Args = std::vector<const char *>;
37 static std::unique_ptr<Interpreter>
38 createInterpreter(const Args &ExtraArgs = {},
39 DiagnosticConsumer *Client = nullptr) {
40 Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
41 ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
42 auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
43 if (Client)
44 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
45 return cantFail(clang::Interpreter::create(std::move(CI)));
46 }
47
48 TEST(InterpreterTest, CatchException) {
49 llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
50 llvm::InitializeNativeTarget();
51 llvm::InitializeNativeTargetAsmPrinter();
52
53 {
54 auto J = llvm::orc::LLJITBuilder().create();
55 if (!J) {
56 // The platform does not support JITs.
57 // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we
58 // can avoid that by going via the C interface.
59 LLVMConsumeError(llvm::wrap(J.takeError()));
60 return;
61 }
62 }
63
64 #define Stringify(s) Stringifyx(s)
65 #define Stringifyx(s) #s
66
67 // We define a custom exception to avoid #include-ing the <exception> header
68 // which would require this test to know about the libstdc++ location.
69 // its own header file.
70 #define CUSTOM_EXCEPTION \
71 struct custom_exception { \
72 custom_exception(const char *Msg) : Message(Msg) {} \
73 const char *Message; \
74 };
75
76 CUSTOM_EXCEPTION;
77
78 std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION);
79 ExceptionCode +=
80 R"(
81 extern "C" int printf(const char*, ...);
82 static void ThrowerAnError(const char* Name) {
83 throw custom_exception(Name);
84 }
85
86 extern "C" int throw_exception() {
87 try {
88 ThrowerAnError("To be caught in JIT");
89 } catch (const custom_exception& E) {
90 printf("Caught: '%s'\n", E.Message);
91 } catch (...) {
92 printf("Unknown exception\n");
93 }
94 ThrowerAnError("To be caught in binary");
95 return 0;
96 }
97 )";
98 std::unique_ptr<Interpreter> Interp = createInterpreter();
99 // FIXME: Re-enable the excluded target triples.
100 const clang::CompilerInstance *CI = Interp->getCompilerInstance();
101 const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple();
102
103 // AIX is unsupported.
104 if (Triple.isOSAIX())
105 return;
106
107 // FIXME: ARM fails due to `Not implemented relocation type!`
108 if (Triple.isARM())
109 return;
110
111 // FIXME: libunwind on darwin is broken, see PR49692.
112 if (Triple.isOSDarwin() && (Triple.getArch() == llvm::Triple::aarch64 ||
113 Triple.getArch() == llvm::Triple::aarch64_32))
114 return;
115
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: 'To be caught in JIT'\n");
123 }
124
125 } // end anonymous namespace