[llvm-branch-commits] [llvm] [Support] Allow restricting verify directives to comments (PR #233698)
Alexander Richardson via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Oct 20 14:57:43 PDT 2026
https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/233698
>From c8500b13268809067ef5dbf1682f385a087b9293 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Tue, 20 Oct 2026 14:56:05 -0700
Subject: [PATCH] [nspr] initial commit
---
.../llvm/Support/SourceMgrDiagnosticVerifier.h | 12 ++++++++++--
llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp | 15 ++++++++++++++-
.../Support/SourceMgrDiagnosticVerifierTest.cpp | 7 ++++---
3 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
index bcd1426e9548f..5ac34ad38a504 100644
--- a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
+++ b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h
@@ -23,6 +23,7 @@
#include "llvm/Support/Regex.h"
#include "llvm/Support/SourceMgr.h"
#include <optional>
+#include <string>
namespace llvm {
@@ -66,9 +67,13 @@ class LLVM_ABI SourceMgrDiagnosticVerifier {
/// bypassing \p mgr's diagnostic handler.
///
/// \p prefixes are the directive prefixes to recognize (e.g. "expected"
- /// for 'expected-error'); they must be non-empty.
+ /// for 'expected-error'); they must be non-empty. If \p commentPrefixes is
+ /// 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.
SourceMgrDiagnosticVerifier(const SourceMgr &mgr, raw_ostream &os,
- ArrayRef<StringRef> prefixes = {"expected"});
+ ArrayRef<StringRef> prefixes = {"expected"},
+ ArrayRef<StringRef> commentPrefixes = {});
/// Matches a diagnostic against the expected diagnostics for \p loc, or
/// against the '@unknown' expected diagnostics if \p loc is std::nullopt.
@@ -134,6 +139,9 @@ class LLVM_ABI SourceMgrDiagnosticVerifier {
/// Regex used to recognize '<prefix>-<kind>' comments.
Regex expected;
+ /// The strings that start a comment, see the constructor.
+ SmallVector<std::string, 0> commentPrefixes;
+
/// 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 fac95397e9ecb..1275016e9bfc0 100644
--- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
+++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
@@ -10,11 +10,13 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
+#include <algorithm>
using namespace llvm;
SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier(
- const SourceMgr &mgr, raw_ostream &os, ArrayRef<StringRef> prefixes)
+ const SourceMgr &mgr, raw_ostream &os, ArrayRef<StringRef> prefixes,
+ ArrayRef<StringRef> commentPrefixes)
: mgr(mgr), os(os) {
assert(!prefixes.empty() && "must have at least one prefix");
std::string prefixAlt;
@@ -29,6 +31,11 @@ SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier(
")-(error|note|remark|warning)(-re)?"
" *(@([+-][0-9]+|above|below|unknown))? *{{(.*)}}$");
+ for (StringRef prefix : commentPrefixes) {
+ assert(!prefix.empty() && "comment prefixes must be non-empty");
+ this->commentPrefixes.push_back(prefix.str());
+ }
+
for (unsigned i = 0, e = mgr.getNumBuffers(); i != e; ++i)
(void)computeExpectedDiags(mgr.getMemoryBuffer(i + 1));
}
@@ -126,6 +133,12 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(const MemoryBuffer *buf) {
buf->getBuffer().split(lines, '\n');
for (unsigned lineNo = 0, e = lines.size(); lineNo < e; ++lineNo) {
StringRef line = lines[lineNo].rtrim();
+ if (!commentPrefixes.empty()) {
+ size_t commentStart = StringRef::npos;
+ for (StringRef prefix : commentPrefixes)
+ commentStart = std::min(commentStart, line.find(prefix));
+ line = line.substr(commentStart);
+ }
// Every directive contains '{{'; check that first to avoid running the
// regex on most lines.
diff --git a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
index 19beb0fd98988..034d181ac379f 100644
--- a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
+++ b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
@@ -122,15 +122,16 @@ TEST_F(SourceMgrDiagnosticVerifierTest, InstalledAsDiagHandler) {
EXPECT_FALSE(Verifier.verify());
}
-TEST_F(SourceMgrDiagnosticVerifierTest, CustomPrefixes) {
+TEST_F(SourceMgrDiagnosticVerifierTest, CustomPrefixesAndCommentScoping) {
const MemoryBuffer *Buf =
addBuffer("op\n"
"// check-error at -1 {{first prefix}}\n"
"# alt-warning at -2 {{second prefix}}\n"
"// uncheck-error at -3 {{longer prefix ignored}}\n"
"// other-check-error at -4 {{hyphenated prefix ignored}}\n"
- "// expected-error at -5 {{default prefix ignored}}\n");
- SourceMgrDiagnosticVerifier Verifier(SM, OS, {"check", "alt"});
+ "// expected-error at -5 {{default prefix ignored}}\n"
+ "op check-error {{outside comment ignored}}\n");
+ SourceMgrDiagnosticVerifier Verifier(SM, OS, {"check", "alt"}, {"//", "#"});
EXPECT_EQ(
Verifier.process(getMessage(Buf, SourceMgr::DK_Error, "first prefix")),
More information about the llvm-branch-commits
mailing list