From f9ebc654986aa2b7fac9f608e4d2df5eb7bab098 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Tue, 4 Nov 2025 09:39:00 -0800 Subject: [PATCH] [flang] Improve warning text 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. --- flang/lib/Evaluate/common.cpp | 19 ++++++++++--------- flang/lib/Evaluate/intrinsics-library.cpp | 2 +- flang/test/Evaluate/folding33.f90 | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) 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 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