https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/233306
>From 9c8f37b963717960cb2e98ab5e4ca840ace6eab0 Mon Sep 17 00:00:00 2001 From: Alex Richardson <[email protected]> Date: Fri, 6 Nov 2026 15:21:05 -0800 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 3c5cc00b0ceb37..51da5f0dbb70b6 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" @@ -113,11 +113,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; @@ -149,9 +144,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 7c00be4f89c6c2..29dcdb91f21d5f 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; @@ -241,14 +244,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, const MemoryBuffer *buf, unsigned lineNo, unsigned colNo, StringRef message, @@ -257,12 +252,7 @@ SourceMgrDiagnosticVerifier::MatchResult SourceMgrDiagnosticVerifier::process( 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; } @@ -320,12 +310,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" _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
