Skip to content

Commit 4772809

Browse files
author
git apple-llvm automerger
committed
Merge commit '802530283a98' from llvm.org/main into next
2 parents 17e2879 + 8025302 commit 4772809

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

flang/include/flang/Parser/message.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class MessageFixedText {
6565
return severity_ == Severity::Error || severity_ == Severity::Todo;
6666
}
6767

68+
static const MessageFixedText endOfFileMessage; // "end of file"_err_en_US
69+
6870
private:
6971
CharBlock text_;
7072
Severity severity_{Severity::None};

flang/lib/Parser/basic-parsers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ struct NextCh {
828828
if (std::optional<const char *> result{state.GetNextChar()}) {
829829
return result;
830830
}
831-
state.Say("end of file"_err_en_US);
831+
state.Say(MessageFixedText::endOfFileMessage);
832832
return std::nullopt;
833833
}
834834
};

flang/lib/Parser/message.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
namespace Fortran::parser {
2323

24+
// The nextCh parser emits this, and Message::GetProvenanceRange() looks for it.
25+
const MessageFixedText MessageFixedText::endOfFileMessage{
26+
"end of file"_err_en_US};
27+
2428
llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const MessageFixedText &t) {
2529
std::size_t n{t.text().size()};
2630
for (std::size_t j{0}; j < n; ++j) {
@@ -232,7 +236,20 @@ std::optional<ProvenanceRange> Message::GetProvenanceRange(
232236
const AllCookedSources &allCooked) const {
233237
return common::visit(
234238
common::visitors{
235-
[&](CharBlock cb) { return allCooked.GetProvenanceRange(cb); },
239+
[&](CharBlock cb) -> std::optional<ProvenanceRange> {
240+
if (auto pr{allCooked.GetProvenanceRange(cb)}) {
241+
return pr;
242+
} else if (const auto *fixed{std::get_if<MessageFixedText>(&text_)};
243+
fixed &&
244+
fixed->text() == MessageFixedText::endOfFileMessage.text() &&
245+
cb.begin() && cb.size() == 1) {
246+
// Failure from "nextCh" due to reaching EOF. Back up one byte
247+
// to the terminal newline so that the output looks better.
248+
return allCooked.GetProvenanceRange(CharBlock{cb.begin() - 1, 1});
249+
} else {
250+
return std::nullopt;
251+
}
252+
},
236253
[](const ProvenanceRange &pr) { return std::make_optional(pr); },
237254
},
238255
location_);

flang/test/Parser/recovery08.f90

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
2+
! CHECK: error: end of file
3+
! CHECK: ^
4+
! CHECK: in the context: END PROGRAM statement
5+
! CHECK: in the context: main program
6+
7+
integer :: i
8+
9+
! Add empty lines for emphasis
10+
11+
i = 5

0 commit comments

Comments
 (0)