diff --git a/flang/unittests/RuntimeGTest/CMakeLists.txt b/flang/unittests/RuntimeGTest/CMakeLists.txt --- a/flang/unittests/RuntimeGTest/CMakeLists.txt +++ b/flang/unittests/RuntimeGTest/CMakeLists.txt @@ -1,5 +1,6 @@ add_flang_unittest(FlangRuntimeTests CharacterTest.cpp + CrashHandlerTest.cpp ) target_link_libraries(FlangRuntimeTests diff --git a/flang/unittests/RuntimeGTest/CrashHandlerTest.cpp b/flang/unittests/RuntimeGTest/CrashHandlerTest.cpp new file mode 100644 --- /dev/null +++ b/flang/unittests/RuntimeGTest/CrashHandlerTest.cpp @@ -0,0 +1,52 @@ +//===-- flang/unittests/RuntimeGTest/CrashHandlerTest.cpp -------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Tests for Fortran runtime's crash handler. +// +//===----------------------------------------------------------------------===// +#include "../../runtime/terminator.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include + +#include "gtest/gtest.h" + +using namespace Fortran::runtime; + +// Override the Fortran runtime's Crash() +[[noreturn]] static void CatchCrash( + const char *sourceFile, int sourceLine, const char *message, va_list &ap) { + char buffer[1000]; + std::vsnprintf(buffer, sizeof buffer, message, ap); + va_end(ap); + llvm::errs() << (sourceFile ? sourceFile : "unknown source file") << ":" + << sourceLine << ": CRASH: " << buffer << '\n'; + std::abort(); +} + +TEST(CrashHandler, SanityCheck_NoSourceFile) { + Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash); + const char *fileName = nullptr; + Terminator terminator(fileName, /*sourceLine=*/321); + + std::string expectedCrashMsg( + "unknown source file:321: CRASH: Hello From A Failing Test"); + EXPECT_DEATH(terminator.Crash("Hello From A Failing Test"), expectedCrashMsg); +} + +TEST(CrashHandler, SanityCheck_WithSourceFile) { + Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash); + const char *fileName = "dummy-file.f90"; + Terminator terminator(fileName, /*sourceLine=*/123); + + std::string expectedCrashMsg( + "dummy-file.f90:123: CRASH: Hello From A Failing Test"); + EXPECT_DEATH(terminator.Crash("Hello From A Failing Test"), + "Hello From A Failing Test"); +}