Skip to content
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

Merged
merged 1 commit into from
Mar 26, 2024
Merged

[Flang] Add new CHECK_MSG() function #86576

merged 1 commit into from
Mar 26, 2024

Conversation

ceseo
Copy link
Contributor

@ceseo ceseo commented Mar 25, 2024

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

@ceseo ceseo requested review from luporl and klausler March 25, 2024 20:51
@ceseo ceseo self-assigned this Mar 25, 2024
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 25, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 25, 2024

@llvm/pr-subscribers-flang-semantics

Author: Carlos Seo (ceseo)

Changes

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

Copy link

github-actions bot commented Mar 25, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link

✅ With the latest revision this PR passed the Python code formatter.

flang/include/flang/Common/idioms.h Outdated Show resolved Hide resolved
flang/include/flang/Common/idioms.h Outdated Show resolved Hide resolved
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
@ceseo ceseo merged commit a51d13f into llvm:main Mar 26, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] Compilation error of a large array with initialization
3 participants