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