1//===- SourceMgrDiagnosticVerifier.cpp -----------------------------------===//
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#include "llvm/Support/SourceMgrDiagnosticVerifier.h"
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/Support/MemoryBuffer.h"
12#include "llvm/Support/raw_ostream.h"
13
14using namespace llvm;
15
16void SourceMgrDiagnosticVerifier::printMessage(raw_ostream &os,
17 const SourceMgr &mgr, SMLoc loc,
18 const Twine &msg,
19 ArrayRef<SMRange> ranges) {
20 mgr.printIncludeStackForDiagnostic(Loc: loc, OS&: os);
21 mgr.GetMessage(Loc: loc, Kind: SourceMgr::DK_Error, Msg: msg, Ranges: ranges).print(ProgName: nullptr, S&: os);
22}
23
24StringRef
25SourceMgrDiagnosticVerifier::getDiagKindStr(SourceMgr::DiagKind kind) {
26 switch (kind) {
27 case SourceMgr::DK_Note:
28 return "note";
29 case SourceMgr::DK_Warning:
30 return "warning";
31 case SourceMgr::DK_Error:
32 return "error";
33 case SourceMgr::DK_Remark:
34 return "remark";
35 }
36 llvm_unreachable("Unknown SourceMgr::DiagKind");
37}
38
39bool SourceMgrDiagnosticVerifier::ExpectedDiag::emitError(
40 raw_ostream &os, SourceMgr &mgr, const Twine &msg) const {
41 if (fileLoc.isValid()) {
42 SMRange range(fileLoc, SMLoc::getFromPointer(Ptr: fileLoc.getPointer() +
43 substring.size()));
44 printMessage(os, mgr, loc: fileLoc, msg, ranges: range);
45 } else {
46 printMessage(os, mgr, loc: fileLoc, msg);
47 }
48 return false;
49}
50
51bool SourceMgrDiagnosticVerifier::ExpectedDiag::match(StringRef str) const {
52 // If this isn't a regex diagnostic, we simply check if the string was
53 // contained.
54 if (substringRegex)
55 return substringRegex->match(String: str);
56 return str.contains(Other: substring);
57}
58
59bool SourceMgrDiagnosticVerifier::ExpectedDiag::computeRegex(raw_ostream &os,
60 SourceMgr &mgr) {
61 std::string regexStr;
62 raw_string_ostream regexOS(regexStr);
63 StringRef strToProcess = substring;
64 while (!strToProcess.empty()) {
65 // Find the next regex block.
66 size_t regexIt = strToProcess.find(Str: "{{");
67 if (regexIt == StringRef::npos) {
68 regexOS << Regex::escape(String: strToProcess);
69 break;
70 }
71 regexOS << Regex::escape(String: strToProcess.take_front(N: regexIt));
72 strToProcess = strToProcess.drop_front(N: regexIt + 2);
73
74 // Find the end of the regex block.
75 size_t regexEndIt = strToProcess.find(Str: "}}");
76 if (regexEndIt == StringRef::npos)
77 return emitError(os, mgr, msg: "found start of regex with no end '}}'");
78 StringRef regexBlock = strToProcess.take_front(N: regexEndIt);
79
80 // Validate that the regex is actually valid.
81 std::string regexError;
82 if (!Regex(regexBlock).isValid(Error&: regexError))
83 return emitError(os, mgr, msg: "invalid regex: " + regexError);
84
85 regexOS << '(' << regexBlock << ')';
86 strToProcess = strToProcess.drop_front(N: regexEndIt + 2);
87 }
88 substringRegex = Regex(regexStr);
89 return true;
90}
91
92static Regex buildExpectedRegex(ArrayRef<StringRef> prefixes) {
93 assert(!prefixes.empty() && "Diagnostic verifier needs at least one prefix");
94 std::string pattern = "(^|[^[:alnum:]_-])(";
95 llvm::interleave(
96 c: prefixes,
97 each_fn: [&](StringRef prefix) {
98 assert(!prefix.empty() && "Diagnostic verifier prefix cannot be empty");
99 pattern += Regex::escape(String: prefix);
100 },
101 between_fn: [&] { pattern += '|'; });
102 pattern += ")-(error|note|remark|warning)(-re)?"
103 "( *@([+-][0-9]+|above|below|unknown))?(:([0-9]+))?"
104 " *{{(.*)}}$";
105 return Regex(pattern);
106}
107
108SourceMgrDiagnosticVerifier::SourceMgrDiagnosticVerifier(
109 ArrayRef<StringRef> prefixes)
110 : expected(buildExpectedRegex(prefixes)) {}
111
112MutableArrayRef<SourceMgrDiagnosticVerifier::ExpectedDiag>
113SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os,
114 SourceMgr &mgr,
115 const MemoryBuffer *buf) {
116 // If the buffer is invalid, return an empty list.
117 if (!buf)
118 return {};
119 auto &expectedDiags = expectedDiagsPerFile[buf->getBufferIdentifier()];
120
121 // The number of the last line that did not correlate to a designator.
122 unsigned lastNonDesignatorLine = 0;
123
124 // The indices of designators that apply to the next non designator line.
125 SmallVector<unsigned, 1> designatorsForNextLine;
126
127 // Scan the file for expected-* designators.
128 SmallVector<StringRef, 100> lines;
129 buf->getBuffer().split(A&: lines, Separator: '\n');
130 for (unsigned lineNo = 0, e = lines.size(); lineNo < e; ++lineNo) {
131 SmallVector<StringRef, 10> matches;
132 if (!expected.match(String: lines[lineNo].rtrim(), Matches: &matches)) {
133 // Check for designators that apply to this line.
134 if (!designatorsForNextLine.empty()) {
135 for (unsigned diagIndex : designatorsForNextLine)
136 expectedDiags[diagIndex].lineNo = lineNo + 1;
137 designatorsForNextLine.clear();
138 }
139 lastNonDesignatorLine = lineNo;
140 continue;
141 }
142
143 // Point to the start of <prefix>-<kind>.
144 SMLoc expectedStart = SMLoc::getFromPointer(Ptr: matches[2].data());
145
146 SourceMgr::DiagKind kind;
147 if (matches[3] == "error")
148 kind = SourceMgr::DK_Error;
149 else if (matches[3] == "warning")
150 kind = SourceMgr::DK_Warning;
151 else if (matches[3] == "remark")
152 kind = SourceMgr::DK_Remark;
153 else {
154 assert(matches[3] == "note");
155 kind = SourceMgr::DK_Note;
156 }
157 ExpectedDiag record(kind, lineNo + 1, expectedStart, matches[9]);
158
159 // Check to see if this is a regex match, i.e. it includes the `-re`.
160 if (!matches[4].empty() && !record.computeRegex(os, mgr)) {
161 ok = false;
162 continue;
163 }
164
165 if (!matches[8].empty() &&
166 (matches[8].getAsInteger(Radix: 10, Result&: record.colNo) || record.colNo == 0)) {
167 record.emitError(os, mgr, msg: "invalid column '" + matches[7] + "'");
168 ok = false;
169 continue;
170 }
171
172 StringRef offsetMatch = matches[6];
173 if (!offsetMatch.empty()) {
174 // Get the integer value without the @ and +/- prefix.
175 if (offsetMatch[0] == '+' || offsetMatch[0] == '-') {
176 int offset;
177 offsetMatch.drop_front().getAsInteger(Radix: 0, Result&: offset);
178
179 if (offsetMatch.front() == '+')
180 record.lineNo += offset;
181 else
182 record.lineNo -= offset;
183 } else if (offsetMatch.consume_front(Prefix: "unknown")) {
184 if (record.colNo != 0) {
185 record.emitError(os, mgr, msg: "cannot specify a column with '@unknown'");
186 ok = false;
187 continue;
188 }
189 // This is matching unknown locations.
190 record.fileLoc = SMLoc();
191 expectedUnknownLocDiags.emplace_back(Args: std::move(record));
192 continue;
193 } else if (offsetMatch.consume_front(Prefix: "above")) {
194 // If the designator applies 'above' we add it to the last non
195 // designator line.
196 record.lineNo = lastNonDesignatorLine + 1;
197 } else {
198 // Otherwise, this is a 'below' designator and applies to the next
199 // non-designator line.
200 assert(offsetMatch.consume_front("below"));
201 designatorsForNextLine.push_back(Elt: expectedDiags.size());
202
203 // Set the line number to the last in the case that this designator
204 // ends up dangling.
205 record.lineNo = e;
206 }
207 }
208 expectedDiags.emplace_back(Args: std::move(record));
209 }
210 return expectedDiags;
211}
212
213std::optional<MutableArrayRef<SourceMgrDiagnosticVerifier::ExpectedDiag>>
214SourceMgrDiagnosticVerifier::getExpectedDiags(StringRef bufName) {
215 auto expectedDiags = expectedDiagsPerFile.find(Key: bufName);
216 if (expectedDiags != expectedDiagsPerFile.end())
217 return MutableArrayRef<ExpectedDiag>(expectedDiags->second);
218 return std::nullopt;
219}
220
221SourceMgrDiagnosticVerifier::MatchResult SourceMgrDiagnosticVerifier::process(
222 raw_ostream &os, SourceMgr &mgr, SourceMgr::DiagKind kind, bool hasLoc,
223 const MemoryBuffer *buf, unsigned lineNo, unsigned colNo, StringRef message,
224 bool reportUnexpected) {
225 MutableArrayRef<ExpectedDiag> diags;
226 if (hasLoc) {
227 // If the buffer couldn't be resolved, `diags` stays empty: a diagnostic
228 // with a location in an unknown file can never match anything.
229 if (buf) {
230 if (auto maybeDiags = getExpectedDiags(bufName: buf->getBufferIdentifier()))
231 diags = *maybeDiags;
232 else
233 diags = computeExpectedDiags(os, mgr, buf);
234 }
235 } else {
236 diags = expectedUnknownLocDiags;
237 }
238
239 // Search for a matching expected diagnostic.
240 // If we find something that is close then emit a more specific error.
241 ExpectedDiag *nearMiss = nullptr;
242
243 // If this was an expected error, remember that we saw it and return.
244 for (auto &e : diags) {
245 // File line must match (unless it's an unknown location).
246 if (hasLoc && e.lineNo != lineNo)
247 continue;
248 if (hasLoc && e.colNo != 0 && e.colNo != colNo)
249 continue;
250 if (e.match(str: message)) {
251 if (e.kind == kind) {
252 e.matched = true;
253 return MatchResult::Matched;
254 }
255
256 // If this only differs based on the diagnostic kind, then consider it
257 // to be a near miss.
258 nearMiss = &e;
259 }
260 }
261
262 if (!reportUnexpected)
263 return MatchResult::Ignored;
264
265 ok = false;
266
267 // Otherwise, emit an error for the near miss.
268 if (nearMiss) {
269 printMessage(os, mgr, loc: nearMiss->fileLoc,
270 msg: "'" + getDiagKindStr(kind) +
271 "' diagnostic emitted when expecting a '" +
272 getDiagKindStr(kind: nearMiss->kind) + "'");
273 return MatchResult::NearMiss;
274 }
275 return MatchResult::Unexpected;
276}
277
278bool SourceMgrDiagnosticVerifier::verify(raw_ostream &os, SourceMgr &mgr) {
279 // Verify that all expected errors were seen.
280 auto checkExpectedDiags = [&](ExpectedDiag &diag) {
281 if (!diag.matched) {
282 diag.emitError(os, mgr,
283 msg: "expected " + getDiagKindStr(kind: diag.kind) + " \"" +
284 diag.substring + "\" was not produced");
285 ok = false;
286 }
287 };
288 for (auto &expectedDiagsPair : expectedDiagsPerFile)
289 for (auto &diag : expectedDiagsPair.second)
290 checkExpectedDiags(diag);
291 for (auto &diag : expectedUnknownLocDiags)
292 checkExpectedDiags(diag);
293 expectedDiagsPerFile.clear();
294 expectedUnknownLocDiags.clear();
295 return ok;
296}
297