Skip to content

Commit eb93322

Browse files
committed
[flang] Implement a runtime routine to report fatal errors with source position
The title says it all. I implemented a routine called "Crash" and added a test. Differential Revision: https://reviews.llvm.org/D118509
1 parent 24f88f5 commit eb93322

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

flang/include/flang/Runtime/stop.h

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ NORETURN void RTNAME(ProgramEndStatement)(NO_ARGUMENTS);
3030
NORETURN void RTNAME(Exit)(int status = EXIT_SUCCESS);
3131
NORETURN void RTNAME(Abort)(NO_ARGUMENTS);
3232

33+
// Crash with an error message when the program dynamically violates a Fortran
34+
// constraint.
35+
NORETURN void RTNAME(Crash)(const char *message, const char *source, int line);
36+
3337
FORTRAN_EXTERN_C_END
3438

3539
#endif // FORTRAN_RUNTIME_STOP_H_

flang/runtime/stop.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,9 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
143143
}
144144

145145
[[noreturn]] void RTNAME(Abort)() { std::abort(); }
146+
147+
[[noreturn]] void RTNAME(Crash)(
148+
const char *message, const char *source, int line) {
149+
Fortran::runtime::Terminator{source, line}.Crash(message);
150+
}
146151
}

flang/unittests/Runtime/Stop.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ TEST(TestProgramEnd, ExitTest) {
8383
}
8484

8585
TEST(TestProgramEnd, AbortTest) { EXPECT_DEATH(RTNAME(Abort)(), ""); }
86+
87+
TEST(TestProgramEnd, CrashTest) {
88+
static const std::string crashMessage{"bad user code"};
89+
static const std::string fileName{"file name"};
90+
static const std::string headMessage{"fatal Fortran runtime error\\("};
91+
static const std::string tailMessage{":343\\): "};
92+
static const std::string fullMessage{
93+
headMessage + fileName + tailMessage + crashMessage};
94+
EXPECT_DEATH(RTNAME(Crash)(crashMessage.c_str(), fileName.c_str(), 343),
95+
fullMessage.c_str());
96+
}

0 commit comments

Comments
 (0)