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 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 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: 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. 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 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 const_cast<Preprocessor*>(PP)->addPPCallbacks( 88 llvm::make_unique<VerifyFileTracker>(*this, *SrcManager)); 89 #endif 90 } 91 } 92 93 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!"); 94 PrimaryClient->BeginSourceFile(LangOpts, PP); 95 } 96 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 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: 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 171 bool isValid(std::string &Error) override { 172 // all strings are considered valid; even empty ones 173 return true; 174 } 175 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: 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 191 bool isValid(std::string &Error) override { 192 if (Regex.isValid(Error)) 193 return true; 194 return false; 195 } 196 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: 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. 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. 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. 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. 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. 282 bool Advance() { 283 C = PEnd; 284 return C < End; 285 } 286 287 // Skip zero or more whitespace. 288 void SkipWhitespace() { 289 for (; C < End && isWhitespace(*C); ++C) 290 ; 291 } 292 293 // Return true if EOF reached. 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. 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 = 406 PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir, 407 nullptr, nullptr, nullptr); 408 if (!FE) { 409 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), 410 diag::err_verify_missing_file) << Filename << KindStr; 411 continue; 412 } 413 414 if (SM.translateFile(FE).isInvalid()) 415 SM.createFileID(FE, Pos, SrcMgr::C_User); 416 417 if (PH.Next(Line) && Line > 0) 418 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1); 419 else if (PH.Next("*")) { 420 MatchAnyLine = true; 421 ExpectedLoc = SM.translateFileLineCol(FE, 1, 1); 422 } 423 } 424 425 if (ExpectedLoc.isInvalid()) { 426 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), 427 diag::err_verify_missing_line) << KindStr; 428 continue; 429 } 430 PH.Advance(); 431 } 432 433 // Skip optional whitespace. 434 PH.SkipWhitespace(); 435 436 // Next optional token: positive integer or a '+'. 437 unsigned Min = 1; 438 unsigned Max = 1; 439 if (PH.Next(Min)) { 440 PH.Advance(); 441 // A positive integer can be followed by a '+' meaning min 442 // or more, or by a '-' meaning a range from min to max. 443 if (PH.Next("+")) { 444 Max = Directive::MaxCount; 445 PH.Advance(); 446 } else if (PH.Next("-")) { 447 PH.Advance(); 448 if (!PH.Next(Max) || Max < Min) { 449 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), 450 diag::err_verify_invalid_range) << KindStr; 451 continue; 452 } 453 PH.Advance(); 454 } else { 455 Max = Min; 456 } 457 } else if (PH.Next("+")) { 458 // '+' on its own means "1 or more". 459 Max = Directive::MaxCount; 460 PH.Advance(); 461 } 462 463 // Skip optional whitespace. 464 PH.SkipWhitespace(); 465 466 // Next token: {{ 467 if (!PH.Next("{{")) { 468 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), 469 diag::err_verify_missing_start) << KindStr; 470 continue; 471 } 472 PH.Advance(); 473 const char* const ContentBegin = PH.C; // mark content begin 474 475 // Search for token: }} 476 if (!PH.SearchClosingBrace("{{", "}}")) { 477 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), 478 diag::err_verify_missing_end) << KindStr; 479 continue; 480 } 481 const char* const ContentEnd = PH.P; // mark content end 482 PH.Advance(); 483 484 // Build directive text; convert \n to newlines. 485 std::string Text; 486 StringRef NewlineStr = "\\n"; 487 StringRef Content(ContentBegin, ContentEnd-ContentBegin); 488 size_t CPos = 0; 489 size_t FPos; 490 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) { 491 Text += Content.substr(CPos, FPos-CPos); 492 Text += '\n'; 493 CPos = FPos + NewlineStr.size(); 494 } 495 if (Text.empty()) 496 Text.assign(ContentBegin, ContentEnd); 497 498 // Check that regex directives contain at least one regex. 499 if (RegexKind && Text.find("{{") == StringRef::npos) { 500 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin), 501 diag::err_verify_missing_regex) << Text; 502 return false; 503 } 504 505 // Construct new directive. 506 std::unique_ptr<Directive> D = Directive::create( 507 RegexKind, Pos, ExpectedLoc, MatchAnyLine, Text, Min, Max); 508 509 std::string Error; 510 if (D->isValid(Error)) { 511 DL->push_back(std::move(D)); 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. 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. 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. 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. 647 static unsigned PrintExpected(DiagnosticsEngine &Diags, 648 SourceManager &SourceMgr, 649 std::vector<Directive *> &DL, const char *Kind) { 650 if (DL.empty()) 651 return 0; 652 653 SmallString<256> Fmt; 654 llvm::raw_svector_ostream OS(Fmt); 655 for (auto *DirPtr : DL) { 656 Directive &D = *DirPtr; 657 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc); 658 if (D.MatchAnyLine) 659 OS << " Line *"; 660 else 661 OS << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); 662 if (D.DirectiveLoc != D.DiagnosticLoc) 663 OS << " (directive at " 664 << SourceMgr.getFilename(D.DirectiveLoc) << ':' 665 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')'; 666 OS << ": " << D.Text; 667 } 668 669 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() 670 << Kind << /*Unexpected=*/false << OS.str(); 671 return DL.size(); 672 } 673 674 /// \brief Determine whether two source locations come from the same file. 675 static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc, 676 SourceLocation DiagnosticLoc) { 677 while (DiagnosticLoc.isMacroID()) 678 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc); 679 680 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc)) 681 return true; 682 683 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc)); 684 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc)) 685 return true; 686 687 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc))); 688 } 689 690 /// CheckLists - Compare expected to seen diagnostic lists and return the 691 /// the difference between them. 692 /// 693 static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, 694 const char *Label, 695 DirectiveList &Left, 696 const_diag_iterator d2_begin, 697 const_diag_iterator d2_end) { 698 std::vector<Directive *> LeftOnly; 699 DiagList Right(d2_begin, d2_end); 700 701 for (auto &Owner : Left) { 702 Directive &D = *Owner; 703 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); 704 705 for (unsigned i = 0; i < D.Max; ++i) { 706 DiagList::iterator II, IE; 707 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) { 708 if (!D.MatchAnyLine) { 709 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first); 710 if (LineNo1 != LineNo2) 711 continue; 712 } 713 714 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first)) 715 continue; 716 717 const std::string &RightText = II->second; 718 if (D.match(RightText)) 719 break; 720 } 721 if (II == IE) { 722 // Not found. 723 if (i >= D.Min) break; 724 LeftOnly.push_back(&D); 725 } else { 726 // Found. The same cannot be found twice. 727 Right.erase(II); 728 } 729 } 730 } 731 // Now all that's left in Right are those that were not matched. 732 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label); 733 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label); 734 return num; 735 } 736 737 /// CheckResults - This compares the expected results to those that 738 /// were actually reported. It emits any discrepencies. Return "true" if there 739 /// were problems. Return "false" otherwise. 740 /// 741 static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, 742 const TextDiagnosticBuffer &Buffer, 743 ExpectedData &ED) { 744 // We want to capture the delta between what was expected and what was 745 // seen. 746 // 747 // Expected \ Seen - set expected but not seen 748 // Seen \ Expected - set seen but not expected 749 unsigned NumProblems = 0; 750 751 // See if there are error mismatches. 752 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, 753 Buffer.err_begin(), Buffer.err_end()); 754 755 // See if there are warning mismatches. 756 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, 757 Buffer.warn_begin(), Buffer.warn_end()); 758 759 // See if there are remark mismatches. 760 NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks, 761 Buffer.remark_begin(), Buffer.remark_end()); 762 763 // See if there are note mismatches. 764 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, 765 Buffer.note_begin(), Buffer.note_end()); 766 767 return NumProblems; 768 } 769 770 void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM, 771 FileID FID, 772 ParsedStatus PS) { 773 // Check SourceManager hasn't changed. 774 setSourceManager(SM); 775 776 #ifndef NDEBUG 777 if (FID.isInvalid()) 778 return; 779 780 const FileEntry *FE = SM.getFileEntryForID(FID); 781 782 if (PS == IsParsed) { 783 // Move the FileID from the unparsed set to the parsed set. 784 UnparsedFiles.erase(FID); 785 ParsedFiles.insert(std::make_pair(FID, FE)); 786 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) { 787 // Add the FileID to the unparsed set if we haven't seen it before. 788 789 // Check for directives. 790 bool FoundDirectives; 791 if (PS == IsUnparsedNoDirectives) 792 FoundDirectives = false; 793 else 794 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts); 795 796 // Add the FileID to the unparsed set. 797 UnparsedFiles.insert(std::make_pair(FID, 798 UnparsedFileStatus(FE, FoundDirectives))); 799 } 800 #endif 801 } 802 803 void VerifyDiagnosticConsumer::CheckDiagnostics() { 804 // Ensure any diagnostics go to the primary client. 805 bool OwnsCurClient = Diags.ownsClient(); 806 DiagnosticConsumer *CurClient = Diags.takeClient(); 807 Diags.setClient(PrimaryClient, false); 808 809 #ifndef NDEBUG 810 // In a debug build, scan through any files that may have been missed 811 // during parsing and issue a fatal error if directives are contained 812 // within these files. If a fatal error occurs, this suggests that 813 // this file is being parsed separately from the main file, in which 814 // case consider moving the directives to the correct place, if this 815 // is applicable. 816 if (UnparsedFiles.size() > 0) { 817 // Generate a cache of parsed FileEntry pointers for alias lookups. 818 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache; 819 for (ParsedFilesMap::iterator I = ParsedFiles.begin(), 820 End = ParsedFiles.end(); I != End; ++I) { 821 if (const FileEntry *FE = I->second) 822 ParsedFileCache.insert(FE); 823 } 824 825 // Iterate through list of unparsed files. 826 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(), 827 End = UnparsedFiles.end(); I != End; ++I) { 828 const UnparsedFileStatus &Status = I->second; 829 const FileEntry *FE = Status.getFile(); 830 831 // Skip files that have been parsed via an alias. 832 if (FE && ParsedFileCache.count(FE)) 833 continue; 834 835 // Report a fatal error if this file contained directives. 836 if (Status.foundDirectives()) { 837 llvm::report_fatal_error(Twine("-verify directives found after rather" 838 " than during normal parsing of ", 839 StringRef(FE ? FE->getName() : "(unknown)"))); 840 } 841 } 842 843 // UnparsedFiles has been processed now, so clear it. 844 UnparsedFiles.clear(); 845 } 846 #endif // !NDEBUG 847 848 if (SrcManager) { 849 // Produce an error if no expected-* directives could be found in the 850 // source file(s) processed. 851 if (Status == HasNoDirectives) { 852 Diags.Report(diag::err_verify_no_directives).setForceEmit(); 853 ++NumErrors; 854 Status = HasNoDirectivesReported; 855 } 856 857 // Check that the expected diagnostics occurred. 858 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED); 859 } else { 860 NumErrors += (PrintUnexpected(Diags, nullptr, Buffer->err_begin(), 861 Buffer->err_end(), "error") + 862 PrintUnexpected(Diags, nullptr, Buffer->warn_begin(), 863 Buffer->warn_end(), "warn") + 864 PrintUnexpected(Diags, nullptr, Buffer->note_begin(), 865 Buffer->note_end(), "note")); 866 } 867 868 Diags.takeClient(); 869 Diags.setClient(CurClient, OwnsCurClient); 870 871 // Reset the buffer, we have processed all the diagnostics in it. 872 Buffer.reset(new TextDiagnosticBuffer()); 873 ED.Reset(); 874 } 875 876 std::unique_ptr<Directive> Directive::create(bool RegexKind, 877 SourceLocation DirectiveLoc, 878 SourceLocation DiagnosticLoc, 879 bool MatchAnyLine, StringRef Text, 880 unsigned Min, unsigned Max) { 881 if (!RegexKind) 882 return llvm::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc, 883 MatchAnyLine, Text, Min, Max); 884 885 // Parse the directive into a regular expression. 886 std::string RegexStr; 887 StringRef S = Text; 888 while (!S.empty()) { 889 if (S.startswith("{{")) { 890 S = S.drop_front(2); 891 size_t RegexMatchLength = S.find("}}"); 892 assert(RegexMatchLength != StringRef::npos); 893 // Append the regex, enclosed in parentheses. 894 RegexStr += "("; 895 RegexStr.append(S.data(), RegexMatchLength); 896 RegexStr += ")"; 897 S = S.drop_front(RegexMatchLength + 2); 898 } else { 899 size_t VerbatimMatchLength = S.find("{{"); 900 if (VerbatimMatchLength == StringRef::npos) 901 VerbatimMatchLength = S.size(); 902 // Escape and append the fixed string. 903 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength)); 904 S = S.drop_front(VerbatimMatchLength); 905 } 906 } 907 908 return llvm::make_unique<RegexDirective>( 909 DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr); 910 } 911