-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flang] Add new CHECK_MSG() function #86576
Conversation
@llvm/pr-subscribers-flang-semantics Author: Carlos Seo (ceseo) ChangesAdded 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:
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;
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
✅ With the latest revision this PR passed the Python code formatter. |
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 llvm#78931
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