[flang-commits] [flang] af6b8d5 - [flang] Refine CR handling

peter klausler via flang-commits flang-commits at lists.llvm.org
Tue Jul 14 14:15:21 PDT 2020


Author: peter klausler
Date: 2020-07-14T14:14:35-07:00
New Revision: af6b8d51390dc1a4af7ae5de4e71947dce8a75f6

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

LOG: [flang] Refine CR handling

We need to retain carriage return characters in source files
that are not parts of multi-byte line endings; they are
significant in CHARACTER literal constants.

Reviewed By: tskeith

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

Added: 
    

Modified: 
    flang/include/flang/Parser/char-buffer.h
    flang/lib/Parser/char-buffer.cpp
    flang/lib/Parser/source.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Parser/char-buffer.h b/flang/include/flang/Parser/char-buffer.h
index e61a3fe3427e..1879e1960c38 100644
--- a/flang/include/flang/Parser/char-buffer.h
+++ b/flang/include/flang/Parser/char-buffer.h
@@ -58,9 +58,6 @@ class CharBuffer {
 
   std::string Marshal() const;
 
-  // Removes carriage returns ('\r') and ensures a final line feed ('\n').
-  std::string MarshalNormalized() const;
-
 private:
   struct Block {
     static constexpr std::size_t capacity{1 << 20};

diff  --git a/flang/lib/Parser/char-buffer.cpp b/flang/lib/Parser/char-buffer.cpp
index e0fc7335424b..780d7e89538f 100644
--- a/flang/lib/Parser/char-buffer.cpp
+++ b/flang/lib/Parser/char-buffer.cpp
@@ -65,26 +65,4 @@ std::string CharBuffer::Marshal() const {
   CHECK(result.size() == bytes_);
   return result;
 }
-
-std::string CharBuffer::MarshalNormalized() const {
-  std::string result;
-  std::size_t bytes{bytes_};
-  result.reserve(bytes + 1 /* for terminal line feed */);
-  char ch{'\0'};
-  for (const Block &block : blocks_) {
-    std::size_t chunk{std::min(bytes, Block::capacity)};
-    for (std::size_t j{0}; j < chunk; ++j) {
-      ch = block.data[j];
-      if (ch != '\r') {
-        result += ch;
-      }
-    }
-    bytes -= chunk;
-  }
-  if (ch != '\n') {
-    result += '\n';
-  }
-  result.shrink_to_fit();
-  return result;
-}
 } // namespace Fortran::parser

diff  --git a/flang/lib/Parser/source.cpp b/flang/lib/Parser/source.cpp
index 4f6c21fc2b48..693138c2711c 100644
--- a/flang/lib/Parser/source.cpp
+++ b/flang/lib/Parser/source.cpp
@@ -85,10 +85,19 @@ std::size_t RemoveCarriageReturns(llvm::MutableArrayRef<char> buf) {
       break;
     }
     std::size_t chunk = crcp - p;
+    auto advance{chunk + 1};
+    if (chunk + 1 >= bytes || crcp[1] == '\n') {
+      // CR followed by LF or EOF: omit
+    } else if ((chunk == 0 && p == buf.data()) || crcp[-1] == '\n') {
+      // CR preceded by LF or BOF: omit
+    } else {
+      // CR in line: retain
+      ++chunk;
+    }
     std::memmove(buffer + wrote, p, chunk);
     wrote += chunk;
-    p += chunk + 1;
-    bytes -= chunk + 1;
+    p += advance;
+    bytes -= advance;
   }
   return wrote;
 }


        


More information about the flang-commits mailing list