1 //===- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This is a concrete diagnostic client, which buffers the diagnostic messages.
11 //===----------------------------------------------------------------------===//
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"
47 using namespace clang;
49 using Directive = VerifyDiagnosticConsumer::Directive;
50 using DirectiveList = VerifyDiagnosticConsumer::DirectiveList;
51 using ExpectedData = VerifyDiagnosticConsumer::ExpectedData;
57 class VerifyFileTracker : public PPCallbacks {
58 VerifyDiagnosticConsumer &Verify;
62 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
63 : Verify(Verify), SM(SM) {}
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 FileID PrevFID) override {
70 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
71 VerifyDiagnosticConsumer::IsParsed);
79 //===----------------------------------------------------------------------===//
80 // Checking diagnostics implementation.
81 //===----------------------------------------------------------------------===//
83 using DiagList = TextDiagnosticBuffer::DiagList;
84 using const_diag_iterator = TextDiagnosticBuffer::const_iterator;
88 /// StandardDirective - Directive with string matching.
89 class StandardDirective : public Directive {
91 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
92 bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
93 unsigned Min, unsigned Max)
94 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyFileAndLine,
95 MatchAnyLine, Text, Min, Max) {}
97 bool isValid(std::string &Error) override {
98 // all strings are considered valid; even empty ones
102 bool match(StringRef S) override {
103 return S.find(Text) != StringRef::npos;
107 /// RegexDirective - Directive with regular-expression matching.
108 class RegexDirective : public Directive {
110 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
111 bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
112 unsigned Min, unsigned Max, StringRef RegexStr)
113 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyFileAndLine,
114 MatchAnyLine, Text, Min, Max),
117 bool isValid(std::string &Error) override {
118 return Regex.isValid(Error);
121 bool match(StringRef S) override {
122 return Regex.match(S);
132 ParseHelper(StringRef S)
133 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin) {}
135 // Return true if string literal is next.
136 bool Next(StringRef S) {
141 return memcmp(P, S.data(), S.size()) == 0;
144 // Return true if number is next.
145 // Output N only if number is next.
146 bool Next(unsigned &N) {
150 for (; PEnd < End && *PEnd >= '0' && *PEnd <= '9'; ++PEnd) {
160 // Return true if a marker is next.
161 // A marker is the longest match for /#[A-Za-z0-9_-]+/.
164 if (P == End || *P != '#')
168 while ((isAlphanumeric(*PEnd) || *PEnd == '-' || *PEnd == '_') &&
174 // Return true if string literal S is matched in content.
175 // When true, P marks begin-position of the match, and calling Advance sets C
176 // to end-position of the match.
177 // If S is the empty string, then search for any letter instead (makes sense
178 // with FinishDirectiveToken=true).
179 // If EnsureStartOfWord, then skip matches that don't start a new word.
180 // If FinishDirectiveToken, then assume the match is the start of a comment
181 // directive for -verify, and extend the match to include the entire first
182 // token of that directive.
183 bool Search(StringRef S, bool EnsureStartOfWord = false,
184 bool FinishDirectiveToken = false) {
187 P = std::search(C, End, S.begin(), S.end());
192 while (P != End && !isLetter(*P))
198 // If not start of word but required, skip and search again.
199 if (EnsureStartOfWord
200 // Check if string literal starts a new word.
201 && !(P == Begin || isWhitespace(P[-1])
202 // Or it could be preceded by the start of a comment.
203 || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*')
206 if (FinishDirectiveToken) {
207 while (PEnd != End && (isAlphanumeric(*PEnd)
208 || *PEnd == '-' || *PEnd == '_'))
210 // Put back trailing digits and hyphens to be parsed later as a count
211 // or count range. Because -verify prefixes must start with letters,
212 // we know the actual directive we found starts with a letter, so
213 // we won't put back the entire directive word and thus record an empty
215 assert(isLetter(*P) && "-verify prefix must start with a letter");
216 while (isDigit(PEnd[-1]) || PEnd[-1] == '-')
224 // Return true if a CloseBrace that closes the OpenBrace at the current nest
225 // level is found. When true, P marks begin-position of CloseBrace.
226 bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) {
230 StringRef S(P, End - P);
231 if (S.startswith(OpenBrace)) {
233 P += OpenBrace.size();
234 } else if (S.startswith(CloseBrace)) {
237 PEnd = P + CloseBrace.size();
240 P += CloseBrace.size();
248 // Advance 1-past previous next/search.
249 // Behavior is undefined if previous next/search failed.
255 // Return the text matched by the previous next/search.
256 // Behavior is undefined if previous next/search failed.
257 StringRef Match() { return StringRef(P, PEnd - P); }
259 // Skip zero or more whitespace.
260 void SkipWhitespace() {
261 for (; C < End && isWhitespace(*C); ++C)
265 // Return true if EOF reached.
270 // Beginning of expected content.
271 const char * const Begin;
273 // End of expected content (1-past).
274 const char * const End;
276 // Position of next char in content.
279 // Previous next/search subject start.
283 // Previous next/search subject end (1-past).
284 const char *PEnd = nullptr;
287 // The information necessary to create a directive.
288 struct UnattachedDirective {
289 DirectiveList *DL = nullptr;
290 bool RegexKind = false;
291 SourceLocation DirectivePos, ContentBegin;
293 unsigned Min = 1, Max = 1;
296 // Attach the specified directive to the line of code indicated by
298 void attachDirective(DiagnosticsEngine &Diags, const UnattachedDirective &UD,
299 SourceLocation ExpectedLoc,
300 bool MatchAnyFileAndLine = false,
301 bool MatchAnyLine = false) {
302 // Construct new directive.
303 std::unique_ptr<Directive> D = Directive::create(
304 UD.RegexKind, UD.DirectivePos, ExpectedLoc, MatchAnyFileAndLine,
305 MatchAnyLine, UD.Text, UD.Min, UD.Max);
308 if (!D->isValid(Error)) {
309 Diags.Report(UD.ContentBegin, diag::err_verify_invalid_content)
310 << (UD.RegexKind ? "regex" : "string") << Error;
313 UD.DL->push_back(std::move(D));
318 // Tracker for markers in the input files. A marker is a comment of the form
322 // ... that can be referred to by a later expected-* directive:
324 // // expected-error@#123 {{undeclared identifier 'n'}}
326 // Marker declarations must be at the start of a comment or preceded by
327 // whitespace to distinguish them from uses of markers in directives.
328 class VerifyDiagnosticConsumer::MarkerTracker {
329 DiagnosticsEngine &Diags;
332 SourceLocation DefLoc;
333 SourceLocation RedefLoc;
334 SourceLocation UseLoc;
336 llvm::StringMap<Marker> Markers;
338 // Directives that couldn't be created yet because they name an unknown
340 llvm::StringMap<llvm::SmallVector<UnattachedDirective, 2>> DeferredDirectives;
343 MarkerTracker(DiagnosticsEngine &Diags) : Diags(Diags) {}
345 // Register a marker.
346 void addMarker(StringRef MarkerName, SourceLocation Pos) {
347 auto InsertResult = Markers.insert(
348 {MarkerName, Marker{Pos, SourceLocation(), SourceLocation()}});
350 Marker &M = InsertResult.first->second;
351 if (!InsertResult.second) {
352 // Marker was redefined.
355 // First definition: build any deferred directives.
356 auto Deferred = DeferredDirectives.find(MarkerName);
357 if (Deferred != DeferredDirectives.end()) {
358 for (auto &UD : Deferred->second) {
359 if (M.UseLoc.isInvalid())
360 M.UseLoc = UD.DirectivePos;
361 attachDirective(Diags, UD, Pos);
363 DeferredDirectives.erase(Deferred);
368 // Register a directive at the specified marker.
369 void addDirective(StringRef MarkerName, const UnattachedDirective &UD) {
370 auto MarkerIt = Markers.find(MarkerName);
371 if (MarkerIt != Markers.end()) {
372 Marker &M = MarkerIt->second;
373 if (M.UseLoc.isInvalid())
374 M.UseLoc = UD.DirectivePos;
375 return attachDirective(Diags, UD, M.DefLoc);
377 DeferredDirectives[MarkerName].push_back(UD);
380 // Ensure we have no remaining deferred directives, and no
381 // multiply-defined-and-used markers.
383 for (auto &MarkerInfo : Markers) {
384 StringRef Name = MarkerInfo.first();
385 Marker &M = MarkerInfo.second;
386 if (M.RedefLoc.isValid() && M.UseLoc.isValid()) {
387 Diags.Report(M.UseLoc, diag::err_verify_ambiguous_marker) << Name;
388 Diags.Report(M.DefLoc, diag::note_verify_ambiguous_marker) << Name;
389 Diags.Report(M.RedefLoc, diag::note_verify_ambiguous_marker) << Name;
393 for (auto &DeferredPair : DeferredDirectives) {
394 Diags.Report(DeferredPair.second.front().DirectivePos,
395 diag::err_verify_no_such_marker)
396 << DeferredPair.first();
401 /// ParseDirective - Go through the comment and see if it indicates expected
402 /// diagnostics. If so, then put them in the appropriate directive list.
404 /// Returns true if any valid directives were found.
405 static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
406 Preprocessor *PP, SourceLocation Pos,
407 VerifyDiagnosticConsumer::DirectiveStatus &Status,
408 VerifyDiagnosticConsumer::MarkerTracker &Markers) {
409 DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
411 // First, scan the comment looking for markers.
412 for (ParseHelper PH(S); !PH.Done();) {
413 if (!PH.Search("#", true))
416 if (!PH.NextMarker()) {
422 Markers.addMarker(PH.Match(), Pos);
425 // A single comment may contain multiple directives.
426 bool FoundDirective = false;
427 for (ParseHelper PH(S); !PH.Done();) {
428 // Search for the initial directive token.
429 // If one prefix, save time by searching only for its directives.
430 // Otherwise, search for any potential directive token and check it later.
431 const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes;
432 if (!(Prefixes.size() == 1 ? PH.Search(*Prefixes.begin(), true, true)
433 : PH.Search("", true, true)))
436 StringRef DToken = PH.Match();
439 // Default directive kind.
440 UnattachedDirective D;
441 const char *KindStr = "string";
443 // Parse the initial directive token in reverse so we can easily determine
444 // its exact actual prefix. If we were to parse it from the front instead,
445 // it would be harder to determine where the prefix ends because there
446 // might be multiple matching -verify prefixes because some might prefix
449 // Regex in initial directive token: -re
450 if (DToken.endswith("-re")) {
453 DToken = DToken.substr(0, DToken.size()-3);
456 // Type in initial directive token: -{error|warning|note|no-diagnostics}
459 if (DToken.endswith(DType="-error"))
460 D.DL = ED ? &ED->Errors : nullptr;
461 else if (DToken.endswith(DType="-warning"))
462 D.DL = ED ? &ED->Warnings : nullptr;
463 else if (DToken.endswith(DType="-remark"))
464 D.DL = ED ? &ED->Remarks : nullptr;
465 else if (DToken.endswith(DType="-note"))
466 D.DL = ED ? &ED->Notes : nullptr;
467 else if (DToken.endswith(DType="-no-diagnostics")) {
474 DToken = DToken.substr(0, DToken.size()-DType.size());
476 // What's left in DToken is the actual prefix. That might not be a -verify
477 // prefix even if there is only one -verify prefix (for example, the full
478 // DToken is foo-bar-warning, but foo is the only -verify prefix).
479 if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken))
483 if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
484 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
485 << /*IsExpectedNoDiagnostics=*/true;
487 Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
490 if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
491 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
492 << /*IsExpectedNoDiagnostics=*/false;
495 Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
497 // If a directive has been found but we're not interested
498 // in storing the directive information, return now.
502 // Next optional token: @
503 SourceLocation ExpectedLoc;
505 bool MatchAnyFileAndLine = false;
506 bool MatchAnyLine = false;
512 bool FoundPlus = PH.Next("+");
513 if (FoundPlus || PH.Next("-")) {
514 // Relative to current line.
516 bool Invalid = false;
517 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
518 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
519 if (FoundPlus) ExpectedLine += Line;
520 else ExpectedLine -= Line;
521 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
523 } else if (PH.Next(Line)) {
524 // Absolute line number.
526 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
527 } else if (PH.NextMarker()) {
529 } else if (PP && PH.Search(":")) {
530 // Specific source file.
531 StringRef Filename(PH.C, PH.P-PH.C);
534 if (Filename == "*") {
535 MatchAnyFileAndLine = true;
537 Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin),
538 diag::err_verify_missing_line)
543 ExpectedLoc = SourceLocation();
545 // Lookup file via Preprocessor, like a #include.
546 const DirectoryLookup *CurDir;
547 Optional<FileEntryRef> File =
548 PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
549 nullptr, nullptr, nullptr, nullptr, nullptr);
551 Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin),
552 diag::err_verify_missing_file)
553 << Filename << KindStr;
557 const FileEntry *FE = &File->getFileEntry();
558 if (SM.translateFile(FE).isInvalid())
559 SM.createFileID(FE, Pos, SrcMgr::C_User);
561 if (PH.Next(Line) && Line > 0)
562 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
563 else if (PH.Next("*")) {
565 ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
568 } else if (PH.Next("*")) {
570 ExpectedLoc = SourceLocation();
573 if (ExpectedLoc.isInvalid() && !MatchAnyLine && Marker.empty()) {
574 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
575 diag::err_verify_missing_line) << KindStr;
581 // Skip optional whitespace.
584 // Next optional token: positive integer or a '+'.
585 if (PH.Next(D.Min)) {
587 // A positive integer can be followed by a '+' meaning min
588 // or more, or by a '-' meaning a range from min to max.
590 D.Max = Directive::MaxCount;
592 } else if (PH.Next("-")) {
594 if (!PH.Next(D.Max) || D.Max < D.Min) {
595 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
596 diag::err_verify_invalid_range) << KindStr;
603 } else if (PH.Next("+")) {
604 // '+' on its own means "1 or more".
605 D.Max = Directive::MaxCount;
609 // Skip optional whitespace.
613 if (!PH.Next("{{")) {
614 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
615 diag::err_verify_missing_start) << KindStr;
619 const char* const ContentBegin = PH.C; // mark content begin
620 // Search for token: }}
621 if (!PH.SearchClosingBrace("{{", "}}")) {
622 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
623 diag::err_verify_missing_end) << KindStr;
626 const char* const ContentEnd = PH.P; // mark content end
629 D.DirectivePos = Pos;
630 D.ContentBegin = Pos.getLocWithOffset(ContentBegin - PH.Begin);
632 // Build directive text; convert \n to newlines.
633 StringRef NewlineStr = "\\n";
634 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
637 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
638 D.Text += Content.substr(CPos, FPos-CPos);
640 CPos = FPos + NewlineStr.size();
643 D.Text.assign(ContentBegin, ContentEnd);
645 // Check that regex directives contain at least one regex.
646 if (D.RegexKind && D.Text.find("{{") == StringRef::npos) {
647 Diags.Report(D.ContentBegin, diag::err_verify_missing_regex) << D.Text;
652 attachDirective(Diags, D, ExpectedLoc, MatchAnyFileAndLine, MatchAnyLine);
654 Markers.addDirective(Marker, D);
655 FoundDirective = true;
658 return FoundDirective;
661 VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_)
662 : Diags(Diags_), PrimaryClient(Diags.getClient()),
663 PrimaryClientOwner(Diags.takeClient()),
664 Buffer(new TextDiagnosticBuffer()), Markers(new MarkerTracker(Diags)),
665 Status(HasNoDirectives) {
666 if (Diags.hasSourceManager())
667 setSourceManager(Diags.getSourceManager());
670 VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
671 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
672 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
673 SrcManager = nullptr;
675 assert(!Diags.ownsClient() &&
676 "The VerifyDiagnosticConsumer takes over ownership of the client!");
679 // DiagnosticConsumer interface.
681 void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
682 const Preprocessor *PP) {
683 // Attach comment handler on first invocation.
684 if (++ActiveSourceFiles == 1) {
686 CurrentPreprocessor = PP;
687 this->LangOpts = &LangOpts;
688 setSourceManager(PP->getSourceManager());
689 const_cast<Preprocessor *>(PP)->addCommentHandler(this);
691 // Debug build tracks parsed files.
692 const_cast<Preprocessor *>(PP)->addPPCallbacks(
693 std::make_unique<VerifyFileTracker>(*this, *SrcManager));
698 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
699 PrimaryClient->BeginSourceFile(LangOpts, PP);
702 void VerifyDiagnosticConsumer::EndSourceFile() {
703 assert(ActiveSourceFiles && "No active source files!");
704 PrimaryClient->EndSourceFile();
706 // Detach comment handler once last active source file completed.
707 if (--ActiveSourceFiles == 0) {
708 if (CurrentPreprocessor)
709 const_cast<Preprocessor *>(CurrentPreprocessor)->
710 removeCommentHandler(this);
712 // Diagnose any used-but-not-defined markers.
715 // Check diagnostics once last file completed.
717 CurrentPreprocessor = nullptr;
722 void VerifyDiagnosticConsumer::HandleDiagnostic(
723 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
724 if (Info.hasSourceManager()) {
725 // If this diagnostic is for a different source manager, ignore it.
726 if (SrcManager && &Info.getSourceManager() != SrcManager)
729 setSourceManager(Info.getSourceManager());
733 // Debug build tracks unparsed files for possible
734 // unparsed expected-* directives.
736 SourceLocation Loc = Info.getLocation();
738 ParsedStatus PS = IsUnparsed;
740 Loc = SrcManager->getExpansionLoc(Loc);
741 FileID FID = SrcManager->getFileID(Loc);
743 const FileEntry *FE = SrcManager->getFileEntryForID(FID);
744 if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
745 // If the file is a modules header file it shall not be parsed
746 // for expected-* directives.
747 HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
748 if (HS.findModuleForHeader(FE))
749 PS = IsUnparsedNoDirectives;
752 UpdateParsedFileStatus(*SrcManager, FID, PS);
757 // Send the diagnostic to the buffer, we will check it once we reach the end
758 // of the source file (or are destructed).
759 Buffer->HandleDiagnostic(DiagLevel, Info);
762 /// HandleComment - Hook into the preprocessor and extract comments containing
763 /// expected errors and warnings.
764 bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
765 SourceRange Comment) {
766 SourceManager &SM = PP.getSourceManager();
768 // If this comment is for a different source manager, ignore it.
769 if (SrcManager && &SM != SrcManager)
772 SourceLocation CommentBegin = Comment.getBegin();
774 const char *CommentRaw = SM.getCharacterData(CommentBegin);
775 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
780 // Fold any "\<EOL>" sequences
781 size_t loc = C.find('\\');
782 if (loc == StringRef::npos) {
783 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status, *Markers);
788 C2.reserve(C.size());
790 for (size_t last = 0;; loc = C.find('\\', last)) {
791 if (loc == StringRef::npos || loc == C.size()) {
792 C2 += C.substr(last);
795 C2 += C.substr(last, loc-last);
798 if (C[last] == '\n' || C[last] == '\r') {
801 // Escape \r\n or \n\r, but not \n\n.
803 if (C[last] == '\n' || C[last] == '\r')
804 if (C[last] != C[last-1])
807 // This was just a normal backslash.
813 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status, *Markers);
818 /// Lex the specified source file to determine whether it contains
819 /// any expected-* directives. As a Lexer is used rather than a full-blown
820 /// Preprocessor, directives inside skipped #if blocks will still be found.
822 /// \return true if any directives were found.
823 static bool findDirectives(SourceManager &SM, FileID FID,
824 const LangOptions &LangOpts) {
825 // Create a raw lexer to pull all the comments out of FID.
829 // Create a lexer to lex all the tokens of the main file in raw mode.
830 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
831 Lexer RawLex(FID, FromFile, SM, LangOpts);
833 // Return comments as tokens, this is how we find expected diagnostics.
834 RawLex.SetCommentRetentionState(true);
837 Tok.setKind(tok::comment);
838 VerifyDiagnosticConsumer::DirectiveStatus Status =
839 VerifyDiagnosticConsumer::HasNoDirectives;
840 while (Tok.isNot(tok::eof)) {
841 RawLex.LexFromRawLexer(Tok);
842 if (!Tok.is(tok::comment)) continue;
844 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
845 if (Comment.empty()) continue;
847 // We don't care about tracking markers for this phase.
848 VerifyDiagnosticConsumer::MarkerTracker Markers(SM.getDiagnostics());
850 // Find first directive.
851 if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
859 /// Takes a list of diagnostics that have been generated but not matched
860 /// by an expected-* directive and produces a diagnostic to the user from this.
861 static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
862 const_diag_iterator diag_begin,
863 const_diag_iterator diag_end,
865 if (diag_begin == diag_end) return 0;
867 SmallString<256> Fmt;
868 llvm::raw_svector_ostream OS(Fmt);
869 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
870 if (I->first.isInvalid() || !SourceMgr)
871 OS << "\n (frontend)";
874 if (const FileEntry *File = SourceMgr->getFileEntryForID(
875 SourceMgr->getFileID(I->first)))
876 OS << " File " << File->getName();
877 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
879 OS << ": " << I->second;
882 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
883 << Kind << /*Unexpected=*/true << OS.str();
884 return std::distance(diag_begin, diag_end);
887 /// Takes a list of diagnostics that were expected to have been generated
888 /// but were not and produces a diagnostic to the user from this.
889 static unsigned PrintExpected(DiagnosticsEngine &Diags,
890 SourceManager &SourceMgr,
891 std::vector<Directive *> &DL, const char *Kind) {
895 SmallString<256> Fmt;
896 llvm::raw_svector_ostream OS(Fmt);
897 for (const auto *D : DL) {
898 if (D->DiagnosticLoc.isInvalid() || D->MatchAnyFileAndLine)
901 OS << "\n File " << SourceMgr.getFilename(D->DiagnosticLoc);
905 OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc);
906 if (D->DirectiveLoc != D->DiagnosticLoc)
907 OS << " (directive at "
908 << SourceMgr.getFilename(D->DirectiveLoc) << ':'
909 << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')';
910 OS << ": " << D->Text;
913 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
914 << Kind << /*Unexpected=*/false << OS.str();
918 /// Determine whether two source locations come from the same file.
919 static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
920 SourceLocation DiagnosticLoc) {
921 while (DiagnosticLoc.isMacroID())
922 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
924 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
927 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
928 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
931 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
934 /// CheckLists - Compare expected to seen diagnostic lists and return the
935 /// the difference between them.
936 static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
939 const_diag_iterator d2_begin,
940 const_diag_iterator d2_end,
941 bool IgnoreUnexpected) {
942 std::vector<Directive *> LeftOnly;
943 DiagList Right(d2_begin, d2_end);
945 for (auto &Owner : Left) {
946 Directive &D = *Owner;
947 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
949 for (unsigned i = 0; i < D.Max; ++i) {
950 DiagList::iterator II, IE;
951 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
952 if (!D.MatchAnyLine) {
953 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
954 if (LineNo1 != LineNo2)
958 if (!D.DiagnosticLoc.isInvalid() && !D.MatchAnyFileAndLine &&
959 !IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
962 const std::string &RightText = II->second;
963 if (D.match(RightText))
968 if (i >= D.Min) break;
969 LeftOnly.push_back(&D);
971 // Found. The same cannot be found twice.
976 // Now all that's left in Right are those that were not matched.
977 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
978 if (!IgnoreUnexpected)
979 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
983 /// CheckResults - This compares the expected results to those that
984 /// were actually reported. It emits any discrepencies. Return "true" if there
985 /// were problems. Return "false" otherwise.
986 static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
987 const TextDiagnosticBuffer &Buffer,
989 // We want to capture the delta between what was expected and what was
992 // Expected \ Seen - set expected but not seen
993 // Seen \ Expected - set seen but not expected
994 unsigned NumProblems = 0;
996 const DiagnosticLevelMask DiagMask =
997 Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
999 // See if there are error mismatches.
1000 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
1001 Buffer.err_begin(), Buffer.err_end(),
1002 bool(DiagnosticLevelMask::Error & DiagMask));
1004 // See if there are warning mismatches.
1005 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
1006 Buffer.warn_begin(), Buffer.warn_end(),
1007 bool(DiagnosticLevelMask::Warning & DiagMask));
1009 // See if there are remark mismatches.
1010 NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
1011 Buffer.remark_begin(), Buffer.remark_end(),
1012 bool(DiagnosticLevelMask::Remark & DiagMask));
1014 // See if there are note mismatches.
1015 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
1016 Buffer.note_begin(), Buffer.note_end(),
1017 bool(DiagnosticLevelMask::Note & DiagMask));
1022 void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
1025 // Check SourceManager hasn't changed.
1026 setSourceManager(SM);
1029 if (FID.isInvalid())
1032 const FileEntry *FE = SM.getFileEntryForID(FID);
1034 if (PS == IsParsed) {
1035 // Move the FileID from the unparsed set to the parsed set.
1036 UnparsedFiles.erase(FID);
1037 ParsedFiles.insert(std::make_pair(FID, FE));
1038 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
1039 // Add the FileID to the unparsed set if we haven't seen it before.
1041 // Check for directives.
1042 bool FoundDirectives;
1043 if (PS == IsUnparsedNoDirectives)
1044 FoundDirectives = false;
1046 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
1048 // Add the FileID to the unparsed set.
1049 UnparsedFiles.insert(std::make_pair(FID,
1050 UnparsedFileStatus(FE, FoundDirectives)));
1055 void VerifyDiagnosticConsumer::CheckDiagnostics() {
1056 // Ensure any diagnostics go to the primary client.
1057 DiagnosticConsumer *CurClient = Diags.getClient();
1058 std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient();
1059 Diags.setClient(PrimaryClient, false);
1062 // In a debug build, scan through any files that may have been missed
1063 // during parsing and issue a fatal error if directives are contained
1064 // within these files. If a fatal error occurs, this suggests that
1065 // this file is being parsed separately from the main file, in which
1066 // case consider moving the directives to the correct place, if this
1068 if (!UnparsedFiles.empty()) {
1069 // Generate a cache of parsed FileEntry pointers for alias lookups.
1070 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
1071 for (const auto &I : ParsedFiles)
1072 if (const FileEntry *FE = I.second)
1073 ParsedFileCache.insert(FE);
1075 // Iterate through list of unparsed files.
1076 for (const auto &I : UnparsedFiles) {
1077 const UnparsedFileStatus &Status = I.second;
1078 const FileEntry *FE = Status.getFile();
1080 // Skip files that have been parsed via an alias.
1081 if (FE && ParsedFileCache.count(FE))
1084 // Report a fatal error if this file contained directives.
1085 if (Status.foundDirectives()) {
1086 llvm::report_fatal_error(Twine("-verify directives found after rather"
1087 " than during normal parsing of ",
1088 StringRef(FE ? FE->getName() : "(unknown)")));
1092 // UnparsedFiles has been processed now, so clear it.
1093 UnparsedFiles.clear();
1098 // Produce an error if no expected-* directives could be found in the
1099 // source file(s) processed.
1100 if (Status == HasNoDirectives) {
1101 Diags.Report(diag::err_verify_no_directives).setForceEmit();
1103 Status = HasNoDirectivesReported;
1106 // Check that the expected diagnostics occurred.
1107 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
1109 const DiagnosticLevelMask DiagMask =
1110 ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
1111 if (bool(DiagnosticLevelMask::Error & DiagMask))
1112 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
1113 Buffer->err_end(), "error");
1114 if (bool(DiagnosticLevelMask::Warning & DiagMask))
1115 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
1116 Buffer->warn_end(), "warn");
1117 if (bool(DiagnosticLevelMask::Remark & DiagMask))
1118 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(),
1119 Buffer->remark_end(), "remark");
1120 if (bool(DiagnosticLevelMask::Note & DiagMask))
1121 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
1122 Buffer->note_end(), "note");
1125 Diags.setClient(CurClient, Owner.release() != nullptr);
1127 // Reset the buffer, we have processed all the diagnostics in it.
1128 Buffer.reset(new TextDiagnosticBuffer());
1132 std::unique_ptr<Directive> Directive::create(bool RegexKind,
1133 SourceLocation DirectiveLoc,
1134 SourceLocation DiagnosticLoc,
1135 bool MatchAnyFileAndLine,
1136 bool MatchAnyLine, StringRef Text,
1137 unsigned Min, unsigned Max) {
1139 return std::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc,
1140 MatchAnyFileAndLine,
1141 MatchAnyLine, Text, Min, Max);
1143 // Parse the directive into a regular expression.
1144 std::string RegexStr;
1146 while (!S.empty()) {
1147 if (S.startswith("{{")) {
1148 S = S.drop_front(2);
1149 size_t RegexMatchLength = S.find("}}");
1150 assert(RegexMatchLength != StringRef::npos);
1151 // Append the regex, enclosed in parentheses.
1153 RegexStr.append(S.data(), RegexMatchLength);
1155 S = S.drop_front(RegexMatchLength + 2);
1157 size_t VerbatimMatchLength = S.find("{{");
1158 if (VerbatimMatchLength == StringRef::npos)
1159 VerbatimMatchLength = S.size();
1160 // Escape and append the fixed string.
1161 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
1162 S = S.drop_front(VerbatimMatchLength);
1166 return std::make_unique<RegexDirective>(DirectiveLoc, DiagnosticLoc,
1167 MatchAnyFileAndLine, MatchAnyLine,
1168 Text, Min, Max, RegexStr);