[flang-commits] [flang] [flang] Improve warning text (PR #166407)

via flang-commits flang-commits at lists.llvm.org
Tue Nov 4 09:42:24 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

When an overflow or other floating-point exception occurs at compilation time while folding a conversion of a math library call to a smaller type, don't confuse the user by mentioning the conversion; just note that the exception was noted while folding the intrinsic function.

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


3 Files Affected:

- (modified) flang/lib/Evaluate/common.cpp (+10-9) 
- (modified) flang/lib/Evaluate/intrinsics-library.cpp (+1-1) 
- (modified) flang/test/Evaluate/folding33.f90 (+1-1) 


``````````diff
diff --git a/flang/lib/Evaluate/common.cpp b/flang/lib/Evaluate/common.cpp
index ed6a0ef93b0db..119ea3c5612a5 100644
--- a/flang/lib/Evaluate/common.cpp
+++ b/flang/lib/Evaluate/common.cpp
@@ -16,25 +16,26 @@ namespace Fortran::evaluate {
 void FoldingContext::RealFlagWarnings(
     const RealFlags &flags, const char *operation) {
   static constexpr auto warning{common::UsageWarning::FoldingException};
+  if (!realFlagWarningContext_.empty()) {
+    // Override 'operation' with a string like
+    // "compilation-time evaluation of a call to '...'"
+    operation = realFlagWarningContext_.c_str();
+  }
   if (flags.test(RealFlag::Overflow)) {
-    Warn(warning, "overflow on %s%s"_warn_en_US, operation,
-        realFlagWarningContext_);
+    Warn(warning, "overflow on %s"_warn_en_US, operation);
   }
   if (flags.test(RealFlag::DivideByZero)) {
     if (std::strcmp(operation, "division") == 0) {
-      Warn(warning, "division by zero%s"_warn_en_US, realFlagWarningContext_);
+      Warn(warning, "division by zero"_warn_en_US);
     } else {
-      Warn(warning, "division by zero on %s%s"_warn_en_US, operation,
-          realFlagWarningContext_);
+      Warn(warning, "division by zero on %s"_warn_en_US, operation);
     }
   }
   if (flags.test(RealFlag::InvalidArgument)) {
-    Warn(warning, "invalid argument on %s%s"_warn_en_US, operation,
-        realFlagWarningContext_);
+    Warn(warning, "invalid argument on %s"_warn_en_US, operation);
   }
   if (flags.test(RealFlag::Underflow)) {
-    Warn(warning, "underflow on %s%s"_warn_en_US, operation,
-        realFlagWarningContext_);
+    Warn(warning, "underflow on %s"_warn_en_US, operation);
   }
 }
 
diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp
index d8af5246fabdd..54726ac539d60 100644
--- a/flang/lib/Evaluate/intrinsics-library.cpp
+++ b/flang/lib/Evaluate/intrinsics-library.cpp
@@ -1052,7 +1052,7 @@ std::optional<HostRuntimeWrapper> GetHostRuntimeWrapper(const std::string &name,
                 .value());
       }
       auto restorer{context.SetRealFlagWarningContext(
-          " after folding a call to '"s + name + "'"s)};
+          "compilation-time evaluation of a call to '"s + name + "'"s)};
       return Fold(context,
           ConvertToType(
               resultType, hostFolderWithChecks(context, std::move(args)))
diff --git a/flang/test/Evaluate/folding33.f90 b/flang/test/Evaluate/folding33.f90
index fb5a23cf1f209..299cb7e1731a5 100644
--- a/flang/test/Evaluate/folding33.f90
+++ b/flang/test/Evaluate/folding33.f90
@@ -1,4 +1,4 @@
 !RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
-!CHECK: warning: overflow on REAL(4) to REAL(2) conversion after folding a call to 'exp' [-Wfolding-exception]
+!CHECK: warning: overflow on compilation-time evaluation of a call to 'exp' [-Wfolding-exception]
 print *, exp((11.265625_2,1._2))
 end

``````````

</details>


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


More information about the flang-commits mailing list