/Users/buildslave/jenkins/workspace/coverage/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 | 18.0k | : 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 | 108k | FileID PrevFID) override { |
70 | 108k | Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc), |
71 | 108k | VerifyDiagnosticConsumer::IsParsed); |
72 | 108k | } |
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 | 341k | MatchAnyLine, Text, Min, Max) {} |
96 | | |
97 | 341k | bool isValid(std::string &Error) override { |
98 | | // all strings are considered valid; even empty ones |
99 | 341k | return true; |
100 | 341k | } |
101 | | |
102 | 458k | 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 | 4.56k | Regex(RegexStr) {} |
114 | | |
115 | 4.56k | bool isValid(std::string &Error) override { |
116 | 4.56k | return Regex.isValid(Error); |
117 | 4.56k | } |
118 | | |
119 | 4.71k | bool match(StringRef S) override { |
120 | 4.71k | return Regex.match(S); |
121 | 4.71k | } |
122 | | |
123 | | private: |
124 | | llvm::Regex Regex; |
125 | | }; |
126 | | |
127 | | class ParseHelper |
128 | | { |
129 | | public: |
130 | | ParseHelper(StringRef S) |
131 | 22.1M | : Begin(S.begin()), End(S.end()), C(Begin), P(Begin) {} |
132 | | |
133 | | // Return true if string literal is next. |
134 | 1.44M | bool Next(StringRef S) { |
135 | 1.44M | P = C; |
136 | 1.44M | PEnd = C + S.size(); |
137 | 1.44M | if (PEnd > End) |
138 | 0 | return false; |
139 | 1.44M | return memcmp(P, S.data(), S.size()) == 0; |
140 | 1.44M | } |
141 | | |
142 | | // Return true if number is next. |
143 | | // Output N only if number is next. |
144 | 416k | bool Next(unsigned &N) { |
145 | 416k | unsigned TMP = 0; |
146 | 416k | P = C; |
147 | 416k | PEnd = P; |
148 | 526k | for (; PEnd < End && *PEnd >= '0' && *PEnd <= '9'427k ; ++PEnd110k ) { |
149 | 110k | TMP *= 10; |
150 | 110k | TMP += *PEnd - '0'; |
151 | 110k | } |
152 | 416k | if (PEnd == C) |
153 | 308k | return false; |
154 | 108k | N = TMP; |
155 | 108k | return true; |
156 | 416k | } |
157 | | |
158 | | // Return true if a marker is next. |
159 | | // A marker is the longest match for /#[A-Za-z0-9_-]+/. |
160 | 292k | bool NextMarker() { |
161 | 292k | P = C; |
162 | 292k | if (P == End || *P != '#') |
163 | 1.88k | return false; |
164 | 290k | PEnd = P; |
165 | 290k | ++PEnd; |
166 | 350k | while ((isAlphanumeric(*PEnd) || *PEnd == '-'290k || *PEnd == '_'290k ) && |
167 | 350k | PEnd < End60.2k ) |
168 | 60.2k | ++PEnd; |
169 | 290k | return PEnd > P + 1; |
170 | 292k | } |
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 | 22.7M | bool FinishDirectiveToken = false) { |
183 | 23.8M | do { |
184 | 23.8M | if (!S.empty()) { |
185 | 22.3M | P = std::search(C, End, S.begin(), S.end()); |
186 | 22.3M | PEnd = P + S.size(); |
187 | 22.3M | } |
188 | 1.50M | else { |
189 | 1.50M | P = C; |
190 | 2.89M | while (P != End && !isLetter(*P)2.87M ) |
191 | 1.38M | ++P; |
192 | 1.50M | PEnd = P + 1; |
193 | 1.50M | } |
194 | 23.8M | if (P == End) |
195 | 21.8M | break; |
196 | | // If not start of word but required, skip and search again. |
197 | 2.00M | if (EnsureStartOfWord |
198 | | // Check if string literal starts a new word. |
199 | 2.00M | && !(2.00M P == Begin2.00M || isWhitespace(P[-1])2.00M |
200 | | // Or it could be preceded by the start of a comment. |
201 | 2.00M | || (1.15M P > (Begin + 1)1.15M && (1.15M P[-1] == '/'1.15M || P[-1] == '*'1.14M ) |
202 | 1.15M | && P[-2] == '/'5.50k ))) |
203 | 1.14M | continue; |
204 | 854k | if (FinishDirectiveToken) { |
205 | 4.77M | while (PEnd != End && (4.75M isAlphanumeric(*PEnd)4.75M |
206 | 4.75M | || *PEnd == '-'937k || *PEnd == '_'549k )) |
207 | 4.21M | ++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 | 563k | assert(isLetter(*P) && "-verify prefix must start with a letter"); |
214 | 572k | while (isDigit(PEnd[-1]) || PEnd[-1] == '-'563k ) |
215 | 8.98k | --PEnd; |
216 | 563k | } |
217 | 0 | return true; |
218 | 2.00M | } while (Advance()1.14M ); |
219 | 21.8M | return false; |
220 | 22.7M | } |
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 | 346k | bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) { |
225 | 346k | unsigned Depth = 1; |
226 | 346k | P = C; |
227 | 15.9M | while (P < End) { |
228 | 15.9M | StringRef S(P, End - P); |
229 | 15.9M | if (S.startswith(OpenBrace)) { |
230 | 5.76k | ++Depth; |
231 | 5.76k | P += OpenBrace.size(); |
232 | 15.9M | } else if (S.startswith(CloseBrace)) { |
233 | 351k | --Depth; |
234 | 351k | if (Depth == 0) { |
235 | 346k | PEnd = P + CloseBrace.size(); |
236 | 346k | return true; |
237 | 346k | } |
238 | 5.76k | P += CloseBrace.size(); |
239 | 15.6M | } else { |
240 | 15.6M | ++P; |
241 | 15.6M | } |
242 | 15.9M | } |
243 | 0 | return false; |
244 | 346k | } |
245 | | |
246 | | // Advance 1-past previous next/search. |
247 | | // Behavior is undefined if previous next/search failed. |
248 | 2.94M | bool Advance() { |
249 | 2.94M | C = PEnd; |
250 | 2.94M | return C < End; |
251 | 2.94M | } |
252 | | |
253 | | // Return the text matched by the previous next/search. |
254 | | // Behavior is undefined if previous next/search failed. |
255 | 575k | StringRef Match() { return StringRef(P, PEnd - P); } |
256 | | |
257 | | // Skip zero or more whitespace. |
258 | 692k | void SkipWhitespace() { |
259 | 1.02M | for (; C < End && isWhitespace(*C); ++C332k ) |
260 | 332k | ; |
261 | 692k | } |
262 | | |
263 | | // Return true if EOF reached. |
264 | 22.9M | bool Done() { |
265 | 22.9M | return !(C < End); |
266 | 22.9M | } |
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 | 346k | bool MatchAnyLine = false) { |
300 | | // Construct new directive. |
301 | 346k | std::unique_ptr<Directive> D = Directive::create( |
302 | 346k | UD.RegexKind, UD.DirectivePos, ExpectedLoc, MatchAnyFileAndLine, |
303 | 346k | MatchAnyLine, UD.Text, UD.Min, UD.Max); |
304 | | |
305 | 346k | std::string Error; |
306 | 346k | 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 | 346k | UD.DL->push_back(std::move(D)); |
312 | 346k | } |
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 | 18.7k | MarkerTracker(DiagnosticsEngine &Diags) : Diags(Diags) {} |
342 | | |
343 | | // Register a marker. |
344 | 11.5k | void addMarker(StringRef MarkerName, SourceLocation Pos) { |
345 | 11.5k | auto InsertResult = Markers.insert( |
346 | 11.5k | {MarkerName, Marker{Pos, SourceLocation(), SourceLocation()}}); |
347 | | |
348 | 11.5k | Marker &M = InsertResult.first->second; |
349 | 11.5k | if (!InsertResult.second) { |
350 | | // Marker was redefined. |
351 | 9.85k | M.RedefLoc = Pos; |
352 | 9.85k | } else { |
353 | | // First definition: build any deferred directives. |
354 | 1.70k | auto Deferred = DeferredDirectives.find(MarkerName); |
355 | 1.70k | if (Deferred != DeferredDirectives.end()) { |
356 | 37 | for (auto &UD : Deferred->second) { |
357 | 37 | if (M.UseLoc.isInvalid()) |
358 | 21 | M.UseLoc = UD.DirectivePos; |
359 | 37 | attachDirective(Diags, UD, Pos); |
360 | 37 | } |
361 | 21 | DeferredDirectives.erase(Deferred); |
362 | 21 | } |
363 | 1.70k | } |
364 | 11.5k | } |
365 | | |
366 | | // Register a directive at the specified marker. |
367 | 417 | void addDirective(StringRef MarkerName, const UnattachedDirective &UD) { |
368 | 417 | auto MarkerIt = Markers.find(MarkerName); |
369 | 417 | if (MarkerIt != Markers.end()) { |
370 | 379 | Marker &M = MarkerIt->second; |
371 | 379 | if (M.UseLoc.isInvalid()) |
372 | 242 | M.UseLoc = UD.DirectivePos; |
373 | 379 | return attachDirective(Diags, UD, M.DefLoc); |
374 | 379 | } |
375 | 38 | DeferredDirectives[MarkerName].push_back(UD); |
376 | 38 | } |
377 | | |
378 | | // Ensure we have no remaining deferred directives, and no |
379 | | // multiply-defined-and-used markers. |
380 | 18.0k | void finalize() { |
381 | 18.0k | for (auto &MarkerInfo : Markers) { |
382 | 1.70k | StringRef Name = MarkerInfo.first(); |
383 | 1.70k | Marker &M = MarkerInfo.second; |
384 | 1.70k | if (M.RedefLoc.isValid() && M.UseLoc.isValid()552 ) { |
385 | 1 | Diags.Report(M.UseLoc, diag::err_verify_ambiguous_marker) << Name; |
386 | 1 | Diags.Report(M.DefLoc, diag::note_verify_ambiguous_marker) << Name; |
387 | 1 | Diags.Report(M.RedefLoc, diag::note_verify_ambiguous_marker) << Name; |
388 | 1 | } |
389 | 1.70k | } |
390 | | |
391 | 18.0k | for (auto &DeferredPair : DeferredDirectives) { |
392 | 1 | Diags.Report(DeferredPair.second.front().DirectivePos, |
393 | 1 | diag::err_verify_no_such_marker) |
394 | 1 | << DeferredPair.first(); |
395 | 1 | } |
396 | 18.0k | } |
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 | 11.0M | VerifyDiagnosticConsumer::MarkerTracker &Markers) { |
407 | 11.0M | DiagnosticsEngine &Diags = PP ? PP->getDiagnostics()11.0M : SM.getDiagnostics()633 ; |
408 | | |
409 | | // First, scan the comment looking for markers. |
410 | 11.3M | for (ParseHelper PH(S); !PH.Done();) { |
411 | 11.3M | if (!PH.Search("#", true)) |
412 | 11.0M | break; |
413 | 289k | PH.C = PH.P; |
414 | 289k | if (!PH.NextMarker()) { |
415 | 278k | PH.Next("#"); |
416 | 278k | PH.Advance(); |
417 | 278k | continue; |
418 | 278k | } |
419 | 11.5k | PH.Advance(); |
420 | 11.5k | Markers.addMarker(PH.Match(), Pos); |
421 | 11.5k | } |
422 | | |
423 | | // A single comment may contain multiple directives. |
424 | 11.0M | bool FoundDirective = false; |
425 | 11.6M | 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 | 11.3M | const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes; |
430 | 11.3M | if (!(Prefixes.size() == 1 ? PH.Search(*Prefixes.begin(), true, true)10.9M |
431 | 11.3M | : PH.Search("", true, true)388k )) |
432 | 10.7M | break; |
433 | | |
434 | 563k | StringRef DToken = PH.Match(); |
435 | 563k | PH.Advance(); |
436 | | |
437 | | // Default directive kind. |
438 | 563k | UnattachedDirective D; |
439 | 563k | 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 | 563k | if (DToken.endswith("-re")) { |
449 | 4.59k | D.RegexKind = true; |
450 | 4.59k | KindStr = "regex"; |
451 | 4.59k | DToken = DToken.substr(0, DToken.size()-3); |
452 | 4.59k | } |
453 | | |
454 | | // Type in initial directive token: -{error|warning|note|no-diagnostics} |
455 | 563k | bool NoDiag = false; |
456 | 563k | StringRef DType; |
457 | 563k | if (DToken.endswith(DType="-error")) |
458 | 200k | D.DL = ED ? &ED->Errors200k : nullptr33 ; |
459 | 362k | else if (DToken.endswith(DType="-warning")) |
460 | 64.9k | D.DL = ED ? &ED->Warnings64.9k : nullptr7 ; |
461 | 297k | else if (DToken.endswith(DType="-remark")) |
462 | 96 | D.DL = ED ? &ED->Remarks : nullptr0 ; |
463 | 297k | else if (DToken.endswith(DType="-note")) |
464 | 95.7k | D.DL = ED ? &ED->Notes95.7k : nullptr10 ; |
465 | 202k | else if (DToken.endswith(DType="-no-diagnostics")) { |
466 | 8.65k | NoDiag = true; |
467 | 8.65k | if (D.RegexKind) |
468 | 0 | continue; |
469 | 8.65k | } |
470 | 193k | else |
471 | 193k | continue; |
472 | 369k | DToken = DToken.substr(0, DToken.size()-DType.size()); |
473 | | |
474 | | // What's left in DToken is the actual prefix. That might not be a -verify |
475 | | // prefix even if there is only one -verify prefix (for example, the full |
476 | | // DToken is foo-bar-warning, but foo is the only -verify prefix). |
477 | 369k | if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken)) |
478 | 15.1k | continue; |
479 | | |
480 | 354k | if (NoDiag) { |
481 | 8.54k | if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives) |
482 | 1 | Diags.Report(Pos, diag::err_verify_invalid_no_diags) |
483 | 1 | << /*IsExpectedNoDiagnostics=*/true; |
484 | 8.54k | else |
485 | 8.54k | Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics; |
486 | 8.54k | continue; |
487 | 8.54k | } |
488 | 346k | if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) { |
489 | 1 | Diags.Report(Pos, diag::err_verify_invalid_no_diags) |
490 | 1 | << /*IsExpectedNoDiagnostics=*/false; |
491 | 1 | continue; |
492 | 1 | } |
493 | 346k | Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives; |
494 | | |
495 | | // If a directive has been found but we're not interested |
496 | | // in storing the directive information, return now. |
497 | 346k | if (!D.DL) |
498 | 50 | return true; |
499 | | |
500 | | // Next optional token: @ |
501 | 346k | SourceLocation ExpectedLoc; |
502 | 346k | StringRef Marker; |
503 | 346k | bool MatchAnyFileAndLine = false; |
504 | 346k | bool MatchAnyLine = false; |
505 | 346k | if (!PH.Next("@")) { |
506 | 278k | ExpectedLoc = Pos; |
507 | 278k | } else { |
508 | 68.0k | PH.Advance(); |
509 | 68.0k | unsigned Line = 0; |
510 | 68.0k | bool FoundPlus = PH.Next("+"); |
511 | 68.0k | if (FoundPlus || PH.Next("-")20.4k ) { |
512 | | // Relative to current line. |
513 | 65.1k | PH.Advance(); |
514 | 65.1k | bool Invalid = false; |
515 | 65.1k | unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid); |
516 | 65.1k | if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine17.5k )) { |
517 | 65.1k | if (FoundPlus) ExpectedLine += Line47.5k ; |
518 | 17.5k | else ExpectedLine -= Line; |
519 | 65.1k | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1); |
520 | 65.1k | } |
521 | 65.1k | } else if (2.91k PH.Next(Line)2.91k ) { |
522 | | // Absolute line number. |
523 | 609 | if (Line > 0) |
524 | 603 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1); |
525 | 2.30k | } else if (PH.NextMarker()) { |
526 | 417 | Marker = PH.Match(); |
527 | 1.88k | } else if (PP && PH.Search(":")) { |
528 | | // Specific source file. |
529 | 1.34k | StringRef Filename(PH.C, PH.P-PH.C); |
530 | 1.34k | PH.Advance(); |
531 | | |
532 | 1.34k | if (Filename == "*") { |
533 | 9 | MatchAnyFileAndLine = true; |
534 | 9 | if (!PH.Next("*")) { |
535 | 1 | Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin), |
536 | 1 | diag::err_verify_missing_line) |
537 | 1 | << "'*'"; |
538 | 1 | continue; |
539 | 1 | } |
540 | 8 | MatchAnyLine = true; |
541 | 8 | ExpectedLoc = SourceLocation(); |
542 | 1.33k | } else { |
543 | | // Lookup file via Preprocessor, like a #include. |
544 | 1.33k | Optional<FileEntryRef> File = |
545 | 1.33k | PP->LookupFile(Pos, Filename, false, nullptr, nullptr, nullptr, |
546 | 1.33k | nullptr, nullptr, nullptr, nullptr, nullptr); |
547 | 1.33k | if (!File) { |
548 | 1 | Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin), |
549 | 1 | diag::err_verify_missing_file) |
550 | 1 | << Filename << KindStr; |
551 | 1 | continue; |
552 | 1 | } |
553 | | |
554 | 1.33k | FileID FID = SM.translateFile(*File); |
555 | 1.33k | if (FID.isInvalid()) |
556 | 51 | FID = SM.createFileID(*File, Pos, SrcMgr::C_User); |
557 | | |
558 | 1.33k | if (PH.Next(Line) && Line > 0656 ) |
559 | 654 | ExpectedLoc = SM.translateLineCol(FID, Line, 1); |
560 | 679 | else if (PH.Next("*")) { |
561 | 676 | MatchAnyLine = true; |
562 | 676 | ExpectedLoc = SM.translateLineCol(FID, 1, 1); |
563 | 676 | } |
564 | 1.33k | } |
565 | 1.34k | } else if (541 PH.Next("*")541 ) { |
566 | 535 | MatchAnyLine = true; |
567 | 535 | ExpectedLoc = SourceLocation(); |
568 | 535 | } |
569 | | |
570 | 68.0k | if (ExpectedLoc.isInvalid() && !MatchAnyLine981 && Marker.empty()438 ) { |
571 | 21 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
572 | 21 | diag::err_verify_missing_line) << KindStr; |
573 | 21 | continue; |
574 | 21 | } |
575 | 67.9k | PH.Advance(); |
576 | 67.9k | } |
577 | | |
578 | | // Skip optional whitespace. |
579 | 346k | PH.SkipWhitespace(); |
580 | | |
581 | | // Next optional token: positive integer or a '+'. |
582 | 346k | if (PH.Next(D.Min)) { |
583 | 40.4k | PH.Advance(); |
584 | | // A positive integer can be followed by a '+' meaning min |
585 | | // or more, or by a '-' meaning a range from min to max. |
586 | 40.4k | if (PH.Next("+")) { |
587 | 2.74k | D.Max = Directive::MaxCount; |
588 | 2.74k | PH.Advance(); |
589 | 37.7k | } else if (PH.Next("-")) { |
590 | 1.28k | PH.Advance(); |
591 | 1.28k | if (!PH.Next(D.Max) || D.Max < D.Min) { |
592 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
593 | 0 | diag::err_verify_invalid_range) << KindStr; |
594 | 0 | continue; |
595 | 0 | } |
596 | 1.28k | PH.Advance(); |
597 | 36.4k | } else { |
598 | 36.4k | D.Max = D.Min; |
599 | 36.4k | } |
600 | 305k | } else if (PH.Next("+")) { |
601 | | // '+' on its own means "1 or more". |
602 | 694 | D.Max = Directive::MaxCount; |
603 | 694 | PH.Advance(); |
604 | 694 | } |
605 | | |
606 | | // Skip optional whitespace. |
607 | 346k | PH.SkipWhitespace(); |
608 | | |
609 | | // Next token: {{ |
610 | 346k | if (!PH.Next("{{")) { |
611 | 7 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
612 | 7 | diag::err_verify_missing_start) << KindStr; |
613 | 7 | continue; |
614 | 7 | } |
615 | 346k | PH.Advance(); |
616 | 346k | const char* const ContentBegin = PH.C; // mark content begin |
617 | | // Search for token: }} |
618 | 346k | if (!PH.SearchClosingBrace("{{", "}}")) { |
619 | 0 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
620 | 0 | diag::err_verify_missing_end) << KindStr; |
621 | 0 | continue; |
622 | 0 | } |
623 | 346k | const char* const ContentEnd = PH.P; // mark content end |
624 | 346k | PH.Advance(); |
625 | | |
626 | 346k | D.DirectivePos = Pos; |
627 | 346k | D.ContentBegin = Pos.getLocWithOffset(ContentBegin - PH.Begin); |
628 | | |
629 | | // Build directive text; convert \n to newlines. |
630 | 346k | StringRef NewlineStr = "\\n"; |
631 | 346k | StringRef Content(ContentBegin, ContentEnd-ContentBegin); |
632 | 346k | size_t CPos = 0; |
633 | 346k | size_t FPos; |
634 | 346k | while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) { |
635 | 89 | D.Text += Content.substr(CPos, FPos-CPos); |
636 | 89 | D.Text += '\n'; |
637 | 89 | CPos = FPos + NewlineStr.size(); |
638 | 89 | } |
639 | 346k | if (D.Text.empty()) |
640 | 345k | D.Text.assign(ContentBegin, ContentEnd); |
641 | | |
642 | | // Check that regex directives contain at least one regex. |
643 | 346k | if (D.RegexKind && D.Text.find("{{") == StringRef::npos4.56k ) { |
644 | 0 | Diags.Report(D.ContentBegin, diag::err_verify_missing_regex) << D.Text; |
645 | 0 | return false; |
646 | 0 | } |
647 | | |
648 | 346k | if (Marker.empty()) |
649 | 345k | attachDirective(Diags, D, ExpectedLoc, MatchAnyFileAndLine, MatchAnyLine); |
650 | 417 | else |
651 | 417 | Markers.addDirective(Marker, D); |
652 | 346k | FoundDirective = true; |
653 | 346k | } |
654 | | |
655 | 11.0M | return FoundDirective; |
656 | 11.0M | } |
657 | | |
658 | | VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_) |
659 | | : Diags(Diags_), PrimaryClient(Diags.getClient()), |
660 | | PrimaryClientOwner(Diags.takeClient()), |
661 | | Buffer(new TextDiagnosticBuffer()), Markers(new MarkerTracker(Diags)), |
662 | 18.1k | Status(HasNoDirectives) { |
663 | 18.1k | if (Diags.hasSourceManager()) |
664 | 0 | setSourceManager(Diags.getSourceManager()); |
665 | 18.1k | } |
666 | | |
667 | 18.0k | VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { |
668 | 18.0k | assert(!ActiveSourceFiles && "Incomplete parsing of source files!"); |
669 | 0 | assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!"); |
670 | 0 | SrcManager = nullptr; |
671 | 18.0k | CheckDiagnostics(); |
672 | 18.0k | assert(!Diags.ownsClient() && |
673 | 18.0k | "The VerifyDiagnosticConsumer takes over ownership of the client!"); |
674 | 18.0k | } |
675 | | |
676 | | // DiagnosticConsumer interface. |
677 | | |
678 | | void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, |
679 | 18.0k | const Preprocessor *PP) { |
680 | | // Attach comment handler on first invocation. |
681 | 18.0k | if (++ActiveSourceFiles == 1) { |
682 | 18.0k | if (PP) { |
683 | 18.0k | CurrentPreprocessor = PP; |
684 | 18.0k | this->LangOpts = &LangOpts; |
685 | 18.0k | setSourceManager(PP->getSourceManager()); |
686 | 18.0k | const_cast<Preprocessor *>(PP)->addCommentHandler(this); |
687 | 18.0k | #ifndef NDEBUG |
688 | | // Debug build tracks parsed files. |
689 | 18.0k | const_cast<Preprocessor *>(PP)->addPPCallbacks( |
690 | 18.0k | std::make_unique<VerifyFileTracker>(*this, *SrcManager)); |
691 | 18.0k | #endif |
692 | 18.0k | } |
693 | 18.0k | } |
694 | | |
695 | 18.0k | assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!"); |
696 | 0 | PrimaryClient->BeginSourceFile(LangOpts, PP); |
697 | 18.0k | } |
698 | | |
699 | 18.0k | void VerifyDiagnosticConsumer::EndSourceFile() { |
700 | 18.0k | assert(ActiveSourceFiles && "No active source files!"); |
701 | 0 | PrimaryClient->EndSourceFile(); |
702 | | |
703 | | // Detach comment handler once last active source file completed. |
704 | 18.0k | if (--ActiveSourceFiles == 0) { |
705 | 18.0k | if (CurrentPreprocessor) |
706 | 18.0k | const_cast<Preprocessor *>(CurrentPreprocessor)-> |
707 | 18.0k | removeCommentHandler(this); |
708 | | |
709 | | // Diagnose any used-but-not-defined markers. |
710 | 18.0k | Markers->finalize(); |
711 | | |
712 | | // Check diagnostics once last file completed. |
713 | 18.0k | CheckDiagnostics(); |
714 | 18.0k | CurrentPreprocessor = nullptr; |
715 | 18.0k | LangOpts = nullptr; |
716 | 18.0k | } |
717 | 18.0k | } |
718 | | |
719 | | void VerifyDiagnosticConsumer::HandleDiagnostic( |
720 | 399k | DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) { |
721 | 399k | if (Info.hasSourceManager()) { |
722 | | // If this diagnostic is for a different source manager, ignore it. |
723 | 399k | if (SrcManager && &Info.getSourceManager() != SrcManager) |
724 | 278 | return; |
725 | | |
726 | 399k | setSourceManager(Info.getSourceManager()); |
727 | 399k | } |
728 | | |
729 | 399k | #ifndef NDEBUG |
730 | | // Debug build tracks unparsed files for possible |
731 | | // unparsed expected-* directives. |
732 | 399k | if (SrcManager) { |
733 | 399k | SourceLocation Loc = Info.getLocation(); |
734 | 399k | if (Loc.isValid()) { |
735 | 399k | ParsedStatus PS = IsUnparsed; |
736 | | |
737 | 399k | Loc = SrcManager->getExpansionLoc(Loc); |
738 | 399k | FileID FID = SrcManager->getFileID(Loc); |
739 | | |
740 | 399k | const FileEntry *FE = SrcManager->getFileEntryForID(FID); |
741 | 399k | if (FE && CurrentPreprocessor399k && SrcManager->isLoadedFileID(FID)399k ) { |
742 | | // If the file is a modules header file it shall not be parsed |
743 | | // for expected-* directives. |
744 | 1.60k | HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo(); |
745 | 1.60k | if (HS.findModuleForHeader(FE)) |
746 | 1.20k | PS = IsUnparsedNoDirectives; |
747 | 1.60k | } |
748 | | |
749 | 399k | UpdateParsedFileStatus(*SrcManager, FID, PS); |
750 | 399k | } |
751 | 399k | } |
752 | 399k | #endif |
753 | | |
754 | | // Send the diagnostic to the buffer, we will check it once we reach the end |
755 | | // of the source file (or are destructed). |
756 | 399k | Buffer->HandleDiagnostic(DiagLevel, Info); |
757 | 399k | } |
758 | | |
759 | | /// HandleComment - Hook into the preprocessor and extract comments containing |
760 | | /// expected errors and warnings. |
761 | | bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP, |
762 | 11.0M | SourceRange Comment) { |
763 | 11.0M | SourceManager &SM = PP.getSourceManager(); |
764 | | |
765 | | // If this comment is for a different source manager, ignore it. |
766 | 11.0M | if (SrcManager && &SM != SrcManager) |
767 | 0 | return false; |
768 | | |
769 | 11.0M | SourceLocation CommentBegin = Comment.getBegin(); |
770 | | |
771 | 11.0M | const char *CommentRaw = SM.getCharacterData(CommentBegin); |
772 | 11.0M | StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw); |
773 | | |
774 | 11.0M | if (C.empty()) |
775 | 2 | return false; |
776 | | |
777 | | // Fold any "\<EOL>" sequences |
778 | 11.0M | size_t loc = C.find('\\'); |
779 | 11.0M | if (loc == StringRef::npos) { |
780 | 10.9M | ParseDirective(C, &ED, SM, &PP, CommentBegin, Status, *Markers); |
781 | 10.9M | return false; |
782 | 10.9M | } |
783 | | |
784 | 144k | std::string C2; |
785 | 144k | C2.reserve(C.size()); |
786 | | |
787 | 306k | for (size_t last = 0;; loc = C.find('\\', last)161k ) { |
788 | 306k | if (loc == StringRef::npos || loc == C.size()161k ) { |
789 | 144k | C2 += C.substr(last); |
790 | 144k | break; |
791 | 144k | } |
792 | 161k | C2 += C.substr(last, loc-last); |
793 | 161k | last = loc + 1; |
794 | | |
795 | 161k | if (C[last] == '\n' || C[last] == '\r'147k ) { |
796 | 14.8k | ++last; |
797 | | |
798 | | // Escape \r\n or \n\r, but not \n\n. |
799 | 14.8k | if (last < C.size()) |
800 | 14.8k | if (C[last] == '\n' || C[last] == '\r') |
801 | 0 | if (C[last] != C[last-1]) |
802 | 0 | ++last; |
803 | 147k | } else { |
804 | | // This was just a normal backslash. |
805 | 147k | C2 += '\\'; |
806 | 147k | } |
807 | 161k | } |
808 | | |
809 | 144k | if (!C2.empty()) |
810 | 144k | ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status, *Markers); |
811 | 144k | return false; |
812 | 11.0M | } |
813 | | |
814 | | #ifndef NDEBUG |
815 | | /// Lex the specified source file to determine whether it contains |
816 | | /// any expected-* directives. As a Lexer is used rather than a full-blown |
817 | | /// Preprocessor, directives inside skipped #if blocks will still be found. |
818 | | /// |
819 | | /// \return true if any directives were found. |
820 | | static bool findDirectives(SourceManager &SM, FileID FID, |
821 | 128 | const LangOptions &LangOpts) { |
822 | | // Create a raw lexer to pull all the comments out of FID. |
823 | 128 | if (FID.isInvalid()) |
824 | 0 | return false; |
825 | | |
826 | | // Create a lexer to lex all the tokens of the main file in raw mode. |
827 | 128 | llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID); |
828 | 128 | Lexer RawLex(FID, FromFile, SM, LangOpts); |
829 | | |
830 | | // Return comments as tokens, this is how we find expected diagnostics. |
831 | 128 | RawLex.SetCommentRetentionState(true); |
832 | | |
833 | 128 | Token Tok; |
834 | 128 | Tok.setKind(tok::comment); |
835 | 128 | VerifyDiagnosticConsumer::DirectiveStatus Status = |
836 | 128 | VerifyDiagnosticConsumer::HasNoDirectives; |
837 | 28.6k | while (Tok.isNot(tok::eof)) { |
838 | 28.5k | RawLex.LexFromRawLexer(Tok); |
839 | 28.5k | if (!Tok.is(tok::comment)) continue27.9k ; |
840 | | |
841 | 633 | std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts); |
842 | 633 | if (Comment.empty()) continue0 ; |
843 | | |
844 | | // We don't care about tracking markers for this phase. |
845 | 633 | VerifyDiagnosticConsumer::MarkerTracker Markers(SM.getDiagnostics()); |
846 | | |
847 | | // Find first directive. |
848 | 633 | if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(), |
849 | 633 | Status, Markers)) |
850 | 50 | return true; |
851 | 633 | } |
852 | 78 | return false; |
853 | 128 | } |
854 | | #endif // !NDEBUG |
855 | | |
856 | | /// Takes a list of diagnostics that have been generated but not matched |
857 | | /// by an expected-* directive and produces a diagnostic to the user from this. |
858 | | static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr, |
859 | | const_diag_iterator diag_begin, |
860 | | const_diag_iterator diag_end, |
861 | 144k | const char *Kind) { |
862 | 144k | if (diag_begin == diag_end) return 0144k ; |
863 | | |
864 | 109 | SmallString<256> Fmt; |
865 | 109 | llvm::raw_svector_ostream OS(Fmt); |
866 | 283 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I174 ) { |
867 | 174 | if (I->first.isInvalid() || !SourceMgr108 ) |
868 | 66 | OS << "\n (frontend)"; |
869 | 108 | else { |
870 | 108 | OS << "\n "; |
871 | 108 | if (const FileEntry *File = SourceMgr->getFileEntryForID( |
872 | 108 | SourceMgr->getFileID(I->first))) |
873 | 108 | OS << " File " << File->getName(); |
874 | 108 | OS << " Line " << SourceMgr->getPresumedLineNumber(I->first); |
875 | 108 | } |
876 | 174 | OS << ": " << I->second; |
877 | 174 | } |
878 | | |
879 | 109 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
880 | 109 | << Kind << /*Unexpected=*/true << OS.str(); |
881 | 109 | return std::distance(diag_begin, diag_end); |
882 | 144k | } |
883 | | |
884 | | /// Takes a list of diagnostics that were expected to have been generated |
885 | | /// but were not and produces a diagnostic to the user from this. |
886 | | static unsigned PrintExpected(DiagnosticsEngine &Diags, |
887 | | SourceManager &SourceMgr, |
888 | 72.3k | std::vector<Directive *> &DL, const char *Kind) { |
889 | 72.3k | if (DL.empty()) |
890 | 72.2k | return 0; |
891 | | |
892 | 26 | SmallString<256> Fmt; |
893 | 26 | llvm::raw_svector_ostream OS(Fmt); |
894 | 128 | for (const auto *D : DL) { |
895 | 128 | if (D->DiagnosticLoc.isInvalid() || D->MatchAnyFileAndLine126 ) |
896 | 2 | OS << "\n File *"; |
897 | 126 | else |
898 | 126 | OS << "\n File " << SourceMgr.getFilename(D->DiagnosticLoc); |
899 | 128 | if (D->MatchAnyLine) |
900 | 3 | OS << " Line *"; |
901 | 125 | else |
902 | 125 | OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc); |
903 | 128 | if (D->DirectiveLoc != D->DiagnosticLoc) |
904 | 21 | OS << " (directive at " |
905 | 21 | << SourceMgr.getFilename(D->DirectiveLoc) << ':' |
906 | 21 | << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')'; |
907 | 128 | OS << ": " << D->Text; |
908 | 128 | } |
909 | | |
910 | 26 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
911 | 26 | << Kind << /*Unexpected=*/false << OS.str(); |
912 | 26 | return DL.size(); |
913 | 72.3k | } |
914 | | |
915 | | /// Determine whether two source locations come from the same file. |
916 | | static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc, |
917 | 426k | SourceLocation DiagnosticLoc) { |
918 | 438k | while (DiagnosticLoc.isMacroID()) |
919 | 11.9k | DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc); |
920 | | |
921 | 426k | if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc)) |
922 | 423k | return true; |
923 | | |
924 | 2.63k | const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc)); |
925 | 2.63k | if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc)13 ) |
926 | 13 | return true; |
927 | | |
928 | 2.62k | return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc))); |
929 | 2.63k | } |
930 | | |
931 | | /// CheckLists - Compare expected to seen diagnostic lists and return the |
932 | | /// the difference between them. |
933 | | static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
934 | | const char *Label, |
935 | | DirectiveList &Left, |
936 | | const_diag_iterator d2_begin, |
937 | | const_diag_iterator d2_end, |
938 | 72.3k | bool IgnoreUnexpected) { |
939 | 72.3k | std::vector<Directive *> LeftOnly; |
940 | 72.3k | DiagList Right(d2_begin, d2_end); |
941 | | |
942 | 346k | for (auto &Owner : Left) { |
943 | 346k | Directive &D = *Owner; |
944 | 346k | unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); |
945 | | |
946 | 745k | for (unsigned i = 0; i < D.Max; ++i399k ) { |
947 | 403k | DiagList::iterator II, IE; |
948 | 2.57M | for (II = Right.begin(), IE = Right.end(); II != IE; ++II2.17M ) { |
949 | 2.57M | if (!D.MatchAnyLine) { |
950 | 2.52M | unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first); |
951 | 2.52M | if (LineNo1 != LineNo2) |
952 | 2.10M | continue; |
953 | 2.52M | } |
954 | | |
955 | 465k | if (!D.DiagnosticLoc.isInvalid() && !D.MatchAnyFileAndLine426k && |
956 | 465k | !IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first)426k ) |
957 | 2.31k | continue; |
958 | | |
959 | 462k | const std::string &RightText = II->second; |
960 | 462k | if (D.match(RightText)) |
961 | 398k | break; |
962 | 462k | } |
963 | 403k | if (II == IE) { |
964 | | // Not found. |
965 | 4.38k | if (i >= D.Min) break4.25k ; |
966 | 128 | LeftOnly.push_back(&D); |
967 | 398k | } else { |
968 | | // Found. The same cannot be found twice. |
969 | 398k | Right.erase(II); |
970 | 398k | } |
971 | 403k | } |
972 | 346k | } |
973 | | // Now all that's left in Right are those that were not matched. |
974 | 72.3k | unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label); |
975 | 72.3k | if (!IgnoreUnexpected) |
976 | 72.2k | num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label); |
977 | 72.3k | return num; |
978 | 72.3k | } |
979 | | |
980 | | /// CheckResults - This compares the expected results to those that |
981 | | /// were actually reported. It emits any discrepencies. Return "true" if there |
982 | | /// were problems. Return "false" otherwise. |
983 | | static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
984 | | const TextDiagnosticBuffer &Buffer, |
985 | 18.0k | ExpectedData &ED) { |
986 | | // We want to capture the delta between what was expected and what was |
987 | | // seen. |
988 | | // |
989 | | // Expected \ Seen - set expected but not seen |
990 | | // Seen \ Expected - set seen but not expected |
991 | 18.0k | unsigned NumProblems = 0; |
992 | | |
993 | 18.0k | const DiagnosticLevelMask DiagMask = |
994 | 18.0k | Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected(); |
995 | | |
996 | | // See if there are error mismatches. |
997 | 18.0k | NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, |
998 | 18.0k | Buffer.err_begin(), Buffer.err_end(), |
999 | 18.0k | bool(DiagnosticLevelMask::Error & DiagMask)); |
1000 | | |
1001 | | // See if there are warning mismatches. |
1002 | 18.0k | NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, |
1003 | 18.0k | Buffer.warn_begin(), Buffer.warn_end(), |
1004 | 18.0k | bool(DiagnosticLevelMask::Warning & DiagMask)); |
1005 | | |
1006 | | // See if there are remark mismatches. |
1007 | 18.0k | NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks, |
1008 | 18.0k | Buffer.remark_begin(), Buffer.remark_end(), |
1009 | 18.0k | bool(DiagnosticLevelMask::Remark & DiagMask)); |
1010 | | |
1011 | | // See if there are note mismatches. |
1012 | 18.0k | NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, |
1013 | 18.0k | Buffer.note_begin(), Buffer.note_end(), |
1014 | 18.0k | bool(DiagnosticLevelMask::Note & DiagMask)); |
1015 | | |
1016 | 18.0k | return NumProblems; |
1017 | 18.0k | } |
1018 | | |
1019 | | void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM, |
1020 | | FileID FID, |
1021 | 508k | ParsedStatus PS) { |
1022 | | // Check SourceManager hasn't changed. |
1023 | 508k | setSourceManager(SM); |
1024 | | |
1025 | 508k | #ifndef NDEBUG |
1026 | 508k | if (FID.isInvalid()) |
1027 | 0 | return; |
1028 | | |
1029 | 508k | const FileEntry *FE = SM.getFileEntryForID(FID); |
1030 | | |
1031 | 508k | if (PS == IsParsed) { |
1032 | | // Move the FileID from the unparsed set to the parsed set. |
1033 | 108k | UnparsedFiles.erase(FID); |
1034 | 108k | ParsedFiles.insert(std::make_pair(FID, FE)); |
1035 | 399k | } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)1.66k ) { |
1036 | | // Add the FileID to the unparsed set if we haven't seen it before. |
1037 | | |
1038 | | // Check for directives. |
1039 | 351 | bool FoundDirectives; |
1040 | 351 | if (PS == IsUnparsedNoDirectives) |
1041 | 223 | FoundDirectives = false; |
1042 | 128 | else |
1043 | 128 | FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts); |
1044 | | |
1045 | | // Add the FileID to the unparsed set. |
1046 | 351 | UnparsedFiles.insert(std::make_pair(FID, |
1047 | 351 | UnparsedFileStatus(FE, FoundDirectives))); |
1048 | 351 | } |
1049 | 508k | #endif |
1050 | 508k | } |
1051 | | |
1052 | 36.1k | void VerifyDiagnosticConsumer::CheckDiagnostics() { |
1053 | | // Ensure any diagnostics go to the primary client. |
1054 | 36.1k | DiagnosticConsumer *CurClient = Diags.getClient(); |
1055 | 36.1k | std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient(); |
1056 | 36.1k | Diags.setClient(PrimaryClient, false); |
1057 | | |
1058 | 36.1k | #ifndef NDEBUG |
1059 | | // In a debug build, scan through any files that may have been missed |
1060 | | // during parsing and issue a fatal error if directives are contained |
1061 | | // within these files. If a fatal error occurs, this suggests that |
1062 | | // this file is being parsed separately from the main file, in which |
1063 | | // case consider moving the directives to the correct place, if this |
1064 | | // is applicable. |
1065 | 36.1k | if (!UnparsedFiles.empty()) { |
1066 | | // Generate a cache of parsed FileEntry pointers for alias lookups. |
1067 | 255 | llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache; |
1068 | 255 | for (const auto &I : ParsedFiles) |
1069 | 537 | if (const FileEntry *FE = I.second) |
1070 | 282 | ParsedFileCache.insert(FE); |
1071 | | |
1072 | | // Iterate through list of unparsed files. |
1073 | 349 | for (const auto &I : UnparsedFiles) { |
1074 | 349 | const UnparsedFileStatus &Status = I.second; |
1075 | 349 | const FileEntry *FE = Status.getFile(); |
1076 | | |
1077 | | // Skip files that have been parsed via an alias. |
1078 | 349 | if (FE && ParsedFileCache.count(FE)345 ) |
1079 | 58 | continue; |
1080 | | |
1081 | | // Report a fatal error if this file contained directives. |
1082 | 291 | if (Status.foundDirectives()) { |
1083 | 0 | llvm::report_fatal_error(Twine("-verify directives found after rather" |
1084 | 0 | " than during normal parsing of ", |
1085 | 0 | StringRef(FE ? FE->getName() : "(unknown)"))); |
1086 | 0 | } |
1087 | 291 | } |
1088 | | |
1089 | | // UnparsedFiles has been processed now, so clear it. |
1090 | 255 | UnparsedFiles.clear(); |
1091 | 255 | } |
1092 | 36.1k | #endif // !NDEBUG |
1093 | | |
1094 | 36.1k | if (SrcManager) { |
1095 | | // Produce an error if no expected-* directives could be found in the |
1096 | | // source file(s) processed. |
1097 | 18.0k | if (Status == HasNoDirectives) { |
1098 | 29 | Diags.Report(diag::err_verify_no_directives).setForceEmit(); |
1099 | 29 | ++NumErrors; |
1100 | 29 | Status = HasNoDirectivesReported; |
1101 | 29 | } |
1102 | | |
1103 | | // Check that the expected diagnostics occurred. |
1104 | 18.0k | NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED); |
1105 | 18.0k | } else { |
1106 | 18.0k | const DiagnosticLevelMask DiagMask = |
1107 | 18.0k | ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected(); |
1108 | 18.0k | if (bool(DiagnosticLevelMask::Error & DiagMask)) |
1109 | 18.0k | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(), |
1110 | 18.0k | Buffer->err_end(), "error"); |
1111 | 18.0k | if (bool(DiagnosticLevelMask::Warning & DiagMask)) |
1112 | 18.0k | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(), |
1113 | 18.0k | Buffer->warn_end(), "warn"); |
1114 | 18.0k | if (bool(DiagnosticLevelMask::Remark & DiagMask)) |
1115 | 18.0k | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(), |
1116 | 18.0k | Buffer->remark_end(), "remark"); |
1117 | 18.0k | if (bool(DiagnosticLevelMask::Note & DiagMask)) |
1118 | 18.0k | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(), |
1119 | 18.0k | Buffer->note_end(), "note"); |
1120 | 18.0k | } |
1121 | | |
1122 | 36.1k | Diags.setClient(CurClient, Owner.release() != nullptr); |
1123 | | |
1124 | | // Reset the buffer, we have processed all the diagnostics in it. |
1125 | 36.1k | Buffer.reset(new TextDiagnosticBuffer()); |
1126 | 36.1k | ED.Reset(); |
1127 | 36.1k | } |
1128 | | |
1129 | | std::unique_ptr<Directive> Directive::create(bool RegexKind, |
1130 | | SourceLocation DirectiveLoc, |
1131 | | SourceLocation DiagnosticLoc, |
1132 | | bool MatchAnyFileAndLine, |
1133 | | bool MatchAnyLine, StringRef Text, |
1134 | 346k | unsigned Min, unsigned Max) { |
1135 | 346k | if (!RegexKind) |
1136 | 341k | return std::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc, |
1137 | 341k | MatchAnyFileAndLine, |
1138 | 341k | MatchAnyLine, Text, Min, Max); |
1139 | | |
1140 | | // Parse the directive into a regular expression. |
1141 | 4.56k | std::string RegexStr; |
1142 | 4.56k | StringRef S = Text; |
1143 | 17.7k | while (!S.empty()) { |
1144 | 13.2k | if (S.startswith("{{")) { |
1145 | 5.75k | S = S.drop_front(2); |
1146 | 5.75k | size_t RegexMatchLength = S.find("}}"); |
1147 | 5.75k | assert(RegexMatchLength != StringRef::npos); |
1148 | | // Append the regex, enclosed in parentheses. |
1149 | 0 | RegexStr += "("; |
1150 | 5.75k | RegexStr.append(S.data(), RegexMatchLength); |
1151 | 5.75k | RegexStr += ")"; |
1152 | 5.75k | S = S.drop_front(RegexMatchLength + 2); |
1153 | 7.47k | } else { |
1154 | 7.47k | size_t VerbatimMatchLength = S.find("{{"); |
1155 | 7.47k | if (VerbatimMatchLength == StringRef::npos) |
1156 | 2.51k | VerbatimMatchLength = S.size(); |
1157 | | // Escape and append the fixed string. |
1158 | 7.47k | RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength)); |
1159 | 7.47k | S = S.drop_front(VerbatimMatchLength); |
1160 | 7.47k | } |
1161 | 13.2k | } |
1162 | | |
1163 | 4.56k | return std::make_unique<RegexDirective>(DirectiveLoc, DiagnosticLoc, |
1164 | 4.56k | MatchAnyFileAndLine, MatchAnyLine, |
1165 | 4.56k | Text, Min, Max, RegexStr); |
1166 | 346k | } |