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

via flang-commits flang-commits at lists.llvm.org
Mon Mar 25 13:52:28 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Carlos Seo (ceseo)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/86576.diff


2 Files Affected:

- (modified) flang/include/flang/Common/idioms.h (+3) 
- (modified) flang/lib/Evaluate/constant.cpp (+1-1) 


``````````diff
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;

``````````

</details>


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


More information about the flang-commits mailing list