[llvm] [FileCheck][NFC] Introduce MatchResultDiag and MatchNoteDiag (PR #195569)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Wed May 6 07:29:44 PDT 2026


https://github.com/jdenny-ornl updated https://github.com/llvm/llvm-project/pull/195569

>From 0730d35e1fa8fa668624f6e2cc363b4389ef4959 Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Sun, 3 May 2026 17:56:44 -0400
Subject: [PATCH 1/6] [FileCheck][NFC] Introduce FileCheckDiagList for
 -dump-input

Problem
=======

`FileCheckDiag` and its `enum MatchType` have outgrown their original
purpose.  The `-dump-input` presentation layer (in
`llvm/utils/FileCheck/FileCheck.cpp`) and the FileCheck library's
diagnostic emission (in `llvm/lib/FileCheck/FileCheck.cpp`) are too
tightly coupled.  The interactions are subtle to understand and
maintain.  It is difficult for the former to reason about the latter's
emitted diagnostics in order to present them in the most readable
manner.

Solution
========

This patch is the first in an NFC series that removes `MatchType` from
`FileCheckDiag` and refactors `FileCheckDiag` as the base class of a
class hierarchy.  That class hierarchy is designed to enable the
FileCheck library to focus on communicating FileCheck diagnostic
information clearly and completely without participating in the
specific presentation decisions of `-dump-input`.  `-dump-input` and
any future diagnostic presentation layer that FileCheck might grow are
then freer to evolve more independently in the way they reason about
emitted diagnostics.  Moreover, the hierarchy enables various
`FileCheckDiag` objects not to carry various data not needed for the
diagnostics they describe.

This Patch
==========

The main goal of this particular patch is to introduce the
`FileCheckDiagList` class and to separate mostly mechanical changes
from future patches to make them more readable.  However, by itself,
it does offer some small improvements: it encapsulates the
`adjustPrevDiags` functionality, and it replaces the cryptic
`ProcessMatchResult` function with more readable code.

Why now?
========

FileCheck's input dumps have usability issues, including confusing
verbosity and the need to look in the test file to see directive
patterns.  There has been a recent push in issue #77257 and in PRs
linked from there to address these issues.  This patch series is a
first step in the most recent strategy discussed there.  Actually, it
is an older strategy that was started in
[D126673](https://reviews.llvm.org/D126673) but never completed.  The
current patch series updates D126673 and splits it apart to facilitate
the review.  The actual usability improvements are implemented by
patches that build on this series.
---
 llvm/include/llvm/FileCheck/FileCheck.h    | 26 +++++-
 llvm/lib/FileCheck/FileCheck.cpp           | 99 +++++++++++-----------
 llvm/lib/FileCheck/FileCheckImpl.h         | 18 ++--
 llvm/unittests/FileCheck/FileCheckTest.cpp | 28 +++---
 llvm/utils/FileCheck/FileCheck.cpp         | 61 +++++++------
 5 files changed, 124 insertions(+), 108 deletions(-)

diff --git a/llvm/include/llvm/FileCheck/FileCheck.h b/llvm/include/llvm/FileCheck/FileCheck.h
index b44ed8ed3f839..07684e6fc6cff 100644
--- a/llvm/include/llvm/FileCheck/FileCheck.h
+++ b/llvm/include/llvm/FileCheck/FileCheck.h
@@ -174,6 +174,30 @@ struct FileCheckDiag {
                          StringRef Note = "");
 };
 
+/// A \c FileCheckDiag series emitted by the FileCheck library.
+class FileCheckDiagList {
+private:
+  std::vector<std::unique_ptr<FileCheckDiag>> DiagList;
+
+public:
+  /// Emplace a new \c FileCheckDiag.
+  template <typename... ArgTys> void emplace_back(const ArgTys &...Args) {
+    DiagList.emplace_back(std::make_unique<FileCheckDiag>(Args...));
+  }
+  /// Adjust recent consecutive diagnostics of the same \c CheckLoc to have
+  /// \c MatchTy.
+  void adjustPrevDiags(FileCheckDiag::MatchType MatchTy) {
+    SMLoc CheckLoc = (*DiagList.rbegin())->CheckLoc;
+    for (auto I = DiagList.rbegin(), E = DiagList.rend();
+         I != E && (*I)->CheckLoc == CheckLoc; ++I)
+      (*I)->MatchTy = MatchTy;
+  }
+  /// The \c FileCheckDiag list.
+  const std::vector<std::unique_ptr<FileCheckDiag>> &getList() const {
+    return DiagList;
+  }
+};
+
 class FileCheckPatternContext;
 struct FileCheckString;
 
@@ -211,7 +235,7 @@ class FileCheck {
   ///
   /// \returns false if the input fails to satisfy the checks.
   LLVM_ABI bool checkInput(SourceMgr &SM, StringRef Buffer,
-                           std::vector<FileCheckDiag> *Diags = nullptr);
+                           FileCheckDiagList *Diags = nullptr);
 };
 
 } // namespace llvm
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index d50e5d9cb088b..7fd672b978e36 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1261,7 +1261,7 @@ unsigned Pattern::computeMatchDistance(StringRef Buffer) const {
 void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
                                  SMRange Range,
                                  FileCheckDiag::MatchType MatchTy,
-                                 std::vector<FileCheckDiag> *Diags) const {
+                                 FileCheckDiagList *Diags) const {
   // Print what we know about substitutions.
   if (!Substitutions.empty()) {
     for (const auto &Substitution : Substitutions) {
@@ -1295,7 +1295,7 @@ void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
 
 void Pattern::printVariableDefs(const SourceMgr &SM,
                                 FileCheckDiag::MatchType MatchTy,
-                                std::vector<FileCheckDiag> *Diags) const {
+                                FileCheckDiagList *Diags) const {
   if (VariableDefs.empty() && NumericVariableDefs.empty())
     return;
   // Build list of variable captures.
@@ -1346,29 +1346,18 @@ void Pattern::printVariableDefs(const SourceMgr &SM,
   }
 }
 
-static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy,
-                                  const SourceMgr &SM, SMLoc Loc,
-                                  Check::FileCheckType CheckTy,
-                                  StringRef Buffer, size_t Pos, size_t Len,
-                                  std::vector<FileCheckDiag> *Diags,
-                                  bool AdjustPrevDiags = false) {
-  SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
-  SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
-  SMRange Range(Start, End);
-  if (Diags) {
-    if (AdjustPrevDiags) {
-      SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
-      for (auto I = Diags->rbegin(), E = Diags->rend();
-           I != E && I->CheckLoc == CheckLoc; ++I)
-        I->MatchTy = MatchTy;
-    } else
-      Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
-  }
-  return Range;
+static SMRange buildMatchRange(StringRef Buffer, size_t Pos, size_t Len) {
+  return SMRange(SMLoc::getFromPointer(Buffer.data() + Pos),
+                 SMLoc::getFromPointer(Buffer.data() + Pos + Len));
+}
+
+static SMRange buildSearchRange(StringRef Buffer) {
+  return SMRange(SMLoc::getFromPointer(Buffer.data()),
+                 SMLoc::getFromPointer(Buffer.data() + Buffer.size()));
 }
 
 void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
-                              std::vector<FileCheckDiag> *Diags) const {
+                              FileCheckDiagList *Diags) const {
   // Attempt to find the closest/best fuzzy match.  Usually an error happens
   // because some string in the output didn't exactly match. In these cases, we
   // would like to show the user a best guess at what "should have" matched, to
@@ -1408,9 +1397,10 @@ void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
   // reasonable and not equal to what we showed in the "scanning from here"
   // line.
   if (Best && Best != StringRef::npos && BestQuality < 50) {
-    SMRange MatchRange =
-        ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(),
-                           getCheckTy(), Buffer, Best, 0, Diags);
+    SMRange MatchRange = buildMatchRange(Buffer, Best, 0);
+    if (Diags)
+      Diags->emplace_back(SM, getCheckTy(), getLoc(), FileCheckDiag::MatchFuzzy,
+                          MatchRange);
     SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
                     "possible intended match here");
 
@@ -2033,8 +2023,7 @@ static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
                         StringRef Prefix, SMLoc Loc, const Pattern &Pat,
                         int MatchedCount, StringRef Buffer,
                         Pattern::MatchResult MatchResult,
-                        const FileCheckRequest &Req,
-                        std::vector<FileCheckDiag> *Diags) {
+                        const FileCheckRequest &Req, FileCheckDiagList *Diags) {
   // Suppress some verbosity if there's no error.
   bool HasError = !ExpectedMatch || MatchResult.TheError;
   bool PrintDiag = true;
@@ -2053,10 +2042,10 @@ static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
   FileCheckDiag::MatchType MatchTy = ExpectedMatch
                                          ? FileCheckDiag::MatchFoundAndExpected
                                          : FileCheckDiag::MatchFoundButExcluded;
-  SMRange MatchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
-                                          Buffer, MatchResult.TheMatch->Pos,
-                                          MatchResult.TheMatch->Len, Diags);
+  SMRange MatchRange = buildMatchRange(Buffer, MatchResult.TheMatch->Pos,
+                                       MatchResult.TheMatch->Len);
   if (Diags) {
+    Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, MatchRange);
     Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, Diags);
     Pat.printVariableDefs(SM, MatchTy, Diags);
   }
