1 | //===-- flang/unittests/Runtime/CrashHandlerFixture.cpp ---------*- C++ -*-===// |
---|---|
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 | #include "CrashHandlerFixture.h" |
9 | #include "../../runtime/terminator.h" |
10 | #include <cstdarg> |
11 | #include <cstdlib> |
12 | |
13 | // Replaces Fortran runtime's crash handler so we can verify the crash message |
14 | [[noreturn]] static void CatchCrash( |
15 | const char *sourceFile, int sourceLine, const char *message, va_list &ap) { |
16 | char buffer[1000]; |
17 | std::vsnprintf(s: buffer, maxlen: sizeof buffer, format: message, arg: ap); |
18 | va_end(ap); |
19 | llvm::errs() |
20 | << "Test " |
21 | << ::testing::UnitTest::GetInstance()->current_test_info()->name() |
22 | << " crashed in file " |
23 | << (sourceFile ? sourceFile : "unknown source file") << '(' << sourceLine |
24 | << "): "<< buffer << '\n'; |
25 | std::exit(EXIT_FAILURE); |
26 | } |
27 | |
28 | // Register the crash handler above when creating each unit test in this suite |
29 | void CrashHandlerFixture::SetUp() { |
30 | static bool isCrashHanlderRegistered{false}; |
31 | |
32 | if (!isCrashHanlderRegistered) { |
33 | Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash); |
34 | } |
35 | |
36 | isCrashHanlderRegistered = true; |
37 | } |
38 |