/src/llvm-project/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ---------===// |
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 is a concrete diagnostic client, which buffers the diagnostic messages. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Frontend/VerifyDiagnosticConsumer.h" |
14 | | #include "clang/Basic/CharInfo.h" |
15 | | #include "clang/Basic/Diagnostic.h" |
16 | | #include "clang/Basic/DiagnosticOptions.h" |
17 | | #include "clang/Basic/FileManager.h" |
18 | | #include "clang/Basic/LLVM.h" |
19 | | #include "clang/Basic/SourceLocation.h" |
20 | | #include "clang/Basic/SourceManager.h" |
21 | | #include "clang/Basic/TokenKinds.h" |
22 | | #include "clang/Frontend/FrontendDiagnostic.h" |
23 | | #include "clang/Frontend/TextDiagnosticBuffer.h" |
24 | | #include "clang/Lex/HeaderSearch.h" |
25 | | #include "clang/Lex/Lexer.h" |
26 | | #include "clang/Lex/PPCallbacks.h" |
27 | | #include "clang/Lex/Preprocessor.h" |
28 | | #include "clang/Lex/Token.h" |
29 | | #include "llvm/ADT/STLExtras.h" |
30 | | #include "llvm/ADT/SmallPtrSet.h" |
31 | | #include "llvm/ADT/SmallString.h" |
32 | | #include "llvm/ADT/StringRef.h" |
33 | | #include "llvm/ADT/Twine.h" |
34 | | #include "llvm/Support/ErrorHandling.h" |
35 | | #include "llvm/Support/Regex.h" |
36 | | #include "llvm/Support/raw_ostream.h" |
37 | | #include <algorithm> |
38 | | #include <cassert> |
39 | | #include <cstddef> |
40 | | #include <cstring> |
41 | | #include <iterator> |
42 | | #include <memory> |
43 | | #include <string> |
44 | | #include <utility> |
45 | | #include <vector> |
46 | | |
47 | | using namespace clang; |
48 | | |
49 | | using Directive = VerifyDiagnosticConsumer::Directive; |
50 | | using DirectiveList = VerifyDiagnosticConsumer::DirectiveList; |
51 | | using ExpectedData = VerifyDiagnosticConsumer::ExpectedData; |
52 | | |
53 | | #ifndef NDEBUG |
54 | | |
55 | | namespace { |
56 | | |
57 | | class VerifyFileTracker : public PPCallbacks { |
58 | | VerifyDiagnosticConsumer &Verify; |
59 | | SourceManager &SM; |
60 | | |
61 | | public: |
62 | | VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM) |
63 | 0 | : Verify(Verify), SM(SM) {} |
64 | | |
65 | | /// Hook into the preprocessor and update the list of parsed |
66 | | /// files when the preprocessor indicates a new file is entered. |
67 | | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
68 | | SrcMgr::CharacteristicKind FileType, |
69 | 0 | FileID PrevFID) override { |
70 | 0 | Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc), |
71 | 0 | VerifyDiagnosticConsumer::IsParsed); |
72 | 0 | } |
73 | | }; |
74 | | |
75 | | } // namespace |
76 | | |
77 | | #endif |
78 | | |
79 | | //===----------------------------------------------------------------------===// |
80 | | // Checking diagnostics implementation. |
81 | | //===----------------------------------------------------------------------===// |
82 | | |
83 | | using DiagList = TextDiagnosticBuffer::DiagList; |
84 | | using const_diag_iterator = TextDiagnosticBuffer::const_iterator; |
85 | | |
86 | | namespace { |
87 | | |
88 | | /// StandardDirective - Directive with string matching. |
89 | | class StandardDirective : public Directive { |
90 | | public: |
91 | | StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, |
92 | | bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, |
93 | | unsigned Min, unsigned Max) |
94 | | : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyFileAndLine, |
95 | 0 | MatchAnyLine, Text, Min, Max) {} |
96 | | |
97 | 0 | bool isValid(std::string &Error) override { |
98 | | // all strings are considered valid; even empty ones |
99 | 0 | return true; |
100 | 0 | } |
101 | | |
102 | 0 | bool match(StringRef S) override { return S.contains(Text); } |
103 | | }; |
104 | | |
105 | | /// RegexDirective - Directive with regular-expression matching. |
106 | | class RegexDirective : public Directive { |
107 | | public: |
108 | | RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, |
109 | | bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, |
110 | | unsigned Min, unsigned Max, StringRef RegexStr) |
111 | | : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyFileAndLine, |
112 | | MatchAnyLine, Text, Min, Max), |
113 | 0 | Regex(RegexStr) {} |
114 | | |
115 | 0 | bool isValid(std::string &Error) override { |
116 | 0 | return Regex.isValid(Error); |
117 | 0 | } |
118 | | |
119 | 0 | bool match(StringRef S) override { |
120 | 0 | return Regex.match(S); |
121 | 0 | } |
122 | | |
123 | | private: |
124 | | llvm::Regex Regex; |
125 | | }; |
126 | | |
127 | | class ParseHelper |
128 | | { |
129 | | public: |
130 | | ParseHelper(StringRef S) |
131 | 0 | : Begin(S.begin()), End(S.end()), C(Begin), P(Begin) {} |
132 | | |
133 | | // Return true if string literal is next. |
134 | 0 | bool Next(StringRef S) { |
135 | 0 | P = C; |
136 | 0 | PEnd = C + S.size(); |
137 | 0 | if (PEnd > End) |
138 | 0 | return false; |
139 | 0 | return memcmp(P, S.data(), S.size()) == 0; |
140 | 0 | } |
141 | | |
142 | | // Return true if number is next. |
143 | | // Output N only if number is next. |
144 | 0 | bool Next(unsigned &N) { |
145 | 0 | unsigned TMP = 0; |
146 | 0 | P = C; |
147 | 0 | PEnd = P; |
148 | 0 | for (; PEnd < End && *PEnd >= '0' && *PEnd <= '9'; ++PEnd) { |
149 | 0 | TMP *= 10; |
150 | 0 | TMP += *PEnd - '0'; |
151 | 0 | } |
152 | 0 | if (PEnd == C) |
153 | 0 | return false; |
154 | 0 | N = TMP; |
155 | 0 | return true; |
156 | 0 | } |
157 | | |
158 | | // Return true if a marker is next. |
159 | | // A marker is the longest match for /#[A-Za-z0-9_-]+/. |
160 | 0 | bool NextMarker() { |
161 | 0 | P = C; |
162 | 0 | if (P == End || *P != '#') |
163 | 0 | return false; |
164 | 0 | PEnd = P; |
165 | 0 | ++PEnd; |
166 | 0 | while ((isAlphanumeric(*PEnd) || *PEnd == '-' || *PEnd == '_') && |
167 | 0 | PEnd < End) |
168 | 0 | ++PEnd; |
169 | 0 | return PEnd > P + 1; |
170 | 0 | } |
171 | | |
172 | | // Return true if string literal S is matched in content. |
173 | | // When true, P marks begin-position of the match, and calling Advance sets C |
174 | | // to end-position of the match. |
175 | | // If S is the empty string, then search for any letter instead (makes sense |
176 | | // with FinishDirectiveToken=true). |
177 | | // If EnsureStartOfWord, then skip matches that don't start a new word. |
178 | | // If FinishDirectiveToken, then assume the match is the start of a comment |
179 | | // directive for -verify, and extend the match to include the entire first |
180 | | // token of that directive. |
181 | | bool Search(StringRef S, bool EnsureStartOfWord = false, |
182 | 0 | bool FinishDirectiveToken = false) { |
183 | 0 | do { |
184 | 0 | if (!S.empty()) { |
185 | 0 | P = std::search(C, End, S.begin(), S.end()); |
186 | 0 | PEnd = P + S.size(); |
187 | 0 | } |
188 | 0 | else { |
189 | 0 | P = C; |
190 | 0 | while (P != End && !isLetter(*P)) |
191 | 0 | ++P; |
192 | 0 | PEnd = P + 1; |
193 | 0 | } |
194 | 0 | if (P == End) |
195 | 0 | break; |
196 | | // If not start of word but required, skip and search again. |
197 | 0 | if (EnsureStartOfWord |
198 | | // Check if string literal starts a new word. |
199 | 0 | && !(P == Begin || isWhitespace(P[-1]) |
200 | | // Or it could be preceded by the start of a comment. |
201 | 0 | || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*') |
202 | 0 | && P[-2] == '/'))) |
203 | 0 | continue; |
204 | 0 | if (FinishDirectiveToken) { |
205 | 0 | while (PEnd != End && (isAlphanumeric(*PEnd) |
206 | 0 | || *PEnd == '-' || *PEnd == '_')) |
207 | 0 | ++PEnd; |
208 | | // Put back trailing digits and hyphens to be parsed later as a count |
209 | | // or count range. Because -verify prefixes must start with letters, |
210 | | // we know the actual directive we found starts with a letter, so |
211 | | // we won't put back the entire directive word and thus record an empty |
212 | | // string. |
213 | 0 | assert(isLetter(*P) && "-verify prefix must start with a letter"); |
214 | 0 | while (isDigit(PEnd[-1]) || PEnd[-1] == '-') |
215 | 0 | --PEnd; |
216 | 0 | } |
217 | 0 | return true; |
218 | 0 | } while (Advance()); |
219 | 0 | return false; |
220 | 0 | } |
221 | | |
222 | | // Return true if a CloseBrace that closes the OpenBrace at the current nest |
223 | | // level is found. When true, P marks begin-position of CloseBrace. |
224 | 0 | bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) { |
225 | 0 | unsigned Depth = 1; |
226 | 0 | P = C; |
227 | 0 | while (P < End) { |
228 | 0 | StringRef S(P, End - P); |
229 | 0 | if (S.starts_with(OpenBrace)) { |
230 | 0 | ++Depth; |
231 | 0 | P += OpenBrace.size(); |
232 | 0 | } else if (S.starts_with(CloseBrace)) { |
233 | 0 | --Depth; |
234 | 0 | if (Depth == 0) { |
235 | 0 | PEnd = P + CloseBrace.size(); |
236 | 0 | return true; |
237 | 0 | } |
238 | 0 | P += CloseBrace.size(); |
239 | 0 | } else { |
240 | 0 | ++P; |
241 | 0 | } |
242 | 0 | } |
243 | 0 | return false; |
244 | 0 | } |
245 | | |
246 | | // Advance 1-past previous next/search. |
247 | | // Behavior is undefined if previous next/search failed. |
248 | 0 | bool Advance() { |
249 | 0 | C = PEnd; |
250 | 0 | return C < End; |
251 | 0 | } |
252 | | |
253 | | // Return the text matched by the previous next/search. |
254 | | // Behavior is undefined if previous next/search failed. |
255 | 0 | StringRef Match() { return StringRef(P, PEnd - P); } |
256 | | |
257 | | // Skip zero or more whitespace. |
258 | 0 | void SkipWhitespace() { |
259 | 0 | for (; C < End && isWhitespace(*C); ++C) |
260 | 0 | ; |
261 | 0 | } |
262 | | |
263 | | // Return true if EOF reached. |
264 | 0 | bool Done() { |
265 | 0 | return !(C < End); |
266 | 0 | } |
267 | | |
268 | | // Beginning of expected content. |
269 | | const char * const Begin; |
270 | | |
271 | | // End of expected content (1-past). |
272 | | const char * const End; |
273 | | |
274 | | // Position of next char in content. |
275 | | const char *C; |
276 | | |
277 | | // Previous next/search subject start. |
278 | | const char *P; |
279 | | |
280 | | private: |
281 | | // Previous next/search subject end (1-past). |
282 | | const char *PEnd = nullptr; |
283 | | }; |
284 | | |
285 | | // The information necessary to create a directive. |
286 | | struct UnattachedDirective { |
287 | | DirectiveList *DL = nullptr; |
288 | | bool RegexKind = false; |
289 | | SourceLocation DirectivePos, ContentBegin; |
290 | | std::string Text; |
291 | | unsigned Min = 1, Max = 1; |
292 | | }; |
293 | | |
294 | | // Attach the specified directive to the line of code indicated by |
295 | | // \p ExpectedLoc. |
296 | | void attachDirective(DiagnosticsEngine &Diags, const UnattachedDirective &UD, |
297 | | SourceLocation ExpectedLoc, |
298 | | bool MatchAnyFileAndLine = false, |
299 | 0 | bool MatchAnyLine = false) { |
300 | | // Construct new directive. |
301 | 0 | std::unique_ptr<Directive> D = Directive::create( |
302 | 0 | UD.RegexKind, UD.DirectivePos, ExpectedLoc, MatchAnyFileAndLine, |
303 | 0 | MatchAnyLine, UD.Text, UD.Min, UD.Max); |
304 | |
|
305 | 0 | std::string Error; |
306 | 0 | if (!D->isValid(Error)) { |
307 | 0 | Diags.Report(UD.ContentBegin, diag::err_verify_invalid_content) |
308 | 0 | << (UD.RegexKind ? "regex" : "string") << Error; |
309 | 0 | } |
310 | |
|
311 | 0 | UD.DL->push_back(std::move(D)); |
312 | 0 | } |
313 | | |
314 | | } // anonymous |
315 | | |
316 | | // Tracker for markers in the input files. A marker is a comment of the form |
317 | | // |
318 | | // n = 123; // #123 |
319 | | // |
320 | | // ... that can be referred to by a later expected-* directive: |
321 | | // |
322 | | // // expected-error@#123 {{undeclared identifier 'n'}} |
323 | | // |
324 | | // Marker declarations must be at the start of a comment or preceded by |
325 | | // whitespace to distinguish them from uses of markers in directives. |
326 | | class VerifyDiagnosticConsumer::MarkerTracker { |
327 | | DiagnosticsEngine &Diags; |
328 | | |
329 | | struct Marker { |
330 | | SourceLocation DefLoc; |
331 | | SourceLocation RedefLoc; |
332 | | SourceLocation UseLoc; |
333 | | }; |
334 | | llvm::StringMap<Marker> Markers; |
335 | | |
336 | | // Directives that couldn't be created yet because they name an unknown |
337 | | // marker. |
338 | | llvm::StringMap<llvm::SmallVector<UnattachedDirective, 2>> DeferredDirectives; |
339 | | |
340 | | public: |
341 | 0 | MarkerTracker(DiagnosticsEngine &Diags) : Diags(Diags) {} |
342 | | |
343 | | // Register a marker. |
344 | 0 | void addMarker(StringRef MarkerName, SourceLocation Pos) { |
345 | 0 | auto InsertResult = Markers.insert( |
346 | 0 | {MarkerName, Marker{Pos, SourceLocation(), SourceLocation()}}); |
347 | |
|
348 | 0 | Marker &M = InsertResult.first->second; |
349 | 0 | if (!InsertResult.second) { |
350 | | // Marker was redefined. |
351 | 0 | M.RedefLoc = Pos; |
352 | 0 | } else { |
353 | | // First definition: build any deferred directives. |
354 | 0 | auto Deferred = DeferredDirectives.find(MarkerName); |
355 | 0 | if (Deferred != DeferredDirectives.end()) { |
356 | 0 | for (auto &UD : Deferred->second) { |
357 | 0 | if (M.UseLoc.isInvalid()) |
358 | 0 | M.UseLoc = UD.DirectivePos; |
359 | 0 | attachDirective(Diags, UD, Pos); |
360 | 0 | } |
361 | 0 | DeferredDirectives.erase(Deferred); |
362 | 0 | } |
363 | 0 | } |
364 | 0 | } |
365 | | |
366 | | // Register a directive at the specified marker. |
367 | 0 | void addDirective(StringRef MarkerName, const UnattachedDirective &UD) { |
368 | 0 | auto MarkerIt = Markers.find(MarkerName); |
369 | 0 | if (MarkerIt != Markers.end()) { |
370 | 0 | Marker &M = MarkerIt->second; |
371 | 0 | if (M.UseLoc.isInvalid()) |
372 | 0 | M.UseLoc = UD.DirectivePos; |
373 | 0 | return attachDirective(Diags, UD, M.DefLoc); |
374 | 0 | } |
375 | 0 | DeferredDirectives[MarkerName].push_back(UD); |
376 | 0 | } |
377 | | |
378 | | // Ensure we have no remaining deferred directives, and no |
379 | | // multiply-defined-and-used markers. |
380 | 0 | void finalize() { |
381 | 0 | for (auto &MarkerInfo : Markers) { |
382 | 0 | StringRef Name = MarkerInfo.first(); |
383 | 0 | Marker &M = MarkerInfo.second; |
384 | 0 | if (M.RedefLoc.isValid() && M.UseLoc.isValid()) { |
385 | 0 | Diags.Report(M.UseLoc, diag::err_verify_ambiguous_marker) << Name; |
386 | 0 | Diags.Report(M.DefLoc, diag::note_verify_ambiguous_marker) << Name; |
387 | 0 | Diags.Report(M.RedefLoc, diag::note_verify_ambiguous_marker) << Name; |
388 | 0 | } |
389 | 0 | } |
390 | |
|
391 | 0 | for (auto &DeferredPair : DeferredDirectives) { |
392 | 0 | Diags.Report(DeferredPair.second.front().DirectivePos, |
393 | 0 | diag::err_verify_no_such_marker) |
394 | 0 | << DeferredPair.first(); |
395 | 0 | } |
396 | 0 | } |
397 | | }; |
398 | | |
399 | | /// ParseDirective - Go through the comment and see if it indicates expected |
400 | | /// diagnostics. If so, then put them in the appropriate directive list. |
401 | | /// |
402 | | /// Returns true if any valid directives were found. |
403 | | static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM, |
404 | | Preprocessor *PP, SourceLocation Pos, |
405 | | VerifyDiagnosticConsumer::DirectiveStatus &Status, |
406 | 0 | VerifyDiagnosticConsumer::MarkerTracker &Markers) { |
407 | 0 | DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics(); |
408 | | |
409 | | // First, scan the comment looking for markers. |
410 | 0 | for (ParseHelper PH(S); !PH.Done();) { |
411 | 0 | if (!PH.Search("#", true)) |
412 | 0 | break; |
413 | 0 | PH.C = PH.P; |
414 | 0 | if (!PH.NextMarker()) { |
415 | 0 | PH.Next("#"); |
416 | 0 | PH.Advance(); |
417 | 0 | continue; |
418 | 0 | } |
419 | 0 | PH.Advance(); |
420 | 0 | Markers.addMarker(PH.Match(), Pos); |
421 | 0 | } |
422 | | |
423 | | // A single comment may contain multiple directives. |
424 | 0 | bool FoundDirective = false; |
425 | 0 | for (ParseHelper PH(S); !PH.Done();) { |
426 | | // Search for the initial directive token. |
427 | | // If one prefix, save time by searching only for its directives. |
428 | | // Otherwise, search for any potential directive token and check it later. |
429 | 0 | const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes; |
430 | 0 | if (!(Prefixes.size() == 1 ? PH.Search(*Prefixes.begin(), true, true) |
431 | 0 | : PH.Search("", true, true))) |
432 | 0 | break; |
433 | | |
434 | 0 | StringRef DToken = PH.Match(); |
435 | 0 | PH.Advance(); |
436 | | |
437 | | // Default directive kind. |
438 | 0 | UnattachedDirective D; |
439 | 0 | const char *KindStr = "string"; |
440 | | |
441 | | // Parse the initial directive token in reverse so we can easily determine |
442 | | // its exact actual prefix. If we were to parse it from the front instead, |
443 | | // it would be harder to determine where the prefix ends because there |
444 | | // might be multiple matching -verify prefixes because some might prefix |
445 | | // others. |
446 | | |
447 | | // Regex in initial directive token: -re |
448 | 0 | if (DToken.ends_with("-re")) { |
449 | 0 | D.RegexKind = true; |
450 | 0 | KindStr = "regex"; |
451 | 0 | DToken = DToken.substr(0, DToken.size()-3); |
452 | 0 | } |
453 | | |
454 | | // Type in initial directive token: -{error|warning|note|no-diagnostics} |
455 | 0 | bool NoDiag = false; |
456 | 0 | StringRef DType; |
457 | 0 | if (DToken.ends_with(DType = "-error")) |
458 | 0 | D.DL = ED ? &ED->Errors : nullptr; |
459 | 0 | else if (DToken.ends_with(DType = "-warning")) |
460 | 0 | D.DL = ED ? &ED->Warnings : nullptr; |
461 | 0 | else if (DToken.ends_with(DType = "-remark")) |
462 | 0 | D.DL = ED ? &ED->Remarks : nullptr; |
463 | 0 | else if (DToken.ends_with(DType = "-note")) |
464 | 0 | D.DL = ED ? &ED->Notes : nullptr; |
465 | 0 | else if (DToken.ends_with(DType = "-no-diagnostics")) { |
466 | 0 | NoDiag = true; |
467 | 0 | if (D.RegexKind) |
468 | 0 | continue; |
469 | 0 | } else |
470 | 0 | continue; |
471 | 0 | DToken = DToken.substr(0, DToken.size()-DType.size()); |
472 | | |
473 | | // What's left in DToken is the actual prefix. That might not be a -verify |
474 | | // prefix even if there is only one -verify prefix (for example, the full |
475 | | // DToken is foo-bar-warning, but foo is the only -verify prefix). |
476 | 0 | if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken)) |
477 | 0 | continue; |
478 | | |
479 | 0 | if (NoDiag) { |
480 | 0 | if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives) |
481 | 0 | Diags.Report(Pos, diag::err_verify_invalid_no_diags) |
482 | 0 | << /*IsExpectedNoDiagnostics=*/true; |
483 | 0 | else |
484 | 0 | Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics; |
485 | 0 | continue; |
486 | 0 | } |
487 | 0 | if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) { |
488 | 0 | Diags.Report(Pos, diag::err_verify_invalid_no_diags) |
489 | 0 | << /*IsExpectedNoDiagnostics=*/false; |
490 | 0 | continue; |
491 | 0 | } |
492 | 0 | Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives; |
493 | | |
494 | | // If a directive has been found but we're not interested |
495 | | // in storing the directive information, return now. |
496 | 0 | if (!D.DL) |
497 | 0 | return true; |
498 | | |
499 | | // Next optional token: @ |
500 | 0 | SourceLocation ExpectedLoc; |
501 | 0 | StringRef Marker; |
502 | 0 | bool MatchAnyFileAndLine = false; |
503 | 0 | bool MatchAnyLine = false; |
504 | 0 | if (!PH.Next("@")) { |
505 | 0 | ExpectedLoc = Pos; |
506 | 0 | } else { |
507 | 0 | PH.Advance(); |
508 | 0 | unsigned Line = 0; |
509 | 0 | bool FoundPlus = PH.Next("+"); |
510 | 0 | if (FoundPlus || PH.Next("-")) { |
511 | | // Relative to current line. |
512 | 0 | PH.Advance(); |
513 | 0 | bool Invalid = false; |
514 | 0 | unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid); |
515 | 0 | if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) { |
516 | 0 | if (FoundPlus) ExpectedLine += Line; |
517 | 0 | else ExpectedLine -= Line; |
518 | 0 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1); |
519 | 0 | } |
520 | 0 | } else if (PH.Next(Line)) { |
521 | | // Absolute line number. |
522 | 0 | if (Line > 0) |
523 | 0 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1); |
524 | 0 | } else if (PH.NextMarker()) { |
525 | 0 | Marker = PH.Match(); |
526 | 0 | } else if (PP && PH.Search(":")) { |
527 | | // Specific source file. |
528 | 0 | StringRef Filename(PH.C, PH.P-PH.C); |
529 | 0 | PH.Advance(); |
530 | |
|
531 | 0 | if (Filename == "*") { |
532 | 0 | MatchAnyFileAndLine = true; |
533 | 0 | if (!PH.Next("*")) { |
534 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin), |
535 | 0 | diag::err_verify_missing_line) |
536 | 0 | << "'*'"; |
537 | 0 | continue; |
538 | 0 | } |
539 | 0 | MatchAnyLine = true; |
540 | 0 | ExpectedLoc = SourceLocation(); |
541 | 0 | } else { |
542 | | // Lookup file via Preprocessor, like a #include. |
543 | 0 | OptionalFileEntryRef File = |
544 | 0 | PP->LookupFile(Pos, Filename, false, nullptr, nullptr, nullptr, |
545 | 0 | nullptr, nullptr, nullptr, nullptr, nullptr); |
546 | 0 | if (!File) { |
547 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin), |
548 | 0 | diag::err_verify_missing_file) |
549 | 0 | << Filename << KindStr; |
550 | 0 | continue; |
551 | 0 | } |
552 | | |
553 | 0 | FileID FID = SM.translateFile(*File); |
554 | 0 | if (FID.isInvalid()) |
555 | 0 | FID = SM.createFileID(*File, Pos, SrcMgr::C_User); |
556 | |
|
557 | 0 | if (PH.Next(Line) && Line > 0) |
558 | 0 | ExpectedLoc = SM.translateLineCol(FID, Line, 1); |
559 | 0 | else if (PH.Next("*")) { |
560 | 0 | MatchAnyLine = true; |
561 | 0 | ExpectedLoc = SM.translateLineCol(FID, 1, 1); |
562 | 0 | } |
563 | 0 | } |
564 | 0 | } else if (PH.Next("*")) { |
565 | 0 | MatchAnyLine = true; |
566 | 0 | ExpectedLoc = SourceLocation(); |
567 | 0 | } |
568 | | |
569 | 0 | if (ExpectedLoc.isInvalid() && !MatchAnyLine && Marker.empty()) { |
570 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
571 | 0 | diag::err_verify_missing_line) << KindStr; |
572 | 0 | continue; |
573 | 0 | } |
574 | 0 | PH.Advance(); |
575 | 0 | } |
576 | | |
577 | | // Skip optional whitespace. |
578 | 0 | PH.SkipWhitespace(); |
579 | | |
580 | | // Next optional token: positive integer or a '+'. |
581 | 0 | if (PH.Next(D.Min)) { |
582 | 0 | PH.Advance(); |
583 | | // A positive integer can be followed by a '+' meaning min |
584 | | // or more, or by a '-' meaning a range from min to max. |
585 | 0 | if (PH.Next("+")) { |
586 | 0 | D.Max = Directive::MaxCount; |
587 | 0 | PH.Advance(); |
588 | 0 | } else if (PH.Next("-")) { |
589 | 0 | PH.Advance(); |
590 | 0 | if (!PH.Next(D.Max) || D.Max < D.Min) { |
591 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
592 | 0 | diag::err_verify_invalid_range) << KindStr; |
593 | 0 | continue; |
594 | 0 | } |
595 | 0 | PH.Advance(); |
596 | 0 | } else { |
597 | 0 | D.Max = D.Min; |
598 | 0 | } |
599 | 0 | } else if (PH.Next("+")) { |
600 | | // '+' on its own means "1 or more". |
601 | 0 | D.Max = Directive::MaxCount; |
602 | 0 | PH.Advance(); |
603 | 0 | } |
604 | | |
605 | | // Skip optional whitespace. |
606 | 0 | PH.SkipWhitespace(); |
607 | | |
608 | | // Next token: {{ |
609 | 0 | if (!PH.Next("{{")) { |
610 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
611 | 0 | diag::err_verify_missing_start) << KindStr; |
612 | 0 | continue; |
613 | 0 | } |
614 | 0 | llvm::SmallString<8> CloseBrace("}}"); |
615 | 0 | const char *const DelimBegin = PH.C; |
616 | 0 | PH.Advance(); |
617 | | // Count the number of opening braces for `string` kinds |
618 | 0 | for (; !D.RegexKind && PH.Next("{"); PH.Advance()) |
619 | 0 | CloseBrace += '}'; |
620 | 0 | const char* const ContentBegin = PH.C; // mark content begin |
621 | | // Search for closing brace |
622 | 0 | StringRef OpenBrace(DelimBegin, ContentBegin - DelimBegin); |
623 | 0 | if (!PH.SearchClosingBrace(OpenBrace, CloseBrace)) { |
624 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin), |
625 | 0 | diag::err_verify_missing_end) |
626 | 0 | << KindStr << CloseBrace; |
627 | 0 | continue; |
628 | 0 | } |
629 | 0 | const char* const ContentEnd = PH.P; // mark content end |
630 | 0 | PH.Advance(); |
631 | |
|
632 | 0 | D.DirectivePos = Pos; |
633 | 0 | D.ContentBegin = Pos.getLocWithOffset(ContentBegin - PH.Begin); |
634 | | |
635 | | // Build directive text; convert \n to newlines. |
636 | 0 | StringRef NewlineStr = "\\n"; |
637 | 0 | StringRef Content(ContentBegin, ContentEnd-ContentBegin); |
638 | 0 | size_t CPos = 0; |
639 | 0 | size_t FPos; |
640 | 0 | while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) { |
641 | 0 | D.Text += Content.substr(CPos, FPos-CPos); |
642 | 0 | D.Text += '\n'; |
643 | 0 | CPos = FPos + NewlineStr.size(); |
644 | 0 | } |
645 | 0 | if (D.Text.empty()) |
646 | 0 | D.Text.assign(ContentBegin, ContentEnd); |
647 | | |
648 | | // Check that regex directives contain at least one regex. |
649 | 0 | if (D.RegexKind && D.Text.find("{{") == StringRef::npos) { |
650 | 0 | Diags.Report(D.ContentBegin, diag::err_verify_missing_regex) << D.Text; |
651 | 0 | return false; |
652 | 0 | } |
653 | | |
654 | 0 | if (Marker.empty()) |
655 | 0 | attachDirective(Diags, D, ExpectedLoc, MatchAnyFileAndLine, MatchAnyLine); |
656 | 0 | else |
657 | 0 | Markers.addDirective(Marker, D); |
658 | 0 | FoundDirective = true; |
659 | 0 | } |
660 | | |
661 | 0 | return FoundDirective; |
662 | 0 | } |
663 | | |
664 | | VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_) |
665 | | : Diags(Diags_), PrimaryClient(Diags.getClient()), |
666 | | PrimaryClientOwner(Diags.takeClient()), |
667 | | Buffer(new TextDiagnosticBuffer()), Markers(new MarkerTracker(Diags)), |
668 | 0 | Status(HasNoDirectives) { |
669 | 0 | if (Diags.hasSourceManager()) |
670 | 0 | setSourceManager(Diags.getSourceManager()); |
671 | 0 | } |
672 | | |
673 | 0 | VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { |
674 | 0 | assert(!ActiveSourceFiles && "Incomplete parsing of source files!"); |
675 | 0 | assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!"); |
676 | 0 | SrcManager = nullptr; |
677 | 0 | CheckDiagnostics(); |
678 | 0 | assert(!Diags.ownsClient() && |
679 | 0 | "The VerifyDiagnosticConsumer takes over ownership of the client!"); |
680 | 0 | } |
681 | | |
682 | | // DiagnosticConsumer interface. |
683 | | |
684 | | void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, |
685 | 0 | const Preprocessor *PP) { |
686 | | // Attach comment handler on first invocation. |
687 | 0 | if (++ActiveSourceFiles == 1) { |
688 | 0 | if (PP) { |
689 | 0 | CurrentPreprocessor = PP; |
690 | 0 | this->LangOpts = &LangOpts; |
691 | 0 | setSourceManager(PP->getSourceManager()); |
692 | 0 | const_cast<Preprocessor *>(PP)->addCommentHandler(this); |
693 | 0 | #ifndef NDEBUG |
694 | | // Debug build tracks parsed files. |
695 | 0 | const_cast<Preprocessor *>(PP)->addPPCallbacks( |
696 | 0 | std::make_unique<VerifyFileTracker>(*this, *SrcManager)); |
697 | 0 | #endif |
698 | 0 | } |
699 | 0 | } |
700 | |
|
701 | 0 | assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!"); |
702 | 0 | PrimaryClient->BeginSourceFile(LangOpts, PP); |
703 | 0 | } |
704 | | |
705 | 0 | void VerifyDiagnosticConsumer::EndSourceFile() { |
706 | 0 | assert(ActiveSourceFiles && "No active source files!"); |
707 | 0 | PrimaryClient->EndSourceFile(); |
708 | | |
709 | | // Detach comment handler once last active source file completed. |
710 | 0 | if (--ActiveSourceFiles == 0) { |
711 | 0 | if (CurrentPreprocessor) |
712 | 0 | const_cast<Preprocessor *>(CurrentPreprocessor)-> |
713 | 0 | removeCommentHandler(this); |
714 | | |
715 | | // Diagnose any used-but-not-defined markers. |
716 | 0 | Markers->finalize(); |
717 | | |
718 | | // Check diagnostics once last file completed. |
719 | 0 | CheckDiagnostics(); |
720 | 0 | CurrentPreprocessor = nullptr; |
721 | 0 | LangOpts = nullptr; |
722 | 0 | } |
723 | 0 | } |
724 | | |
725 | | void VerifyDiagnosticConsumer::HandleDiagnostic( |
726 | 0 | DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) { |
727 | 0 | if (Info.hasSourceManager()) { |
728 | | // If this diagnostic is for a different source manager, ignore it. |
729 | 0 | if (SrcManager && &Info.getSourceManager() != SrcManager) |
730 | 0 | return; |
731 | | |
732 | 0 | setSourceManager(Info.getSourceManager()); |
733 | 0 | } |
734 | | |
735 | 0 | #ifndef NDEBUG |
736 | | // Debug build tracks unparsed files for possible |
737 | | // unparsed expected-* directives. |
738 | 0 | if (SrcManager) { |
739 | 0 | SourceLocation Loc = Info.getLocation(); |
740 | 0 | if (Loc.isValid()) { |
741 | 0 | ParsedStatus PS = IsUnparsed; |
742 | |
|
743 | 0 | Loc = SrcManager->getExpansionLoc(Loc); |
744 | 0 | FileID FID = SrcManager->getFileID(Loc); |
745 | |
|
746 | 0 | auto FE = SrcManager->getFileEntryRefForID(FID); |
747 | 0 | if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) { |
748 | | // If the file is a modules header file it shall not be parsed |
749 | | // for expected-* directives. |
750 | 0 | HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo(); |
751 | 0 | if (HS.findModuleForHeader(*FE)) |
752 | 0 | PS = IsUnparsedNoDirectives; |
753 | 0 | } |
754 | |
|
755 | 0 | UpdateParsedFileStatus(*SrcManager, FID, PS); |
756 | 0 | } |
757 | 0 | } |
758 | 0 | #endif |
759 | | |
760 | | // Send the diagnostic to the buffer, we will check it once we reach the end |
761 | | // of the source file (or are destructed). |
762 | 0 | Buffer->HandleDiagnostic(DiagLevel, Info); |
763 | 0 | } |
764 | | |
765 | | /// HandleComment - Hook into the preprocessor and extract comments containing |
766 | | /// expected errors and warnings. |
767 | | bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP, |
768 | 0 | SourceRange Comment) { |
769 | 0 | SourceManager &SM = PP.getSourceManager(); |
770 | | |
771 | | // If this comment is for a different source manager, ignore it. |
772 | 0 | if (SrcManager && &SM != SrcManager) |
773 | 0 | return false; |
774 | | |
775 | 0 | SourceLocation CommentBegin = Comment.getBegin(); |
776 | |
|
777 | 0 | const char *CommentRaw = SM.getCharacterData(CommentBegin); |
778 | 0 | StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw); |
779 | |
|
780 | 0 | if (C.empty()) |
781 | 0 | return false; |
782 | | |
783 | | // Fold any "\<EOL>" sequences |
784 | 0 | size_t loc = C.find('\\'); |
785 | 0 | if (loc == StringRef::npos) { |
786 | 0 | ParseDirective(C, &ED, SM, &PP, CommentBegin, Status, *Markers); |
787 | 0 | return false; |
788 | 0 | } |
789 | | |
790 | 0 | std::string C2; |
791 | 0 | C2.reserve(C.size()); |
792 | |
|
793 | 0 | for (size_t last = 0;; loc = C.find('\\', last)) { |
794 | 0 | if (loc == StringRef::npos || loc == C.size()) { |
795 | 0 | C2 += C.substr(last); |
796 | 0 | break; |
797 | 0 | } |
798 | 0 | C2 += C.substr(last, loc-last); |
799 | 0 | last = loc + 1; |
800 | |
|
801 | 0 | if (C[last] == '\n' || C[last] == '\r') { |
802 | 0 | ++last; |
803 | | |
804 | | // Escape \r\n or \n\r, but not \n\n. |
805 | 0 | if (last < C.size()) |
806 | 0 | if (C[last] == '\n' || C[last] == '\r') |
807 | 0 | if (C[last] != C[last-1]) |
808 | 0 | ++last; |
809 | 0 | } else { |
810 | | // This was just a normal backslash. |
811 | 0 | C2 += '\\'; |
812 | 0 | } |
813 | 0 | } |
814 | |
|
815 | 0 | if (!C2.empty()) |
816 | 0 | ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status, *Markers); |
817 | 0 | return false; |
818 | 0 | } |
819 | | |
820 | | #ifndef NDEBUG |
821 | | /// Lex the specified source file to determine whether it contains |
822 | | /// any expected-* directives. As a Lexer is used rather than a full-blown |
823 | | /// Preprocessor, directives inside skipped #if blocks will still be found. |
824 | | /// |
825 | | /// \return true if any directives were found. |
826 | | static bool findDirectives(SourceManager &SM, FileID FID, |
827 | 0 | const LangOptions &LangOpts) { |
828 | | // Create a raw lexer to pull all the comments out of FID. |
829 | 0 | if (FID.isInvalid()) |
830 | 0 | return false; |
831 | | |
832 | | // Create a lexer to lex all the tokens of the main file in raw mode. |
833 | 0 | llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID); |
834 | 0 | Lexer RawLex(FID, FromFile, SM, LangOpts); |
835 | | |
836 | | // Return comments as tokens, this is how we find expected diagnostics. |
837 | 0 | RawLex.SetCommentRetentionState(true); |
838 | |
|
839 | 0 | Token Tok; |
840 | 0 | Tok.setKind(tok::comment); |
841 | 0 | VerifyDiagnosticConsumer::DirectiveStatus Status = |
842 | 0 | VerifyDiagnosticConsumer::HasNoDirectives; |
843 | 0 | while (Tok.isNot(tok::eof)) { |
844 | 0 | RawLex.LexFromRawLexer(Tok); |
845 | 0 | if (!Tok.is(tok::comment)) continue; |
846 | | |
847 | 0 | std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts); |
848 | 0 | if (Comment.empty()) continue; |
849 | | |
850 | | // We don't care about tracking markers for this phase. |
851 | 0 | VerifyDiagnosticConsumer::MarkerTracker Markers(SM.getDiagnostics()); |
852 | | |
853 | | // Find first directive. |
854 | 0 | if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(), |
855 | 0 | Status, Markers)) |
856 | 0 | return true; |
857 | 0 | } |
858 | 0 | return false; |
859 | 0 | } |
860 | | #endif // !NDEBUG |
861 | | |
862 | | /// Takes a list of diagnostics that have been generated but not matched |
863 | | /// by an expected-* directive and produces a diagnostic to the user from this. |
864 | | static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr, |
865 | | const_diag_iterator diag_begin, |
866 | | const_diag_iterator diag_end, |
867 | 0 | const char *Kind) { |
868 | 0 | if (diag_begin == diag_end) return 0; |
869 | | |
870 | 0 | SmallString<256> Fmt; |
871 | 0 | llvm::raw_svector_ostream OS(Fmt); |
872 | 0 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) { |
873 | 0 | if (I->first.isInvalid() || !SourceMgr) |
874 | 0 | OS << "\n (frontend)"; |
875 | 0 | else { |
876 | 0 | OS << "\n "; |
877 | 0 | if (OptionalFileEntryRef File = |
878 | 0 | SourceMgr->getFileEntryRefForID(SourceMgr->getFileID(I->first))) |
879 | 0 | OS << " File " << File->getName(); |
880 | 0 | OS << " Line " << SourceMgr->getPresumedLineNumber(I->first); |
881 | 0 | } |
882 | 0 | OS << ": " << I->second; |
883 | 0 | } |
884 | |
|
885 | 0 | std::string Prefix = *Diags.getDiagnosticOptions().VerifyPrefixes.begin(); |
886 | 0 | std::string KindStr = Prefix + "-" + Kind; |
887 | 0 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
888 | 0 | << KindStr << /*Unexpected=*/true << OS.str(); |
889 | 0 | return std::distance(diag_begin, diag_end); |
890 | 0 | } |
891 | | |
892 | | /// Takes a list of diagnostics that were expected to have been generated |
893 | | /// but were not and produces a diagnostic to the user from this. |
894 | | static unsigned PrintExpected(DiagnosticsEngine &Diags, |
895 | | SourceManager &SourceMgr, |
896 | 0 | std::vector<Directive *> &DL, const char *Kind) { |
897 | 0 | if (DL.empty()) |
898 | 0 | return 0; |
899 | | |
900 | 0 | SmallString<256> Fmt; |
901 | 0 | llvm::raw_svector_ostream OS(Fmt); |
902 | 0 | for (const auto *D : DL) { |
903 | 0 | if (D->DiagnosticLoc.isInvalid() || D->MatchAnyFileAndLine) |
904 | 0 | OS << "\n File *"; |
905 | 0 | else |
906 | 0 | OS << "\n File " << SourceMgr.getFilename(D->DiagnosticLoc); |
907 | 0 | if (D->MatchAnyLine) |
908 | 0 | OS << " Line *"; |
909 | 0 | else |
910 | 0 | OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc); |
911 | 0 | if (D->DirectiveLoc != D->DiagnosticLoc) |
912 | 0 | OS << " (directive at " |
913 | 0 | << SourceMgr.getFilename(D->DirectiveLoc) << ':' |
914 | 0 | << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')'; |
915 | 0 | OS << ": " << D->Text; |
916 | 0 | } |
917 | |
|
918 | 0 | std::string Prefix = *Diags.getDiagnosticOptions().VerifyPrefixes.begin(); |
919 | 0 | std::string KindStr = Prefix + "-" + Kind; |
920 | 0 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
921 | 0 | << KindStr << /*Unexpected=*/false << OS.str(); |
922 | 0 | return DL.size(); |
923 | 0 | } |
924 | | |
925 | | /// Determine whether two source locations come from the same file. |
926 | | static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc, |
927 | 0 | SourceLocation DiagnosticLoc) { |
928 | 0 | while (DiagnosticLoc.isMacroID()) |
929 | 0 | DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc); |
930 | |
|
931 | 0 | if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc)) |
932 | 0 | return true; |
933 | | |
934 | 0 | const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc)); |
935 | 0 | if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc)) |
936 | 0 | return true; |
937 | | |
938 | 0 | return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc))); |
939 | 0 | } |
940 | | |
941 | | /// CheckLists - Compare expected to seen diagnostic lists and return the |
942 | | /// the difference between them. |
943 | | static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
944 | | const char *Label, |
945 | | DirectiveList &Left, |
946 | | const_diag_iterator d2_begin, |
947 | | const_diag_iterator d2_end, |
948 | 0 | bool IgnoreUnexpected) { |
949 | 0 | std::vector<Directive *> LeftOnly; |
950 | 0 | DiagList Right(d2_begin, d2_end); |
951 | |
|
952 | 0 | for (auto &Owner : Left) { |
953 | 0 | Directive &D = *Owner; |
954 | 0 | unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); |
955 | |
|
956 | 0 | for (unsigned i = 0; i < D.Max; ++i) { |
957 | 0 | DiagList::iterator II, IE; |
958 | 0 | for (II = Right.begin(), IE = Right.end(); II != IE; ++II) { |
959 | 0 | if (!D.MatchAnyLine) { |
960 | 0 | unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first); |
961 | 0 | if (LineNo1 != LineNo2) |
962 | 0 | continue; |
963 | 0 | } |
964 | | |
965 | 0 | if (!D.DiagnosticLoc.isInvalid() && !D.MatchAnyFileAndLine && |
966 | 0 | !IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first)) |
967 | 0 | continue; |
968 | | |
969 | 0 | const std::string &RightText = II->second; |
970 | 0 | if (D.match(RightText)) |
971 | 0 | break; |
972 | 0 | } |
973 | 0 | if (II == IE) { |
974 | | // Not found. |
975 | 0 | if (i >= D.Min) break; |
976 | 0 | LeftOnly.push_back(&D); |
977 | 0 | } else { |
978 | | // Found. The same cannot be found twice. |
979 | 0 | Right.erase(II); |
980 | 0 | } |
981 | 0 | } |
982 | 0 | } |
983 | | // Now all that's left in Right are those that were not matched. |
984 | 0 | unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label); |
985 | 0 | if (!IgnoreUnexpected) |
986 | 0 | num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label); |
987 | 0 | return num; |
988 | 0 | } |
989 | | |
990 | | /// CheckResults - This compares the expected results to those that |
991 | | /// were actually reported. It emits any discrepencies. Return "true" if there |
992 | | /// were problems. Return "false" otherwise. |
993 | | static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
994 | | const TextDiagnosticBuffer &Buffer, |
995 | 0 | ExpectedData &ED) { |
996 | | // We want to capture the delta between what was expected and what was |
997 | | // seen. |
998 | | // |
999 | | // Expected \ Seen - set expected but not seen |
1000 | | // Seen \ Expected - set seen but not expected |
1001 | 0 | unsigned NumProblems = 0; |
1002 | |
|
1003 | 0 | const DiagnosticLevelMask DiagMask = |
1004 | 0 | Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected(); |
1005 | | |
1006 | | // See if there are error mismatches. |
1007 | 0 | NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, |
1008 | 0 | Buffer.err_begin(), Buffer.err_end(), |
1009 | 0 | bool(DiagnosticLevelMask::Error & DiagMask)); |
1010 | | |
1011 | | // See if there are warning mismatches. |
1012 | 0 | NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, |
1013 | 0 | Buffer.warn_begin(), Buffer.warn_end(), |
1014 | 0 | bool(DiagnosticLevelMask::Warning & DiagMask)); |
1015 | | |
1016 | | // See if there are remark mismatches. |
1017 | 0 | NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks, |
1018 | 0 | Buffer.remark_begin(), Buffer.remark_end(), |
1019 | 0 | bool(DiagnosticLevelMask::Remark & DiagMask)); |
1020 | | |
1021 | | // See if there are note mismatches. |
1022 | 0 | NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, |
1023 | 0 | Buffer.note_begin(), Buffer.note_end(), |
1024 | 0 | bool(DiagnosticLevelMask::Note & DiagMask)); |
1025 | |
|
1026 | 0 | return NumProblems; |
1027 | 0 | } |
1028 | | |
1029 | | void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM, |
1030 | | FileID FID, |
1031 | 0 | ParsedStatus PS) { |
1032 | | // Check SourceManager hasn't changed. |
1033 | 0 | setSourceManager(SM); |
1034 | |
|
1035 | 0 | #ifndef NDEBUG |
1036 | 0 | if (FID.isInvalid()) |
1037 | 0 | return; |
1038 | | |
1039 | 0 | OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID); |
1040 | |
|
1041 | 0 | if (PS == IsParsed) { |
1042 | | // Move the FileID from the unparsed set to the parsed set. |
1043 | 0 | UnparsedFiles.erase(FID); |
1044 | 0 | ParsedFiles.insert(std::make_pair(FID, FE ? &FE->getFileEntry() : nullptr)); |
1045 | 0 | } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) { |
1046 | | // Add the FileID to the unparsed set if we haven't seen it before. |
1047 | | |
1048 | | // Check for directives. |
1049 | 0 | bool FoundDirectives; |
1050 | 0 | if (PS == IsUnparsedNoDirectives) |
1051 | 0 | FoundDirectives = false; |
1052 | 0 | else |
1053 | 0 | FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts); |
1054 | | |
1055 | | // Add the FileID to the unparsed set. |
1056 | 0 | UnparsedFiles.insert(std::make_pair(FID, |
1057 | 0 | UnparsedFileStatus(FE, FoundDirectives))); |
1058 | 0 | } |
1059 | 0 | #endif |
1060 | 0 | } |
1061 | | |
1062 | 0 | void VerifyDiagnosticConsumer::CheckDiagnostics() { |
1063 | | // Ensure any diagnostics go to the primary client. |
1064 | 0 | DiagnosticConsumer *CurClient = Diags.getClient(); |
1065 | 0 | std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient(); |
1066 | 0 | Diags.setClient(PrimaryClient, false); |
1067 | |
|
1068 | 0 | #ifndef NDEBUG |
1069 | | // In a debug build, scan through any files that may have been missed |
1070 | | // during parsing and issue a fatal error if directives are contained |
1071 | | // within these files. If a fatal error occurs, this suggests that |
1072 | | // this file is being parsed separately from the main file, in which |
1073 | | // case consider moving the directives to the correct place, if this |
1074 | | // is applicable. |
1075 | 0 | if (!UnparsedFiles.empty()) { |
1076 | | // Generate a cache of parsed FileEntry pointers for alias lookups. |
1077 | 0 | llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache; |
1078 | 0 | for (const auto &I : ParsedFiles) |
1079 | 0 | if (const FileEntry *FE = I.second) |
1080 | 0 | ParsedFileCache.insert(FE); |
1081 | | |
1082 | | // Iterate through list of unparsed files. |
1083 | 0 | for (const auto &I : UnparsedFiles) { |
1084 | 0 | const UnparsedFileStatus &Status = I.second; |
1085 | 0 | OptionalFileEntryRef FE = Status.getFile(); |
1086 | | |
1087 | | // Skip files that have been parsed via an alias. |
1088 | 0 | if (FE && ParsedFileCache.count(*FE)) |
1089 | 0 | continue; |
1090 | | |
1091 | | // Report a fatal error if this file contained directives. |
1092 | 0 | if (Status.foundDirectives()) { |
1093 | 0 | llvm::report_fatal_error("-verify directives found after rather" |
1094 | 0 | " than during normal parsing of " + |
1095 | 0 | (FE ? FE->getName() : "(unknown)")); |
1096 | 0 | } |
1097 | 0 | } |
1098 | | |
1099 | | // UnparsedFiles has been processed now, so clear it. |
1100 | 0 | UnparsedFiles.clear(); |
1101 | 0 | } |
1102 | 0 | #endif // !NDEBUG |
1103 | |
|
1104 | 0 | if (SrcManager) { |
1105 | | // Produce an error if no expected-* directives could be found in the |
1106 | | // source file(s) processed. |
1107 | 0 | if (Status == HasNoDirectives) { |
1108 | 0 | Diags.Report(diag::err_verify_no_directives).setForceEmit(); |
1109 | 0 | ++NumErrors; |
1110 | 0 | Status = HasNoDirectivesReported; |
1111 | 0 | } |
1112 | | |
1113 | | // Check that the expected diagnostics occurred. |
1114 | 0 | NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED); |
1115 | 0 | } else { |
1116 | 0 | const DiagnosticLevelMask DiagMask = |
1117 | 0 | ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected(); |
1118 | 0 | if (bool(DiagnosticLevelMask::Error & DiagMask)) |
1119 | 0 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(), |
1120 | 0 | Buffer->err_end(), "error"); |
1121 | 0 | if (bool(DiagnosticLevelMask::Warning & DiagMask)) |
1122 | 0 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(), |
1123 | 0 | Buffer->warn_end(), "warn"); |
1124 | 0 | if (bool(DiagnosticLevelMask::Remark & DiagMask)) |
1125 | 0 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(), |
1126 | 0 | Buffer->remark_end(), "remark"); |
1127 | 0 | if (bool(DiagnosticLevelMask::Note & DiagMask)) |
1128 | 0 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(), |
1129 | 0 | Buffer->note_end(), "note"); |
1130 | 0 | } |
1131 | |
|
1132 | 0 | Diags.setClient(CurClient, Owner.release() != nullptr); |
1133 | | |
1134 | | // Reset the buffer, we have processed all the diagnostics in it. |
1135 | 0 | Buffer.reset(new TextDiagnosticBuffer()); |
1136 | 0 | ED.Reset(); |
1137 | 0 | } |
1138 | | |
1139 | | std::unique_ptr<Directive> Directive::create(bool RegexKind, |
1140 | | SourceLocation DirectiveLoc, |
1141 | | SourceLocation DiagnosticLoc, |
1142 | | bool MatchAnyFileAndLine, |
1143 | | bool MatchAnyLine, StringRef Text, |
1144 | 0 | unsigned Min, unsigned Max) { |
1145 | 0 | if (!RegexKind) |
1146 | 0 | return std::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc, |
1147 | 0 | MatchAnyFileAndLine, |
1148 | 0 | MatchAnyLine, Text, Min, Max); |
1149 | | |
1150 | | // Parse the directive into a regular expression. |
1151 | 0 | std::string RegexStr; |
1152 | 0 | StringRef S = Text; |
1153 | 0 | while (!S.empty()) { |
1154 | 0 | if (S.consume_front("{{")) { |
1155 | 0 | size_t RegexMatchLength = S.find("}}"); |
1156 | 0 | assert(RegexMatchLength != StringRef::npos); |
1157 | | // Append the regex, enclosed in parentheses. |
1158 | 0 | RegexStr += "("; |
1159 | 0 | RegexStr.append(S.data(), RegexMatchLength); |
1160 | 0 | RegexStr += ")"; |
1161 | 0 | S = S.drop_front(RegexMatchLength + 2); |
1162 | 0 | } else { |
1163 | 0 | size_t VerbatimMatchLength = S.find("{{"); |
1164 | 0 | if (VerbatimMatchLength == StringRef::npos) |
1165 | 0 | VerbatimMatchLength = S.size(); |
1166 | | // Escape and append the fixed string. |
1167 | 0 | RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength)); |
1168 | 0 | S = S.drop_front(VerbatimMatchLength); |
1169 | 0 | } |
1170 | 0 | } |
1171 | |
|
1172 | 0 | return std::make_unique<RegexDirective>(DirectiveLoc, DiagnosticLoc, |
1173 | 0 | MatchAnyFileAndLine, MatchAnyLine, |
1174 | 0 | Text, Min, Max, RegexStr); |
1175 | 0 | } |