@@ -2102,8 +2091,7 @@ static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
 static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
                           StringRef Prefix, SMLoc Loc, const Pattern &Pat,
                           int MatchedCount, StringRef Buffer, Error MatchError,
-                          bool VerboseVerbose,
-                          std::vector<FileCheckDiag> *Diags) {
+                          bool VerboseVerbose, FileCheckDiagList *Diags) {
   // Print any pattern errors, and record them to be added to Diags later.
   bool HasError = ExpectedMatch;
   bool HasPatternError = false;
@@ -2141,9 +2129,9 @@ static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
   // errors.  The reason is that we need to attach pattern errors as notes
   // somewhere in the input, and the input search range from the "not found"
   // diagnostic is all we have to anchor them.
-  SMRange SearchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
-                                           Buffer, 0, Buffer.size(), Diags);
+  SMRange SearchRange = buildSearchRange(Buffer);
   if (Diags) {
+    Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, SearchRange);
     SMRange NoteRange = SMRange(SearchRange.Start, SearchRange.Start);
     for (StringRef ErrorMsg : ErrorMsgs)
       Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, NoteRange,
@@ -2187,7 +2175,7 @@ static Error reportMatchResult(bool ExpectedMatch, const SourceMgr &SM,
                                int MatchedCount, StringRef Buffer,
                                Pattern::MatchResult MatchResult,
                                const FileCheckRequest &Req,
-                               std::vector<FileCheckDiag> *Diags) {
+                               FileCheckDiagList *Diags) {
   if (MatchResult.TheMatch)
     return printMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
                       std::move(MatchResult), Req, Diags);
@@ -2222,7 +2210,7 @@ static unsigned CountNumNewlinesBetween(StringRef Range,
 size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
                               bool IsLabelScanMode, size_t &MatchLen,
                               FileCheckRequest &Req,
-                              std::vector<FileCheckDiag> *Diags) const {
+                              FileCheckDiagList *Diags) const {
   size_t LastPos = 0;
   std::vector<const DagNotPrefixInfo *> NotStrings;
 
@@ -2276,18 +2264,30 @@ size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
     // If this check is a "CHECK-NEXT", verify that the previous match was on
     // the previous line (i.e. that there is one newline between them).
     if (CheckNext(SM, SkippedRegion)) {
-      ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
-                         Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
-                         Diags, Req.Verbose);
+      if (Diags) {
+        if (Req.Verbose) {
+          Diags->adjustPrevDiags(FileCheckDiag::MatchFoundButWrongLine);
+        } else {
+          Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
+                              FileCheckDiag::MatchFoundButWrongLine,
+                              buildMatchRange(MatchBuffer, MatchPos, MatchLen));
+        }
+      }
       return StringRef::npos;
     }
 
     // If this check is a "CHECK-SAME", verify that the previous match was on
     // the same line (i.e. that there is no newline between them).
     if (CheckSame(SM, SkippedRegion)) {
-      ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
-                         Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
-                         Diags, Req.Verbose);
+      if (Diags) {
+        if (Req.Verbose) {
+          Diags->adjustPrevDiags(FileCheckDiag::MatchFoundButWrongLine);
+        } else {
+          Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
+                              FileCheckDiag::MatchFoundButWrongLine,
+                              buildMatchRange(MatchBuffer, MatchPos, MatchLen));
+        }
+      }
       return StringRef::npos;
     }
 
@@ -2364,7 +2364,7 @@ bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
 bool FileCheckString::CheckNot(
     const SourceMgr &SM, StringRef Buffer,
     const std::vector<const DagNotPrefixInfo *> &NotStrings,
-    const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const {
+    const FileCheckRequest &Req, FileCheckDiagList *Diags) const {
   bool DirectiveFail = false;
   for (auto NotInfo : NotStrings) {
     assert((NotInfo->DagNotPat.getCheckTy() == Check::CheckNot) &&
@@ -2386,7 +2386,7 @@ size_t
 FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
                           std::vector<const DagNotPrefixInfo *> &NotStrings,
                           const FileCheckRequest &Req,
-                          std::vector<FileCheckDiag> *Diags) const {
+                          FileCheckDiagList *Diags) const {
   if (DagNotStrings.empty())
     return 0;
 
@@ -2475,18 +2475,15 @@ FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
         // Due to their verbosity, we don't print verbose diagnostics here if
         // we're gathering them for a different rendering, but we always print
         // other diagnostics.
-        if (!Diags) {
+        if (Diags) {
+          Diags->adjustPrevDiags(FileCheckDiag::MatchFoundButDiscarded);
+        } else {
           SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
           SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
           SMRange OldRange(OldStart, OldEnd);
           SM.PrintMessage(OldStart, SourceMgr::DK_Note,
                           "match discarded, overlaps earlier DAG match here",
                           {OldRange});
-        } else {
-          SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
-          for (auto I = Diags->rbegin(), E = Diags->rend();
-               I != E && I->CheckLoc == CheckLoc; ++I)
-            I->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
         }
       }
       MatchPos = MI->End;
@@ -2725,7 +2722,7 @@ void FileCheckPatternContext::clearLocalVars() {
 }
 
 bool FileCheck::checkInput(SourceMgr &SM, StringRef Buffer,
-                           std::vector<FileCheckDiag> *Diags) {
+                           FileCheckDiagList *Diags) {
   bool ChecksFailed = false;
 
   unsigned i = 0, j = 0, e = CheckStrings.size();
diff --git a/llvm/lib/FileCheck/FileCheckImpl.h b/llvm/lib/FileCheck/FileCheckImpl.h
index 5851cfc4b5d5c..547c93f2acca5 100644
--- a/llvm/lib/FileCheck/FileCheckImpl.h
+++ b/llvm/lib/FileCheck/FileCheckImpl.h
@@ -734,16 +734,16 @@ class Pattern {
   /// Prints the value of successful substitutions.
   void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
                           SMRange MatchRange, FileCheckDiag::MatchType MatchTy,
-                          std::vector<FileCheckDiag> *Diags) const;
+                          FileCheckDiagList *Diags) const;
   void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
-                       std::vector<FileCheckDiag> *Diags) const;
+                       FileCheckDiagList *Diags) const;
 
   bool hasVariable() const {
     return !(Substitutions.empty() && VariableDefs.empty());
   }
-  LLVM_ABI_FOR_TEST void
-  printVariableDefs(const SourceMgr &SM, FileCheckDiag::MatchType MatchTy,
-                    std::vector<FileCheckDiag> *Diags) const;
+  LLVM_ABI_FOR_TEST void printVariableDefs(const SourceMgr &SM,
+                                           FileCheckDiag::MatchType MatchTy,
+                                           FileCheckDiagList *Diags) const;
 
   Check::FileCheckType getCheckTy() const { return CheckTy; }
 
@@ -870,7 +870,7 @@ struct FileCheckString {
   /// Matches check string and its "not strings" and/or "dag strings".
   size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
                size_t &MatchLen, FileCheckRequest &Req,
-               std::vector<FileCheckDiag> *Diags) const;
+               FileCheckDiagList *Diags) const;
 
   /// Verifies that there is a single line in the given \p Buffer. Errors are
   /// reported against \p SM.
@@ -883,13 +883,11 @@ struct FileCheckString {
   /// \p Diags according to the verbosity level set in \p Req.
   bool CheckNot(const SourceMgr &SM, StringRef Buffer,
                 const std::vector<const DagNotPrefixInfo *> &NotStrings,
-                const FileCheckRequest &Req,
-                std::vector<FileCheckDiag> *Diags) const;
+                const FileCheckRequest &Req, FileCheckDiagList *Diags) const;
   /// Matches "dag strings" and their mixed "not strings".
   size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
                   std::vector<const DagNotPrefixInfo *> &NotStrings,
-                  const FileCheckRequest &Req,
-                  std::vector<FileCheckDiag> *Diags) const;
+                  const FileCheckRequest &Req, FileCheckDiagList *Diags) const;
 };
 
 } // namespace llvm
diff --git a/llvm/unittests/FileCheck/FileCheckTest.cpp b/llvm/unittests/FileCheck/FileCheckTest.cpp
index 2c4130330e970..cff958a114c63 100644
--- a/llvm/unittests/FileCheck/FileCheckTest.cpp
+++ b/llvm/unittests/FileCheck/FileCheckTest.cpp
@@ -915,7 +915,7 @@ class PatternTester {
   }
 
   void printVariableDefs(FileCheckDiag::MatchType MatchTy,
-                         std::vector<FileCheckDiag> &Diags) {
+                         FileCheckDiagList &Diags) {
     P.printVariableDefs(SM, MatchTy, &Diags);
   }
 };
