[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 14:01:15 PDT 2024


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

>From 580be8dbc94ca3894e14a4759c568dabec83fe82 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 | 4 ++++
 flang/lib/Evaluate/constant.cpp     | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/flang/include/flang/Common/idioms.h b/flang/include/flang/Common/idioms.h
index f6c9cbbc0f7cd0..a24c11d2bfc026 100644
--- a/flang/include/flang/Common/idioms.h
+++ b/flang/include/flang/Common/idioms.h
@@ -68,6 +68,8 @@ 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 +89,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