38 lines · cpp
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-exception6//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 message15[[noreturn]] static void CatchCrash(16 const char *sourceFile, int sourceLine, const char *message, va_list &ap) {17 char buffer[1000];18 std::vsnprintf(buffer, sizeof buffer, message, ap);19 va_end(ap);20 std::cerr << "Test "21 << ::testing::UnitTest::GetInstance()->current_test_info()->name()22 << " crashed in file "23 << (sourceFile ? sourceFile : "unknown source file") << '('24 << sourceLine << "): " << buffer << '\n';25 std::exit(EXIT_FAILURE);26}27 28// Register the crash handler above when creating each unit test in this suite29void CrashHandlerFixture::SetUp() {30 static bool isCrashHanlderRegistered{false};31 32 if (!isCrashHanlderRegistered) {33 Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash);34 }35 36 isCrashHanlderRegistered = true;37}38