@@ -1641,20 +1641,20 @@ TEST_F(FileCheckTest, CapturedVarDiags) {
   PatternTester Tester;
   ASSERT_FALSE(Tester.parsePattern("[[STRVAR:[a-z]+]] [[#NUMVAR:@LINE]]"));
   EXPECT_THAT_EXPECTED(Tester.match("foobar 2"), Succeeded());
-  std::vector<FileCheckDiag> Diags;
+  FileCheckDiagList Diags;
   Tester.printVariableDefs(FileCheckDiag::MatchFoundAndExpected, Diags);
-  EXPECT_EQ(Diags.size(), 2ul);
-  for (const FileCheckDiag &Diag : Diags) {
-    EXPECT_EQ(Diag.CheckTy, Check::CheckPlain);
-    EXPECT_EQ(Diag.MatchTy, FileCheckDiag::MatchFoundAndExpected);
-    EXPECT_EQ(Diag.InputStartLine, 1u);
-    EXPECT_EQ(Diag.InputEndLine, 1u);
+  EXPECT_EQ(Diags.getList().size(), 2ul);
+  for (const std::unique_ptr<FileCheckDiag> &Diag : Diags.getList()) {
+    EXPECT_EQ(Diag->CheckTy, Check::CheckPlain);
+    EXPECT_EQ(Diag->MatchTy, FileCheckDiag::MatchFoundAndExpected);
+    EXPECT_EQ(Diag->InputStartLine, 1u);
+    EXPECT_EQ(Diag->InputEndLine, 1u);
   }
-  EXPECT_EQ(Diags[0].InputStartCol, 1u);
-  EXPECT_EQ(Diags[0].InputEndCol, 7u);
-  EXPECT_EQ(Diags[1].InputStartCol, 8u);
-  EXPECT_EQ(Diags[1].InputEndCol, 9u);
-  EXPECT_EQ(Diags[0].Note, "captured var \"STRVAR\"");
-  EXPECT_EQ(Diags[1].Note, "captured var \"NUMVAR\"");
+  EXPECT_EQ(Diags.getList()[0]->InputStartCol, 1u);
+  EXPECT_EQ(Diags.getList()[0]->InputEndCol, 7u);
+  EXPECT_EQ(Diags.getList()[1]->InputStartCol, 8u);
+  EXPECT_EQ(Diags.getList()[1]->InputEndCol, 9u);
+  EXPECT_EQ(Diags.getList()[0]->Note, "captured var \"STRVAR\"");
+  EXPECT_EQ(Diags.getList()[1]->Note, "captured var \"NUMVAR\"");
 }
 } // namespace
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 12fdbfd45279d..6cf762bda68d0 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -377,9 +377,9 @@ static std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
 }
 
 static void
-BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
+buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
                       const std::pair<unsigned, unsigned> &ImpPatBufferIDRange,
