https://github.com/jdenny-ornl updated https://github.com/llvm/llvm-project/pull/207485
>From f43bf1ebb9ae39f98b05e95c01021c458cc82ff1 Mon Sep 17 00:00:00 2001 From: "Joel E. Denny" <[email protected]> Date: Sat, 4 Jul 2026 00:49:35 -0400 Subject: [PATCH] [FileCheck][NFC] Introduce FileCheckDiagAnnotator In the way that `SearchRangeAnnotator` encapsulates generating search range annotations, this patch introduces `FileCheckDiagAnnotator` to encapsulate generating the main annotation for a `FileCheckDiag`. This change makes `buildInputAnnotations` easier to read, especially after a third annotator class that will be introduced in a future patch. --- llvm/utils/FileCheck/FileCheck.cpp | 91 +++++++++++++++++++----------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp index 4252442919da2..33625e0c9e1f1 100644 --- a/llvm/utils/FileCheck/FileCheck.cpp +++ b/llvm/utils/FileCheck/FileCheck.cpp @@ -622,11 +622,6 @@ class SearchRangeAnnotator { /// annotations have been added already for the most recent /// \c MatchResultDiag. const MatchResultDiag *MRD; - /// Would a \c SearchRangeAnnotator make any search range annotations for - /// \p MRD? - static bool makesAnnotationsFor(const MatchResultDiag &MRD) { - return !MRD.getMatchRange() || MRD.isError(); - } /// Assuming \c makesAnnotationsFor(MRD), would a \c SearchRangeAnnotator make /// a one-line search range annotation for \p MRD? Either way, the search /// range computed for \p MRD is stored in \p SearchRange. @@ -671,6 +666,11 @@ class SearchRangeAnnotator { } public: + /// Would a \c SearchRangeAnnotator make any search range annotations for + /// \p MRD? + static bool makesAnnotationsFor(const MatchResultDiag &MRD) { + return !MRD.getMatchRange() || MRD.isError(); + } /// Tell the labeler how many times this will call /// \c InputAnnotationLabeler::makeLabel for \c MRD. void predictLabelsFor(const MatchResultDiag &MRD) { @@ -679,12 +679,6 @@ class SearchRangeAnnotator { MarkerRange SearchRange; Labeler.expect(MRD, makesOneLinerFor(SM, MRD, SearchRange) ? 1 : 2); } - /// Are the search range annotations generated by a \c SearchRangeAnnotator - /// sufficient for \p MRD? Otherwise, \p MRD needs to be rendered as a - /// separate annotation. - static bool sufficesFor(const MatchResultDiag &MRD) { - return makesAnnotationsFor(MRD) && getMarker(MRD).Note.empty(); - } /// \p Annotations is where this annotator should append search range /// annotations. SearchRangeAnnotator(const SourceMgr &SM, InputAnnotationLabeler &Labeler, @@ -710,31 +704,38 @@ class SearchRangeAnnotator { makeAnnotation(/*Start=*/false); } }; -} // namespace -static void -buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID, - const std::pair<unsigned, unsigned> &ImpPatBufferIDRange, - const FileCheckDiagList &Diags, - std::vector<InputAnnotation> &Annotations, - unsigned &LabelWidthGlobal) { - InputAnnotationLabeler Labeler(SM, CheckFileBufferID, ImpPatBufferIDRange); - SearchRangeAnnotator TheSearchRangeAnnotator(SM, Labeler, Annotations); - for (const FileCheckDiag &Diag : Diags) { - if (const MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(&Diag)) { - TheSearchRangeAnnotator.predictLabelsFor(*MRD); - if (!SearchRangeAnnotator::sufficesFor(*MRD)) - Labeler.expect(*MRD, 1); - } else { - Labeler.expect(Diag, 1); - } +/// Emits the main annotations for each \c FileCheckDiag as it is encountered. +class FileCheckDiagAnnotator { +private: + const SourceMgr &SM; + InputAnnotationLabeler &Labeler; + std::vector<InputAnnotation> &Annotations; + /// Would a \c FileCheckDiagAnnotator make any annotations for \p Diag? + static bool makesAnnotationsFor(const FileCheckDiag &Diag) { + const MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(&Diag); + if (!MRD) + return true; + if (SearchRangeAnnotator::makesAnnotationsFor(*MRD) && + getMarker(*MRD).Note.empty()) + return false; + return true; } - for (const FileCheckDiag &Diag : Diags) { - if (const MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(&Diag)) { - TheSearchRangeAnnotator.newMatchResultDiag(*MRD); - if (SearchRangeAnnotator::sufficesFor(*MRD)) - continue; - } + +public: + /// Tell the labeler how many times this will call + /// \c InputAnnotationLabeler::makeLabel for \c Diag. + void predictLabelsFor(const FileCheckDiag &Diag) { + Labeler.expect(Diag, makesAnnotationsFor(Diag) ? 1 : 0); + } + /// \p Annotations is where this annotator should append annotations. + FileCheckDiagAnnotator(const SourceMgr &SM, InputAnnotationLabeler &Labeler, + std::vector<InputAnnotation> &Annotations) + : SM(SM), Labeler(Labeler), Annotations(Annotations) {} + /// Emit any annotations for \c Diag. + void makeAnnotations(const FileCheckDiag &Diag) { + if (!makesAnnotationsFor(Diag)) + return; // Build label that is unique for this input annotation before it is // potentially broken across multiple lines. @@ -797,6 +798,28 @@ buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID, } } } +}; +} // namespace + +static void +buildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID, + const std::pair<unsigned, unsigned> &ImpPatBufferIDRange, + const FileCheckDiagList &Diags, + std::vector<InputAnnotation> &Annotations, + unsigned &LabelWidthGlobal) { + InputAnnotationLabeler Labeler(SM, CheckFileBufferID, ImpPatBufferIDRange); + SearchRangeAnnotator TheSearchRangeAnnotator(SM, Labeler, Annotations); + FileCheckDiagAnnotator TheFileCheckDiagAnnotator(SM, Labeler, Annotations); + for (const FileCheckDiag &Diag : Diags) { + if (const MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(&Diag)) + TheSearchRangeAnnotator.predictLabelsFor(*MRD); + TheFileCheckDiagAnnotator.predictLabelsFor(Diag); + } + for (const FileCheckDiag &Diag : Diags) { + if (const MatchResultDiag *MRD = dyn_cast<MatchResultDiag>(&Diag)) + TheSearchRangeAnnotator.newMatchResultDiag(*MRD); + TheFileCheckDiagAnnotator.makeAnnotations(Diag); + } TheSearchRangeAnnotator.endDiags(); LabelWidthGlobal = Labeler.getLabelWidthGlobal(); } _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
