[llvm-branch-commits] [llvm] [Support] Add an option to match each verify directive only once (PR #233699)
Alexander Richardson via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Oct 20 14:57:44 PDT 2026
https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/233699
>From d4d9c939281cc3df672bb2962fcf7279c5bf35f5 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Tue, 20 Oct 2026 14:56:06 -0700
Subject: [PATCH] [nspr] initial commit
---
.../Support/SourceMgrDiagnosticVerifier.h | 11 +++++++-
.../Support/SourceMgrDiagnosticVerifier.cpp | 7 +++--
.../SourceMgrDiagnosticVerifierTest.cpp | 28 +++++++++++++++++++
3 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
index 5ac34ad38a504..a9004b322a539 100644
--- a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
+++ b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
@@ -71,9 +71,15 @@ class LLVM_ABI SourceMgrDiagnosticVerifier {
/// non-empty, directives are only recognized after the first occurrence of
/// one of these strings on a line, so that text outside of comments is
/// ignored.
+ ///
+ /// If \p oneDiagPerDirective is true, each directive matches at most one
+ /// diagnostic, as in Clang, so that a duplicated diagnostic is reported as
+ /// unexpected. Otherwise, a directive matches any number of identical
+ /// diagnostics on its line.
SourceMgrDiagnosticVerifier(const SourceMgr &mgr, raw_ostream &os,
ArrayRef<StringRef> prefixes = {"expected"},
- ArrayRef<StringRef> commentPrefixes = {});
+ ArrayRef<StringRef> commentPrefixes = {},
+ bool oneDiagPerDirective = false);
/// Matches a diagnostic against the expected diagnostics for \p loc, or
/// against the '@unknown' expected diagnostics if \p loc is std::nullopt.
@@ -142,6 +148,9 @@ class LLVM_ABI SourceMgrDiagnosticVerifier {
/// The strings that start a comment, see the constructor.
SmallVector<std::string, 0> commentPrefixes;
+ /// Whether a directive can only match one diagnostic, see the constructor.
+ bool oneDiagPerDirective;
+
/// 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;
diff --git a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
index 1275016e9bfc0..59faacc901444 100644
--- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
+++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
@@ -16,8 +16,8 @@ using namespace llvm;
SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier(
const SourceMgr &mgr, raw_ostream &os, ArrayRef<StringRef> prefixes,
- ArrayRef<StringRef> commentPrefixes)
- : mgr(mgr), os(os) {
+ ArrayRef<StringRef> commentPrefixes, bool oneDiagPerDirective)
+ : mgr(mgr), os(os), oneDiagPerDirective(oneDiagPerDirective) {
assert(!prefixes.empty() && "must have at least one prefix");
std::string prefixAlt;
raw_string_ostream prefixOs(prefixAlt);
@@ -240,6 +240,9 @@ SourceMgrDiagnosticVerifier::process(SourceMgr::DiagKind kind,
// File line must match (unless it's an unknown location).
if (loc && e.lineNo != loc->lineNo)
continue;
+ // A directive that already matched a diagnostic is used up.
+ if (oneDiagPerDirective && e.matched)
+ continue;
if (e.match(message)) {
if (e.kind == kind) {
e.matched = true;
diff --git a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
index 034d181ac379f..794d2b68ce6f2 100644
--- a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
+++ b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
@@ -142,3 +142,31 @@ TEST_F(SourceMgrDiagnosticVerifierTest, CustomPrefixesAndCommentScoping) {
EXPECT_TRUE(Verifier.verify());
EXPECT_EQ(Output, "");
}
+
+TEST_F(SourceMgrDiagnosticVerifierTest, DirectiveMatchesDuplicates) {
+ const MemoryBuffer *Buf = addBuffer("op\n# expected-error at -1 {{dup}}\n");
+ SourceMgrDiagnosticVerifier Verifier(SM, OS);
+
+ SMDiagnostic Dup = getMessage(Buf, SourceMgr::DK_Error, "dup");
+ EXPECT_EQ(Verifier.process(Dup), MatchResult::Matched);
+ EXPECT_EQ(Verifier.process(Dup), MatchResult::Matched);
+ EXPECT_TRUE(Verifier.verify());
+ EXPECT_EQ(Output, "");
+}
+
+TEST_F(SourceMgrDiagnosticVerifierTest, OneDiagPerDirective) {
+ const MemoryBuffer *Buf = addBuffer("op\n"
+ "# expected-error at -1 {{dup}}\n"
+ "# expected-error at -2 {{dup}}\n");
+ SourceMgrDiagnosticVerifier Verifier(SM, OS, {"expected"}, {},
+ /*oneDiagPerDirective=*/true);
+
+ // Each directive matches one diagnostic, so the third one is unexpected.
+ SMDiagnostic Dup = getMessage(Buf, SourceMgr::DK_Error, "dup");
+ EXPECT_EQ(Verifier.process(Dup), MatchResult::Matched);
+ EXPECT_EQ(Verifier.process(Dup), MatchResult::Matched);
+ EXPECT_EQ(Verifier.process(Dup), MatchResult::Unexpected);
+ EXPECT_FALSE(Verifier.verify());
+ EXPECT_EQ(StringRef(Output).count("error: dup"), 1u);
+ EXPECT_EQ(StringRef(Output).find("was not produced"), StringRef::npos);
+}
More information about the llvm-branch-commits
mailing list