xref: /llvm-project/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp (revision 1dba6b37bdc70210f75a480eff3715ebe1f1d8be)
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::InitializeNativeTarget();
50   llvm::InitializeNativeTargetAsmPrinter();
51 
52   {
53     auto J = llvm::orc::LLJITBuilder().create();
54     if (!J) {
55       // The platform does not support JITs.
56       // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we
57       // can avoid that by going via the C interface.
58       LLVMConsumeError(llvm::wrap(J.takeError()));
59       return;
60     }
61   }
62 
63 #define Stringify(s) Stringifyx(s)
64 #define Stringifyx(s) #s
65 
66   // We define a custom exception to avoid #include-ing the <exception> header
67   // which would require this test to know about the libstdc++ location.
68   // its own header file.
69 #define CUSTOM_EXCEPTION                                                       \
70   struct custom_exception {                                                    \
71     custom_exception(const char *Msg) : Message(Msg) {}                        \
72     const char *Message;                                                       \
73   };
74 
75   CUSTOM_EXCEPTION;
76 
77   std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION);
78   ExceptionCode +=
79       R"(
80 extern "C" int printf(const char*, ...);
81 static void ThrowerAnError(const char* Name) {
82   throw custom_exception(Name);
83 }
84 
85 extern "C" int throw_exception() {
86   try {
87     ThrowerAnError("To be caught in JIT");
88   } catch (const custom_exception& E) {
89     printf("Caught: '%s'\n", E.Message);
90   } catch (...) {
91     printf("Unknown exception\n");
92   }
93   ThrowerAnError("To be caught in binary");
94   return 0;
95 }
96     )";
97   std::unique_ptr<Interpreter> Interp = createInterpreter();
98   // FIXME: Re-enable the excluded target triples.
99   const clang::CompilerInstance *CI = Interp->getCompilerInstance();
100   const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple();
101   // FIXME: PPC fails due to `Symbols not found: [DW.ref.__gxx_personality_v0]`
102   // The current understanding is that the JIT should emit this symbol if it was
103   // not (eg. the way passing clang -fPIC does it).
104   if (Triple.isPPC())
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   llvm::llvm_shutdown();
125 }
126 
127 } // end anonymous namespace
128