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 = 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 = Directive::create( 506 RegexKind, Pos, ExpectedLoc, MatchAnyLine, Text, Min, Max); 507 508 std::string Error; 509 if (D->isValid(Error)) { 510 DL->push_back(std::move(D)); 511 FoundDirective = true; 512 } else { 513 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin), 514 diag::err_verify_invalid_content) 515 << KindStr << Error; 516 } 517 } 518 519 return FoundDirective; 520 } 521 522 /// HandleComment - Hook into the preprocessor and extract comments containing 523 /// expected errors and warnings. 524 bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP, 525 SourceRange Comment) { 526 SourceManager &SM = PP.getSourceManager(); 527 528 // If this comment is for a different source manager, ignore it. 529 if (SrcManager && &SM != SrcManager) 530 return false; 531 532 SourceLocation CommentBegin = Comment.getBegin(); 533 534 const char *CommentRaw = SM.getCharacterData(CommentBegin); 535 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw); 536 537 if (C.empty()) 538 return false; 539 540 // Fold any "\<EOL>" sequences 541 size_t loc = C.find('\\'); 542 if (loc == StringRef::npos) { 543 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status); 544 return false; 545 } 546 547 std::string C2; 548 C2.reserve(C.size()); 549 550 for (size_t last = 0;; loc = C.find('\\', last)) { 551 if (loc == StringRef::npos || loc == C.size()) { 552 C2 += C.substr(last); 553 break; 554 } 555 C2 += C.substr(last, loc-last); 556 last = loc + 1; 557 558 if (C[last] == '\n' || C[last] == '\r') { 559 ++last; 560 561 // Escape \r\n or \n\r, but not \n\n. 562 if (last < C.size()) 563 if (C[last] == '\n' || C[last] == '\r') 564 if (C[last] != C[last-1]) 565 ++last; 566 } else { 567 // This was just a normal backslash. 568 C2 += '\\'; 569 } 570 } 571 572 if (!C2.empty()) 573 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status); 574 return false; 575 } 576 577 #ifndef NDEBUG 578 /// \brief Lex the specified source file to determine whether it contains 579 /// any expected-* directives. As a Lexer is used rather than a full-blown 580 /// Preprocessor, directives inside skipped #if blocks will still be found. 581 /// 582 /// \return true if any directives were found. 583 static bool findDirectives(SourceManager &SM, FileID FID, 584 const LangOptions &LangOpts) { 585 // Create a raw lexer to pull all the comments out of FID. 586 if (FID.isInvalid()) 587 return false; 588 589 // Create a lexer to lex all the tokens of the main file in raw mode. 590 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); 591 Lexer RawLex(FID, FromFile, SM, LangOpts); 592 593 // Return comments as tokens, this is how we find expected diagnostics. 594 RawLex.SetCommentRetentionState(true); 595 596 Token Tok; 597 Tok.setKind(tok::comment); 598 VerifyDiagnosticConsumer::DirectiveStatus Status = 599 VerifyDiagnosticConsumer::HasNoDirectives; 600 while (Tok.isNot(tok::eof)) { 601 RawLex.LexFromRawLexer(Tok); 602 if (!Tok.is(tok::comment)) continue; 603 604 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts); 605 if (Comment.empty()) continue; 606 607 // Find first directive. 608 if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(), 609 Status)) 610 return true; 611 } 612 return false; 613 } 614 #endif // !NDEBUG 615 616 /// \brief Takes a list of diagnostics that have been generated but not matched 617 /// by an expected-* directive and produces a diagnostic to the user from this. 618 static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr, 619 const_diag_iterator diag_begin, 620 const_diag_iterator diag_end, 621 const char *Kind) { 622 if (diag_begin == diag_end) return 0; 623 624 SmallString<256> Fmt; 625 llvm::raw_svector_ostream OS(Fmt); 626 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) { 627 if (I->first.isInvalid() || !SourceMgr) 628 OS << "\n (frontend)"; 629 else { 630 OS << "\n "; 631 if (const FileEntry *File = SourceMgr->getFileEntryForID( 632 SourceMgr->getFileID(I->first))) 633 OS << " File " << File->getName(); 634 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first); 635 } 636 OS << ": " << I->second; 637 } 638 639 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() 640 << Kind << /*Unexpected=*/true << OS.str(); 641 return std::distance(diag_begin, diag_end); 642 } 643 644 /// \brief Takes a list of diagnostics that were expected to have been generated 645 /// but were not and produces a diagnostic to the user from this. 646 static unsigned PrintExpected(DiagnosticsEngine &Diags, 647 SourceManager &SourceMgr, 648 std::vector<Directive *> &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 (auto *DirPtr : DL) { 655 Directive &D = *DirPtr; 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. 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 /// 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 std::vector<Directive *> LeftOnly; 698 DiagList Right(d2_begin, d2_end); 699 700 for (auto &Owner : Left) { 701 Directive &D = *Owner; 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(&D); 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 /// 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 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 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 875 std::unique_ptr<Directive> Directive::create(bool RegexKind, 876 SourceLocation DirectiveLoc, 877 SourceLocation DiagnosticLoc, 878 bool MatchAnyLine, StringRef Text, 879 unsigned Min, unsigned Max) { 880 if (!RegexKind) 881 return llvm::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc, 882 MatchAnyLine, Text, Min, Max); 883 884 // Parse the directive into a regular expression. 885 std::string RegexStr; 886 StringRef S = Text; 887 while (!S.empty()) { 888 if (S.startswith("{{")) { 889 S = S.drop_front(2); 890 size_t RegexMatchLength = S.find("}}"); 891 assert(RegexMatchLength != StringRef::npos); 892 // Append the regex, enclosed in parentheses. 893 RegexStr += "("; 894 RegexStr.append(S.data(), RegexMatchLength); 895 RegexStr += ")"; 896 S = S.drop_front(RegexMatchLength + 2); 897 } else { 898 size_t VerbatimMatchLength = S.find("{{"); 899 if (VerbatimMatchLength == StringRef::npos) 900 VerbatimMatchLength = S.size(); 901 // Escape and append the fixed string. 902 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength)); 903 S = S.drop_front(VerbatimMatchLength); 904 } 905 } 906 907 return llvm::make_unique<RegexDirective>( 908 DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr); 909 } 910