[PATCH] D78578: [flang] Fix handling of files without terminating newlines.

David Truby via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 21 11:52:50 PDT 2020


DavidTruby created this revision.
DavidTruby added a reviewer: sscalpone.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78578

Files:
  flang/lib/Parser/source.cpp


Index: flang/lib/Parser/source.cpp
===================================================================
--- flang/lib/Parser/source.cpp
+++ flang/lib/Parser/source.cpp
@@ -133,6 +133,18 @@
     buf_->getBuffer()[0] = '\n';
   }
   buf_end_ = RemoveCarriageReturns(buf_->getBuffer());
+  if (content().back() != '\n') {
+    // Don't bother to copy if we have spare memory
+    if (content().size() >= buf_->getBufferSize()) {
+      auto tmp_buf = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
+          content().size() + 1);
+      llvm::copy(content(), tmp_buf->getBufferStart());
+      Close();
+      buf_ = std::move(tmp_buf);
+    }
+    buf_end_++;
+    buf_->getBuffer()[buf_end_ - 1] = '\n';
+  }
   IdentifyPayload();
   RecordLineStarts();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78578.259066.patch
Type: text/x-patch
Size: 761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200421/22cdc77c/attachment.bin>


More information about the llvm-commits mailing list