[llvm-branch-commits] [llvm] [Support] Highlight the whole directive in verifier errors (PR #233309)

Alexander Richardson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Oct 18 16:09:23 PDT 2026


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

>From d2cdfed7455afaba4d62320ade4b7eb32ffd34d4 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Sun, 18 Oct 2026 15:09:11 -0700
Subject: [PATCH] [nspr] initial commit

---
 llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp      |  4 ++--
 .../Support/SourceMgrDiagnosticVerifierTest.cpp       | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
index ff9c21b76f3a0..3fbfb851266f0 100644
--- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
+++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp
@@ -67,9 +67,9 @@ bool SourceMgrDiagnosticVerifier::emitError(const ExpectedDiag &Diag,
   SMRange Range;
   ArrayRef<SMRange> Ranges;
   if (Diag.FileLoc.isValid()) {
+    // Highlight the whole directive, up to and including the closing '}}'.
     Range =
-        SMRange(Diag.FileLoc, SMLoc::getFromPointer(Diag.FileLoc.getPointer() +
-                                                    Diag.Substring.size()));
+        SMRange(Diag.FileLoc, SMLoc::getFromPointer(Diag.Substring.end() + 2));
     Ranges = Range;
   }
   printDiag(Mgr.GetMessage(Diag.FileLoc, SourceMgr::DK_Error, Msg, Ranges));
diff --git a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
index 6bbd12e720c80..16954bed53515 100644
--- a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
+++ b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp
@@ -262,3 +262,14 @@ TEST_F(SourceMgrDiagnosticVerifierTest, AlreadyMatchedIsNotNearMiss) {
   EXPECT_EQ(StringRef(Output).find("diagnostic emitted when expecting"),
             StringRef::npos);
 }
+
+TEST_F(SourceMgrDiagnosticVerifierTest, ErrorHighlightsWholeDirective) {
+  addBuffer("op\n# expected-error at -1 {{missing}}\n");
+  SourceMgrDiagnosticVerifier Verifier(SM, OS);
+
+  EXPECT_FALSE(Verifier.verify());
+  EXPECT_NE(StringRef(Output).find("# expected-error at -1 {{missing}}\n"
+                                   "  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"),
+            StringRef::npos)
+      << Output;
+}



More information about the llvm-branch-commits mailing list