[flang-commits] [flang] 7c708ad - [flang][runtime] Trim FORMATs echoed to error messages

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Jul 6 18:58:57 PDT 2022


Author: Peter Klausler
Date: 2022-07-06T18:55:03-07:00
New Revision: 7c708adb4a14ef9f181e8107235fb27c53214bc1

URL: https://github.com/llvm/llvm-project/commit/7c708adb4a14ef9f181e8107235fb27c53214bc1
DIFF: https://github.com/llvm/llvm-project/commit/7c708adb4a14ef9f181e8107235fb27c53214bc1.diff

LOG: [flang][runtime] Trim FORMATs echoed to error messages

Since dynamic FORMAT strings usually come from blank-padded fixed-length
CHARACTER variables, trim leading and trailing blanks from them when they
are echoed to error messages for better readability.

Differential Revision: https://reviews.llvm.org/D129024

Added: 
    

Modified: 
    flang/runtime/format.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/format.h b/flang/runtime/format.h
index 40a0f30f9940..d0b16bb04f72 100644
--- a/flang/runtime/format.h
+++ b/flang/runtime/format.h
@@ -150,11 +150,24 @@ template <typename CONTEXT> class FormatControl {
 
   void ReportBadFormat(Context &context, const char *msg, int offset) const {
     if constexpr (std::is_same_v<CharType, char>) {
-      context.SignalError(IostatErrorInFormat,
-          "%s; at offset %d in format '%s'", msg, offset, format_);
-    } else {
-      context.SignalError(IostatErrorInFormat, "%s; at offset %d", msg, offset);
+      // Echo the bad format in the error message, but trim any leading or
+      // trailing spaces.
+      int firstNonBlank{0};
+      while (firstNonBlank < formatLength_ && format_[firstNonBlank] == ' ') {
+        ++firstNonBlank;
+      }
+      int lastNonBlank{formatLength_ - 1};
+      while (lastNonBlank > firstNonBlank && format_[lastNonBlank] == ' ') {
+        --lastNonBlank;
+      }
+      if (firstNonBlank <= lastNonBlank) {
+        context.SignalError(IostatErrorInFormat,
+            "%s; at offset %d in format '%.*s'", msg, offset,
+            lastNonBlank - firstNonBlank + 1, format_ + firstNonBlank);
+        return;
+      }
     }
+    context.SignalError(IostatErrorInFormat, "%s; at offset %d", msg, offset);
   }
 
   // Data members are arranged and typed so as to reduce size.


        


More information about the flang-commits mailing list