1 //===---- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a concrete diagnostic client, which buffers the diagnostic messages.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Frontend/TextDiagnosticBuffer.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/Regex.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 using namespace clang;
26 typedef VerifyDiagnosticConsumer::Directive Directive;
27 typedef VerifyDiagnosticConsumer::DirectiveList DirectiveList;
28 typedef VerifyDiagnosticConsumer::ExpectedData ExpectedData;
29
VerifyDiagnosticConsumer(DiagnosticsEngine & _Diags)30 VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
31 : Diags(_Diags),
32 PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()),
33 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(nullptr),
34 LangOpts(nullptr), SrcManager(nullptr), ActiveSourceFiles(0),
35 Status(HasNoDirectives)
36 {
37 Diags.takeClient();
38 if (Diags.hasSourceManager())
39 setSourceManager(Diags.getSourceManager());
40 }
41
~VerifyDiagnosticConsumer()42 VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
43 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
44 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
45 SrcManager = nullptr;
46 CheckDiagnostics();
47 Diags.takeClient();
48 if (OwnsPrimaryClient)
49 delete PrimaryClient;
50 }
51
52 #ifndef NDEBUG
53 namespace {
54 class VerifyFileTracker : public PPCallbacks {
55 VerifyDiagnosticConsumer &Verify;
56 SourceManager &SM;
57
58 public:
VerifyFileTracker(VerifyDiagnosticConsumer & Verify,SourceManager & SM)59 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
60 : Verify(Verify), SM(SM) { }
61
62 /// \brief Hook into the preprocessor and update the list of parsed
63 /// files when the preprocessor indicates a new file is entered.
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind FileType,FileID PrevFID)64 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
65 SrcMgr::CharacteristicKind FileType,
66 FileID PrevFID) {
67 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
68 VerifyDiagnosticConsumer::IsParsed);
69 }
70 };
71 } // End anonymous namespace.
72 #endif
73
74 // DiagnosticConsumer interface.
75
BeginSourceFile(const LangOptions & LangOpts,const Preprocessor * PP)76 void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
77 const Preprocessor *PP) {
78 // Attach comment handler on first invocation.
79 if (++ActiveSourceFiles == 1) {
80 if (PP) {
81 CurrentPreprocessor = PP;
82 this->LangOpts = &LangOpts;
83 setSourceManager(PP->getSourceManager());
84 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
85 #ifndef NDEBUG
86 // Debug build tracks parsed files.
87 VerifyFileTracker *V = new VerifyFileTracker(*this, *SrcManager);
88 const_cast<Preprocessor*>(PP)->addPPCallbacks(V);
89 #endif
90 }
91 }
92
93 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
94 PrimaryClient->BeginSourceFile(LangOpts, PP);
95 }
96
EndSourceFile()97 void VerifyDiagnosticConsumer::EndSourceFile() {
98 assert(ActiveSourceFiles && "No active source files!");
99 PrimaryClient->EndSourceFile();
100
101 // Detach comment handler once last active source file completed.
102 if (--ActiveSourceFiles == 0) {
103 if (CurrentPreprocessor)
104 const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this);
105
106 // Check diagnostics once last file completed.
107 CheckDiagnostics();
108 CurrentPreprocessor = nullptr;
109 LangOpts = nullptr;
110 }
111 }
112
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const Diagnostic & Info)113 void VerifyDiagnosticConsumer::HandleDiagnostic(
114 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
115 if (Info.hasSourceManager()) {
116 // If this diagnostic is for a different source manager, ignore it.
117 if (SrcManager && &Info.getSourceManager() != SrcManager)
118 return;
119
120 setSourceManager(Info.getSourceManager());
121 }
122
123 #ifndef NDEBUG
124 // Debug build tracks unparsed files for possible
125 // unparsed expected-* directives.
126 if (SrcManager) {
127 SourceLocation Loc = Info.getLocation();
128 if (Loc.isValid()) {
129 ParsedStatus PS = IsUnparsed;
130
131 Loc = SrcManager->getExpansionLoc(Loc);
132 FileID FID = SrcManager->getFileID(Loc);
133
134 const FileEntry *FE = SrcManager->getFileEntryForID(FID);
135 if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
136 // If the file is a modules header file it shall not be parsed
137 // for expected-* directives.
138 HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
139 if (HS.findModuleForHeader(FE))
140 PS = IsUnparsedNoDirectives;
141 }
142
143 UpdateParsedFileStatus(*SrcManager, FID, PS);
144 }
145 }
146 #endif
147
148 // Send the diagnostic to the buffer, we will check it once we reach the end
149 // of the source file (or are destructed).
150 Buffer->HandleDiagnostic(DiagLevel, Info);
151 }
152
153 //===----------------------------------------------------------------------===//
154 // Checking diagnostics implementation.
155 //===----------------------------------------------------------------------===//
156
157 typedef TextDiagnosticBuffer::DiagList DiagList;
158 typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
159
160 namespace {
161
162 /// StandardDirective - Directive with string matching.
163 ///
164 class StandardDirective : public Directive {
165 public:
StandardDirective(SourceLocation DirectiveLoc,SourceLocation DiagnosticLoc,bool MatchAnyLine,StringRef Text,unsigned Min,unsigned Max)166 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
167 bool MatchAnyLine, StringRef Text, unsigned Min,
168 unsigned Max)
169 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max) { }
170
isValid(std::string & Error)171 bool isValid(std::string &Error) override {
172 // all strings are considered valid; even empty ones
173 return true;
174 }
175
match(StringRef S)176 bool match(StringRef S) override {
177 return S.find(Text) != StringRef::npos;
178 }
179 };
180
181 /// RegexDirective - Directive with regular-expression matching.
182 ///
183 class RegexDirective : public Directive {
184 public:
RegexDirective(SourceLocation DirectiveLoc,SourceLocation DiagnosticLoc,bool MatchAnyLine,StringRef Text,unsigned Min,unsigned Max,StringRef RegexStr)185 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
186 bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max,
187 StringRef RegexStr)
188 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max),
189 Regex(RegexStr) { }
190
isValid(std::string & Error)191 bool isValid(std::string &Error) override {
192 if (Regex.isValid(Error))
193 return true;
194 return false;
195 }
196
match(StringRef S)197 bool match(StringRef S) override {
198 return Regex.match(S);
199 }
200
201 private:
202 llvm::Regex Regex;
203 };
204
205 class ParseHelper
206 {
207 public:
ParseHelper(StringRef S)208 ParseHelper(StringRef S)
209 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(nullptr) {}
210
211 // Return true if string literal is next.
Next(StringRef S)212 bool Next(StringRef S) {
213 P = C;
214 PEnd = C + S.size();
215 if (PEnd > End)
216 return false;
217 return !memcmp(P, S.data(), S.size());
218 }
219
220 // Return true if number is next.
221 // Output N only if number is next.
Next(unsigned & N)222 bool Next(unsigned &N) {
223 unsigned TMP = 0;
224 P = C;
225 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
226 TMP *= 10;
227 TMP += P[0] - '0';
228 }
229 if (P == C)
230 return false;
231 PEnd = P;
232 N = TMP;
233 return true;
234 }
235
236 // Return true if string literal is found.
237 // When true, P marks begin-position of S in content.
Search(StringRef S,bool EnsureStartOfWord=false)238 bool Search(StringRef S, bool EnsureStartOfWord = false) {
239 do {
240 P = std::search(C, End, S.begin(), S.end());
241 PEnd = P + S.size();
242 if (P == End)
243 break;
244 if (!EnsureStartOfWord
245 // Check if string literal starts a new word.
246 || P == Begin || isWhitespace(P[-1])
247 // Or it could be preceded by the start of a comment.
248 || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*')
249 && P[-2] == '/'))
250 return true;
251 // Otherwise, skip and search again.
252 } while (Advance());
253 return false;
254 }
255
256 // Return true if a CloseBrace that closes the OpenBrace at the current nest
257 // level is found. When true, P marks begin-position of CloseBrace.
SearchClosingBrace(StringRef OpenBrace,StringRef CloseBrace)258 bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) {
259 unsigned Depth = 1;
260 P = C;
261 while (P < End) {
262 StringRef S(P, End - P);
263 if (S.startswith(OpenBrace)) {
264 ++Depth;
265 P += OpenBrace.size();
266 } else if (S.startswith(CloseBrace)) {
267 --Depth;
268 if (Depth == 0) {
269 PEnd = P + CloseBrace.size();
270 return true;
271 }
272 P += CloseBrace.size();
273 } else {
274 ++P;
275 }
276 }
277 return false;
278 }
279
280 // Advance 1-past previous next/search.
281 // Behavior is undefined if previous next/search failed.
Advance()282 bool Advance() {
283 C = PEnd;
284 return C < End;
285 }
286
287 // Skip zero or more whitespace.
SkipWhitespace()288 void SkipWhitespace() {
289 for (; C < End && isWhitespace(*C); ++C)
290 ;
291 }
292
293 // Return true if EOF reached.
Done()294 bool Done() {
295 return !(C < End);
296 }
297
298 const char * const Begin; // beginning of expected content
299 const char * const End; // end of expected content (1-past)
300 const char *C; // position of next char in content
301 const char *P;
302
303 private:
304 const char *PEnd; // previous next/search subject end (1-past)
305 };
306
307 } // namespace anonymous
308
309 /// ParseDirective - Go through the comment and see if it indicates expected
310 /// diagnostics. If so, then put them in the appropriate directive list.
311 ///
312 /// Returns true if any valid directives were found.
ParseDirective(StringRef S,ExpectedData * ED,SourceManager & SM,Preprocessor * PP,SourceLocation Pos,VerifyDiagnosticConsumer::DirectiveStatus & Status)313 static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
314 Preprocessor *PP, SourceLocation Pos,
315 VerifyDiagnosticConsumer::DirectiveStatus &Status) {
316 DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
317
318 // A single comment may contain multiple directives.
319 bool FoundDirective = false;
320 for (ParseHelper PH(S); !PH.Done();) {
321 // Search for token: expected
322 if (!PH.Search("expected", true))
323 break;
324 PH.Advance();
325
326 // Next token: -
327 if (!PH.Next("-"))
328 continue;
329 PH.Advance();
330
331 // Next token: { error | warning | note }
332 DirectiveList *DL = nullptr;
333 if (PH.Next("error"))
334 DL = ED ? &ED->Errors : nullptr;
335 else if (PH.Next("warning"))
336 DL = ED ? &ED->Warnings : nullptr;
337 else if (PH.Next("remark"))
338 DL = ED ? &ED->Remarks : nullptr;
339 else if (PH.Next("note"))
340 DL = ED ? &ED->Notes : nullptr;
341 else if (PH.Next("no-diagnostics")) {
342 if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
343 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
344 << /*IsExpectedNoDiagnostics=*/true;
345 else
346 Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
347 continue;
348 } else
349 continue;
350 PH.Advance();
351
352 if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
353 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
354 << /*IsExpectedNoDiagnostics=*/false;
355 continue;
356 }
357 Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
358
359 // If a directive has been found but we're not interested
360 // in storing the directive information, return now.
361 if (!DL)
362 return true;
363
364 // Default directive kind.
365 bool RegexKind = false;
366 const char* KindStr = "string";
367
368 // Next optional token: -
369 if (PH.Next("-re")) {
370 PH.Advance();
371 RegexKind = true;
372 KindStr = "regex";
373 }
374
375 // Next optional token: @
376 SourceLocation ExpectedLoc;
377 bool MatchAnyLine = false;
378 if (!PH.Next("@")) {
379 ExpectedLoc = Pos;
380 } else {
381 PH.Advance();
382 unsigned Line = 0;
383 bool FoundPlus = PH.Next("+");
384 if (FoundPlus || PH.Next("-")) {
385 // Relative to current line.
386 PH.Advance();
387 bool Invalid = false;
388 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
389 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
390 if (FoundPlus) ExpectedLine += Line;
391 else ExpectedLine -= Line;
392 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
393 }
394 } else if (PH.Next(Line)) {
395 // Absolute line number.
396 if (Line > 0)
397 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
398 } else if (PP && PH.Search(":")) {
399 // Specific source file.
400 StringRef Filename(PH.C, PH.P-PH.C);
401 PH.Advance();
402
403 // Lookup file via Preprocessor, like a #include.
404 const DirectoryLookup *CurDir;
405 const FileEntry *FE = PP->LookupFile(Pos, Filename, false, nullptr,
406 CurDir, nullptr, nullptr, nullptr);
407 if (!FE) {
408 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
409 diag::err_verify_missing_file) << Filename << KindStr;
410 continue;
411 }
412
413 if (SM.translateFile(FE).isInvalid())
414 SM.createFileID(FE, Pos, SrcMgr::C_User);
415
416 if (PH.Next(Line) && Line > 0)
417 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
418 else if (PH.Next("*")) {
419 MatchAnyLine = true;
420 ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
421 }
422 }
423
424 if (ExpectedLoc.isInvalid()) {
425 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
426 diag::err_verify_missing_line) << KindStr;
427 continue;
428 }
429 PH.Advance();
430 }
431
432 // Skip optional whitespace.
433 PH.SkipWhitespace();
434
435 // Next optional token: positive integer or a '+'.
436 unsigned Min = 1;
437 unsigned Max = 1;
438 if (PH.Next(Min)) {
439 PH.Advance();
440 // A positive integer can be followed by a '+' meaning min
441 // or more, or by a '-' meaning a range from min to max.
442 if (PH.Next("+")) {
443 Max = Directive::MaxCount;
444 PH.Advance();
445 } else if (PH.Next("-")) {
446 PH.Advance();
447 if (!PH.Next(Max) || Max < Min) {
448 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
449 diag::err_verify_invalid_range) << KindStr;
450 continue;
451 }
452 PH.Advance();
453 } else {
454 Max = Min;
455 }
456 } else if (PH.Next("+")) {
457 // '+' on its own means "1 or more".
458 Max = Directive::MaxCount;
459 PH.Advance();
460 }
461
462 // Skip optional whitespace.
463 PH.SkipWhitespace();
464
465 // Next token: {{
466 if (!PH.Next("{{")) {
467 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
468 diag::err_verify_missing_start) << KindStr;
469 continue;
470 }
471 PH.Advance();
472 const char* const ContentBegin = PH.C; // mark content begin
473
474 // Search for token: }}
475 if (!PH.SearchClosingBrace("{{", "}}")) {
476 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
477 diag::err_verify_missing_end) << KindStr;
478 continue;
479 }
480 const char* const ContentEnd = PH.P; // mark content end
481 PH.Advance();
482
483 // Build directive text; convert \n to newlines.
484 std::string Text;
485 StringRef NewlineStr = "\\n";
486 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
487 size_t CPos = 0;
488 size_t FPos;
489 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
490 Text += Content.substr(CPos, FPos-CPos);
491 Text += '\n';
492 CPos = FPos + NewlineStr.size();
493 }
494 if (Text.empty())
495 Text.assign(ContentBegin, ContentEnd);
496
497 // Check that regex directives contain at least one regex.
498 if (RegexKind && Text.find("{{") == StringRef::npos) {
499 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
500 diag::err_verify_missing_regex) << Text;
501 return false;
502 }
503
504 // Construct new directive.
505 std::unique_ptr<Directive> D(
506 Directive::create(RegexKind, Pos, ExpectedLoc, MatchAnyLine, Text,
507 Min, Max));
508
509 std::string Error;
510 if (D->isValid(Error)) {
511 DL->push_back(D.release());
512 FoundDirective = true;
513 } else {
514 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
515 diag::err_verify_invalid_content)
516 << KindStr << Error;
517 }
518 }
519
520 return FoundDirective;
521 }
522
523 /// HandleComment - Hook into the preprocessor and extract comments containing
524 /// expected errors and warnings.
HandleComment(Preprocessor & PP,SourceRange Comment)525 bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
526 SourceRange Comment) {
527 SourceManager &SM = PP.getSourceManager();
528
529 // If this comment is for a different source manager, ignore it.
530 if (SrcManager && &SM != SrcManager)
531 return false;
532
533 SourceLocation CommentBegin = Comment.getBegin();
534
535 const char *CommentRaw = SM.getCharacterData(CommentBegin);
536 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
537
538 if (C.empty())
539 return false;
540
541 // Fold any "\<EOL>" sequences
542 size_t loc = C.find('\\');
543 if (loc == StringRef::npos) {
544 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status);
545 return false;
546 }
547
548 std::string C2;
549 C2.reserve(C.size());
550
551 for (size_t last = 0;; loc = C.find('\\', last)) {
552 if (loc == StringRef::npos || loc == C.size()) {
553 C2 += C.substr(last);
554 break;
555 }
556 C2 += C.substr(last, loc-last);
557 last = loc + 1;
558
559 if (C[last] == '\n' || C[last] == '\r') {
560 ++last;
561
562 // Escape \r\n or \n\r, but not \n\n.
563 if (last < C.size())
564 if (C[last] == '\n' || C[last] == '\r')
565 if (C[last] != C[last-1])
566 ++last;
567 } else {
568 // This was just a normal backslash.
569 C2 += '\\';
570 }
571 }
572
573 if (!C2.empty())
574 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status);
575 return false;
576 }
577
578 #ifndef NDEBUG
579 /// \brief Lex the specified source file to determine whether it contains
580 /// any expected-* directives. As a Lexer is used rather than a full-blown
581 /// Preprocessor, directives inside skipped #if blocks will still be found.
582 ///
583 /// \return true if any directives were found.
findDirectives(SourceManager & SM,FileID FID,const LangOptions & LangOpts)584 static bool findDirectives(SourceManager &SM, FileID FID,
585 const LangOptions &LangOpts) {
586 // Create a raw lexer to pull all the comments out of FID.
587 if (FID.isInvalid())
588 return false;
589
590 // Create a lexer to lex all the tokens of the main file in raw mode.
591 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
592 Lexer RawLex(FID, FromFile, SM, LangOpts);
593
594 // Return comments as tokens, this is how we find expected diagnostics.
595 RawLex.SetCommentRetentionState(true);
596
597 Token Tok;
598 Tok.setKind(tok::comment);
599 VerifyDiagnosticConsumer::DirectiveStatus Status =
600 VerifyDiagnosticConsumer::HasNoDirectives;
601 while (Tok.isNot(tok::eof)) {
602 RawLex.LexFromRawLexer(Tok);
603 if (!Tok.is(tok::comment)) continue;
604
605 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
606 if (Comment.empty()) continue;
607
608 // Find first directive.
609 if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
610 Status))
611 return true;
612 }
613 return false;
614 }
615 #endif // !NDEBUG
616
617 /// \brief Takes a list of diagnostics that have been generated but not matched
618 /// by an expected-* directive and produces a diagnostic to the user from this.
PrintUnexpected(DiagnosticsEngine & Diags,SourceManager * SourceMgr,const_diag_iterator diag_begin,const_diag_iterator diag_end,const char * Kind)619 static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
620 const_diag_iterator diag_begin,
621 const_diag_iterator diag_end,
622 const char *Kind) {
623 if (diag_begin == diag_end) return 0;
624
625 SmallString<256> Fmt;
626 llvm::raw_svector_ostream OS(Fmt);
627 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
628 if (I->first.isInvalid() || !SourceMgr)
629 OS << "\n (frontend)";
630 else {
631 OS << "\n ";
632 if (const FileEntry *File = SourceMgr->getFileEntryForID(
633 SourceMgr->getFileID(I->first)))
634 OS << " File " << File->getName();
635 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
636 }
637 OS << ": " << I->second;
638 }
639
640 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
641 << Kind << /*Unexpected=*/true << OS.str();
642 return std::distance(diag_begin, diag_end);
643 }
644
645 /// \brief Takes a list of diagnostics that were expected to have been generated
646 /// but were not and produces a diagnostic to the user from this.
PrintExpected(DiagnosticsEngine & Diags,SourceManager & SourceMgr,DirectiveList & DL,const char * Kind)647 static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
648 DirectiveList &DL, const char *Kind) {
649 if (DL.empty())
650 return 0;
651
652 SmallString<256> Fmt;
653 llvm::raw_svector_ostream OS(Fmt);
654 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
655 Directive &D = **I;
656 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc);
657 if (D.MatchAnyLine)
658 OS << " Line *";
659 else
660 OS << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
661 if (D.DirectiveLoc != D.DiagnosticLoc)
662 OS << " (directive at "
663 << SourceMgr.getFilename(D.DirectiveLoc) << ':'
664 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
665 OS << ": " << D.Text;
666 }
667
668 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
669 << Kind << /*Unexpected=*/false << OS.str();
670 return DL.size();
671 }
672
673 /// \brief Determine whether two source locations come from the same file.
IsFromSameFile(SourceManager & SM,SourceLocation DirectiveLoc,SourceLocation DiagnosticLoc)674 static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
675 SourceLocation DiagnosticLoc) {
676 while (DiagnosticLoc.isMacroID())
677 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
678
679 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
680 return true;
681
682 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
683 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
684 return true;
685
686 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
687 }
688
689 /// CheckLists - Compare expected to seen diagnostic lists and return the
690 /// the difference between them.
691 ///
CheckLists(DiagnosticsEngine & Diags,SourceManager & SourceMgr,const char * Label,DirectiveList & Left,const_diag_iterator d2_begin,const_diag_iterator d2_end)692 static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
693 const char *Label,
694 DirectiveList &Left,
695 const_diag_iterator d2_begin,
696 const_diag_iterator d2_end) {
697 DirectiveList LeftOnly;
698 DiagList Right(d2_begin, d2_end);
699
700 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
701 Directive& D = **I;
702 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
703
704 for (unsigned i = 0; i < D.Max; ++i) {
705 DiagList::iterator II, IE;
706 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
707 if (!D.MatchAnyLine) {
708 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
709 if (LineNo1 != LineNo2)
710 continue;
711 }
712
713 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
714 continue;
715
716 const std::string &RightText = II->second;
717 if (D.match(RightText))
718 break;
719 }
720 if (II == IE) {
721 // Not found.
722 if (i >= D.Min) break;
723 LeftOnly.push_back(*I);
724 } else {
725 // Found. The same cannot be found twice.
726 Right.erase(II);
727 }
728 }
729 }
730 // Now all that's left in Right are those that were not matched.
731 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
732 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
733 return num;
734 }
735
736 /// CheckResults - This compares the expected results to those that
737 /// were actually reported. It emits any discrepencies. Return "true" if there
738 /// were problems. Return "false" otherwise.
739 ///
CheckResults(DiagnosticsEngine & Diags,SourceManager & SourceMgr,const TextDiagnosticBuffer & Buffer,ExpectedData & ED)740 static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
741 const TextDiagnosticBuffer &Buffer,
742 ExpectedData &ED) {
743 // We want to capture the delta between what was expected and what was
744 // seen.
745 //
746 // Expected \ Seen - set expected but not seen
747 // Seen \ Expected - set seen but not expected
748 unsigned NumProblems = 0;
749
750 // See if there are error mismatches.
751 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
752 Buffer.err_begin(), Buffer.err_end());
753
754 // See if there are warning mismatches.
755 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
756 Buffer.warn_begin(), Buffer.warn_end());
757
758 // See if there are remark mismatches.
759 NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
760 Buffer.remark_begin(), Buffer.remark_end());
761
762 // See if there are note mismatches.
763 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
764 Buffer.note_begin(), Buffer.note_end());
765
766 return NumProblems;
767 }
768
UpdateParsedFileStatus(SourceManager & SM,FileID FID,ParsedStatus PS)769 void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
770 FileID FID,
771 ParsedStatus PS) {
772 // Check SourceManager hasn't changed.
773 setSourceManager(SM);
774
775 #ifndef NDEBUG
776 if (FID.isInvalid())
777 return;
778
779 const FileEntry *FE = SM.getFileEntryForID(FID);
780
781 if (PS == IsParsed) {
782 // Move the FileID from the unparsed set to the parsed set.
783 UnparsedFiles.erase(FID);
784 ParsedFiles.insert(std::make_pair(FID, FE));
785 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
786 // Add the FileID to the unparsed set if we haven't seen it before.
787
788 // Check for directives.
789 bool FoundDirectives;
790 if (PS == IsUnparsedNoDirectives)
791 FoundDirectives = false;
792 else
793 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
794
795 // Add the FileID to the unparsed set.
796 UnparsedFiles.insert(std::make_pair(FID,
797 UnparsedFileStatus(FE, FoundDirectives)));
798 }
799 #endif
800 }
801
CheckDiagnostics()802 void VerifyDiagnosticConsumer::CheckDiagnostics() {
803 // Ensure any diagnostics go to the primary client.
804 bool OwnsCurClient = Diags.ownsClient();
805 DiagnosticConsumer *CurClient = Diags.takeClient();
806 Diags.setClient(PrimaryClient, false);
807
808 #ifndef NDEBUG
809 // In a debug build, scan through any files that may have been missed
810 // during parsing and issue a fatal error if directives are contained
811 // within these files. If a fatal error occurs, this suggests that
812 // this file is being parsed separately from the main file, in which
813 // case consider moving the directives to the correct place, if this
814 // is applicable.
815 if (UnparsedFiles.size() > 0) {
816 // Generate a cache of parsed FileEntry pointers for alias lookups.
817 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
818 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
819 End = ParsedFiles.end(); I != End; ++I) {
820 if (const FileEntry *FE = I->second)
821 ParsedFileCache.insert(FE);
822 }
823
824 // Iterate through list of unparsed files.
825 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
826 End = UnparsedFiles.end(); I != End; ++I) {
827 const UnparsedFileStatus &Status = I->second;
828 const FileEntry *FE = Status.getFile();
829
830 // Skip files that have been parsed via an alias.
831 if (FE && ParsedFileCache.count(FE))
832 continue;
833
834 // Report a fatal error if this file contained directives.
835 if (Status.foundDirectives()) {
836 llvm::report_fatal_error(Twine("-verify directives found after rather"
837 " than during normal parsing of ",
838 StringRef(FE ? FE->getName() : "(unknown)")));
839 }
840 }
841
842 // UnparsedFiles has been processed now, so clear it.
843 UnparsedFiles.clear();
844 }
845 #endif // !NDEBUG
846
847 if (SrcManager) {
848 // Produce an error if no expected-* directives could be found in the
849 // source file(s) processed.
850 if (Status == HasNoDirectives) {
851 Diags.Report(diag::err_verify_no_directives).setForceEmit();
852 ++NumErrors;
853 Status = HasNoDirectivesReported;
854 }
855
856 // Check that the expected diagnostics occurred.
857 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
858 } else {
859 NumErrors += (PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
860 Buffer->err_end(), "error") +
861 PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
862 Buffer->warn_end(), "warn") +
863 PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
864 Buffer->note_end(), "note"));
865 }
866
867 Diags.takeClient();
868 Diags.setClient(CurClient, OwnsCurClient);
869
870 // Reset the buffer, we have processed all the diagnostics in it.
871 Buffer.reset(new TextDiagnosticBuffer());
872 ED.Reset();
873 }
874
create(bool RegexKind,SourceLocation DirectiveLoc,SourceLocation DiagnosticLoc,bool MatchAnyLine,StringRef Text,unsigned Min,unsigned Max)875 Directive *Directive::create(bool RegexKind, SourceLocation DirectiveLoc,
876 SourceLocation DiagnosticLoc, bool MatchAnyLine,
877 StringRef Text, unsigned Min, unsigned Max) {
878 if (!RegexKind)
879 return new StandardDirective(DirectiveLoc, DiagnosticLoc, MatchAnyLine,
880 Text, Min, Max);
881
882 // Parse the directive into a regular expression.
883 std::string RegexStr;
884 StringRef S = Text;
885 while (!S.empty()) {
886 if (S.startswith("{{")) {
887 S = S.drop_front(2);
888 size_t RegexMatchLength = S.find("}}");
889 assert(RegexMatchLength != StringRef::npos);
890 // Append the regex, enclosed in parentheses.
891 RegexStr += "(";
892 RegexStr.append(S.data(), RegexMatchLength);
893 RegexStr += ")";
894 S = S.drop_front(RegexMatchLength + 2);
895 } else {
896 size_t VerbatimMatchLength = S.find("{{");
897 if (VerbatimMatchLength == StringRef::npos)
898 VerbatimMatchLength = S.size();
899 // Escape and append the fixed string.
900 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
901 S = S.drop_front(VerbatimMatchLength);
902 }
903 }
904
905 return new RegexDirective(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text,
906 Min, Max, RegexStr);
907 }
908