1 //===- unittests/Interpreter/InterpreterExceptionTest.cpp -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Unit tests for Clang's Interpreter library.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Interpreter/Interpreter.h"
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"
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"
29 #include "gmock/gmock.h"
30 #include "gtest/gtest.h"
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; }
40 using namespace clang
;
43 using Args
= std::vector
<const char *>;
44 static std::unique_ptr
<Interpreter
>
45 createInterpreter(const Args
&ExtraArgs
= {},
46 DiagnosticConsumer
*Client
= nullptr) {
47 Args ClangArgs
= {"-Xclang", "-emit-llvm-only"};
48 ClangArgs
.insert(ClangArgs
.end(), ExtraArgs
.begin(), ExtraArgs
.end());
49 auto CB
= clang::IncrementalCompilerBuilder();
50 CB
.SetCompilerArgs(ClangArgs
);
51 auto CI
= cantFail(CB
.CreateCpp());
53 CI
->getDiagnostics().setClient(Client
, /*ShouldOwnClient=*/false);
54 return cantFail(clang::Interpreter::create(std::move(CI
)));
57 TEST(InterpreterTest
, CatchException
) {
58 llvm::llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
59 llvm::InitializeNativeTarget();
60 llvm::InitializeNativeTargetAsmPrinter();
63 auto J
= llvm::orc::LLJITBuilder().create();
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(llvm::wrap(J
.takeError()));
73 #define Stringify(s) Stringifyx(s)
74 #define Stringifyx(s) #s
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; \
87 std::string ExceptionCode
= Stringify(CUSTOM_EXCEPTION
);
90 extern "C
" int printf(const char*, ...);
91 static void ThrowerAnError(const char* Name) {
92 throw custom_exception(Name);
95 extern "C
" int throw_exception() {
97 ThrowerAnError("To be caught in JIT
");
98 } catch (const custom_exception& E) {
99 printf("Caught
: '%s'\n", E.Message);
101 printf("Unknown exception
\n");
103 ThrowerAnError("To be caught in binary
");
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();
112 // AIX is unsupported.
113 if (Triple
.isOSAIX())
116 // FIXME: ARM fails due to `Not implemented relocation type!`
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
))
125 llvm::cantFail(Interp
->ParseAndExecute(ExceptionCode
));
126 testing::internal::CaptureStdout();
127 auto ThrowException
=
128 llvm::cantFail(Interp
->getSymbolAddress("throw_exception"))
130 EXPECT_ANY_THROW(ThrowException());
131 std::string CapturedStdOut
= testing::internal::GetCapturedStdout();
132 EXPECT_EQ(CapturedStdOut
, "Caught: 'To be caught in JIT'\n");
135 } // end anonymous namespace