[flang-commits] [PATCH] D106321: [flang] Don't require newline at EOF in unformatted sequetial runtime input

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jul 19 16:00:01 PDT 2021


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

F18 <https://reviews.llvm.org/F18> was sigalling an end-of-file error condition when reading an
 unformatted sequential input file without an ultimate newline
 (or CR-LF).  Other Fortran implementations can handle it, so change
 the runtime to support it.


https://reviews.llvm.org/D106321

Files:
  flang/runtime/file.cpp
  flang/runtime/unit.cpp


Index: flang/runtime/unit.cpp
===================================================================
--- flang/runtime/unit.cpp
+++ flang/runtime/unit.cpp
@@ -341,19 +341,22 @@
 bool ExternalFileUnit::SetSequentialVariableFormattedRecordLength() {
   if (recordLength || access != Access::Sequential) {
     return true;
-  }
-  if (FrameLength() > recordOffsetInFrame_) {
+  } else if (FrameLength() > recordOffsetInFrame_) {
     const char *record{Frame() + recordOffsetInFrame_};
-    if (const char *nl{reinterpret_cast<const char *>(
-            std::memchr(record, '\n', FrameLength() - recordOffsetInFrame_))}) {
+    std::size_t bytes{FrameLength() - recordOffsetInFrame_};
+    if (const char *nl{
+            reinterpret_cast<const char *>(std::memchr(record, '\n', bytes))}) {
       recordLength = nl - record;
       if (*recordLength > 0 && record[*recordLength - 1] == '\r') {
         --*recordLength;
       }
-      return true;
+    } else {
+      recordLength = bytes; // final record w/o \n
     }
+    return true;
+  } else {
+    return false;
   }
-  return false;
 }
 
 void ExternalFileUnit::SetLeftTabLimit() {
@@ -413,8 +416,10 @@
         if (Frame()[recordOffsetInFrame_ + *recordLength] == '\r') {
           ++recordOffsetInFrame_;
         }
-        recordOffsetInFrame_ += *recordLength + 1;
-        RUNTIME_CHECK(handler, Frame()[recordOffsetInFrame_ - 1] == '\n');
+        if (Frame()[recordOffsetInFrame_ + *recordLength] == '\n') {
+          ++recordOffsetInFrame_;
+        }
+        recordOffsetInFrame_ += *recordLength;
         recordLength.reset();
       }
     }
Index: flang/runtime/file.cpp
===================================================================
--- flang/runtime/file.cpp
+++ flang/runtime/file.cpp
@@ -199,7 +199,6 @@
   while (got < minBytes) {
     auto chunk{::read(fd_, buffer + got, maxBytes - got)};
     if (chunk == 0) {
-      handler.SignalEnd();
       break;
     } else if (chunk < 0) {
       auto err{errno};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106321.359948.patch
Type: text/x-patch
Size: 1994 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210719/6a3b7d36/attachment.bin>


More information about the flang-commits mailing list