brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.7 KiB · c8522e9 Raw
140 lines · cpp
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-exception6//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// 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_BUILD36#include <sanitizer/lsan_interface.h>37LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }38#endif39 40#if defined(_AIX) || defined(__MVS__)41#define CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT42#endif43 44using namespace clang;45 46namespace {47using Args = std::vector<const char *>;48static std::unique_ptr<Interpreter>49createInterpreter(const Args &ExtraArgs = {},50                  DiagnosticConsumer *Client = nullptr) {51  Args ClangArgs = {"-Xclang", "-emit-llvm-only"};52  llvm::append_range(ClangArgs, ExtraArgs);53  auto CB = clang::IncrementalCompilerBuilder();54  CB.SetCompilerArgs(ClangArgs);55  auto CI = cantFail(CB.CreateCpp());56  if (Client)57    CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);58  return cantFail(clang::Interpreter::create(std::move(CI)));59}60 61#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT62TEST(InterpreterExceptionTest, DISABLED_CatchException) {63#else64TEST(InterpreterExceptionTest, CatchException) {65#endif66  llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.67  llvm::InitializeNativeTarget();68  llvm::InitializeNativeTargetAsmPrinter();69 70  {71    auto J = llvm::orc::LLJITBuilder().create();72    if (!J) {73      // The platform does not support JITs.74      // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we75      // can avoid that by going via the C interface.76      LLVMConsumeError(llvm::wrap(J.takeError()));77      GTEST_SKIP();78    }79  }80 81#define Stringify(s) Stringifyx(s)82#define Stringifyx(s) #s83 84  // We define a custom exception to avoid #include-ing the <exception> header85  // which would require this test to know about the libstdc++ location.86  // its own header file.87#define CUSTOM_EXCEPTION                                                       \88  struct custom_exception {                                                    \89    custom_exception(const char *Msg) : Message(Msg) {}                        \90    const char *Message;                                                       \91  };92 93  CUSTOM_EXCEPTION;94 95  std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION);96  ExceptionCode +=97      R"(98extern "C" int printf(const char*, ...);99static void ThrowerAnError(const char* Name) {100  throw custom_exception(Name);101}102 103extern "C" int throw_exception() {104  try {105    ThrowerAnError("To be caught in JIT");106  } catch (const custom_exception& E) {107    printf("Caught: '%s'\n", E.Message);108  } catch (...) {109    printf("Unknown exception\n");110  }111  ThrowerAnError("To be caught in binary");112  return 0;113}114    )";115  std::unique_ptr<Interpreter> Interp = createInterpreter();116  // FIXME: Re-enable the excluded target triples.117  const clang::CompilerInstance *CI = Interp->getCompilerInstance();118  const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple();119 120  // FIXME: ARM fails due to `Not implemented relocation type!`121  if (Triple.isARM())122    GTEST_SKIP();123 124  // FIXME: libunwind on darwin is broken, see PR49692.125  if (Triple.isOSDarwin() && (Triple.getArch() == llvm::Triple::aarch64 ||126                              Triple.getArch() == llvm::Triple::aarch64_32))127    GTEST_SKIP();128 129  llvm::cantFail(Interp->ParseAndExecute(ExceptionCode));130  testing::internal::CaptureStdout();131  auto ThrowException =132      llvm::cantFail(Interp->getSymbolAddress("throw_exception"))133          .toPtr<int (*)()>();134  EXPECT_ANY_THROW(ThrowException());135  std::string CapturedStdOut = testing::internal::GetCapturedStdout();136  EXPECT_EQ(CapturedStdOut, "Caught: 'To be caught in JIT'\n");137}138 139} // end anonymous namespace140