[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
Wed Oct 28 11:12:46 PDT 2026


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

>From ab6ddaceff247253d522ed832a8b90cfa13ff96b 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 3ac01df9e9f89..6b4f6c5874126 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"
@@ -98,11 +98,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;
@@ -140,9 +135,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 512b6597d5ea8..8b42c3ecdc798 100644
--- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
+++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
@@ -108,7 +108,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;
@@ -221,14 +224,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,
@@ -238,12 +233,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;
   }
@@ -329,12 +319,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 3c730ebcdcb81..6e85b768506ba 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