-                      const std::vector<FileCheckDiag> &Diags,
+                      const std::vector<std::unique_ptr<FileCheckDiag>> &Diags,
                       std::vector<InputAnnotation> &Annotations,
                       unsigned &LabelWidth) {
   struct CompareSMLoc {
@@ -389,25 +389,23 @@ BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
   };
   // How many diagnostics does each pattern have?
   std::map<SMLoc, unsigned, CompareSMLoc> DiagCountPerPattern;
-  for (const FileCheckDiag &Diag : Diags)
-    ++DiagCountPerPattern[Diag.CheckLoc];
+  for (const std::unique_ptr<FileCheckDiag> &Diag : Diags)
+    ++DiagCountPerPattern[Diag->CheckLoc];
   // How many diagnostics have we seen so far per pattern?
   std::map<SMLoc, unsigned, CompareSMLoc> DiagIndexPerPattern;
   // How many total diagnostics have we seen so far?
   unsigned DiagIndex = 0;
   // What's the widest label?
   LabelWidth = 0;
-  for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd;
-       ++DiagItr) {
+  for (const std::unique_ptr<FileCheckDiag> &Diag : Diags) {
     InputAnnotation A;
     A.DiagIndex = DiagIndex++;
 
     // Build label, which uniquely identifies this check result.
-    unsigned CheckBufferID = SM.FindBufferContainingLoc(DiagItr->CheckLoc);
-    auto CheckLineAndCol =
-        SM.getLineAndColumn(DiagItr->CheckLoc, CheckBufferID);
+    unsigned CheckBufferID = SM.FindBufferContainingLoc(Diag->CheckLoc);
+    auto CheckLineAndCol = SM.getLineAndColumn(Diag->CheckLoc, CheckBufferID);
     llvm::raw_string_ostream Label(A.Label);
-    Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":";
+    Label << GetCheckTypeAbbreviation(Diag->CheckTy) << ":";
     if (CheckBufferID == CheckFileBufferID)
       Label << CheckLineAndCol.first;
     else if (ImpPatBufferIDRange.first <= CheckBufferID &&
@@ -416,53 +414,52 @@ BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
     else
       llvm_unreachable("expected diagnostic's check location to be either in "
                        "the check file or for an implicit pattern");
-    if (DiagCountPerPattern[DiagItr->CheckLoc] > 1)
-      Label << "'" << DiagIndexPerPattern[DiagItr->CheckLoc]++;
+    if (DiagCountPerPattern[Diag->CheckLoc] > 1)
+      Label << "'" << DiagIndexPerPattern[Diag->CheckLoc]++;
     LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
 
-    A.Marker = GetMarker(DiagItr->MatchTy);
-    if (!DiagItr->Note.empty()) {
-      A.Marker.Note = DiagItr->Note;
+    A.Marker = GetMarker(Diag->MatchTy);
+    if (!Diag->Note.empty()) {
+      A.Marker.Note = Diag->Note;
       // It's less confusing if notes that don't actually have ranges don't have
       // markers.  For example, a marker for 'with "VAR" equal to "5"' would
       // seem to indicate where "VAR" matches, but the location we actually have
       // for the marker simply points to the start of the match/search range for
       // the full pattern of which the substitution is potentially just one
       // component.
-      if (DiagItr->InputStartLine == DiagItr->InputEndLine &&
-          DiagItr->InputStartCol == DiagItr->InputEndCol)
+      if (Diag->InputStartLine == Diag->InputEndLine &&
+          Diag->InputStartCol == Diag->InputEndCol)
         A.Marker.Lead = ' ';
     }
-    if (DiagItr->MatchTy == FileCheckDiag::MatchFoundErrorNote) {
-      assert(!DiagItr->Note.empty() &&
+    if (Diag->MatchTy == FileCheckDiag::MatchFoundErrorNote) {
+      assert(!Diag->Note.empty() &&
              "expected custom note for MatchFoundErrorNote");
       A.Marker.Note = "error: " + A.Marker.Note;
     }
     A.FoundAndExpectedMatch =
-        DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected;
+        Diag->MatchTy == FileCheckDiag::MatchFoundAndExpected;
 
     // Compute the mark location, and break annotation into multiple
     // annotations if it spans multiple lines.
     A.IsFirstLine = true;
-    A.InputLine = DiagItr->InputStartLine;
-    A.InputStartCol = DiagItr->InputStartCol;
-    if (DiagItr->InputStartLine == DiagItr->InputEndLine) {
+    A.InputLine = Diag->InputStartLine;
+    A.InputStartCol = Diag->InputStartCol;
+    if (Diag->InputStartLine == Diag->InputEndLine) {
       // Sometimes ranges are empty in order to indicate a specific point, but
       // that would mean nothing would be marked, so adjust the range to
       // include the following character.
-      A.InputEndCol =
-          std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol);
+      A.InputEndCol = std::max(Diag->InputStartCol + 1, Diag->InputEndCol);
       Annotations.push_back(A);
     } else {
-      assert(DiagItr->InputStartLine < DiagItr->InputEndLine &&
+      assert(Diag->InputStartLine < Diag->InputEndLine &&
              "expected input range not to be inverted");
       A.InputEndCol = UINT_MAX;
       Annotations.push_back(A);
-      for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine;
+      for (unsigned L = Diag->InputStartLine + 1, E = Diag->InputEndLine;
            L <= E; ++L) {
         // If a range ends before the first column on a line, then it has no
         // characters on that line, so there's nothing to render.
-        if (DiagItr->InputEndCol == 1 && L == E)
+        if (Diag->InputEndCol == 1 && L == E)
           break;
         InputAnnotation B;
         B.DiagIndex = A.DiagIndex;
@@ -476,7 +473,7 @@ BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
         if (L != E)
           B.InputEndCol = UINT_MAX;
         else
-          B.InputEndCol = DiagItr->InputEndCol;
+          B.InputEndCol = Diag->InputEndCol;
         B.FoundAndExpectedMatch = A.FoundAndExpectedMatch;
         Annotations.push_back(B);
       }
@@ -853,7 +850,7 @@ int main(int argc, char **argv) {
                             InputFileText, InputFile.getBufferIdentifier()),
                         SMLoc());
 
-  std::vector<FileCheckDiag> Diags;
+  FileCheckDiagList Diags;
   int ExitCode = FC.checkInput(SM, InputFileText,
                                DumpInput == DumpInputNever ? nullptr : &Diags)
                      ? EXIT_SUCCESS
@@ -868,8 +865,8 @@ int main(int argc, char **argv) {
            << "\n";
     std::vector<InputAnnotation> Annotations;
     unsigned LabelWidth;
-    BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags,
-                          Annotations, LabelWidth);
+    buildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange,
+                          Diags.getList(), Annotations, LabelWidth);
     DumpAnnotatedInput(errs(), Req, DumpInputFilter, DumpInputContext,
                        InputFileText, Annotations, LabelWidth);
   }

>From 1f468c7903b13911e75c8704a129e8b6e86aa18b Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Sun, 3 May 2026 17:58:37 -0400
Subject: [PATCH 2/6] [FileCheck][NFC] Introduce MatchResultDiag and
 MatchNoteDiag

This patch depends on PR #???? and continues its effort to decouple
the `-dump-input` presentation layer (in
`llvm/utils/FileCheck/FileCheck.cpp`) and the FileCheck library's
diagnostic emission (in `llvm/lib/FileCheck/FileCheck.cpp`).

Similar to compiler errors/warnings/remarks vs. notes, the
`FileCheckDiag` series emitted by the FileCheck library contains match
results, each of which might be followed by a series of associated
notes before the next match result.  Without this patch series, that
association is not formally modeled by `FileCheckDiag` or clearly
documented, and `-dump-input` is not able to easily reason about it.

This patch improves the situation by introducing two `FileCheckDiag`
derived classes: `MatchResultDiag` and `MatchNoteDiag`.  It extends
`FileCheckDiagList` to directly associate each `MatchNoteDiag` with
its `MatchResultDiag`.  Thus:
- `FileCheckDiagList::adjustPrevDiags` no longer has to determine that
  association based on directive location info.
- Match result instances of `FileCheckDiag` no longer need to store a
  redundant custom note field, which only makes sense for notes.
- Note instances of `FileCheckDiag` no longer need to store the
  directive type and location, which are already stored by their match
  result instances.
- In both the FileCheck library and the `-dump-input` presentation
  layer, it is now clearer when a `FileCheckDiag` is a match result
  vs. a note.
---
 llvm/include/llvm/FileCheck/FileCheck.h    | 153 ++++++++++++++++-----
 llvm/lib/FileCheck/FileCheck.cpp           |  48 +++----
 llvm/unittests/FileCheck/FileCheckTest.cpp |  32 +++--
 llvm/utils/FileCheck/FileCheck.cpp         | 101 ++++++++------
 4 files changed, 221 insertions(+), 113 deletions(-)

diff --git a/llvm/include/llvm/FileCheck/FileCheck.h b/llvm/include/llvm/FileCheck/FileCheck.h
index 07684e6fc6cff..b171d1048cddb 100644
--- a/llvm/include/llvm/FileCheck/FileCheck.h
+++ b/llvm/include/llvm/FileCheck/FileCheck.h
@@ -14,6 +14,7 @@
 #define LLVM_FILECHECK_FILECHECK_H
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Casting.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Support/SMLoc.h"
@@ -110,24 +111,29 @@ class FileCheckType {
 };
 } // namespace Check
 
-/// Summary of a FileCheck diagnostic.
-struct FileCheckDiag {
-  /// What is the FileCheck directive for this diagnostic?
-  Check::FileCheckType CheckTy;
-  /// Where is the FileCheck directive for this diagnostic?
-  SMLoc CheckLoc;
+class MatchResultDiag;
+
+/// Abstract base class for recording a FileCheck diagnostic for a pattern
+/// (e.g., \c CHECK-NEXT directive or \c --implicit-check-not).
+///
+/// \c FileCheckDiag has two direct derived classes:
+/// - \c MatchResultDiag records a match result for a pattern.  There might be
+///   more than one for a single pattern.  For example, for \c CHECK-DAG there
+///   might be several discarded matches before either a good match or a failure
+///   to match.
+/// - \c MatchNoteDiag provides an additional note about the most recent
+///   \c MatchResultDiag emitted by a FileCheck invocation.  For example, there
+///   might be a fuzzy match after a failure to match.
+class FileCheckDiag {
+public:
+  enum FileCheckDiagKind { FCDK_MatchResultDiag, FCDK_MatchNoteDiag };
+
   /// What type of match result does this diagnostic describe?
   ///
   /// A directive's supplied pattern is said to be either expected or excluded
   /// depending on whether the pattern must have or must not have a match in
   /// order for the directive to succeed.  For example, a CHECK directive's
   /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
-  ///
-  /// There might be more than one match result for a single pattern.  For
-  /// example, there might be several discarded matches
-  /// (MatchFoundButDiscarded) before either a good match
-  /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
-  /// and there might be a fuzzy match (MatchFuzzy) after the latter.
   enum MatchType {
     /// Indicates a good match for an expected pattern.
     MatchFoundAndExpected,
@@ -158,39 +164,122 @@ struct FileCheckDiag {
     /// Indicates a fuzzy match that serves as a suggestion for the next
     /// intended match for an expected pattern with too few or no good matches.
     MatchFuzzy,
-  } MatchTy;
+  };
+
+private:
+  const FileCheckDiagKind Kind;
+  MatchType MatchTy;
+  SMRange InputRange;
+
+public:
+  FileCheckDiag(FileCheckDiagKind Kind, MatchType MatchTy, SMRange InputRange)
+      : Kind(Kind), MatchTy(MatchTy), InputRange(InputRange) {}
+  /// Destructor is purely virtual to ensure this remains an abstract class.
+  virtual ~FileCheckDiag() = 0;
+  /// Of what derived class is this an instance?
+  FileCheckDiagKind getKind() const { return Kind; }
+  /// If this is a \c MatchResultDiag, return itself.  If this is a
+  /// \c MatchNoteDiag, return its associated \c MatchResultDiag.
+  virtual const MatchResultDiag &getMatchResultDiag() const = 0;
+  /// Adjust the match type.
+  void adjustMatchType(MatchType MatchTy) { this->MatchTy = MatchTy; }
+  /// Get the match type.
+  MatchType getMatchType() const { return MatchTy; }
   /// The search range if MatchTy starts with MatchNone, or the match range
   /// otherwise.
-  unsigned InputStartLine;
-  unsigned InputStartCol;
-  unsigned InputEndLine;
-  unsigned InputEndCol;
-  /// A note to replace the one normally indicated by MatchTy, or the empty
-  /// string if none.
-  std::string Note;
-  LLVM_ABI FileCheckDiag(const SourceMgr &SM,
-                         const Check::FileCheckType &CheckTy, SMLoc CheckLoc,
-                         MatchType MatchTy, SMRange InputRange,
-                         StringRef Note = "");
+  SMRange getInputRange() const { return InputRange; }
+};
+
+/// Class for recording a FileCheck diagnostic that reports a match result for a
+/// pattern.
+class MatchResultDiag : public FileCheckDiag {
+private:
+  Check::FileCheckType CheckTy;
+  SMLoc CheckLoc;
+
+public:
+  MatchResultDiag(const Check::FileCheckType &CheckTy, SMLoc CheckLoc,
+                  MatchType MatchTy, SMRange InputRange)
+      : FileCheckDiag(FCDK_MatchResultDiag, MatchTy, InputRange),
+        CheckTy(CheckTy), CheckLoc(CheckLoc) {}
+  /// Is \p FCD an instance of \c MatchResultDiag?
+  static bool classof(const FileCheckDiag *FCD) {
+    return FCD->getKind() == FCDK_MatchResultDiag;
+  }
+  /// Get itself.
+  virtual const MatchResultDiag &getMatchResultDiag() const override {
+    return *this;
+  }
+  /// What is the type of pattern for this match result?
+  Check::FileCheckType getCheckTy() const { return CheckTy; }
+  /// Where is the pattern for this match result?
+  SMLoc getCheckLoc() const { return CheckLoc; }
+};
+
+/// Class for recording a FileCheck diagnostic that provides an additional note
+/// (possibly an additional error) about the most recent \c MatchResultDiag.
+class MatchNoteDiag : public FileCheckDiag {
+private:
+  MatchResultDiag *MRD;
+  std::optional<std::string> CustomNote;
+
+public:
+  MatchNoteDiag(MatchType MatchTy, SMRange InputRange,
+                std::optional<StringRef> CustomNote = std::nullopt)
+      : FileCheckDiag(FCDK_MatchNoteDiag, MatchTy, InputRange), MRD(nullptr),
+        CustomNote(CustomNote) {}
+  /// Is \p FCD an instance of \c MatchNoteDiag?
+  static bool classof(const FileCheckDiag *FCD) {
+    return FCD->getKind() == FCDK_MatchNoteDiag;
+  }
+  /// Get the note's associated \c MatchResultDiag.
+  virtual const MatchResultDiag &getMatchResultDiag() const override {
+    return *MRD;
+  }
+  /// Set the note's associated \c MatchResultDiag.
+  void setMatchResultDiag(MatchResultDiag *MRDNew) {
+    assert(!MRD && "expected setMatchResultDiag to be called only once");
+    MRD = MRDNew;
+  }
+  /// A note to replace the one normally indicated by the match type, or the
+  /// empty string if none.
+  const std::optional<std::string> &getCustomNote() const { return CustomNote; }
 };
 
 /// A \c FileCheckDiag series emitted by the FileCheck library.
 class FileCheckDiagList {
 private:
+  MatchResultDiag *CurMatchResultDiag = nullptr;
   std::vector<std::unique_ptr<FileCheckDiag>> DiagList;
 
 public:
-  /// Emplace a new \c FileCheckDiag.
-  template <typename... ArgTys> void emplace_back(const ArgTys &...Args) {
-    DiagList.emplace_back(std::make_unique<FileCheckDiag>(Args...));
+  /// Emplace a new \c FileCheckDiag of type \c DiagTy.  If it's a
+  /// \c MatchNoteDiag, associate it with its \c MatchResultDiag.
+  ///
+  /// \c FileCheckTest.cpp calls \c Pattern::printVariableDefs directly, so it
+  /// can add a \c MatchNoteDiag without a previous \c MatchResultDiag.
+  /// Otherwise, there should always be a previous \c MatchResultDiag.
+  template <typename DiagTy, typename... ArgTys>
+  void emplace(const ArgTys &...Args) {
+    DiagList.emplace_back(std::make_unique<DiagTy>(Args...));
+    FileCheckDiag *Diag = DiagList.back().get();
+    if (MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(Diag)) {
+      CurMatchResultDiag = MRD;
+      return;
+    }
+    MatchNoteDiag *Note = cast<MatchNoteDiag>(Diag);
+    if (!CurMatchResultDiag)
+      return;
+    Note->setMatchResultDiag(CurMatchResultDiag);
   }
-  /// Adjust recent consecutive diagnostics of the same \c CheckLoc to have
-  /// \c MatchTy.
+  /// Adjust the most recent \c MatchResultDiag, which must exist, and every
+  /// \c MatchResultNote after it to have the match type \c MatchTy.
   void adjustPrevDiags(FileCheckDiag::MatchType MatchTy) {
-    SMLoc CheckLoc = (*DiagList.rbegin())->CheckLoc;
+    assert(CurMatchResultDiag && "expected previous MatchResultDiag");
     for (auto I = DiagList.rbegin(), E = DiagList.rend();
-         I != E && (*I)->CheckLoc == CheckLoc; ++I)
-      (*I)->MatchTy = MatchTy;
+         I != E && &**I != CurMatchResultDiag; ++I)
+      (*I)->adjustMatchType(MatchTy);
+    CurMatchResultDiag->adjustMatchType(MatchTy);
   }
   /// The \c FileCheckDiag list.
   const std::vector<std::unique_ptr<FileCheckDiag>> &getList() const {
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 7fd672b978e36..b493eb67fa173 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1285,8 +1285,8 @@ void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
       // Indicating a non-zero-length range might instead seem to imply that the
       // substitution matches or was captured from exactly that range.
       if (Diags)
-        Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy,
-                            SMRange(Range.Start, Range.Start), OS.str());
+        Diags->emplace<MatchNoteDiag>(
+            MatchTy, SMRange(Range.Start, Range.Start), OS.str());
       else
         SM.PrintMessage(Range.Start, SourceMgr::DK_Note, OS.str());
     }
@@ -1340,7 +1340,7 @@ void Pattern::printVariableDefs(const SourceMgr &SM,
     raw_svector_ostream OS(Msg);
     OS << "captured var \"" << VC.Name << "\"";
     if (Diags)
-      Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy, VC.Range, OS.str());
+      Diags->emplace<MatchNoteDiag>(MatchTy, VC.Range, OS.str());
     else
       SM.PrintMessage(VC.Range.Start, SourceMgr::DK_Note, OS.str(), VC.Range);
   }
@@ -1399,8 +1399,7 @@ void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
   if (Best && Best != StringRef::npos && BestQuality < 50) {
     SMRange MatchRange = buildMatchRange(Buffer, Best, 0);
     if (Diags)
-      Diags->emplace_back(SM, getCheckTy(), getLoc(), FileCheckDiag::MatchFuzzy,
-                          MatchRange);
+      Diags->emplace<MatchNoteDiag>(FileCheckDiag::MatchFuzzy, MatchRange);
     SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
                     "possible intended match here");
 
@@ -1507,18 +1506,7 @@ StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB,
   return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
 }
 
-FileCheckDiag::FileCheckDiag(const SourceMgr &SM,
-                             const Check::FileCheckType &CheckTy,
-                             SMLoc CheckLoc, MatchType MatchTy,
-                             SMRange InputRange, StringRef Note)
-    : CheckTy(CheckTy), CheckLoc(CheckLoc), MatchTy(MatchTy), Note(Note) {
-  auto Start = SM.getLineAndColumn(InputRange.Start);
-  auto End = SM.getLineAndColumn(InputRange.End);
-  InputStartLine = Start.first;
-  InputStartCol = Start.second;
-  InputEndLine = End.first;
-  InputEndCol = End.second;
-}
+FileCheckDiag::~FileCheckDiag() {}
 
 static bool IsPartOfWord(char c) {
   return (isAlnum(c) || c == '-' || c == '_');
@@ -2045,7 +2033,7 @@ static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
   SMRange MatchRange = buildMatchRange(Buffer, MatchResult.TheMatch->Pos,
                                        MatchResult.TheMatch->Len);
   if (Diags) {
-    Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, MatchRange);
+    Diags->emplace<MatchResultDiag>(Pat.getCheckTy(), Loc, MatchTy, MatchRange);
     Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, Diags);
     Pat.printVariableDefs(SM, MatchTy, Diags);
   }
@@ -2077,9 +2065,9 @@ static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
                   [&](const ErrorDiagnostic &E) {
                     E.log(errs());
                     if (Diags) {
-                      Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
-                                          FileCheckDiag::MatchFoundErrorNote,
-                                          E.getRange(), E.getMessage().str());
+                      Diags->emplace<MatchNoteDiag>(
+                          FileCheckDiag::MatchFoundErrorNote, E.getRange(),
+                          E.getMessage().str());
                     }
                   });
   return ErrorReported::reportedOrSuccess(HasError);
