LLVM 24.0.0git
llvm::SourceMgrDiagnosticVerifier Class Reference

Scans SourceMgr source buffers for 'expected-<kind>' comments (e.g. More...

#include "llvm/Support/SourceMgrDiagnosticVerifier.h"

Classes

struct  ExpectedDiag
 A single diagnostic expected via an 'expected-<kind>' comment. More...

Public Types

enum class  MatchResult { Matched , NearMiss , Unexpected , Ignored }
 The result of matching a single actual diagnostic against the expected diagnostics recorded for its location. More...

Public Member Functions

 SourceMgrDiagnosticVerifier (ArrayRef< StringRef > prefixes={ "expected"})
 Recognizes directives starting with any of prefixes (e.g.
 SourceMgrDiagnosticVerifier (const SourceMgrDiagnosticVerifier &)=delete
SourceMgrDiagnosticVerifier & operator= (const SourceMgrDiagnosticVerifier &)=delete
MutableArrayRef< ExpectedDiag > computeExpectedDiags (raw_ostream &os, SourceMgr &mgr, const MemoryBuffer *buf)
 Computes and caches the list of expected diagnostics for buf, if not already cached.
std::optional< MutableArrayRef< ExpectedDiag > > getExpectedDiags (StringRef bufName)
 Returns the cached expected diagnostics for the buffer named bufName, or std::nullopt if computeExpectedDiags hasn't been called for it.
MutableArrayRef< ExpectedDiag > getExpectedUnknownLocDiags ()
 Returns the expected diagnostics with an '@unknown' location.
MatchResult process (raw_ostream &os, SourceMgr &mgr, SourceMgr::DiagKind kind, bool hasLoc, const MemoryBuffer *buf, unsigned lineNo, unsigned colNo, StringRef message, bool reportUnexpected=true)
 Matches a single actual diagnostic against the expected diagnostics recorded for buf / lineNo / colNo (1-based), computing them first via computeExpectedDiags if they haven't been already.
bool verify (raw_ostream &os, SourceMgr &mgr)
 Reports (through mgr) any expected diagnostic that was never matched by a call to process.

Static Public Member Functions

static StringRef getDiagKindStr (SourceMgr::DiagKind kind)
 Given a diagnostic kind, return a human readable string for it.
static void printMessage (raw_ostream &os, const SourceMgr &mgr, SMLoc loc, const Twine &msg, ArrayRef< SMRange > ranges={})
 Prints an error to os without going through mgr's diagnostic handler, which may be the one feeding the verifier.

Detailed Description

Scans SourceMgr source buffers for 'expected-<kind>' comments (e.g.

'expected-error {{message}}') and verifies that the diagnostics reported to it via process match them, and that every expected diagnostic is matched at least once.

Definition at line 36 of file SourceMgrDiagnosticVerifier.h.

Member Enumeration Documentation

◆ MatchResult

The result of matching a single actual diagnostic against the expected diagnostics recorded for its location.

Enumerator
Matched 

The diagnostic matched an expected diagnostic (kind and text).

NearMiss 

The diagnostic's text matched an expected diagnostic, but its kind didn't; a message about this has already been printed.

Unexpected 

The diagnostic did not match any expected diagnostic.

The caller is responsible for reporting it, if desired.

Ignored 

The diagnostic did not match, but reportUnexpected was false, so nothing was printed and nothing needs to be done.

Definition at line 40 of file SourceMgrDiagnosticVerifier.h.

Constructor & Destructor Documentation

◆ SourceMgrDiagnosticVerifier() [1/2]

SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier ( ArrayRef< StringRef > prefixes = { "expected"})
explicit

Recognizes directives starting with any of prefixes (e.g.

'expected-error').

Definition at line 108 of file SourceMgrDiagnosticVerifier.cpp.

References buildExpectedRegex().

Referenced by operator=(), and SourceMgrDiagnosticVerifier().

◆ SourceMgrDiagnosticVerifier() [2/2]

llvm::SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier ( const SourceMgrDiagnosticVerifier & )
delete

Member Function Documentation

◆ computeExpectedDiags()

◆ getDiagKindStr()

StringRef SourceMgrDiagnosticVerifier::getDiagKindStr ( SourceMgr::DiagKind kind)
static

Given a diagnostic kind, return a human readable string for it.

Definition at line 25 of file SourceMgrDiagnosticVerifier.cpp.

References llvm::SourceMgr::DK_Error, llvm::SourceMgr::DK_Note, llvm::SourceMgr::DK_Remark, llvm::SourceMgr::DK_Warning, and llvm_unreachable.

Referenced by operator=(), process(), and verify().

◆ getExpectedDiags()

std::optional< MutableArrayRef< SourceMgrDiagnosticVerifier::ExpectedDiag > > SourceMgrDiagnosticVerifier::getExpectedDiags ( StringRef bufName)

Returns the cached expected diagnostics for the buffer named bufName, or std::nullopt if computeExpectedDiags hasn't been called for it.

Definition at line 214 of file SourceMgrDiagnosticVerifier.cpp.

References llvm::MutableArrayRef().

Referenced by process().

◆ getExpectedUnknownLocDiags()

MutableArrayRef< ExpectedDiag > llvm::SourceMgrDiagnosticVerifier::getExpectedUnknownLocDiags ( )
inline

Returns the expected diagnostics with an '@unknown' location.

Definition at line 120 of file SourceMgrDiagnosticVerifier.h.

◆ operator=()

SourceMgrDiagnosticVerifier & llvm::SourceMgrDiagnosticVerifier::operator= ( const SourceMgrDiagnosticVerifier & )
delete

◆ printMessage()

void SourceMgrDiagnosticVerifier::printMessage ( raw_ostream & os,
const SourceMgr & mgr,
SMLoc loc,
const Twine & msg,
ArrayRef< SMRange > ranges = {} )
static

Prints an error to os without going through mgr's diagnostic handler, which may be the one feeding the verifier.

Definition at line 16 of file SourceMgrDiagnosticVerifier.cpp.

References llvm::SourceMgr::DK_Error, llvm::SourceMgr::GetMessage(), llvm::SMDiagnostic::print(), and llvm::SourceMgr::printIncludeStackForDiagnostic().

Referenced by llvm::SourceMgrDiagnosticVerifier::ExpectedDiag::emitError(), operator=(), and process().

◆ process()

SourceMgrDiagnosticVerifier::MatchResult SourceMgrDiagnosticVerifier::process ( raw_ostream & os,
SourceMgr & mgr,
SourceMgr::DiagKind kind,
bool hasLoc,
const MemoryBuffer * buf,
unsigned lineNo,
unsigned colNo,
StringRef message,
bool reportUnexpected = true )

Matches a single actual diagnostic against the expected diagnostics recorded for buf / lineNo / colNo (1-based), computing them first via computeExpectedDiags if they haven't been already.

If hasLoc is false, the diagnostic has no location and is matched against the '@unknown' list instead (buf / lineNo / colNo are ignored). If hasLoc is true but 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 mgr. reportUnexpected controls whether near misses / unexpected diagnostics are reported at all.

Definition at line 221 of file SourceMgrDiagnosticVerifier.cpp.

References computeExpectedDiags(), llvm::SourceMgrDiagnosticVerifier::ExpectedDiag::fileLoc, llvm::MemoryBuffer::getBufferIdentifier(), getDiagKindStr(), getExpectedDiags(), hasLoc(), Ignored, llvm::SourceMgrDiagnosticVerifier::ExpectedDiag::kind, Matched, NearMiss, printMessage(), and Unexpected.

◆ verify()

bool SourceMgrDiagnosticVerifier::verify ( raw_ostream & os,
SourceMgr & mgr )

Reports (through mgr) any expected diagnostic that was never matched by a call to process.

Returns whether verification succeeded overall, i.e. no diagnostic mismatches were recorded either here or by process.

Definition at line 278 of file SourceMgrDiagnosticVerifier.cpp.

References getDiagKindStr().


The documentation for this class was generated from the following files: