[flang-commits] [PATCH] D129024: [flang][runtime] Trim FORMATs echoed to error messages

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jul 1 15:21:20 PDT 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

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.


https://reviews.llvm.org/D129024

Files:
  flang/runtime/format.h


Index: flang/runtime/format.h
===================================================================
--- flang/runtime/format.h
+++ flang/runtime/format.h
@@ -150,11 +150,24 @@
 
   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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129024.441799.patch
Type: text/x-patch
Size: 1338 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220701/fec51d30/attachment.bin>


More information about the flang-commits mailing list