@@ -2131,11 +2119,11 @@ static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
   // diagnostic is all we have to anchor them.
   SMRange SearchRange = buildSearchRange(Buffer);
   if (Diags) {
-    Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, SearchRange);
+    Diags->emplace<MatchResultDiag>(Pat.getCheckTy(), Loc, MatchTy,
+                                    SearchRange);
     SMRange NoteRange = SMRange(SearchRange.Start, SearchRange.Start);
     for (StringRef ErrorMsg : ErrorMsgs)
-      Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, NoteRange,
-                          ErrorMsg);
+      Diags->emplace<MatchNoteDiag>(MatchTy, NoteRange, ErrorMsg);
     Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, Diags);
   }
   if (!PrintDiag) {
@@ -2268,9 +2256,9 @@ size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
         if (Req.Verbose) {
           Diags->adjustPrevDiags(FileCheckDiag::MatchFoundButWrongLine);
         } else {
-          Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
-                              FileCheckDiag::MatchFoundButWrongLine,
-                              buildMatchRange(MatchBuffer, MatchPos, MatchLen));
+          Diags->emplace<MatchResultDiag>(
+              Pat.getCheckTy(), Loc, FileCheckDiag::MatchFoundButWrongLine,
+              buildMatchRange(MatchBuffer, MatchPos, MatchLen));
         }
       }
       return StringRef::npos;
@@ -2283,9 +2271,9 @@ size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
         if (Req.Verbose) {
           Diags->adjustPrevDiags(FileCheckDiag::MatchFoundButWrongLine);
         } else {
-          Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
-                              FileCheckDiag::MatchFoundButWrongLine,
-                              buildMatchRange(MatchBuffer, MatchPos, MatchLen));
+          Diags->emplace<MatchResultDiag>(
+              Pat.getCheckTy(), Loc, FileCheckDiag::MatchFoundButWrongLine,
+              buildMatchRange(MatchBuffer, MatchPos, MatchLen));
         }
       }
       return StringRef::npos;
diff --git a/llvm/unittests/FileCheck/FileCheckTest.cpp b/llvm/unittests/FileCheck/FileCheckTest.cpp
index cff958a114c63..9282296b4c540 100644
--- a/llvm/unittests/FileCheck/FileCheckTest.cpp
+++ b/llvm/unittests/FileCheck/FileCheckTest.cpp
@@ -918,8 +918,21 @@ class PatternTester {
                          FileCheckDiagList &Diags) {
     P.printVariableDefs(SM, MatchTy, &Diags);
   }
+
+  SourceMgr &getSourceMgr() { return SM; }
 };
 
