[PATCH] D98601: [flang][unittest] Add unit tests for runtime's crash handler

Andrzej Warzynski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 14 09:24:22 PDT 2021


awarzynski created this revision.
Herald added a subscriber: mgorny.
Herald added a reviewer: sscalpone.
awarzynski requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98601

Files:
  flang/unittests/RuntimeGTest/CMakeLists.txt
  flang/unittests/RuntimeGTest/CrashHandlerTest.cpp


Index: flang/unittests/RuntimeGTest/CrashHandlerTest.cpp
===================================================================
--- /dev/null
+++ 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 <cstdarg>
+#include <cstring>
+#include <string>
+
+#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");
+}
Index: flang/unittests/RuntimeGTest/CMakeLists.txt
===================================================================
--- flang/unittests/RuntimeGTest/CMakeLists.txt
+++ flang/unittests/RuntimeGTest/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_flang_unittest(FlangRuntimeTests
   CharacterTest.cpp
+  CrashHandlerTest.cpp
 )
 
 target_link_libraries(FlangRuntimeTests


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98601.330514.patch
Type: text/x-patch
Size: 2519 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210314/7c2d5f05/attachment.bin>


More information about the llvm-commits mailing list