https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/227567
>From a5a368829a56e0f7b7dc154d81a0d5ad86b3fca3 Mon Sep 17 00:00:00 2001 From: Alex Richardson <[email protected]> Date: Sun, 18 Oct 2026 16:08:41 -0700 Subject: [PATCH] [nspr] initial commit --- .../Support/SourceMgrDiagnosticVerifier.h | 101 +++++++------- .../Support/SourceMgrDiagnosticVerifier.cpp | 118 +++++++++-------- llvm/unittests/Support/CMakeLists.txt | 1 + .../SourceMgrDiagnosticVerifierTest.cpp | 123 ++++++++++++++++++ .../secondary/llvm/unittests/Support/BUILD.gn | 1 + mlir/lib/IR/Diagnostics.cpp | 29 ++--- 6 files changed, 256 insertions(+), 117 deletions(-) create mode 100644 llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp diff --git a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h index e60812b6f51153..278c688921e2ab 100644 --- a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h +++ b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h @@ -43,14 +43,47 @@ class LLVM_ABI SourceMgrDiagnosticVerifier { /// The diagnostic's text matched an expected diagnostic, but its kind /// didn't; a message about this has already been printed. NearMiss, - /// The diagnostic did not match any expected diagnostic. The caller is - /// responsible for reporting it, if desired. + /// The diagnostic did not match any expected diagnostic. Unexpected, /// The diagnostic did not match, but \p ReportUnexpected was false, so /// nothing was printed and nothing needs to be done. Ignored, }; + /// The location of an actual diagnostic. + struct Location { + /// The buffer containing the diagnostic, or null if it is in a file that + /// is not known to the SourceMgr (such a diagnostic never matches). + const MemoryBuffer *Buf; + /// The 1-based line number of the diagnostic. + unsigned LineNo; + }; + + /// Scans all buffers currently registered with \p Mgr for expected + /// diagnostics. Buffers added later are scanned the first time a + /// diagnostic is reported in them. Verifier errors (near misses, unmatched + /// expected diagnostics, malformed directives) are printed to \p OS, + /// bypassing \p Mgr's diagnostic handler. + SourceMgrDiagnosticVerifier(const SourceMgr &Mgr, raw_ostream &OS); + + /// Matches a diagnostic against the expected diagnostics for \p Loc, or + /// against the '@unknown' expected diagnostics if \p Loc is std::nullopt. + /// Prints a message on a near miss. Reporting an unexpected diagnostic is + /// left to the caller. If \p ReportUnexpected is false, a diagnostic that + /// does not match is ignored instead of failing verification. + MatchResult process(SourceMgr::DiagKind Kind, std::optional<Location> Loc, + StringRef Message, bool ReportUnexpected = true); + + /// Like the overload above, but takes the location from \p Diag and also + /// prints \p Diag if it is unexpected. + MatchResult process(const SMDiagnostic &Diag, bool ReportUnexpected = true); + + /// Reports any expected diagnostic that was never matched by a call to + /// \c process. Returns whether verification succeeded overall, i.e. no + /// diagnostic mismatches were recorded either here or by \c process. + bool verify(); + +private: /// A single diagnostic expected via an 'expected-<kind>' comment. struct ExpectedDiag { ExpectedDiag(SourceMgr::DiagKind Kind, unsigned LineNo, SMLoc FileLoc, @@ -60,16 +93,6 @@ class LLVM_ABI SourceMgrDiagnosticVerifier { /// Returns true if this diagnostic matches the given message. LLVM_ABI bool match(StringRef Str) const; - /// Computes the regex matcher for a '-re' diagnostic's substring. - /// Returns false and prints a message through \p Mgr on error. - LLVM_ABI bool computeRegex(raw_ostream &OS, SourceMgr &Mgr); - - /// Prints \p Msg at this diagnostic's location and returns false, for - /// use as `return emitError(...);` in functions that report failure via - /// a bool return. - LLVM_ABI bool emitError(raw_ostream &OS, SourceMgr &Mgr, - const Twine &Msg) const; - /// The severity of the diagnostic expected. SourceMgr::DiagKind Kind; /// The line number the expected diagnostic should be on. @@ -85,45 +108,25 @@ class LLVM_ABI SourceMgrDiagnosticVerifier { std::optional<Regex> SubstringRegex; }; - SourceMgrDiagnosticVerifier() = default; - - /// Computes and caches the list of expected diagnostics for \p Buf, if not - /// already cached. Returns the (mutable) cached list. - MutableArrayRef<ExpectedDiag> computeExpectedDiags(raw_ostream &OS, - SourceMgr &Mgr, - const MemoryBuffer *Buf); - - /// Returns the cached expected diagnostics for the buffer named \p BufName, - /// or std::nullopt if \p computeExpectedDiags hasn't been called for it. - std::optional<MutableArrayRef<ExpectedDiag>> - getExpectedDiags(StringRef BufName); - - /// Returns the expected diagnostics with an '@unknown' location. - MutableArrayRef<ExpectedDiag> getExpectedUnknownLocDiags() { - return ExpectedUnknownLocDiags; - } - - /// Matches a single actual diagnostic against the expected diagnostics - /// recorded for \p Buf / \p LineNo, computing them first via \p - /// computeExpectedDiags if they haven't been already. If \p HasLoc is - /// false, the diagnostic has no location and is matched against the - /// '@unknown' list instead (\p Buf / \p LineNo are ignored). If \p HasLoc - /// is true but \p Buf is null (e.g. the diagnostic's file isn't a known - /// buffer), the diagnostic is matched against an empty list, i.e. it can - /// never match and is always unexpected. On a near miss, prints a message - /// through \p Mgr. \p ReportUnexpected controls whether near misses / - /// unexpected diagnostics are reported at all. - MatchResult process(raw_ostream &OS, SourceMgr &Mgr, SourceMgr::DiagKind Kind, - bool HasLoc, const MemoryBuffer *Buf, unsigned LineNo, - StringRef Message, bool ReportUnexpected = true); + /// Prints \p Diag to \c OS, bypassing the SourceMgr's diagnostic handler so + /// that a verifier installed as that handler does not re-enter itself. + void printDiag(const SMDiagnostic &Diag) const; - /// Reports (through \p Mgr) any expected diagnostic that was never matched - /// by a call to \p process. Returns whether verification succeeded overall, - /// i.e. no diagnostic mismatches were recorded either here or by \p - /// process. - bool verify(raw_ostream &OS, SourceMgr &Mgr); + /// Records a verification failure, prints \p Msg at \p Diag's location and + /// returns false. + bool emitError(const ExpectedDiag &Diag, const Twine &Msg); + + /// Computes the regex matcher for a '-re' diagnostic's substring. Returns + /// false and prints a message on error. + bool computeRegex(ExpectedDiag &Diag); + + /// Scans \p Buf for expected diagnostics and returns the list for its + /// buffer identifier. + MutableArrayRef<ExpectedDiag> computeExpectedDiags(const MemoryBuffer *Buf); + + const SourceMgr &Mgr; + raw_ostream &OS; -private: /// Regex used to recognize 'expected-<kind>' comments. Regex Expected{"expected-(error|note|remark|warning)(-re)? " "*(@([+-][0-9]+|above|below|unknown))? *{{(.*)}}$"}; diff --git a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp index a031adb774cf96..68358400b3d2a0 100644 --- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp +++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp @@ -12,6 +12,13 @@ using namespace llvm; +SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier(const SourceMgr &Mgr, + raw_ostream &OS) + : Mgr(Mgr), OS(OS) { + for (unsigned I = 0, E = Mgr.getNumBuffers(); I != E; ++I) + (void)computeExpectedDiags(Mgr.getMemoryBuffer(I + 1)); +} + /// Given a diagnostic kind, return a human readable string for it. static StringRef getDiagKindStr(SourceMgr::DiagKind Kind) { switch (Kind) { @@ -27,15 +34,23 @@ static StringRef getDiagKindStr(SourceMgr::DiagKind Kind) { llvm_unreachable("Unknown SourceMgr::DiagKind"); } -bool SourceMgrDiagnosticVerifier::ExpectedDiag::emitError( - raw_ostream &OS, SourceMgr &Mgr, const Twine &Msg) const { - if (FileLoc.isValid()) { - SMRange Range(FileLoc, SMLoc::getFromPointer(FileLoc.getPointer() + - Substring.size())); - Mgr.PrintMessage(OS, FileLoc, SourceMgr::DK_Error, Msg, Range); - } else { - Mgr.PrintMessage(OS, FileLoc, SourceMgr::DK_Error, Msg); +void SourceMgrDiagnosticVerifier::printDiag(const SMDiagnostic &Diag) const { + Mgr.printIncludeStackForDiagnostic(Diag.getLoc(), OS); + Diag.print(nullptr, OS); +} + +bool SourceMgrDiagnosticVerifier::emitError(const ExpectedDiag &Diag, + const Twine &Msg) { + OK = false; + SMRange Range; + ArrayRef<SMRange> Ranges; + if (Diag.FileLoc.isValid()) { + Range = + SMRange(Diag.FileLoc, SMLoc::getFromPointer(Diag.FileLoc.getPointer() + + Diag.Substring.size())); + Ranges = Range; } + printDiag(Mgr.GetMessage(Diag.FileLoc, SourceMgr::DK_Error, Msg, Ranges)); return false; } @@ -47,11 +62,10 @@ bool SourceMgrDiagnosticVerifier::ExpectedDiag::match(StringRef Str) const { return Str.contains(Substring); } -bool SourceMgrDiagnosticVerifier::ExpectedDiag::computeRegex(raw_ostream &OS, - SourceMgr &Mgr) { +bool SourceMgrDiagnosticVerifier::computeRegex(ExpectedDiag &Diag) { std::string RegexStr; raw_string_ostream RegexOS(RegexStr); - StringRef StrToProcess = Substring; + StringRef StrToProcess = Diag.Substring; while (!StrToProcess.empty()) { // Find the next regex block. size_t RegexIt = StrToProcess.find("{{"); @@ -65,25 +79,23 @@ bool SourceMgrDiagnosticVerifier::ExpectedDiag::computeRegex(raw_ostream &OS, // Find the end of the regex block. size_t RegexEndIt = StrToProcess.find("}}"); if (RegexEndIt == StringRef::npos) - return emitError(OS, Mgr, "found start of regex with no end '}}'"); + return emitError(Diag, "found start of regex with no end '}}'"); StringRef RegexBlock = StrToProcess.take_front(RegexEndIt); // Validate that the regex is actually valid. std::string RegexError; if (!Regex(RegexBlock).isValid(RegexError)) - return emitError(OS, Mgr, "invalid regex: " + RegexError); + return emitError(Diag, "invalid regex: " + RegexError); RegexOS << '(' << RegexBlock << ')'; StrToProcess = StrToProcess.drop_front(RegexEndIt + 2); } - SubstringRegex = Regex(RegexStr); + Diag.SubstringRegex = Regex(RegexStr); return true; } MutableArrayRef<SourceMgrDiagnosticVerifier::ExpectedDiag> -SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &OS, - SourceMgr &Mgr, - const MemoryBuffer *Buf) { +SourceMgrDiagnosticVerifier::computeExpectedDiags(const MemoryBuffer *Buf) { // If the buffer is invalid, return an empty list. if (!Buf) return {}; @@ -128,10 +140,8 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &OS, ExpectedDiag Record(Kind, LineNo + 1, ExpectedStart, Matches[5]); // Check to see if this is a regex match, i.e. it includes the `-re`. - if (!Matches[2].empty() && !Record.computeRegex(OS, Mgr)) { - OK = false; + if (!Matches[2].empty() && !computeRegex(Record)) continue; - } StringRef OffsetMatch = Matches[3]; if (!OffsetMatch.empty()) { @@ -171,28 +181,20 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &OS, return ExpectedDiags; } -std::optional<MutableArrayRef<SourceMgrDiagnosticVerifier::ExpectedDiag>> -SourceMgrDiagnosticVerifier::getExpectedDiags(StringRef BufName) { - auto ExpectedDiags = ExpectedDiagsPerFile.find(BufName); - if (ExpectedDiags != ExpectedDiagsPerFile.end()) - return MutableArrayRef<ExpectedDiag>(ExpectedDiags->second); - return std::nullopt; -} - SourceMgrDiagnosticVerifier::MatchResult -SourceMgrDiagnosticVerifier::process(raw_ostream &OS, SourceMgr &Mgr, - SourceMgr::DiagKind Kind, bool HasLoc, - const MemoryBuffer *Buf, unsigned LineNo, +SourceMgrDiagnosticVerifier::process(SourceMgr::DiagKind Kind, + std::optional<Location> Loc, StringRef Message, bool ReportUnexpected) { MutableArrayRef<ExpectedDiag> Diags; - if (HasLoc) { + if (Loc) { // If the buffer couldn't be resolved, `Diags` stays empty: a diagnostic // with a location in an unknown file can never match anything. - if (Buf) { - if (auto MaybeDiags = getExpectedDiags(Buf->getBufferIdentifier())) - Diags = *MaybeDiags; + if (Loc->Buf) { + auto It = ExpectedDiagsPerFile.find(Loc->Buf->getBufferIdentifier()); + if (It != ExpectedDiagsPerFile.end()) + Diags = It->second; else - Diags = computeExpectedDiags(OS, Mgr, Buf); + Diags = computeExpectedDiags(Loc->Buf); } } else { Diags = ExpectedUnknownLocDiags; @@ -205,7 +207,7 @@ SourceMgrDiagnosticVerifier::process(raw_ostream &OS, SourceMgr &Mgr, // If this was an expected error, remember that we saw it and return. for (auto &E : Diags) { // File line must match (unless it's an unknown location). - if (HasLoc && E.LineNo != LineNo) + if (Loc && E.LineNo != Loc->LineNo) continue; if (E.match(Message)) { if (E.Kind == Kind) { @@ -226,29 +228,43 @@ SourceMgrDiagnosticVerifier::process(raw_ostream &OS, SourceMgr &Mgr, // Otherwise, emit an error for the near miss. if (NearMiss) { - Mgr.PrintMessage(OS, NearMiss->FileLoc, SourceMgr::DK_Error, - "'" + getDiagKindStr(Kind) + - "' diagnostic emitted when expecting a '" + - getDiagKindStr(NearMiss->Kind) + "'"); + printDiag(Mgr.GetMessage(NearMiss->FileLoc, SourceMgr::DK_Error, + "'" + getDiagKindStr(Kind) + + "' diagnostic emitted when expecting a '" + + getDiagKindStr(NearMiss->Kind) + "'")); return MatchResult::NearMiss; } return MatchResult::Unexpected; } -bool SourceMgrDiagnosticVerifier::verify(raw_ostream &OS, SourceMgr &Mgr) { +SourceMgrDiagnosticVerifier::MatchResult +SourceMgrDiagnosticVerifier::process(const SMDiagnostic &Diag, + bool ReportUnexpected) { + std::optional<Location> Loc; + if (Diag.getLoc().isValid()) { + const MemoryBuffer *Buf = nullptr; + if (unsigned ID = Mgr.FindBufferContainingLoc(Diag.getLoc())) + Buf = Mgr.getMemoryBuffer(ID); + Loc = Location{Buf, static_cast<unsigned>(Diag.getLineNo())}; + } + MatchResult Result = + process(Diag.getKind(), Loc, Diag.getMessage(), ReportUnexpected); + if (Result == MatchResult::Unexpected) + printDiag(Diag); + return Result; +} + +bool SourceMgrDiagnosticVerifier::verify() { // Verify that all expected errors were seen. - auto CheckExpectedDiags = [&](ExpectedDiag &Diag) { - if (!Diag.Matched) { - Diag.emitError(OS, Mgr, - "expected " + getDiagKindStr(Diag.Kind) + " \"" + - Diag.Substring + "\" was not produced"); - OK = false; - } + auto CheckExpectedDiags = [&](const ExpectedDiag &Diag) { + if (!Diag.Matched) + emitError(Diag, "expected " + getDiagKindStr(Diag.Kind) + " \"" + + Diag.Substring + "\" was not produced"); }; for (auto &ExpectedDiagsPair : ExpectedDiagsPerFile) - for (auto &Diag : ExpectedDiagsPair.second) + for (const ExpectedDiag &Diag : ExpectedDiagsPair.second) CheckExpectedDiags(Diag); - for (auto &Diag : ExpectedUnknownLocDiags) + for (const ExpectedDiag &Diag : ExpectedUnknownLocDiags) CheckExpectedDiags(Diag); ExpectedDiagsPerFile.clear(); return OK; diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index f9732ff4e0e4bb..c5db78950d21b4 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -90,6 +90,7 @@ add_llvm_unittest(SupportTests SignalsTest.cpp SipHashTest.cpp SourceMgrTest.cpp + SourceMgrDiagnosticVerifierTest.cpp SpecialCaseListTest.cpp SuffixTreeTest.cpp SwapByteOrderTest.cpp diff --git a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp new file mode 100644 index 00000000000000..06e7f19ea2232a --- /dev/null +++ b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp @@ -0,0 +1,123 @@ +//===- unittests/Support/SourceMgrDiagnosticVerifierTest.cpp --------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/SourceMgrDiagnosticVerifier.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" + +using namespace llvm; +using MatchResult = SourceMgrDiagnosticVerifier::MatchResult; + +namespace { + +class SourceMgrDiagnosticVerifierTest : public testing::Test { +protected: + SourceMgr SM; + std::string Output; + raw_string_ostream OS{Output}; + + const MemoryBuffer *addBuffer(StringRef Text, + StringRef BufferName = "test.s") { + unsigned ID = SM.AddNewSourceBuffer( + MemoryBuffer::getMemBuffer(Text, BufferName), SMLoc()); + return SM.getMemoryBuffer(ID); + } + + SMDiagnostic getMessage(const MemoryBuffer *Buf, SourceMgr::DiagKind Kind, + StringRef Msg) { + return SM.GetMessage(SMLoc::getFromPointer(Buf->getBufferStart()), Kind, + Msg); + } +}; + +} // namespace + +TEST_F(SourceMgrDiagnosticVerifierTest, Match) { + const MemoryBuffer *Buf = + addBuffer("bad_op\n# expected-error@-1 {{invalid operand}}\n"); + SourceMgrDiagnosticVerifier Verifier(SM, OS); + + EXPECT_EQ( + Verifier.process(getMessage(Buf, SourceMgr::DK_Error, "invalid operand")), + MatchResult::Matched); + EXPECT_TRUE(Verifier.verify()); + EXPECT_EQ(Output, ""); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, UnknownLocation) { + addBuffer("# expected-warning@unknown {{no location warning}}\n"); + SourceMgrDiagnosticVerifier Verifier(SM, OS); + + SMDiagnostic Diag = + SM.GetMessage(SMLoc(), SourceMgr::DK_Warning, "no location warning"); + EXPECT_EQ(Verifier.process(Diag), MatchResult::Matched); + EXPECT_TRUE(Verifier.verify()); + EXPECT_EQ(Output, ""); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, UnmatchedUnknownLocation) { + addBuffer("# expected-error@unknown {{missing unknown error}}\n"); + SourceMgrDiagnosticVerifier Verifier(SM, OS); + + EXPECT_FALSE(Verifier.verify()); + EXPECT_NE( + StringRef(Output).find( + "error: expected error \"missing unknown error\" was not produced"), + StringRef::npos); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, ReportUnexpectedFalse) { + const MemoryBuffer *Buf = + addBuffer("op\n# expected-error@-1 {{expected msg}}\n"); + SourceMgrDiagnosticVerifier Verifier(SM, OS); + + // An unexpected diagnostic with ReportUnexpected = false is ignored and does + // not fail verification. + EXPECT_EQ( + Verifier.process(getMessage(Buf, SourceMgr::DK_Warning, "unrelated"), + /*ReportUnexpected=*/false), + MatchResult::Ignored); + EXPECT_EQ( + Verifier.process(getMessage(Buf, SourceMgr::DK_Error, "expected msg"), + /*ReportUnexpected=*/false), + MatchResult::Matched); + EXPECT_TRUE(Verifier.verify()); + EXPECT_EQ(Output, ""); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, UnknownFileNeverMatches) { + addBuffer("# expected-error {{anything}}\n"); + SourceMgrDiagnosticVerifier Verifier(SM, OS); + + SourceMgrDiagnosticVerifier::Location Loc{/*Buf=*/nullptr, /*LineNo=*/1}; + EXPECT_EQ(Verifier.process(SourceMgr::DK_Error, Loc, "anything"), + MatchResult::Unexpected); + EXPECT_FALSE(Verifier.verify()); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, InstalledAsDiagHandler) { + addBuffer("op\n# expected-warning@-1 {{bad op}}\n"); + SourceMgrDiagnosticVerifier Verifier(SM, OS); + SM.setDiagHandler( + [](const SMDiagnostic &Diag, void *Ctx) { + static_cast<SourceMgrDiagnosticVerifier *>(Ctx)->process(Diag); + }, + &Verifier); + + // The near-miss message must be printed to OS rather than re-entering the + // verifier through the SourceMgr's diagnostic handler. + SM.PrintMessage( + SMLoc::getFromPointer(SM.getMemoryBuffer(1)->getBufferStart()), + SourceMgr::DK_Error, "bad op"); + EXPECT_NE(StringRef(Output).find( + "error: 'error' diagnostic emitted when expecting a 'warning'"), + StringRef::npos); + EXPECT_FALSE(Verifier.verify()); +} diff --git a/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn b/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn index 168b609279cab6..7569f7e6fcbb91 100644 --- a/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn @@ -93,6 +93,7 @@ unittest("SupportTests") { "SignalsTest.cpp", "SipHashTest.cpp", "SourceMgrTest.cpp", + "SourceMgrDiagnosticVerifierTest.cpp", "SpecialCaseListTest.cpp", "SuffixTreeTest.cpp", "SwapByteOrderTest.cpp", diff --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp index ae678e1c44dfe0..dd4494a6d1ba64 100644 --- a/mlir/lib/IR/Diagnostics.cpp +++ b/mlir/lib/IR/Diagnostics.cpp @@ -640,8 +640,9 @@ namespace mlir { namespace detail { struct SourceMgrDiagnosticVerifierHandlerImpl { SourceMgrDiagnosticVerifierHandlerImpl( + llvm::SourceMgr &srcMgr, raw_ostream &out, SourceMgrDiagnosticVerifierHandler::Level level) - : level(level) {} + : verifier(srcMgr, out), level(level) {} /// The generic SourceMgr-based verifier core that this handler delegates /// scanning and matching of 'expected-*' diagnostics to. @@ -671,13 +672,7 @@ static StringRef getDiagKindStr(DiagnosticSeverity kind) { SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler( llvm::SourceMgr &srcMgr, MLIRContext *ctx, raw_ostream &out, Level level) : SourceMgrDiagnosticHandler(srcMgr, ctx, out), - impl(new SourceMgrDiagnosticVerifierHandlerImpl(level)) { - // Compute the expected diagnostics for each of the current files in the - // source manager. - for (unsigned i = 0, e = mgr.getNumBuffers(); i != e; ++i) - (void)impl->verifier.computeExpectedDiags(out, mgr, - mgr.getMemoryBuffer(i + 1)); - + impl(new SourceMgrDiagnosticVerifierHandlerImpl(srcMgr, out, level)) { // The base class registered a handler that prints every diagnostic. The // verifier takes its place: it consumes diagnostics and reports unexpected // ones itself. @@ -697,7 +692,7 @@ SourceMgrDiagnosticVerifierHandler::~SourceMgrDiagnosticVerifierHandler() { /// diagnostics were emitted. This return success if all diagnostics were /// verified correctly, failure otherwise. LogicalResult SourceMgrDiagnosticVerifierHandler::verify() { - return success(impl->verifier.verify(os, mgr)); + return success(impl->verifier.verify()); } std::unique_ptr<ScopedDiagnosticHandler> @@ -718,16 +713,16 @@ void SourceMgrDiagnosticVerifierHandler::process(Diagnostic &diag) { void SourceMgrDiagnosticVerifierHandler::process(LocationAttr loc, StringRef msg, DiagnosticSeverity kind) { - FileLineColLoc fileLoc = loc.findInstanceOf<FileLineColLoc>(); - const llvm::MemoryBuffer *buf = - fileLoc ? getBufferForFile(fileLoc.getFilename()) : nullptr; + using Verifier = llvm::SourceMgrDiagnosticVerifier; + std::optional<Verifier::Location> verifierLoc; + if (FileLineColLoc fileLoc = loc.findInstanceOf<FileLineColLoc>()) + verifierLoc = Verifier::Location{getBufferForFile(fileLoc.getFilename()), + fileLoc.getLine()}; bool reportUnexpected = impl->level != Level::OnlyExpected; - using MatchResult = llvm::SourceMgrDiagnosticVerifier::MatchResult; - MatchResult result = impl->verifier.process( - os, mgr, getDiagKind(kind), /*hasLoc=*/static_cast<bool>(fileLoc), buf, - fileLoc ? fileLoc.getLine() : 0, msg, reportUnexpected); - if (result == MatchResult::Unexpected) + Verifier::MatchResult result = impl->verifier.process( + getDiagKind(kind), verifierLoc, msg, reportUnexpected); + if (result == Verifier::MatchResult::Unexpected) emitDiagnostic(loc, "unexpected " + getDiagKindStr(kind) + ": " + msg, DiagnosticSeverity::Error); } _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
