Skip to content

Commit 580be8d

Browse files
committed
[Flang] Add new CHECK_MSG() function
Added a new variant of the CHECK() function that takes a custom message as a parameter. This is useful for more meaninful error messages when the compiler is expected to crash. Fixes #78931
1 parent 0fe0ef4 commit 580be8d

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

flang/include/flang/Common/idioms.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ template <typename... LAMBDAS> visitors(LAMBDAS... x) -> visitors<LAMBDAS...>;
6868
[[noreturn]] void die(const char *, ...);
6969

7070
#define DIE(x) Fortran::common::die(x " at " __FILE__ "(%d)", __LINE__)
71+
#define DIE_MSG(x, y) \
72+
Fortran::common::die(x " at " __FILE__ "(%d): %s", __LINE__, y)
7173

7274
// For switch statement default: labels.
7375
#define CRASH_NO_CASE DIE("no case")
@@ -87,6 +89,8 @@ template <typename... LAMBDAS> visitors(LAMBDAS... x) -> visitors<LAMBDAS...>;
8789
// To disable, compile with '-DCHECK=(void)'
8890
#ifndef CHECK
8991
#define CHECK(x) ((x) || (DIE("CHECK(" #x ") failed"), false))
92+
// Same as above, but with a custom error message.
93+
#define CHECK_MSG(x, y) ((x) || (DIE_MSG("CHECK(" #x ") failed: ", y), false))
9094
#endif
9195

9296
// User-defined type traits that default to false:

flang/lib/Evaluate/constant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ template <typename RESULT, typename ELEMENT>
160160
auto ConstantBase<RESULT, ELEMENT>::Reshape(
161161
const ConstantSubscripts &dims) const -> std::vector<Element> {
162162
std::optional<uint64_t> optN{TotalElementCount(dims)};
163-
CHECK(optN);
163+
CHECK_MSG(optN, "Overflow in TotalElementCount");
164164
uint64_t n{*optN};
165165
CHECK(!empty() || n == 0);
166166
std::vector<Element> elements;

0 commit comments

Comments
 (0)