[flang-commits] [flang] [flang] remove sequences of duplicate messages (PR #161916)

Andre Kuhlenschmidt via flang-commits flang-commits at lists.llvm.org
Fri Oct 3 16:15:07 PDT 2025


https://github.com/akuhlens updated https://github.com/llvm/llvm-project/pull/161916

>From 3e11d688a7bb19ddb1a893e5b016e368acc82092 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Fri, 3 Oct 2025 14:53:58 -0700
Subject: [PATCH 1/3] initial commit

---
 flang/include/flang/Parser/message.h |  2 +-
 flang/lib/Parser/message.cpp         | 24 ++++++++++++++++++++----
 flang/test/Semantics/associated.f90  |  2 --
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/flang/include/flang/Parser/message.h b/flang/include/flang/Parser/message.h
index 224263e4be860..7c639eff1eeef 100644
--- a/flang/include/flang/Parser/message.h
+++ b/flang/include/flang/Parser/message.h
@@ -307,9 +307,9 @@ class Message : public common::ReferenceCounted<Message> {
   bool Merge(const Message &);
   bool operator==(const Message &that) const;
   bool operator!=(const Message &that) const { return !(*this == that); }
+  bool AtSameLocation(const Message &) const;
 
 private:
-  bool AtSameLocation(const Message &) const;
   std::variant<ProvenanceRange, CharBlock> location_;
   std::variant<MessageFixedText, MessageFormattedText, MessageExpectedText>
       text_;
diff --git a/flang/lib/Parser/message.cpp b/flang/lib/Parser/message.cpp
index 2c4f930c0b088..0e06711c5b71b 100644
--- a/flang/lib/Parser/message.cpp
+++ b/flang/lib/Parser/message.cpp
@@ -477,15 +477,31 @@ void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
   }
   std::stable_sort(sorted.begin(), sorted.end(),
       [](const Message *x, const Message *y) { return x->SortBefore(*y); });
-  const Message *lastMsg{nullptr};
+  std::vector<const Message *> msgsWithLastLocation;
   std::size_t errorsEmitted{0};
   for (const Message *msg : sorted) {
-    if (lastMsg && *msg == *lastMsg) {
-      // Don't emit two identical messages for the same location
+    bool shouldSkipMsg = false;
+    // Don't emit two identical messages for the same location
+    // At the same location messages are sorted by the order they were
+    // added to the list, which is a decent proxy for the causality
+    // of the messages.
+    if (!msgsWithLastLocation.empty()) {
+      if (msgsWithLastLocation[0]->AtSameLocation(*msg)) {
+        for (const Message *msgAtThisLocation : msgsWithLastLocation) {
+          if (*msg == *msgAtThisLocation) {
+            shouldSkipMsg = true; // continue loop over sorted messages
+            break;
+          }
+        }
+      } else {
+        msgsWithLastLocation.clear();
+      }
+    }
+    if (shouldSkipMsg) {
       continue;
     }
+    msgsWithLastLocation.push_back(msg);
     msg->Emit(o, allCooked, echoSourceLines, hintFlagPtr);
-    lastMsg = msg;
     if (warningsAreErrors || msg->IsFatal()) {
       ++errorsEmitted;
     }
diff --git a/flang/test/Semantics/associated.f90 b/flang/test/Semantics/associated.f90
index 7cb6c240db226..731f2d828995f 100644
--- a/flang/test/Semantics/associated.f90
+++ b/flang/test/Semantics/associated.f90
@@ -253,8 +253,6 @@ subroutine test(assumedRank)
     lvar = associated(intPointerVar1, targetIntCoarray[1])
     !ERROR: 'neverdeclared' is not a procedure
     !ERROR: Could not characterize intrinsic function actual argument 'badpointer'
-    !ERROR: 'neverdeclared' is not a procedure
-    !ERROR: Could not characterize intrinsic function actual argument 'badpointer'
     lvar = associated(badPointer)
   end subroutine test
 end subroutine assoc

>From 22e8f069f67cc740d36d6da52441a54d9093cbfe Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Fri, 3 Oct 2025 15:29:05 -0700
Subject: [PATCH 2/3] don't used brace initialization

---
 flang/lib/Parser/message.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Parser/message.cpp b/flang/lib/Parser/message.cpp
index 0e06711c5b71b..006d3be74b6fe 100644
--- a/flang/lib/Parser/message.cpp
+++ b/flang/lib/Parser/message.cpp
@@ -480,7 +480,7 @@ void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
   std::vector<const Message *> msgsWithLastLocation;
   std::size_t errorsEmitted{0};
   for (const Message *msg : sorted) {
-    bool shouldSkipMsg = false;
+    bool shouldSkipMsg{false};
     // Don't emit two identical messages for the same location
     // At the same location messages are sorted by the order they were
     // added to the list, which is a decent proxy for the causality

>From 7bb937ed5ad529d292d1b8a3a32de56f8255d18d Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Fri, 3 Oct 2025 15:44:46 -0700
Subject: [PATCH 3/3] clean up comment

---
 flang/lib/Parser/message.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/flang/lib/Parser/message.cpp b/flang/lib/Parser/message.cpp
index 006d3be74b6fe..cfcd08b0861ef 100644
--- a/flang/lib/Parser/message.cpp
+++ b/flang/lib/Parser/message.cpp
@@ -481,10 +481,10 @@ void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
   std::size_t errorsEmitted{0};
   for (const Message *msg : sorted) {
     bool shouldSkipMsg{false};
-    // Don't emit two identical messages for the same location
-    // At the same location messages are sorted by the order they were
-    // added to the list, which is a decent proxy for the causality
-    // of the messages.
+    // Don't emit two identical messages for the same location.
+    // At the same location, messages are sorted by the order they were
+    // added to the Messages buffer, which is a decent proxy for the
+    // causality of the messages.
     if (!msgsWithLastLocation.empty()) {
       if (msgsWithLastLocation[0]->AtSameLocation(*msg)) {
         for (const Message *msgAtThisLocation : msgsWithLastLocation) {



More information about the flang-commits mailing list