xref: /llvm-project/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp (revision 8a62d6ba7edc3a7d397e52884a9ce63b4e579ae1)
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()
56                  .setEnableDebuggerSupport(true)
57                  .create();
58     if (!J) {
59       // The platform does not support JITs.
60       // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we
61       // can avoid that by going via the C interface.
62       LLVMConsumeError(llvm::wrap(J.takeError()));
63       GTEST_SKIP();
64     }
65   }
66 
67 #define Stringify(s) Stringifyx(s)
68 #define Stringifyx(s) #s
69 
70   // We define a custom exception to avoid #include-ing the <exception> header
71   // which would require this test to know about the libstdc++ location.
72   // its own header file.
73 #define CUSTOM_EXCEPTION                                                       \
74   struct custom_exception {                                                    \
75     custom_exception(const char *Msg) : Message(Msg) {}                        \
76     const char *Message;                                                       \
77   };
78 
79   CUSTOM_EXCEPTION;
80 
81   std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION);
82   ExceptionCode +=
83       R"(
84 extern "C" int printf(const char*, ...);
85 static void ThrowerAnError(const char* Name) {
86   throw custom_exception(Name);
87 }
88 
89 extern "C" int throw_exception() {
90   try {
91     ThrowerAnError("To be caught in JIT");
92   } catch (const custom_exception& E) {
93     printf("Caught: '%s'\n", E.Message);
94   } catch (...) {
95     printf("Unknown exception\n");
96   }
97   ThrowerAnError("To be caught in binary");
98   return 0;
99 }
100     )";
101   std::unique_ptr<Interpreter> Interp = createInterpreter();
102   // FIXME: Re-enable the excluded target triples.
103   const clang::CompilerInstance *CI = Interp->getCompilerInstance();
104   const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple();
105 
106   // AIX is unsupported.
107   if (Triple.isOSAIX())
108     GTEST_SKIP();
109 
110   // FIXME: ARM fails due to `Not implemented relocation type!`
111   if (Triple.isARM())
112     GTEST_SKIP();
113 
114   // FIXME: libunwind on darwin is broken, see PR49692.
115   if (Triple.isOSDarwin() && (Triple.getArch() == llvm::Triple::aarch64 ||
116                               Triple.getArch() == llvm::Triple::aarch64_32))
117     GTEST_SKIP();
118 
119   llvm::cantFail(Interp->ParseAndExecute(ExceptionCode));
120   testing::internal::CaptureStdout();
121   auto ThrowException =
122       llvm::cantFail(Interp->getSymbolAddress("throw_exception"))
123           .toPtr<int (*)()>();
124   EXPECT_ANY_THROW(ThrowException());
125   std::string CapturedStdOut = testing::internal::GetCapturedStdout();
126   EXPECT_EQ(CapturedStdOut, "Caught: 'To be caught in JIT'\n");
127 }
128 
129 } // end anonymous namespace
130