+#define EXPECT_SM_RANGE(SM, Range, StartLineExpected, StartColExpected,        \
+                        EndLineExpected, EndColExpected)                       \
+  do {                                                                         \
+    auto StartActual = SM.getLineAndColumn(Range.Start);                       \
+    auto EndActual = SM.getLineAndColumn(Range.End);                           \
+    EXPECT_EQ(StartActual.first, StartLineExpected);                           \
+    EXPECT_EQ(StartActual.second, StartColExpected);                           \
+    EXPECT_EQ(EndActual.first, EndLineExpected);                               \
+    EXPECT_EQ(EndActual.second, EndColExpected);                               \
+  } while (0)
+
 TEST_F(FileCheckTest, ParseNumericSubstitutionBlock) {
   PatternTester Tester;
 
@@ -1639,22 +1652,21 @@ TEST_F(FileCheckTest, FileCheckContext) {
 
 TEST_F(FileCheckTest, CapturedVarDiags) {
   PatternTester Tester;
+  SourceMgr &SM = Tester.getSourceMgr();
   ASSERT_FALSE(Tester.parsePattern("[[STRVAR:[a-z]+]] [[#NUMVAR:@LINE]]"));
   EXPECT_THAT_EXPECTED(Tester.match("foobar 2"), Succeeded());
   FileCheckDiagList Diags;
   Tester.printVariableDefs(FileCheckDiag::MatchFoundAndExpected, Diags);
   EXPECT_EQ(Diags.getList().size(), 2ul);
+  SmallVector<MatchNoteDiag, 2> Notes;
   for (const std::unique_ptr<FileCheckDiag> &Diag : Diags.getList()) {
-    EXPECT_EQ(Diag->CheckTy, Check::CheckPlain);
-    EXPECT_EQ(Diag->MatchTy, FileCheckDiag::MatchFoundAndExpected);
-    EXPECT_EQ(Diag->InputStartLine, 1u);
-    EXPECT_EQ(Diag->InputEndLine, 1u);
+    EXPECT_EQ(Diag->getKind(), FileCheckDiag::FCDK_MatchNoteDiag);
+    EXPECT_EQ(Diag->getMatchType(), FileCheckDiag::MatchFoundAndExpected);
+    Notes.push_back(cast<MatchNoteDiag>(*Diag));
   }
-  EXPECT_EQ(Diags.getList()[0]->InputStartCol, 1u);
-  EXPECT_EQ(Diags.getList()[0]->InputEndCol, 7u);
-  EXPECT_EQ(Diags.getList()[1]->InputStartCol, 8u);
-  EXPECT_EQ(Diags.getList()[1]->InputEndCol, 9u);
-  EXPECT_EQ(Diags.getList()[0]->Note, "captured var \"STRVAR\"");
-  EXPECT_EQ(Diags.getList()[1]->Note, "captured var \"NUMVAR\"");
+  EXPECT_SM_RANGE(SM, Notes[0].getInputRange(), 1u, 1u, 1u, 7u);
+  EXPECT_SM_RANGE(SM, Notes[1].getInputRange(), 1u, 8u, 1u, 9u);
+  EXPECT_EQ(Notes[0].getCustomNote(), "captured var \"STRVAR\"");
+  EXPECT_EQ(Notes[1].getCustomNote(), "captured var \"NUMVAR\"");
 }
 } // namespace
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 6cf762bda68d0..7b29238844e4f 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -390,76 +390,95 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
   // How many diagnostics does each pattern have?
   std::map<SMLoc, unsigned, CompareSMLoc> DiagCountPerPattern;
   for (const std::unique_ptr<FileCheckDiag> &Diag : Diags)
-    ++DiagCountPerPattern[Diag->CheckLoc];
+    ++DiagCountPerPattern[Diag->getMatchResultDiag().getCheckLoc()];
   // How many diagnostics have we seen so far per pattern?
   std::map<SMLoc, unsigned, CompareSMLoc> DiagIndexPerPattern;
   // How many total diagnostics have we seen so far?
   unsigned DiagIndex = 0;
   // What's the widest label?
   LabelWidth = 0;
+  // The label prefix that uniquely identifies the current MatchResultDiag and
+  // its MatchNoteDiag series.
+  std::string CurLabelPrefix;
   for (const std::unique_ptr<FileCheckDiag> &Diag : Diags) {
     InputAnnotation A;
     A.DiagIndex = DiagIndex++;
+    if (MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(Diag.get())) {
+      CurLabelPrefix.clear();
+      llvm::raw_string_ostream LabelStrm(CurLabelPrefix);
+      LabelStrm << GetCheckTypeAbbreviation(MRD->getCheckTy()) << ":";
+      unsigned CheckBufferID = SM.FindBufferContainingLoc(MRD->getCheckLoc());
+      if (CheckBufferID == CheckFileBufferID)
+        LabelStrm
+            << SM.getLineAndColumn(MRD->getCheckLoc(), CheckBufferID).first;
+      else if (ImpPatBufferIDRange.first <= CheckBufferID &&
+               CheckBufferID < ImpPatBufferIDRange.second)
+        LabelStrm << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1);
+      else
+        llvm_unreachable("expected diagnostic's check location to be either in "
+                         "the check file or for an implicit pattern");
+    }
 
-    // Build label, which uniquely identifies this check result.
-    unsigned CheckBufferID = SM.FindBufferContainingLoc(Diag->CheckLoc);
-    auto CheckLineAndCol = SM.getLineAndColumn(Diag->CheckLoc, CheckBufferID);
-    llvm::raw_string_ostream Label(A.Label);
-    Label << GetCheckTypeAbbreviation(Diag->CheckTy) << ":";
-    if (CheckBufferID == CheckFileBufferID)
-      Label << CheckLineAndCol.first;
-    else if (ImpPatBufferIDRange.first <= CheckBufferID &&
-             CheckBufferID < ImpPatBufferIDRange.second)
-      Label << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1);
-    else
-      llvm_unreachable("expected diagnostic's check location to be either in "
-                       "the check file or for an implicit pattern");
-    if (DiagCountPerPattern[Diag->CheckLoc] > 1)
-      Label << "'" << DiagIndexPerPattern[Diag->CheckLoc]++;
+    // Build label that uniquely identifies this FileCheckDiag.
+    llvm::raw_string_ostream LabelStrm(A.Label);
+    LabelStrm << CurLabelPrefix;
+    if (DiagCountPerPattern[Diag->getMatchResultDiag().getCheckLoc()] > 1)
+      LabelStrm
+          << "'"
+          << DiagIndexPerPattern[Diag->getMatchResultDiag().getCheckLoc()]++;
     LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
 
