[flang-commits] [flang] b43e083 - [flang] Don't require newline at EOF in unformatted sequential runtime input

peter klausler via flang-commits flang-commits at lists.llvm.org
Tue Jul 20 15:25:21 PDT 2021


Author: peter klausler
Date: 2021-07-20T15:25:09-07:00
New Revision: b43e083bb6b145905cac576b728f238a692a0048

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

LOG: [flang] Don't require newline at EOF in unformatted sequential runtime input

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.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp
index 73b7a24324da..35d305d295da 100644
--- a/flang/runtime/file.cpp
+++ b/flang/runtime/file.cpp
@@ -199,7 +199,6 @@ std::size_t OpenFile::Read(FileOffset at, char *buffer, std::size_t minBytes,
   while (got < minBytes) {
     auto chunk{::read(fd_, buffer + got, maxBytes - got)};
     if (chunk == 0) {
-      handler.SignalEnd();
       break;
     } else if (chunk < 0) {
       auto err{errno};

diff  --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index e1ff6e7fd293..8e870c00637b 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -341,19 +341,22 @@ const char *ExternalFileUnit::FrameNextInput(
 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 @@ void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
         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();
       }
     }


        


More information about the flang-commits mailing list