xref: /llvm-project/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp (revision 319ce98011742141dad8dd95a2f9de9c0449be5c)
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/Decl.h"
16 #include "clang/AST/DeclGroup.h"
17 #include "clang/Basic/Version.h"
18 #include "clang/Config/config.h"
19 #include "clang/Frontend/CompilerInstance.h"
20 #include "clang/Frontend/TextDiagnosticPrinter.h"
21 
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
24 #include "llvm/Support/TargetSelect.h"
25 
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28 
29 using namespace clang;
30 
31 namespace {
32 using Args = std::vector<const char *>;
33 static std::unique_ptr<Interpreter>
34 createInterpreter(const Args &ExtraArgs = {},
35                   DiagnosticConsumer *Client = nullptr) {
36   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
37   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
38   auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
39   if (Client)
40     CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
41   return cantFail(clang::Interpreter::create(std::move(CI)));
42 }
43 
44 // This function isn't referenced outside its translation unit, but it
45 // can't use the "static" keyword because its address is used for
46 // GetMainExecutable (since some platforms don't support taking the
47 // address of main, and some platforms can't implement GetMainExecutable
48 // without being given the address of a function in the main executable).
49 std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
50   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
51 }
52 
53 static std::string MakeResourcesPath() {
54   // Dir is bin/ or lib/, depending on where BinaryPath is.
55   void *MainAddr = (void *)(intptr_t)GetExecutablePath;
56   std::string BinaryPath = GetExecutablePath(/*Argv0=*/nullptr, MainAddr);
57 
58   // build/tools/clang/unittests/Interpreter/Executable -> build/
59   llvm::StringRef Dir = llvm::sys::path::parent_path(BinaryPath);
60 
61   Dir = llvm::sys::path::parent_path(Dir);
62   Dir = llvm::sys::path::parent_path(Dir);
63   Dir = llvm::sys::path::parent_path(Dir);
64   Dir = llvm::sys::path::parent_path(Dir);
65   Dir = llvm::sys::path::parent_path(Dir);
66   SmallString<128> P(Dir);
67   llvm::sys::path::append(P, Twine("lib") + CLANG_LIBDIR_SUFFIX, "clang",
68                           CLANG_VERSION_STRING);
69 
70   return std::string(P.str());
71 }
72 
73 TEST(InterpreterTest, CatchException) {
74   llvm::InitializeNativeTarget();
75   llvm::InitializeNativeTargetAsmPrinter();
76 
77   {
78     auto J = llvm::orc::LLJITBuilder().create();
79     if (!J) {
80       // The platform does not support JITs.
81       // We can't use llvm::consumeError as it needs typeinfo for ErrorInfoBase.
82       auto E = J.takeError();
83       (void)E;
84       return;
85     }
86   }
87   const char ExceptionCode[] =
88       R"(
89 #include <stdexcept>
90 #include <stdio.h>
91 
92 static void ThrowerAnError(const char* Name) {
93   throw std::runtime_error(Name);
94 }
95 
96 extern "C" int throw_exception() {
97   try {
98     ThrowerAnError("In JIT");
99   } catch (const std::exception& E) {
100     printf("Caught: '%s'\n", E.what());
101   } catch (...) {
102     printf("Unknown exception\n");
103   }
104   ThrowerAnError("From JIT");
105   return 0;
106 }
107     )";
108   std::string ResourceDir = MakeResourcesPath();
109   std::unique_ptr<Interpreter> Interp =
110       createInterpreter({"-resource-dir", ResourceDir.c_str()});
111   // Adjust the resource-dir
112   llvm::cantFail(Interp->ParseAndExecute(ExceptionCode));
113   testing::internal::CaptureStdout();
114   auto ThrowException =
115       (int (*)())llvm::cantFail(Interp->getSymbolAddress("throw_exception"));
116   EXPECT_THROW(ThrowException(), std::exception);
117   std::string CapturedStdOut = testing::internal::GetCapturedStdout();
118   EXPECT_EQ(CapturedStdOut, "Caught: 'In JIT'\n");
119 }
120 
121 } // end anonymous namespace
122