[llvm-branch-commits] [llvm] [Support] Track verify directives per buffer instead of per file name (PR #233306)

Alexander Richardson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 6 08:52:40 PST 2026


https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/233306

>From c4a8439837dd2575c36365a10d6c40c34c306d54 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Wed, 28 Oct 2026 11:11:45 -0700
Subject: [PATCH] [nspr] initial commit

---
 .../Support/SourceMgrDiagnosticVerifier.h     | 13 +++-----
 .../Support/SourceMgrDiagnosticVerifier.cpp   | 31 ++++++++-----------
 .../test/tools/llvm-mc/verify/include-twice.s |  6 +---
 3 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
index 750fbc8c93a9ab..d1fdb80ff88ca8 100644
--- a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
+++ b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
@@ -17,8 +17,8 @@
 #define LLVM_SUPPORT_SOURCEMGRDIAGNOSTICVERIFIER_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringMap.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Support/SourceMgr.h"
@@ -110,11 +110,6 @@ class LLVM_ABI SourceMgrDiagnosticVerifier {
                                                      SourceMgr &mgr,
                                                      const MemoryBuffer *buf);
 
-  /// Returns the cached expected diagnostics for the buffer named \p bufName,
-  /// or std::nullopt if \p computeExpectedDiags hasn't been called for it.
-  std::optional<MutableArrayRef<ExpectedDiag>>
-  getExpectedDiags(StringRef bufName);
-
   /// Returns the expected diagnostics with an '@unknown' location.
   MutableArrayRef<ExpectedDiag> getExpectedUnknownLocDiags() {
     return expectedUnknownLocDiags;
@@ -144,9 +139,9 @@ class LLVM_ABI SourceMgrDiagnosticVerifier {
   /// Regex used to recognize '<prefix>-<kind>' comments.
   Regex expected;
 
-  /// The expected diagnostics for each buffer that has been scanned so far,
-  /// keyed by buffer identifier (i.e. file name).
-  StringMap<SmallVector<ExpectedDiag, 2>> expectedDiagsPerFile;
+  /// The expected diagnostics for each buffer that has been scanned so far.
+  DenseMap<const MemoryBuffer *, SmallVector<ExpectedDiag, 2>>
+      expectedDiagsPerBuffer;
 
   /// The expected diagnostics with an '@unknown' location.
   SmallVector<ExpectedDiag, 2> expectedUnknownLocDiags;
diff --git a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
index 5856ec425337b2..d7dac0ed49793c 100644
--- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
+++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
@@ -116,7 +116,10 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os,
   // If the buffer is invalid, return an empty list.
   if (!buf)
     return {};
-  auto &expectedDiags = expectedDiagsPerFile[buf->getBufferIdentifier()];
+  auto [It, Inserted] = expectedDiagsPerBuffer.try_emplace(buf);
+  if (!Inserted)
+    return It->second;
+  auto &expectedDiags = It->second;
 
   // The number of the last line that did not correlate to a designator.
   unsigned lastNonDesignatorLine = 0;
@@ -229,14 +232,6 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os,
   return expectedDiags;
 }
 
-std::optional<MutableArrayRef<SourceMgrDiagnosticVerifier::ExpectedDiag>>
-SourceMgrDiagnosticVerifier::getExpectedDiags(StringRef bufName) {
-  auto expectedDiags = expectedDiagsPerFile.find(bufName);
-  if (expectedDiags != expectedDiagsPerFile.end())
-    return MutableArrayRef<ExpectedDiag>(expectedDiags->second);
-  return std::nullopt;
-}
-
 SourceMgrDiagnosticVerifier::MatchResult
 SourceMgrDiagnosticVerifier::process(raw_ostream &os, SourceMgr &mgr,
                                      SourceMgr::DiagKind kind, bool hasLoc,
@@ -246,12 +241,7 @@ SourceMgrDiagnosticVerifier::process(raw_ostream &os, SourceMgr &mgr,
   if (hasLoc) {
     // If the buffer couldn't be resolved, `diags` stays empty: a diagnostic
     // with a location in an unknown file can never match anything.
-    if (buf) {
-      if (auto maybeDiags = getExpectedDiags(buf->getBufferIdentifier()))
-        diags = *maybeDiags;
-      else
-        diags = computeExpectedDiags(os, mgr, buf);
-    }
+    diags = computeExpectedDiags(os, mgr, buf);
   } else {
     diags = expectedUnknownLocDiags;
   }
@@ -307,12 +297,17 @@ bool SourceMgrDiagnosticVerifier::verify(raw_ostream &os, SourceMgr &mgr) {
                          " of " + Twine(diag.numExpected) + " times");
     ok = false;
   };
-  for (auto &expectedDiagsPair : expectedDiagsPerFile)
-    for (auto &diag : expectedDiagsPair.second)
+  // Check buffers in registration order so that the output is deterministic.
+  for (unsigned i = 0, e = mgr.getNumBuffers(); i != e; ++i) {
+    auto it = expectedDiagsPerBuffer.find(mgr.getMemoryBuffer(i + 1));
+    if (it == expectedDiagsPerBuffer.end())
+      continue;
+    for (auto &diag : it->second)
       checkExpectedDiags(diag);
+  }
   for (auto &diag : expectedUnknownLocDiags)
     checkExpectedDiags(diag);
-  expectedDiagsPerFile.clear();
+  expectedDiagsPerBuffer.clear();
   expectedUnknownLocDiags.clear();
   return ok;
 }
diff --git a/llvm/test/tools/llvm-mc/verify/include-twice.s b/llvm/test/tools/llvm-mc/verify/include-twice.s
index 3c730ebcdcb81b..6e85b768506ba7 100644
--- a/llvm/test/tools/llvm-mc/verify/include-twice.s
+++ b/llvm/test/tools/llvm-mc/verify/include-twice.s
@@ -3,11 +3,7 @@
 
 # REQUIRES: x86-registered-target
 # RUN: rm -rf %t && split-file %s %t && cd %t
-## FIXME: Both inclusions share one list of directives, so the second
-## diagnostic finds the directive already used.
-# RUN: not llvm-mc --triple=x86_64 --verify main.s 2>&1 \
-# RUN:   | FileCheck %s --implicit-check-not=error:
-# CHECK: inc.s:1:1: error: unexpected error: unknown directive
+# RUN: llvm-mc --triple=x86_64 --verify main.s
 
 #--- main.s
 .include "inc.s"



More information about the llvm-branch-commits mailing list