llvm-project/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
Vassil Vassilev 6fe2beba7d Reland "[clang-repl] Re-implement clang-interpreter as a test case."
Original commit message: "
    Original commit message:"
      The current infrastructure in lib/Interpreter has a tool, clang-repl, very
      similar to clang-interpreter which also allows incremental compilation.

      This patch moves clang-interpreter as a test case and drops it as conditionally
      built example as we already have clang-repl in place.

      Differential revision: https://reviews.llvm.org/D107049
    "

    This patch also ignores ppc due to missing weak symbol for __gxx_personality_v0
    which may be a feature request for the jit infrastructure. Also, adds a missing
    build system dependency to the orc jit.
"

Additionally, this patch defines a custom exception type and thus avoids the
requirement to include header <exception>, making it easier to deploy across
systems without standard location of the c++ headers.

Differential revision: https://reviews.llvm.org/D107049
2021-09-03 12:02:58 +00:00

128 lines
4.2 KiB
C++

//===- unittests/Interpreter/InterpreterExceptionTest.cpp -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Unit tests for Clang's Interpreter library.
//
//===----------------------------------------------------------------------===//
#include "clang/Interpreter/Interpreter.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Basic/Version.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Config/config.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ManagedStatic.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace clang;
namespace {
using Args = std::vector<const char *>;
static std::unique_ptr<Interpreter>
createInterpreter(const Args &ExtraArgs = {},
DiagnosticConsumer *Client = nullptr) {
Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
if (Client)
CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
return cantFail(clang::Interpreter::create(std::move(CI)));
}
TEST(InterpreterTest, CatchException) {
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
{
auto J = llvm::orc::LLJITBuilder().create();
if (!J) {
// The platform does not support JITs.
// We can't use llvm::consumeError as it needs typeinfo for ErrorInfoBase.
auto E = J.takeError();
(void)E;
return;
}
}
#define Stringify(s) Stringifyx(s)
#define Stringifyx(s) #s
// We define a custom exception to avoid #include-ing the <exception> header
// which would require this test to know about the libstdc++ location.
// its own header file.
#define CUSTOM_EXCEPTION \
struct custom_exception { \
custom_exception(const char* Msg) : Message(Msg) {} \
const char* Message; \
};
CUSTOM_EXCEPTION;
std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION);
ExceptionCode +=
R"(
extern "C" int printf(const char*, ...);
static void ThrowerAnError(const char* Name) {
throw custom_exception(Name);
}
extern "C" int throw_exception() {
try {
ThrowerAnError("In JIT");
} catch (const custom_exception& E) {
printf("Caught: '%s'\n", E.Message);
} catch (...) {
printf("Unknown exception\n");
}
ThrowerAnError("From JIT");
return 0;
}
)";
std::unique_ptr<Interpreter> Interp = createInterpreter();
// FIXME: Re-enable the excluded target triples.
const clang::CompilerInstance *CI = Interp->getCompilerInstance();
const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple();
// FIXME: PPC fails due to `Symbols not found: [DW.ref.__gxx_personality_v0]`
// The current understanding is that the JIT should emit this symbol if it was
// not (eg. the way passing clang -fPIC does it).
if (Triple.isPPC())
return;
// FIXME: ARM fails due to `Not implemented relocation type!`
if (Triple.isARM())
return;
// FIXME: Hexagon fails due to `No available targets are compatible with
// triple "x86_64-unknown-linux-gnu"`
if (Triple.getArch() == llvm::Triple::hexagon)
return;
// Adjust the resource-dir
llvm::cantFail(Interp->ParseAndExecute(ExceptionCode));
testing::internal::CaptureStdout();
auto ThrowException =
(int (*)())llvm::cantFail(Interp->getSymbolAddress("throw_exception"));
EXPECT_ANY_THROW(ThrowException());
std::string CapturedStdOut = testing::internal::GetCapturedStdout();
EXPECT_EQ(CapturedStdOut, "Caught: 'In JIT'\n");
llvm::llvm_shutdown();
}
} // end anonymous namespace