Skip to content

Commit

Permalink
[Flang] Add new CHECK_MSG() function (#86576)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ceseo committed Mar 26, 2024
1 parent 47f4a07 commit a51d13f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions flang/include/flang/Common/idioms.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ template <typename... LAMBDAS> visitors(LAMBDAS... x) -> visitors<LAMBDAS...>;
// To disable, compile with '-DCHECK=(void)'
#ifndef CHECK
#define CHECK(x) ((x) || (DIE("CHECK(" #x ") failed"), false))
// Same as above, but with a custom error message.
#define CHECK_MSG(x, y) ((x) || (DIE("CHECK(" #x ") failed: " #y), false))
#endif

// User-defined type traits that default to false:
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Evaluate/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ template <typename RESULT, typename ELEMENT>
auto ConstantBase<RESULT, ELEMENT>::Reshape(
const ConstantSubscripts &dims) const -> std::vector<Element> {
std::optional<uint64_t> optN{TotalElementCount(dims)};
CHECK(optN);
CHECK_MSG(optN, "Overflow in TotalElementCount");
uint64_t n{*optN};
CHECK(!empty() || n == 0);
std::vector<Element> elements;
Expand Down

0 comments on commit a51d13f

Please sign in to comment.