[flang-commits] [flang] [Flang] Add new CHECK_MSG() function (PR #86576)

Carlos Seo via flang-commits flang-commits at lists.llvm.org
Mon Mar 25 13:51:58 PDT 2024


https://github.com/ceseo created https://github.com/llvm/llvm-project/pull/86576

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

>From 5662aae74b9776b3a408feccec63496a4e318ae6 Mon Sep 17 00:00:00 2001
From: Carlos Eduardo Seo <carlos.seo at linaro.org>
Date: Mon, 25 Mar 2024 20:47:50 +0000
Subject: [PATCH] [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
---
 flang/include/flang/Common/idioms.h | 3 +++
 flang/lib/Evaluate/constant.cpp     | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/flang/include/flang/Common/idioms.h b/flang/include/flang/Common/idioms.h
index f6c9cbbc0f7cd0..455ddb8084b201 100644
--- a/flang/include/flang/Common/idioms.h
+++ b/flang/include/flang/Common/idioms.h
@@ -68,6 +68,7 @@ template <typename... LAMBDAS> visitors(LAMBDAS... x) -> visitors<LAMBDAS...>;
 [[noreturn]] void die(const char *, ...);
 
 #define DIE(x) Fortran::common::die(x " at " __FILE__ "(%d)", __LINE__)
+#define DIE_MSG(x, y) Fortran::common::die(x " at " __FILE__ "(%d): %s", __LINE__, y)
 
 // For switch statement default: labels.
 #define CRASH_NO_CASE DIE("no case")
@@ -87,6 +88,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_MSG("CHECK(" #x ") failed: ", y), false))
 #endif
 
 // User-defined type traits that default to false:
diff --git a/flang/lib/Evaluate/constant.cpp b/flang/lib/Evaluate/constant.cpp
index a3bdefb76a414c..990339958399ea 100644
--- a/flang/lib/Evaluate/constant.cpp
+++ b/flang/lib/Evaluate/constant.cpp
@@ -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;



More information about the flang-commits mailing list