-    A.Marker = GetMarker(Diag->MatchTy);
-    if (!Diag->Note.empty()) {
-      A.Marker.Note = Diag->Note;
-      // It's less confusing if notes that don't actually have ranges don't have
-      // markers.  For example, a marker for 'with "VAR" equal to "5"' would
-      // seem to indicate where "VAR" matches, but the location we actually have
-      // for the marker simply points to the start of the match/search range for
-      // the full pattern of which the substitution is potentially just one
-      // component.
-      if (Diag->InputStartLine == Diag->InputEndLine &&
-          Diag->InputStartCol == Diag->InputEndCol)
-        A.Marker.Lead = ' ';
+    A.Marker = GetMarker(Diag->getMatchType());
+    std::optional<StringRef> CustomNote = std::nullopt;
+    if (const MatchNoteDiag *NoteDiag = dyn_cast<MatchNoteDiag>(&*Diag)) {
+      CustomNote = NoteDiag->getCustomNote();
+      if (CustomNote) {
+        A.Marker.Note = *CustomNote;
+        // It's less confusing if notes that don't actually have ranges don't
+        // have markers.  For example, a marker for 'with "VAR" equal to "5"'
+        // would seem to indicate where "VAR" matches, but the location we
+        // actually have for the marker simply points to the start of the
+        // match/search range for the full pattern of which the substitution is
+        // potentially just one component.
+        SMRange InputRange = Diag->getInputRange();
+        if (InputRange.Start == InputRange.End)
+          A.Marker.Lead = ' ';
+      }
     }
-    if (Diag->MatchTy == FileCheckDiag::MatchFoundErrorNote) {
-      assert(!Diag->Note.empty() &&
-             "expected custom note for MatchFoundErrorNote");
+    if (Diag->getMatchType() == FileCheckDiag::MatchFoundErrorNote) {
+      assert(CustomNote && "expected custom note for MatchFoundErrorNote");
       A.Marker.Note = "error: " + A.Marker.Note;
     }
     A.FoundAndExpectedMatch =
-        Diag->MatchTy == FileCheckDiag::MatchFoundAndExpected;
+        Diag->getMatchType() == FileCheckDiag::MatchFoundAndExpected;
 
     // Compute the mark location, and break annotation into multiple
     // annotations if it spans multiple lines.
+    SMRange InputRange = Diag->getInputRange();
+    auto InputStartLineAndCol = SM.getLineAndColumn(InputRange.Start);
+    auto InputEndLineAndCol = SM.getLineAndColumn(InputRange.End);
+    unsigned InputStartLine = InputStartLineAndCol.first;
+    unsigned InputStartCol = InputStartLineAndCol.second;
+    unsigned InputEndLine = InputEndLineAndCol.first;
+    unsigned InputEndCol = InputEndLineAndCol.second;
     A.IsFirstLine = true;
-    A.InputLine = Diag->InputStartLine;
-    A.InputStartCol = Diag->InputStartCol;
-    if (Diag->InputStartLine == Diag->InputEndLine) {
+    A.InputLine = InputStartLine;
+    A.InputStartCol = InputStartCol;
+    if (InputStartLine == InputEndLine) {
       // Sometimes ranges are empty in order to indicate a specific point, but
       // that would mean nothing would be marked, so adjust the range to
       // include the following character.
-      A.InputEndCol = std::max(Diag->InputStartCol + 1, Diag->InputEndCol);
+      A.InputEndCol = std::max(InputStartCol + 1, InputEndCol);
       Annotations.push_back(A);
     } else {
-      assert(Diag->InputStartLine < Diag->InputEndLine &&
+      assert(InputStartLine < InputEndLine &&
              "expected input range not to be inverted");
       A.InputEndCol = UINT_MAX;
       Annotations.push_back(A);
-      for (unsigned L = Diag->InputStartLine + 1, E = Diag->InputEndLine;
-           L <= E; ++L) {
+      for (unsigned L = InputStartLine + 1, E = InputEndLine; L <= E; ++L) {
         // If a range ends before the first column on a line, then it has no
         // characters on that line, so there's nothing to render.
-        if (Diag->InputEndCol == 1 && L == E)
+        if (InputEndCol == 1 && L == E)
           break;
         InputAnnotation B;
         B.DiagIndex = A.DiagIndex;
@@ -473,7 +492,7 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
         if (L != E)
           B.InputEndCol = UINT_MAX;
         else
-          B.InputEndCol = Diag->InputEndCol;
+          B.InputEndCol = InputEndCol;
         B.FoundAndExpectedMatch = A.FoundAndExpectedMatch;
         Annotations.push_back(B);
       }

>From 1ff98e8507b0303d0a95230a7de43cf4e0d96b7f Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Mon, 4 May 2026 10:17:46 -0400
Subject: [PATCH 3/6] Fix emplace_back.

---
 llvm/include/llvm/FileCheck/FileCheck.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/FileCheck/FileCheck.h b/llvm/include/llvm/FileCheck/FileCheck.h
index 07684e6fc6cff..5dac41406585c 100644
--- a/llvm/include/llvm/FileCheck/FileCheck.h
+++ b/llvm/include/llvm/FileCheck/FileCheck.h
@@ -181,8 +181,9 @@ class FileCheckDiagList {
 
 public:
   /// Emplace a new \c FileCheckDiag.
-  template <typename... ArgTys> void emplace_back(const ArgTys &...Args) {
-    DiagList.emplace_back(std::make_unique<FileCheckDiag>(Args...));
+  template <typename... ArgTys> void emplace_back(ArgTys &&...Args) {
+    DiagList.emplace_back(
+        std::make_unique<FileCheckDiag>(std::forward<ArgTys>(Args)...));
   }
   /// Adjust recent consecutive diagnostics of the same \c CheckLoc to have
   /// \c MatchTy.

>From 919b0f024235ed247df43885b1bce4804835d70d Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Mon, 4 May 2026 12:19:16 -0400
Subject: [PATCH 4/6] Replace getList with begin, end, operator[], etc.

The new `const_iterator` then relieves clients of dealing with the
`std::unique_ptr`.
---
 llvm/include/llvm/FileCheck/FileCheck.h    | 47 ++++++++++++++++--
 llvm/unittests/FileCheck/FileCheckTest.cpp | 24 +++++-----
 llvm/utils/FileCheck/FileCheck.cpp         | 56 +++++++++++-----------
 3 files changed, 82 insertions(+), 45 deletions(-)

diff --git a/llvm/include/llvm/FileCheck/FileCheck.h b/llvm/include/llvm/FileCheck/FileCheck.h
index 5dac41406585c..ff3de0d4aef20 100644
--- a/llvm/include/llvm/FileCheck/FileCheck.h
+++ b/llvm/include/llvm/FileCheck/FileCheck.h
@@ -18,6 +18,7 @@
 #include "llvm/Support/Regex.h"
 #include "llvm/Support/SMLoc.h"
 #include <bitset>
+#include <iterator>
 #include <memory>
 #include <string>
 #include <vector>
@@ -177,7 +178,8 @@ struct FileCheckDiag {
 /// A \c FileCheckDiag series emitted by the FileCheck library.
 class FileCheckDiagList {
 private:
-  std::vector<std::unique_ptr<FileCheckDiag>> DiagList;
+  using vector_type = std::vector<std::unique_ptr<FileCheckDiag>>;
+  vector_type DiagList;
 
 public:
   /// Emplace a new \c FileCheckDiag.
@@ -193,10 +195,45 @@ class FileCheckDiagList {
          I != E && (*I)->CheckLoc == CheckLoc; ++I)
       (*I)->MatchTy = MatchTy;
   }
-  /// The \c FileCheckDiag list.
-  const std::vector<std::unique_ptr<FileCheckDiag>> &getList() const {
-    return DiagList;
-  }
+  class const_iterator {
+    friend FileCheckDiagList;
+
+  public:
+    using difference_type = std::ptrdiff_t;
+    using value_type = FileCheckDiag;
+    using pointer = const FileCheckDiag *;
+    using reference = const FileCheckDiag &;
+    using iterator_category = std::forward_iterator_tag;
+
+  private:
+    vector_type::const_iterator Itr;
+    const_iterator(vector_type::const_iterator Itr) : Itr(Itr) {}
+
+  public:
+    reference operator*() const { return **Itr; }
+    pointer operator->() const { return &operator*(); }
+    const_iterator &operator++() {
+      ++Itr;
+      return *this;
+    }
+    const_iterator operator++(int) {
+      const_iterator Old = *this;
+      ++Itr;
+      return Old;
+    }
+    bool operator==(const const_iterator &Other) const {
+      return Itr == Other.Itr;
+    }
+    bool operator!=(const const_iterator &Other) const {
+      return Itr != Other.Itr;
+    }
+  };
+
+  using size_type = vector_type::size_type;
+  const_iterator begin() const { return const_iterator(DiagList.begin()); }
+  const_iterator end() const { return const_iterator(DiagList.end()); }
+  const FileCheckDiag &operator[](size_type I) const { return *DiagList[I]; }
+  size_type size() const { return DiagList.size(); }
 };
 
 class FileCheckPatternContext;
diff --git a/llvm/unittests/FileCheck/FileCheckTest.cpp b/llvm/unittests/FileCheck/FileCheckTest.cpp
index cff958a114c63..cc6d4f9ce9325 100644
--- a/llvm/unittests/FileCheck/FileCheckTest.cpp
+++ b/llvm/unittests/FileCheck/FileCheckTest.cpp
@@ -1643,18 +1643,18 @@ TEST_F(FileCheckTest, CapturedVarDiags) {
   EXPECT_THAT_EXPECTED(Tester.match("foobar 2"), Succeeded());
   FileCheckDiagList Diags;
   Tester.printVariableDefs(FileCheckDiag::MatchFoundAndExpected, Diags);
-  EXPECT_EQ(Diags.getList().size(), 2ul);
-  for (const std::unique_ptr<FileCheckDiag> &Diag : Diags.getList()) {
-    EXPECT_EQ(Diag->CheckTy, Check::CheckPlain);
-    EXPECT_EQ(Diag->MatchTy, FileCheckDiag::MatchFoundAndExpected);
-    EXPECT_EQ(Diag->InputStartLine, 1u);
-    EXPECT_EQ(Diag->InputEndLine, 1u);
+  EXPECT_EQ(Diags.size(), 2ul);
+  for (const FileCheckDiag &Diag : Diags) {
+    EXPECT_EQ(Diag.CheckTy, Check::CheckPlain);
+    EXPECT_EQ(Diag.MatchTy, FileCheckDiag::MatchFoundAndExpected);
+    EXPECT_EQ(Diag.InputStartLine, 1u);
+    EXPECT_EQ(Diag.InputEndLine, 1u);
   }
-  EXPECT_EQ(Diags.getList()[0]->InputStartCol, 1u);
-  EXPECT_EQ(Diags.getList()[0]->InputEndCol, 7u);
-  EXPECT_EQ(Diags.getList()[1]->InputStartCol, 8u);
-  EXPECT_EQ(Diags.getList()[1]->InputEndCol, 9u);
-  EXPECT_EQ(Diags.getList()[0]->Note, "captured var \"STRVAR\"");
-  EXPECT_EQ(Diags.getList()[1]->Note, "captured var \"NUMVAR\"");
+  EXPECT_EQ(Diags[0].InputStartCol, 1u);
+  EXPECT_EQ(Diags[0].InputEndCol, 7u);
+  EXPECT_EQ(Diags[1].InputStartCol, 8u);
+  EXPECT_EQ(Diags[1].InputEndCol, 9u);
+  EXPECT_EQ(Diags[0].Note, "captured var \"STRVAR\"");
+  EXPECT_EQ(Diags[1].Note, "captured var \"NUMVAR\"");
 }
 } // namespace
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 6cf762bda68d0..45d823dca6808 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -379,7 +379,7 @@ static std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
 static void
 buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
                       const std::pair<unsigned, unsigned> &ImpPatBufferIDRange,
-                      const std::vector<std::unique_ptr<FileCheckDiag>> &Diags,
+                      const FileCheckDiagList &Diags,
                       std::vector<InputAnnotation> &Annotations,
                       unsigned &LabelWidth) {
   struct CompareSMLoc {
@@ -389,23 +389,23 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
   };
   // How many diagnostics does each pattern have?
   std::map<SMLoc, unsigned, CompareSMLoc> DiagCountPerPattern;
-  for (const std::unique_ptr<FileCheckDiag> &Diag : Diags)
-    ++DiagCountPerPattern[Diag->CheckLoc];
+  for (const FileCheckDiag &Diag : Diags)
+    ++DiagCountPerPattern[Diag.CheckLoc];
   // How many diagnostics have we seen so far per pattern?
   std::map<SMLoc, unsigned, CompareSMLoc> DiagIndexPerPattern;
   // How many total diagnostics have we seen so far?
   unsigned DiagIndex = 0;
   // What's the widest label?
   LabelWidth = 0;
-  for (const std::unique_ptr<FileCheckDiag> &Diag : Diags) {
+  for (const FileCheckDiag &Diag : Diags) {
     InputAnnotation A;
     A.DiagIndex = DiagIndex++;
 
     // Build label, which uniquely identifies this check result.
-    unsigned CheckBufferID = SM.FindBufferContainingLoc(Diag->CheckLoc);
-    auto CheckLineAndCol = SM.getLineAndColumn(Diag->CheckLoc, CheckBufferID);
+    unsigned CheckBufferID = SM.FindBufferContainingLoc(Diag.CheckLoc);
+    auto CheckLineAndCol = SM.getLineAndColumn(Diag.CheckLoc, CheckBufferID);
     llvm::raw_string_ostream Label(A.Label);
-    Label << GetCheckTypeAbbreviation(Diag->CheckTy) << ":";
+    Label << GetCheckTypeAbbreviation(Diag.CheckTy) << ":";
     if (CheckBufferID == CheckFileBufferID)
       Label << CheckLineAndCol.first;
     else if (ImpPatBufferIDRange.first <= CheckBufferID &&
@@ -414,52 +414,52 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
     else
       llvm_unreachable("expected diagnostic's check location to be either in "
                        "the check file or for an implicit pattern");
-    if (DiagCountPerPattern[Diag->CheckLoc] > 1)
-      Label << "'" << DiagIndexPerPattern[Diag->CheckLoc]++;
+    if (DiagCountPerPattern[Diag.CheckLoc] > 1)
+      Label << "'" << DiagIndexPerPattern[Diag.CheckLoc]++;
     LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
 
-    A.Marker = GetMarker(Diag->MatchTy);
-    if (!Diag->Note.empty()) {
-      A.Marker.Note = Diag->Note;
+    A.Marker = GetMarker(Diag.MatchTy);
+    if (!Diag.Note.empty()) {
+      A.Marker.Note = Diag.Note;
       // It's less confusing if notes that don't actually have ranges don't have
       // markers.  For example, a marker for 'with "VAR" equal to "5"' would
       // seem to indicate where "VAR" matches, but the location we actually have
       // for the marker simply points to the start of the match/search range for
       // the full pattern of which the substitution is potentially just one
       // component.
-      if (Diag->InputStartLine == Diag->InputEndLine &&
-          Diag->InputStartCol == Diag->InputEndCol)
+      if (Diag.InputStartLine == Diag.InputEndLine &&
+          Diag.InputStartCol == Diag.InputEndCol)
         A.Marker.Lead = ' ';
     }
-    if (Diag->MatchTy == FileCheckDiag::MatchFoundErrorNote) {
-      assert(!Diag->Note.empty() &&
+    if (Diag.MatchTy == FileCheckDiag::MatchFoundErrorNote) {
+      assert(!Diag.Note.empty() &&
              "expected custom note for MatchFoundErrorNote");
       A.Marker.Note = "error: " + A.Marker.Note;
     }
     A.FoundAndExpectedMatch =
-        Diag->MatchTy == FileCheckDiag::MatchFoundAndExpected;
+        Diag.MatchTy == FileCheckDiag::MatchFoundAndExpected;
 
     // Compute the mark location, and break annotation into multiple
     // annotations if it spans multiple lines.
     A.IsFirstLine = true;
-    A.InputLine = Diag->InputStartLine;
-    A.InputStartCol = Diag->InputStartCol;
-    if (Diag->InputStartLine == Diag->InputEndLine) {
+    A.InputLine = Diag.InputStartLine;
+    A.InputStartCol = Diag.InputStartCol;
+    if (Diag.InputStartLine == Diag.InputEndLine) {
       // Sometimes ranges are empty in order to indicate a specific point, but
       // that would mean nothing would be marked, so adjust the range to
       // include the following character.
-      A.InputEndCol = std::max(Diag->InputStartCol + 1, Diag->InputEndCol);
+      A.InputEndCol = std::max(Diag.InputStartCol + 1, Diag.InputEndCol);
       Annotations.push_back(A);
     } else {
-      assert(Diag->InputStartLine < Diag->InputEndLine &&
+      assert(Diag.InputStartLine < Diag.InputEndLine &&
              "expected input range not to be inverted");
       A.InputEndCol = UINT_MAX;
       Annotations.push_back(A);
-      for (unsigned L = Diag->InputStartLine + 1, E = Diag->InputEndLine;
-           L <= E; ++L) {
+      for (unsigned L = Diag.InputStartLine + 1, E = Diag.InputEndLine; L <= E;
+           ++L) {
         // If a range ends before the first column on a line, then it has no
         // characters on that line, so there's nothing to render.
-        if (Diag->InputEndCol == 1 && L == E)
+        if (Diag.InputEndCol == 1 && L == E)
           break;
         InputAnnotation B;
         B.DiagIndex = A.DiagIndex;
@@ -473,7 +473,7 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
         if (L != E)
           B.InputEndCol = UINT_MAX;
         else
-          B.InputEndCol = Diag->InputEndCol;
+          B.InputEndCol = Diag.InputEndCol;
         B.FoundAndExpectedMatch = A.FoundAndExpectedMatch;
         Annotations.push_back(B);
       }
@@ -865,8 +865,8 @@ int main(int argc, char **argv) {
            << "\n";
     std::vector<InputAnnotation> Annotations;
     unsigned LabelWidth;
-    buildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange,
-                          Diags.getList(), Annotations, LabelWidth);
+    buildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags,
+                          Annotations, LabelWidth);
     DumpAnnotatedInput(errs(), Req, DumpInputFilter, DumpInputContext,
                        InputFileText, Annotations, LabelWidth);
   }

>From 425edb5ff7f2012b3ecdb5db9f82418232af1545 Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Tue, 5 May 2026 09:19:46 -0400
Subject: [PATCH 5/6] Drop virtual if override

---
 llvm/include/llvm/FileCheck/FileCheck.h | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/FileCheck/FileCheck.h b/llvm/include/llvm/FileCheck/FileCheck.h
index c2cb73cf746f4..9a95e76a98cd8 100644
--- a/llvm/include/llvm/FileCheck/FileCheck.h
+++ b/llvm/include/llvm/FileCheck/FileCheck.h
@@ -208,9 +208,7 @@ class MatchResultDiag : public FileCheckDiag {
     return FCD->getKind() == FCDK_MatchResultDiag;
   }
   /// Get itself.
-  virtual const MatchResultDiag &getMatchResultDiag() const override {
-    return *this;
-  }
+  const MatchResultDiag &getMatchResultDiag() const override { return *this; }
   /// What is the type of pattern for this match result?
   Check::FileCheckType getCheckTy() const { return CheckTy; }
   /// Where is the pattern for this match result?
@@ -234,9 +232,7 @@ class MatchNoteDiag : public FileCheckDiag {
     return FCD->getKind() == FCDK_MatchNoteDiag;
   }
   /// Get the note's associated \c MatchResultDiag.
-  virtual const MatchResultDiag &getMatchResultDiag() const override {
-    return *MRD;
-  }
+  const MatchResultDiag &getMatchResultDiag() const override { return *MRD; }
   /// Set the note's associated \c MatchResultDiag.
   void setMatchResultDiag(MatchResultDiag *MRDNew) {
     assert(!MRD && "expected setMatchResultDiag to be called only once");

>From b6fa0615c4a4ba3dd905e81fec8184d39916c7a6 Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Tue, 5 May 2026 09:38:33 -0400
Subject: [PATCH 6/6] Drop redundant init

---
 llvm/utils/FileCheck/FileCheck.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 6492b32861f47..0a8d0e1d10572 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -429,7 +429,7 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
     LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
 
     A.Marker = GetMarker(Diag.getMatchType());
-    std::optional<StringRef> CustomNote = std::nullopt;
+    std::optional<StringRef> CustomNote;
     if (const MatchNoteDiag *NoteDiag = dyn_cast<MatchNoteDiag>(&Diag)) {
       CustomNote = NoteDiag->getCustomNote();
       if (CustomNote) {



More information about the llvm-commits mailing list