Skip to content

Commit b1e29ec

Browse files
authored
[flang] remove sequences of duplicate messages (#161916)
Fixes bug exposed by #161915 by keeping a cache of messages printed at a given location.
1 parent e61b6f6 commit b1e29ec

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

flang/include/flang/Parser/message.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ class Message : public common::ReferenceCounted<Message> {
307307
bool Merge(const Message &);
308308
bool operator==(const Message &that) const;
309309
bool operator!=(const Message &that) const { return !(*this == that); }
310+
bool AtSameLocation(const Message &) const;
310311

311312
private:
312-
bool AtSameLocation(const Message &) const;
313313
std::variant<ProvenanceRange, CharBlock> location_;
314314
std::variant<MessageFixedText, MessageFormattedText, MessageExpectedText>
315315
text_;

flang/lib/Parser/message.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,31 @@ void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
477477
}
478478
std::stable_sort(sorted.begin(), sorted.end(),
479479
[](const Message *x, const Message *y) { return x->SortBefore(*y); });
480-
const Message *lastMsg{nullptr};
480+
std::vector<const Message *> msgsWithLastLocation;
481481
std::size_t errorsEmitted{0};
482482
for (const Message *msg : sorted) {
483-
if (lastMsg && *msg == *lastMsg) {
484-
// Don't emit two identical messages for the same location
483+
bool shouldSkipMsg{false};
484+
// Don't emit two identical messages for the same location.
485+
// At the same location, messages are sorted by the order they were
486+
// added to the Messages buffer, which is a decent proxy for the
487+
// causality of the messages.
488+
if (!msgsWithLastLocation.empty()) {
489+
if (msgsWithLastLocation[0]->AtSameLocation(*msg)) {
490+
for (const Message *msgAtThisLocation : msgsWithLastLocation) {
491+
if (*msg == *msgAtThisLocation) {
492+
shouldSkipMsg = true; // continue loop over sorted messages
493+
break;
494+
}
495+
}
496+
} else {
497+
msgsWithLastLocation.clear();
498+
}
499+
}
500+
if (shouldSkipMsg) {
485501
continue;
486502
}
503+
msgsWithLastLocation.push_back(msg);
487504
msg->Emit(o, allCooked, echoSourceLines, hintFlagPtr);
488-
lastMsg = msg;
489505
if (warningsAreErrors || msg->IsFatal()) {
490506
++errorsEmitted;
491507
}

flang/test/Semantics/associated.f90

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@ subroutine test(assumedRank)
253253
lvar = associated(intPointerVar1, targetIntCoarray[1])
254254
!ERROR: 'neverdeclared' is not a procedure
255255
!ERROR: Could not characterize intrinsic function actual argument 'badpointer'
256-
!ERROR: 'neverdeclared' is not a procedure
257-
!ERROR: Could not characterize intrinsic function actual argument 'badpointer'
258256
lvar = associated(badPointer)
259257
end subroutine test
260258
end subroutine assoc

0 commit comments

Comments
 (0)