Skip to content

Commit b547e1a

Browse files
committed
[flang] Fix handling of files without terminating newlines.
Reviewers: sscalpone Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D78578
1 parent 9f1e81f commit b547e1a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

flang/lib/Parser/source.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,19 @@ bool SourceFile::ReadStandardInput(llvm::raw_ostream &error) {
127127
}
128128

129129
void SourceFile::ReadFile() {
130-
if (buf_->getBuffer().size() == 0) {
131-
Close();
132-
buf_ = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(1);
133-
buf_->getBuffer()[0] = '\n';
134-
}
135130
buf_end_ = RemoveCarriageReturns(buf_->getBuffer());
131+
if (content().size() == 0 || content().back() != '\n') {
132+
// Don't bother to copy if we have spare memory
133+
if (content().size() >= buf_->getBufferSize()) {
134+
auto tmp_buf{llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
135+
content().size() + 1)};
136+
llvm::copy(content(), tmp_buf->getBufferStart());
137+
Close();
138+
buf_ = std::move(tmp_buf);
139+
}
140+
buf_end_++;
141+
buf_->getBuffer()[buf_end_ - 1] = '\n';
142+
}
136143
IdentifyPayload();
137144
RecordLineStarts();
138145
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
! RUN: echo -n "end program" > %t.f90
2+
! RUN: %f18 -fparse-only %t.f90
3+
! RUN: echo -ne "\rend program" > %t.f90
4+
! RUN: %f18 -fparse-only %t.f90

0 commit comments

Comments
 (0)