LLVM 24.0.0git
SourceMgrDiagnosticVerifier.h
Go to the documentation of this file.
1//===- SourceMgrDiagnosticVerifier.h ---------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a utility for verifying that diagnostics reported through
10// a SourceMgr match 'expected-<kind>' comments in the source, for
11// implementing '-verify'-style diagnostic tests on top of a plain
12// llvm::SourceMgr.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_SUPPORT_SOURCEMGRDIAGNOSTICVERIFIER_H
17#define LLVM_SUPPORT_SOURCEMGRDIAGNOSTICVERIFIER_H
18
19#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/StringMap.h"
23#include "llvm/Support/Regex.h"
25#include <optional>
26
27namespace llvm {
28
29class MemoryBuffer;
30class raw_ostream;
31
32/// Scans SourceMgr source buffers for 'expected-<kind>' comments (e.g.
33/// 'expected-error {{message}}') and verifies that the diagnostics reported
34/// to it via \c process match them, and that every expected diagnostic is
35/// matched at least once.
37public:
38 /// The result of matching a single actual diagnostic against the expected
39 /// diagnostics recorded for its location.
40 enum class MatchResult {
41 /// The diagnostic matched an expected diagnostic (kind and text).
43 /// The diagnostic's text matched an expected diagnostic, but its kind
44 /// didn't; a message about this has already been printed.
46 /// The diagnostic did not match any expected diagnostic. The caller is
47 /// responsible for reporting it, if desired.
49 /// The diagnostic did not match, but \p reportUnexpected was false, so
50 /// nothing was printed and nothing needs to be done.
52 };
53
54 /// A single diagnostic expected via an 'expected-<kind>' comment.
55 struct ExpectedDiag {
59
60 /// Returns true if this diagnostic matches the given message.
61 LLVM_ABI bool match(StringRef str) const;
62
63 /// Computes the regex matcher for a '-re' diagnostic's substring.
64 /// Returns false and prints a message through \p mgr on error.
65 LLVM_ABI bool computeRegex(raw_ostream &os, SourceMgr &mgr);
66
67 /// Prints \p msg at this diagnostic's location and returns false, for
68 /// use as `return emitError(...);` in functions that report failure via
69 /// a bool return.
70 LLVM_ABI bool emitError(raw_ostream &os, SourceMgr &mgr,
71 const Twine &msg) const;
72
73 /// The severity of the diagnostic expected.
75 /// The line number the expected diagnostic should be on.
76 unsigned lineNo;
77 /// The location of the expected diagnostic within the input file.
79 /// A flag indicating if the expected diagnostic has been matched yet.
80 bool matched = false;
81 /// The substring that is expected to be within the diagnostic.
83 /// An optional regex matcher, if the expected diagnostic substring was a
84 /// regex string.
85 std::optional<Regex> substringRegex;
86 };
87
89
90 /// Computes and caches the list of expected diagnostics for \p buf, if not
91 /// already cached. Returns the (mutable) cached list.
93 SourceMgr &mgr,
94 const MemoryBuffer *buf);
95
96 /// Returns the cached expected diagnostics for the buffer named \p bufName,
97 /// or std::nullopt if \p computeExpectedDiags hasn't been called for it.
98 std::optional<MutableArrayRef<ExpectedDiag>>
100
101 /// Returns the expected diagnostics with an '@unknown' location.
103 return expectedUnknownLocDiags;
104 }
105
106 /// Matches a single actual diagnostic against the expected diagnostics
107 /// recorded for \p buf / \p lineNo, computing them first via \p
108 /// computeExpectedDiags if they haven't been already. If \p hasLoc is
109 /// false, the diagnostic has no location and is matched against the
110 /// '@unknown' list instead (\p buf / \p lineNo are ignored). If \p hasLoc
111 /// is true but \p buf is null (e.g. the diagnostic's file isn't a known
112 /// buffer), the diagnostic is matched against an empty list, i.e. it can
113 /// never match and is always unexpected. On a near miss, prints a message
114 /// through \p mgr. \p reportUnexpected controls whether near misses /
115 /// unexpected diagnostics are reported at all.
116 MatchResult process(raw_ostream &os, SourceMgr &mgr, SourceMgr::DiagKind kind,
117 bool hasLoc, const MemoryBuffer *buf, unsigned lineNo,
118 StringRef message, bool reportUnexpected = true);
119
120 /// Reports (through \p mgr) any expected diagnostic that was never matched
121 /// by a call to \p process. Returns whether verification succeeded overall,
122 /// i.e. no diagnostic mismatches were recorded either here or by \p
123 /// process.
124 bool verify(raw_ostream &os, SourceMgr &mgr);
125
126private:
127 /// Regex used to recognize 'expected-<kind>' comments.
128 Regex expected{"expected-(error|note|remark|warning)(-re)? "
129 "*(@([+-][0-9]+|above|below|unknown))? *{{(.*)}}$"};
130
131 /// The expected diagnostics for each buffer that has been scanned so far,
132 /// keyed by buffer identifier (i.e. file name).
133 StringMap<SmallVector<ExpectedDiag, 2>> expectedDiagsPerFile;
134
135 /// The expected diagnostics with an '@unknown' location.
136 SmallVector<ExpectedDiag, 2> expectedUnknownLocDiags;
137
138 /// Whether any diagnostic mismatch has been recorded so far.
139 bool ok = true;
140};
141
142} // namespace llvm
143
144#endif // LLVM_SUPPORT_SOURCEMGRDIAGNOSTICVERIFIER_H
This file defines the StringMap class.
#define LLVM_ABI
Definition Compiler.h:215
bool hasLoc(const Instruction &I)
Definition Debugify.cpp:403
ppc ctr loops verify
This file defines the SmallVector class.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Represents a location in source code.
Definition SMLoc.h:22
MatchResult
The result of matching a single actual diagnostic against the expected diagnostics recorded for its l...
@ Matched
The diagnostic matched an expected diagnostic (kind and text).
@ Unexpected
The diagnostic did not match any expected diagnostic.
@ Ignored
The diagnostic did not match, but reportUnexpected was false, so nothing was printed and nothing need...
@ NearMiss
The diagnostic's text matched an expected diagnostic, but its kind didn't; a message about this has a...
MutableArrayRef< ExpectedDiag > getExpectedUnknownLocDiags()
Returns the expected diagnostics with an '@unknown' location.
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 computeExpec...
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:34
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
bool match(Val *V, const Pattern &P)
This is an optimization pass for GlobalISel generic memory operations.
std::optional< Regex > substringRegex
An optional regex matcher, if the expected diagnostic substring was a regex string.
StringRef substring
The substring that is expected to be within the diagnostic.
ExpectedDiag(SourceMgr::DiagKind kind, unsigned lineNo, SMLoc fileLoc, StringRef substring)
SourceMgr::DiagKind kind
The severity of the diagnostic expected.
SMLoc fileLoc
The location of the expected diagnostic within the input file.
unsigned lineNo
The line number the expected diagnostic should be on.
bool matched
A flag indicating if the expected diagnostic has been matched yet.