blob: 427d15ed703aea7bb2bf3b2f8d5dc6670bc5e781 [file] [log] [blame]
David Blaikie69609dc2011-09-26 00:38:03 +00001//===---- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ------===//
Daniel Dunbar34818552009-11-14 03:23:19 +00002//
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
David Blaikie69609dc2011-09-26 00:38:03 +000014#include "clang/Frontend/VerifyDiagnosticConsumer.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000015#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/FileManager.h"
Daniel Dunbar34818552009-11-14 03:23:19 +000017#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/TextDiagnosticBuffer.h"
Jordan Roseb00073d2012-08-10 01:06:16 +000019#include "clang/Lex/HeaderSearch.h"
Daniel Dunbar34818552009-11-14 03:23:19 +000020#include "clang/Lex/Preprocessor.h"
21#include "llvm/ADT/SmallString.h"
Chris Lattnere82411b2010-04-28 20:02:30 +000022#include "llvm/Support/Regex.h"
Daniel Dunbar34818552009-11-14 03:23:19 +000023#include "llvm/Support/raw_ostream.h"
Anna Zaks2c74eed2011-12-15 02:58:00 +000024
Daniel Dunbar34818552009-11-14 03:23:19 +000025using namespace clang;
Jordan Rose6dae7612012-07-10 02:56:15 +000026typedef VerifyDiagnosticConsumer::Directive Directive;
27typedef VerifyDiagnosticConsumer::DirectiveList DirectiveList;
28typedef VerifyDiagnosticConsumer::ExpectedData ExpectedData;
Daniel Dunbar34818552009-11-14 03:23:19 +000029
Justin Bognerba7add32014-10-16 06:00:55 +000030VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_)
31 : Diags(Diags_),
Alexander Kornienko41c247a2014-11-17 23:46:02 +000032 PrimaryClient(Diags.getClient()), PrimaryClientOwner(Diags.takeClient()),
Craig Topper49a27902014-05-22 04:46:25 +000033 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(nullptr),
34 LangOpts(nullptr), SrcManager(nullptr), ActiveSourceFiles(0),
35 Status(HasNoDirectives)
Douglas Gregor2b9b4642011-09-13 01:26:44 +000036{
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000037 if (Diags.hasSourceManager())
38 setSourceManager(Diags.getSourceManager());
Daniel Dunbar34818552009-11-14 03:23:19 +000039}
40
David Blaikie69609dc2011-09-26 00:38:03 +000041VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Jordan Roseb00073d2012-08-10 01:06:16 +000042 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
43 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
Craig Topper49a27902014-05-22 04:46:25 +000044 SrcManager = nullptr;
Alexander Kornienko41c247a2014-11-17 23:46:02 +000045 CheckDiagnostics();
Chandler Carrutha667ace2016-11-03 18:03:14 +000046 assert(!Diags.ownsClient() &&
47 "The VerifyDiagnosticConsumer takes over ownership of the client!");
Daniel Dunbar34818552009-11-14 03:23:19 +000048}
49
Jordan Roseb00073d2012-08-10 01:06:16 +000050#ifndef NDEBUG
51namespace {
52class VerifyFileTracker : public PPCallbacks {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000053 VerifyDiagnosticConsumer &Verify;
Jordan Roseb00073d2012-08-10 01:06:16 +000054 SourceManager &SM;
55
56public:
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000057 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
58 : Verify(Verify), SM(SM) { }
Jordan Roseb00073d2012-08-10 01:06:16 +000059
60 /// \brief Hook into the preprocessor and update the list of parsed
61 /// files when the preprocessor indicates a new file is entered.
Alexander Kornienko34eb2072015-04-11 02:00:23 +000062 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
63 SrcMgr::CharacteristicKind FileType,
64 FileID PrevFID) override {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000065 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
66 VerifyDiagnosticConsumer::IsParsed);
Jordan Roseb00073d2012-08-10 01:06:16 +000067 }
68};
69} // End anonymous namespace.
70#endif
71
David Blaikiee2eefae2011-09-25 23:39:51 +000072// DiagnosticConsumer interface.
Daniel Dunbar34818552009-11-14 03:23:19 +000073
David Blaikie69609dc2011-09-26 00:38:03 +000074void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor32fbe312012-01-20 16:28:04 +000075 const Preprocessor *PP) {
Jordan Roseb00073d2012-08-10 01:06:16 +000076 // Attach comment handler on first invocation.
77 if (++ActiveSourceFiles == 1) {
78 if (PP) {
79 CurrentPreprocessor = PP;
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000080 this->LangOpts = &LangOpts;
81 setSourceManager(PP->getSourceManager());
Jordan Roseb00073d2012-08-10 01:06:16 +000082 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
83#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000084 // Debug build tracks parsed files.
Craig Topperb8a70532014-09-10 04:53:53 +000085 const_cast<Preprocessor*>(PP)->addPPCallbacks(
86 llvm::make_unique<VerifyFileTracker>(*this, *SrcManager));
Jordan Roseb00073d2012-08-10 01:06:16 +000087#endif
88 }
89 }
Daniel Dunbar34818552009-11-14 03:23:19 +000090
Jordan Roseb00073d2012-08-10 01:06:16 +000091 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
Daniel Dunbar34818552009-11-14 03:23:19 +000092 PrimaryClient->BeginSourceFile(LangOpts, PP);
93}
94
David Blaikie69609dc2011-09-26 00:38:03 +000095void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Roseb00073d2012-08-10 01:06:16 +000096 assert(ActiveSourceFiles && "No active source files!");
Daniel Dunbar34818552009-11-14 03:23:19 +000097 PrimaryClient->EndSourceFile();
98
Jordan Roseb00073d2012-08-10 01:06:16 +000099 // Detach comment handler once last active source file completed.
100 if (--ActiveSourceFiles == 0) {
101 if (CurrentPreprocessor)
102 const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this);
103
104 // Check diagnostics once last file completed.
105 CheckDiagnostics();
Craig Topper49a27902014-05-22 04:46:25 +0000106 CurrentPreprocessor = nullptr;
107 LangOpts = nullptr;
Jordan Roseb00073d2012-08-10 01:06:16 +0000108 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000109}
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000110
David Blaikie69609dc2011-09-26 00:38:03 +0000111void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikieb5784322011-09-26 01:18:08 +0000112 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Douglas Gregor6b930962013-05-03 22:58:43 +0000113 if (Info.hasSourceManager()) {
114 // If this diagnostic is for a different source manager, ignore it.
115 if (SrcManager && &Info.getSourceManager() != SrcManager)
116 return;
117
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000118 setSourceManager(Info.getSourceManager());
Douglas Gregor6b930962013-05-03 22:58:43 +0000119 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000120
Jordan Roseb00073d2012-08-10 01:06:16 +0000121#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000122 // Debug build tracks unparsed files for possible
123 // unparsed expected-* directives.
124 if (SrcManager) {
125 SourceLocation Loc = Info.getLocation();
126 if (Loc.isValid()) {
127 ParsedStatus PS = IsUnparsed;
128
129 Loc = SrcManager->getExpansionLoc(Loc);
130 FileID FID = SrcManager->getFileID(Loc);
131
132 const FileEntry *FE = SrcManager->getFileEntryForID(FID);
133 if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
134 // If the file is a modules header file it shall not be parsed
135 // for expected-* directives.
136 HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
137 if (HS.findModuleForHeader(FE))
138 PS = IsUnparsedNoDirectives;
139 }
140
141 UpdateParsedFileStatus(*SrcManager, FID, PS);
142 }
Axel Naumannac50dcf2011-07-25 19:18:12 +0000143 }
Jordan Roseb00073d2012-08-10 01:06:16 +0000144#endif
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000145
Daniel Dunbar34818552009-11-14 03:23:19 +0000146 // Send the diagnostic to the buffer, we will check it once we reach the end
147 // of the source file (or are destructed).
148 Buffer->HandleDiagnostic(DiagLevel, Info);
149}
150
Daniel Dunbar34818552009-11-14 03:23:19 +0000151//===----------------------------------------------------------------------===//
152// Checking diagnostics implementation.
153//===----------------------------------------------------------------------===//
154
155typedef TextDiagnosticBuffer::DiagList DiagList;
156typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
157
Chris Lattnere82411b2010-04-28 20:02:30 +0000158namespace {
159
Chris Lattnere82411b2010-04-28 20:02:30 +0000160/// StandardDirective - Directive with string matching.
161///
162class StandardDirective : public Directive {
163public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000164 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000165 bool MatchAnyLine, StringRef Text, unsigned Min,
166 unsigned Max)
167 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000168
Craig Topperafa7cb32014-03-13 06:07:04 +0000169 bool isValid(std::string &Error) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000170 // all strings are considered valid; even empty ones
171 return true;
172 }
173
Craig Topperafa7cb32014-03-13 06:07:04 +0000174 bool match(StringRef S) override {
Jordan Rose6dae7612012-07-10 02:56:15 +0000175 return S.find(Text) != StringRef::npos;
Chris Lattnere82411b2010-04-28 20:02:30 +0000176 }
177};
178
179/// RegexDirective - Directive with regular-expression matching.
180///
181class RegexDirective : public Directive {
182public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000183 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000184 bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max,
185 StringRef RegexStr)
186 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max),
187 Regex(RegexStr) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000188
Craig Topperafa7cb32014-03-13 06:07:04 +0000189 bool isValid(std::string &Error) override {
Alexander Kornienko5583e6f2015-12-28 15:15:16 +0000190 return Regex.isValid(Error);
Chris Lattnere82411b2010-04-28 20:02:30 +0000191 }
192
Craig Topperafa7cb32014-03-13 06:07:04 +0000193 bool match(StringRef S) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000194 return Regex.match(S);
195 }
196
197private:
198 llvm::Regex Regex;
199};
200
Chris Lattnere82411b2010-04-28 20:02:30 +0000201class ParseHelper
202{
203public:
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000204 ParseHelper(StringRef S)
Craig Topper49a27902014-05-22 04:46:25 +0000205 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(nullptr) {}
Chris Lattnere82411b2010-04-28 20:02:30 +0000206
207 // Return true if string literal is next.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208 bool Next(StringRef S) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000209 P = C;
Benjamin Kramer2bd4cee2010-09-01 17:28:48 +0000210 PEnd = C + S.size();
Chris Lattnere82411b2010-04-28 20:02:30 +0000211 if (PEnd > End)
212 return false;
Benjamin Kramer2bd4cee2010-09-01 17:28:48 +0000213 return !memcmp(P, S.data(), S.size());
Chris Lattnere82411b2010-04-28 20:02:30 +0000214 }
215
216 // Return true if number is next.
217 // Output N only if number is next.
218 bool Next(unsigned &N) {
219 unsigned TMP = 0;
220 P = C;
221 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
222 TMP *= 10;
223 TMP += P[0] - '0';
224 }
225 if (P == C)
226 return false;
227 PEnd = P;
228 N = TMP;
229 return true;
230 }
231
232 // Return true if string literal is found.
233 // When true, P marks begin-position of S in content.
Andy Gibbsac51de62012-10-19 12:36:49 +0000234 bool Search(StringRef S, bool EnsureStartOfWord = false) {
235 do {
236 P = std::search(C, End, S.begin(), S.end());
237 PEnd = P + S.size();
238 if (P == End)
239 break;
240 if (!EnsureStartOfWord
241 // Check if string literal starts a new word.
Jordan Rosea7d03842013-02-08 22:30:41 +0000242 || P == Begin || isWhitespace(P[-1])
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000243 // Or it could be preceded by the start of a comment.
Andy Gibbsac51de62012-10-19 12:36:49 +0000244 || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*')
245 && P[-2] == '/'))
246 return true;
247 // Otherwise, skip and search again.
248 } while (Advance());
249 return false;
Chris Lattnere82411b2010-04-28 20:02:30 +0000250 }
251
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000252 // Return true if a CloseBrace that closes the OpenBrace at the current nest
253 // level is found. When true, P marks begin-position of CloseBrace.
254 bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) {
255 unsigned Depth = 1;
256 P = C;
257 while (P < End) {
258 StringRef S(P, End - P);
259 if (S.startswith(OpenBrace)) {
260 ++Depth;
261 P += OpenBrace.size();
262 } else if (S.startswith(CloseBrace)) {
263 --Depth;
264 if (Depth == 0) {
265 PEnd = P + CloseBrace.size();
266 return true;
267 }
268 P += CloseBrace.size();
269 } else {
270 ++P;
271 }
272 }
273 return false;
274 }
275
Chris Lattnere82411b2010-04-28 20:02:30 +0000276 // Advance 1-past previous next/search.
277 // Behavior is undefined if previous next/search failed.
278 bool Advance() {
279 C = PEnd;
280 return C < End;
281 }
282
283 // Skip zero or more whitespace.
284 void SkipWhitespace() {
Jordan Rosea7d03842013-02-08 22:30:41 +0000285 for (; C < End && isWhitespace(*C); ++C)
Chris Lattnere82411b2010-04-28 20:02:30 +0000286 ;
287 }
288
289 // Return true if EOF reached.
290 bool Done() {
291 return !(C < End);
292 }
293
294 const char * const Begin; // beginning of expected content
295 const char * const End; // end of expected content (1-past)
296 const char *C; // position of next char in content
297 const char *P;
298
299private:
300 const char *PEnd; // previous next/search subject end (1-past)
301};
302
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000303} // namespace anonymous
Chris Lattnere82411b2010-04-28 20:02:30 +0000304
305/// ParseDirective - Go through the comment and see if it indicates expected
306/// diagnostics. If so, then put them in the appropriate directive list.
307///
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000308/// Returns true if any valid directives were found.
Jordan Roseb00073d2012-08-10 01:06:16 +0000309static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000310 Preprocessor *PP, SourceLocation Pos,
Andy Gibbs0fea0452012-10-19 12:49:32 +0000311 VerifyDiagnosticConsumer::DirectiveStatus &Status) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000312 DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
313
Chris Lattnere82411b2010-04-28 20:02:30 +0000314 // A single comment may contain multiple directives.
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000315 bool FoundDirective = false;
316 for (ParseHelper PH(S); !PH.Done();) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000317 // Search for token: expected
Andy Gibbsac51de62012-10-19 12:36:49 +0000318 if (!PH.Search("expected", true))
Chris Lattnere82411b2010-04-28 20:02:30 +0000319 break;
320 PH.Advance();
321
Jordan Rosee1572eb2012-07-10 02:57:03 +0000322 // Next token: -
Chris Lattnere82411b2010-04-28 20:02:30 +0000323 if (!PH.Next("-"))
324 continue;
325 PH.Advance();
326
Jordan Rosee1572eb2012-07-10 02:57:03 +0000327 // Next token: { error | warning | note }
Craig Topper49a27902014-05-22 04:46:25 +0000328 DirectiveList *DL = nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000329 if (PH.Next("error"))
Craig Topper49a27902014-05-22 04:46:25 +0000330 DL = ED ? &ED->Errors : nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000331 else if (PH.Next("warning"))
Craig Topper49a27902014-05-22 04:46:25 +0000332 DL = ED ? &ED->Warnings : nullptr;
Tobias Grosser86a85672014-05-01 14:06:01 +0000333 else if (PH.Next("remark"))
Craig Topper49a27902014-05-22 04:46:25 +0000334 DL = ED ? &ED->Remarks : nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000335 else if (PH.Next("note"))
Craig Topper49a27902014-05-22 04:46:25 +0000336 DL = ED ? &ED->Notes : nullptr;
Andy Gibbs0fea0452012-10-19 12:49:32 +0000337 else if (PH.Next("no-diagnostics")) {
338 if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
339 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
340 << /*IsExpectedNoDiagnostics=*/true;
341 else
342 Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
343 continue;
344 } else
Chris Lattnere82411b2010-04-28 20:02:30 +0000345 continue;
346 PH.Advance();
347
Andy Gibbs0fea0452012-10-19 12:49:32 +0000348 if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
349 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
350 << /*IsExpectedNoDiagnostics=*/false;
351 continue;
352 }
353 Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
354
Jordan Roseb00073d2012-08-10 01:06:16 +0000355 // If a directive has been found but we're not interested
356 // in storing the directive information, return now.
357 if (!DL)
358 return true;
359
Jordan Rosee1572eb2012-07-10 02:57:03 +0000360 // Default directive kind.
Alp Toker6ed72512013-12-14 01:07:05 +0000361 bool RegexKind = false;
Chris Lattnere82411b2010-04-28 20:02:30 +0000362 const char* KindStr = "string";
363
Alp Toker6ed72512013-12-14 01:07:05 +0000364 // Next optional token: -
365 if (PH.Next("-re")) {
366 PH.Advance();
367 RegexKind = true;
368 KindStr = "regex";
369 }
370
Jordan Rosee1572eb2012-07-10 02:57:03 +0000371 // Next optional token: @
372 SourceLocation ExpectedLoc;
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000373 bool MatchAnyLine = false;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000374 if (!PH.Next("@")) {
375 ExpectedLoc = Pos;
376 } else {
377 PH.Advance();
378 unsigned Line = 0;
379 bool FoundPlus = PH.Next("+");
380 if (FoundPlus || PH.Next("-")) {
381 // Relative to current line.
382 PH.Advance();
383 bool Invalid = false;
384 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
385 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
386 if (FoundPlus) ExpectedLine += Line;
387 else ExpectedLine -= Line;
388 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
389 }
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000390 } else if (PH.Next(Line)) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000391 // Absolute line number.
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000392 if (Line > 0)
Jordan Rosee1572eb2012-07-10 02:57:03 +0000393 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000394 } else if (PP && PH.Search(":")) {
395 // Specific source file.
396 StringRef Filename(PH.C, PH.P-PH.C);
397 PH.Advance();
398
399 // Lookup file via Preprocessor, like a #include.
400 const DirectoryLookup *CurDir;
Richard Smith25d50752014-10-20 00:15:49 +0000401 const FileEntry *FE =
402 PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
Duncan P. N. Exon Smithcfc1f6a2017-04-27 21:41:51 +0000403 nullptr, nullptr, nullptr, nullptr);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000404 if (!FE) {
405 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
406 diag::err_verify_missing_file) << Filename << KindStr;
407 continue;
408 }
409
410 if (SM.translateFile(FE).isInvalid())
411 SM.createFileID(FE, Pos, SrcMgr::C_User);
412
413 if (PH.Next(Line) && Line > 0)
414 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000415 else if (PH.Next("*")) {
416 MatchAnyLine = true;
417 ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
418 }
Jordan Rosee1572eb2012-07-10 02:57:03 +0000419 }
420
421 if (ExpectedLoc.isInvalid()) {
422 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
423 diag::err_verify_missing_line) << KindStr;
424 continue;
425 }
426 PH.Advance();
427 }
428
429 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000430 PH.SkipWhitespace();
431
Jordan Rosee1572eb2012-07-10 02:57:03 +0000432 // Next optional token: positive integer or a '+'.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000433 unsigned Min = 1;
434 unsigned Max = 1;
435 if (PH.Next(Min)) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000436 PH.Advance();
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000437 // A positive integer can be followed by a '+' meaning min
438 // or more, or by a '-' meaning a range from min to max.
439 if (PH.Next("+")) {
440 Max = Directive::MaxCount;
441 PH.Advance();
442 } else if (PH.Next("-")) {
443 PH.Advance();
444 if (!PH.Next(Max) || Max < Min) {
445 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
446 diag::err_verify_invalid_range) << KindStr;
447 continue;
448 }
449 PH.Advance();
450 } else {
451 Max = Min;
452 }
453 } else if (PH.Next("+")) {
454 // '+' on its own means "1 or more".
455 Max = Directive::MaxCount;
Anna Zaksa2510072011-12-15 02:28:16 +0000456 PH.Advance();
457 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000458
Jordan Rosee1572eb2012-07-10 02:57:03 +0000459 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000460 PH.SkipWhitespace();
461
Jordan Rosee1572eb2012-07-10 02:57:03 +0000462 // Next token: {{
Chris Lattnere82411b2010-04-28 20:02:30 +0000463 if (!PH.Next("{{")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000464 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
465 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar34818552009-11-14 03:23:19 +0000466 continue;
467 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000468 PH.Advance();
469 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar34818552009-11-14 03:23:19 +0000470
Jordan Rosee1572eb2012-07-10 02:57:03 +0000471 // Search for token: }}
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000472 if (!PH.SearchClosingBrace("{{", "}}")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000473 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
474 diag::err_verify_missing_end) << KindStr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000475 continue;
Daniel Dunbar34818552009-11-14 03:23:19 +0000476 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000477 const char* const ContentEnd = PH.P; // mark content end
478 PH.Advance();
Daniel Dunbar34818552009-11-14 03:23:19 +0000479
Jordan Rosee1572eb2012-07-10 02:57:03 +0000480 // Build directive text; convert \n to newlines.
Chris Lattnere82411b2010-04-28 20:02:30 +0000481 std::string Text;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000482 StringRef NewlineStr = "\\n";
483 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattnere82411b2010-04-28 20:02:30 +0000484 size_t CPos = 0;
485 size_t FPos;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000486 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000487 Text += Content.substr(CPos, FPos-CPos);
488 Text += '\n';
489 CPos = FPos + NewlineStr.size();
Daniel Dunbar34818552009-11-14 03:23:19 +0000490 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000491 if (Text.empty())
492 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar34818552009-11-14 03:23:19 +0000493
Alp Toker6ed72512013-12-14 01:07:05 +0000494 // Check that regex directives contain at least one regex.
495 if (RegexKind && Text.find("{{") == StringRef::npos) {
496 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
497 diag::err_verify_missing_regex) << Text;
498 return false;
499 }
500
Jordan Rosee1572eb2012-07-10 02:57:03 +0000501 // Construct new directive.
David Blaikie93106512014-08-29 16:30:23 +0000502 std::unique_ptr<Directive> D = Directive::create(
503 RegexKind, Pos, ExpectedLoc, MatchAnyLine, Text, Min, Max);
Nico Weber568dacc2014-04-24 05:32:03 +0000504
Chris Lattnere82411b2010-04-28 20:02:30 +0000505 std::string Error;
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000506 if (D->isValid(Error)) {
David Blaikie93106512014-08-29 16:30:23 +0000507 DL->push_back(std::move(D));
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000508 FoundDirective = true;
509 } else {
Jordan Rose6dae7612012-07-10 02:56:15 +0000510 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
511 diag::err_verify_invalid_content)
Chris Lattnere82411b2010-04-28 20:02:30 +0000512 << KindStr << Error;
Daniel Dunbar34818552009-11-14 03:23:19 +0000513 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000514 }
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000515
516 return FoundDirective;
517}
518
519/// HandleComment - Hook into the preprocessor and extract comments containing
520/// expected errors and warnings.
521bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
522 SourceRange Comment) {
523 SourceManager &SM = PP.getSourceManager();
Douglas Gregor6b930962013-05-03 22:58:43 +0000524
525 // If this comment is for a different source manager, ignore it.
526 if (SrcManager && &SM != SrcManager)
527 return false;
528
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000529 SourceLocation CommentBegin = Comment.getBegin();
530
531 const char *CommentRaw = SM.getCharacterData(CommentBegin);
532 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
533
534 if (C.empty())
535 return false;
536
537 // Fold any "\<EOL>" sequences
538 size_t loc = C.find('\\');
539 if (loc == StringRef::npos) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000540 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000541 return false;
542 }
543
544 std::string C2;
545 C2.reserve(C.size());
546
547 for (size_t last = 0;; loc = C.find('\\', last)) {
548 if (loc == StringRef::npos || loc == C.size()) {
549 C2 += C.substr(last);
550 break;
551 }
552 C2 += C.substr(last, loc-last);
553 last = loc + 1;
554
555 if (C[last] == '\n' || C[last] == '\r') {
556 ++last;
557
558 // Escape \r\n or \n\r, but not \n\n.
559 if (last < C.size())
560 if (C[last] == '\n' || C[last] == '\r')
561 if (C[last] != C[last-1])
562 ++last;
563 } else {
564 // This was just a normal backslash.
565 C2 += '\\';
566 }
567 }
568
569 if (!C2.empty())
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000570 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000571 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000572}
573
Jordan Roseb00073d2012-08-10 01:06:16 +0000574#ifndef NDEBUG
575/// \brief Lex the specified source file to determine whether it contains
576/// any expected-* directives. As a Lexer is used rather than a full-blown
577/// Preprocessor, directives inside skipped #if blocks will still be found.
578///
579/// \return true if any directives were found.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000580static bool findDirectives(SourceManager &SM, FileID FID,
581 const LangOptions &LangOpts) {
Axel Naumannac50dcf2011-07-25 19:18:12 +0000582 // Create a raw lexer to pull all the comments out of FID.
583 if (FID.isInvalid())
Jordan Roseb00073d2012-08-10 01:06:16 +0000584 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000585
586 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner710bb872009-11-30 04:18:44 +0000587 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000588 Lexer RawLex(FID, FromFile, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000589
590 // Return comments as tokens, this is how we find expected diagnostics.
591 RawLex.SetCommentRetentionState(true);
592
593 Token Tok;
594 Tok.setKind(tok::comment);
Andy Gibbs0fea0452012-10-19 12:49:32 +0000595 VerifyDiagnosticConsumer::DirectiveStatus Status =
596 VerifyDiagnosticConsumer::HasNoDirectives;
Daniel Dunbar34818552009-11-14 03:23:19 +0000597 while (Tok.isNot(tok::eof)) {
Eli Friedman0834a4b2013-09-19 00:41:32 +0000598 RawLex.LexFromRawLexer(Tok);
Daniel Dunbar34818552009-11-14 03:23:19 +0000599 if (!Tok.is(tok::comment)) continue;
600
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000601 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000602 if (Comment.empty()) continue;
603
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000604 // Find first directive.
Craig Topper49a27902014-05-22 04:46:25 +0000605 if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
606 Status))
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000607 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000608 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000609 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000610}
Jordan Roseb00073d2012-08-10 01:06:16 +0000611#endif // !NDEBUG
Daniel Dunbar34818552009-11-14 03:23:19 +0000612
Jordan Rosee1572eb2012-07-10 02:57:03 +0000613/// \brief Takes a list of diagnostics that have been generated but not matched
614/// by an expected-* directive and produces a diagnostic to the user from this.
615static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
616 const_diag_iterator diag_begin,
617 const_diag_iterator diag_end,
618 const char *Kind) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000619 if (diag_begin == diag_end) return 0;
620
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000621 SmallString<256> Fmt;
Daniel Dunbar34818552009-11-14 03:23:19 +0000622 llvm::raw_svector_ostream OS(Fmt);
623 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000624 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar34818552009-11-14 03:23:19 +0000625 OS << "\n (frontend)";
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000626 else {
627 OS << "\n ";
628 if (const FileEntry *File = SourceMgr->getFileEntryForID(
629 SourceMgr->getFileID(I->first)))
630 OS << " File " << File->getName();
631 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
632 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000633 OS << ": " << I->second;
634 }
635
Jordan Rose6f524ac2012-07-11 16:50:36 +0000636 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000637 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar34818552009-11-14 03:23:19 +0000638 return std::distance(diag_begin, diag_end);
639}
640
Jordan Rosee1572eb2012-07-10 02:57:03 +0000641/// \brief Takes a list of diagnostics that were expected to have been generated
642/// but were not and produces a diagnostic to the user from this.
David Blaikie93106512014-08-29 16:30:23 +0000643static unsigned PrintExpected(DiagnosticsEngine &Diags,
644 SourceManager &SourceMgr,
645 std::vector<Directive *> &DL, const char *Kind) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000646 if (DL.empty())
647 return 0;
648
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000649 SmallString<256> Fmt;
Chris Lattnere82411b2010-04-28 20:02:30 +0000650 llvm::raw_svector_ostream OS(Fmt);
David Blaikie93106512014-08-29 16:30:23 +0000651 for (auto *DirPtr : DL) {
652 Directive &D = *DirPtr;
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000653 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc);
654 if (D.MatchAnyLine)
655 OS << " Line *";
656 else
657 OS << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Jordan Rosee1572eb2012-07-10 02:57:03 +0000658 if (D.DirectiveLoc != D.DiagnosticLoc)
659 OS << " (directive at "
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000660 << SourceMgr.getFilename(D.DirectiveLoc) << ':'
661 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
Chris Lattnere82411b2010-04-28 20:02:30 +0000662 OS << ": " << D.Text;
663 }
664
Jordan Rose6f524ac2012-07-11 16:50:36 +0000665 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000666 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattnere82411b2010-04-28 20:02:30 +0000667 return DL.size();
668}
669
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000670/// \brief Determine whether two source locations come from the same file.
671static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
672 SourceLocation DiagnosticLoc) {
673 while (DiagnosticLoc.isMacroID())
674 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
675
Eli Friedman5ba37d52013-08-22 00:27:10 +0000676 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000677 return true;
678
679 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
Eli Friedman5ba37d52013-08-22 00:27:10 +0000680 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000681 return true;
682
683 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
684}
685
Chris Lattnere82411b2010-04-28 20:02:30 +0000686/// CheckLists - Compare expected to seen diagnostic lists and return the
687/// the difference between them.
Daniel Dunbar34818552009-11-14 03:23:19 +0000688///
David Blaikie9c902b52011-09-25 23:23:43 +0000689static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattnere82411b2010-04-28 20:02:30 +0000690 const char *Label,
691 DirectiveList &Left,
692 const_diag_iterator d2_begin,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000693 const_diag_iterator d2_end,
694 bool IgnoreUnexpected) {
David Blaikie93106512014-08-29 16:30:23 +0000695 std::vector<Directive *> LeftOnly;
Daniel Dunbar34818552009-11-14 03:23:19 +0000696 DiagList Right(d2_begin, d2_end);
697
David Blaikie93106512014-08-29 16:30:23 +0000698 for (auto &Owner : Left) {
699 Directive &D = *Owner;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000700 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar34818552009-11-14 03:23:19 +0000701
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000702 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000703 DiagList::iterator II, IE;
704 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000705 if (!D.MatchAnyLine) {
706 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
707 if (LineNo1 != LineNo2)
708 continue;
709 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000710
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000711 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
712 continue;
713
Chris Lattnere82411b2010-04-28 20:02:30 +0000714 const std::string &RightText = II->second;
Jordan Rose6dae7612012-07-10 02:56:15 +0000715 if (D.match(RightText))
Chris Lattnere82411b2010-04-28 20:02:30 +0000716 break;
Daniel Dunbar34818552009-11-14 03:23:19 +0000717 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000718 if (II == IE) {
719 // Not found.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000720 if (i >= D.Min) break;
David Blaikie93106512014-08-29 16:30:23 +0000721 LeftOnly.push_back(&D);
Chris Lattnere82411b2010-04-28 20:02:30 +0000722 } else {
723 // Found. The same cannot be found twice.
724 Right.erase(II);
725 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000726 }
727 }
728 // Now all that's left in Right are those that were not matched.
Jordan Rosee1572eb2012-07-10 02:57:03 +0000729 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
Eric Fiselier098e6de2015-06-13 07:11:40 +0000730 if (!IgnoreUnexpected)
731 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumi7bfba2f2011-12-17 13:00:31 +0000732 return num;
Daniel Dunbar34818552009-11-14 03:23:19 +0000733}
734
735/// CheckResults - This compares the expected results to those that
736/// were actually reported. It emits any discrepencies. Return "true" if there
737/// were problems. Return "false" otherwise.
738///
David Blaikie9c902b52011-09-25 23:23:43 +0000739static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar34818552009-11-14 03:23:19 +0000740 const TextDiagnosticBuffer &Buffer,
Chris Lattnere82411b2010-04-28 20:02:30 +0000741 ExpectedData &ED) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000742 // We want to capture the delta between what was expected and what was
743 // seen.
744 //
745 // Expected \ Seen - set expected but not seen
746 // Seen \ Expected - set seen but not expected
747 unsigned NumProblems = 0;
748
Eric Fiselier098e6de2015-06-13 07:11:40 +0000749 const DiagnosticLevelMask DiagMask =
750 Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
751
Daniel Dunbar34818552009-11-14 03:23:19 +0000752 // See if there are error mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000753 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000754 Buffer.err_begin(), Buffer.err_end(),
755 bool(DiagnosticLevelMask::Error & DiagMask));
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000756
Daniel Dunbar34818552009-11-14 03:23:19 +0000757 // See if there are warning mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000758 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000759 Buffer.warn_begin(), Buffer.warn_end(),
760 bool(DiagnosticLevelMask::Warning & DiagMask));
Daniel Dunbar34818552009-11-14 03:23:19 +0000761
Tobias Grosser86a85672014-05-01 14:06:01 +0000762 // See if there are remark mismatches.
763 NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000764 Buffer.remark_begin(), Buffer.remark_end(),
765 bool(DiagnosticLevelMask::Remark & DiagMask));
Tobias Grosser86a85672014-05-01 14:06:01 +0000766
Daniel Dunbar34818552009-11-14 03:23:19 +0000767 // See if there are note mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000768 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000769 Buffer.note_begin(), Buffer.note_end(),
770 bool(DiagnosticLevelMask::Note & DiagMask));
Daniel Dunbar34818552009-11-14 03:23:19 +0000771
772 return NumProblems;
773}
774
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000775void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
776 FileID FID,
777 ParsedStatus PS) {
778 // Check SourceManager hasn't changed.
779 setSourceManager(SM);
780
781#ifndef NDEBUG
782 if (FID.isInvalid())
783 return;
784
785 const FileEntry *FE = SM.getFileEntryForID(FID);
786
787 if (PS == IsParsed) {
788 // Move the FileID from the unparsed set to the parsed set.
789 UnparsedFiles.erase(FID);
790 ParsedFiles.insert(std::make_pair(FID, FE));
791 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
792 // Add the FileID to the unparsed set if we haven't seen it before.
793
794 // Check for directives.
795 bool FoundDirectives;
796 if (PS == IsUnparsedNoDirectives)
797 FoundDirectives = false;
798 else
799 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
800
801 // Add the FileID to the unparsed set.
802 UnparsedFiles.insert(std::make_pair(FID,
803 UnparsedFileStatus(FE, FoundDirectives)));
804 }
805#endif
806}
807
David Blaikie69609dc2011-09-26 00:38:03 +0000808void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar34818552009-11-14 03:23:19 +0000809 // Ensure any diagnostics go to the primary client.
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000810 DiagnosticConsumer *CurClient = Diags.getClient();
811 std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient();
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000812 Diags.setClient(PrimaryClient, false);
Daniel Dunbar34818552009-11-14 03:23:19 +0000813
Jordan Roseb00073d2012-08-10 01:06:16 +0000814#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000815 // In a debug build, scan through any files that may have been missed
816 // during parsing and issue a fatal error if directives are contained
817 // within these files. If a fatal error occurs, this suggests that
818 // this file is being parsed separately from the main file, in which
819 // case consider moving the directives to the correct place, if this
820 // is applicable.
821 if (UnparsedFiles.size() > 0) {
822 // Generate a cache of parsed FileEntry pointers for alias lookups.
823 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
824 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
825 End = ParsedFiles.end(); I != End; ++I) {
826 if (const FileEntry *FE = I->second)
827 ParsedFileCache.insert(FE);
828 }
829
830 // Iterate through list of unparsed files.
831 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
832 End = UnparsedFiles.end(); I != End; ++I) {
833 const UnparsedFileStatus &Status = I->second;
834 const FileEntry *FE = Status.getFile();
835
836 // Skip files that have been parsed via an alias.
837 if (FE && ParsedFileCache.count(FE))
Jordan Roseb00073d2012-08-10 01:06:16 +0000838 continue;
839
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000840 // Report a fatal error if this file contained directives.
841 if (Status.foundDirectives()) {
Jordan Roseb00073d2012-08-10 01:06:16 +0000842 llvm::report_fatal_error(Twine("-verify directives found after rather"
843 " than during normal parsing of ",
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000844 StringRef(FE ? FE->getName() : "(unknown)")));
845 }
Axel Naumann744f1212011-08-24 13:36:19 +0000846 }
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000847
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000848 // UnparsedFiles has been processed now, so clear it.
849 UnparsedFiles.clear();
850 }
851#endif // !NDEBUG
852
853 if (SrcManager) {
Andy Gibbs0fea0452012-10-19 12:49:32 +0000854 // Produce an error if no expected-* directives could be found in the
855 // source file(s) processed.
856 if (Status == HasNoDirectives) {
857 Diags.Report(diag::err_verify_no_directives).setForceEmit();
858 ++NumErrors;
859 Status = HasNoDirectivesReported;
860 }
861
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000862 // Check that the expected diagnostics occurred.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000863 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000864 } else {
Eric Fiselier098e6de2015-06-13 07:11:40 +0000865 const DiagnosticLevelMask DiagMask =
866 ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
867 if (bool(DiagnosticLevelMask::Error & DiagMask))
868 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
869 Buffer->err_end(), "error");
870 if (bool(DiagnosticLevelMask::Warning & DiagMask))
871 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
872 Buffer->warn_end(), "warn");
873 if (bool(DiagnosticLevelMask::Remark & DiagMask))
874 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(),
875 Buffer->remark_end(), "remark");
876 if (bool(DiagnosticLevelMask::Note & DiagMask))
877 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
878 Buffer->note_end(), "note");
Daniel Dunbar34818552009-11-14 03:23:19 +0000879 }
880
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000881 Diags.setClient(CurClient, Owner.release() != nullptr);
Daniel Dunbar34818552009-11-14 03:23:19 +0000882
883 // Reset the buffer, we have processed all the diagnostics in it.
884 Buffer.reset(new TextDiagnosticBuffer());
Nico Weberdc493cf2014-04-24 05:39:55 +0000885 ED.Reset();
Daniel Dunbar34818552009-11-14 03:23:19 +0000886}
Chris Lattnere82411b2010-04-28 20:02:30 +0000887
David Blaikie93106512014-08-29 16:30:23 +0000888std::unique_ptr<Directive> Directive::create(bool RegexKind,
889 SourceLocation DirectiveLoc,
890 SourceLocation DiagnosticLoc,
891 bool MatchAnyLine, StringRef Text,
892 unsigned Min, unsigned Max) {
Alp Toker6ed72512013-12-14 01:07:05 +0000893 if (!RegexKind)
David Blaikie93106512014-08-29 16:30:23 +0000894 return llvm::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc,
895 MatchAnyLine, Text, Min, Max);
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000896
897 // Parse the directive into a regular expression.
898 std::string RegexStr;
899 StringRef S = Text;
900 while (!S.empty()) {
901 if (S.startswith("{{")) {
902 S = S.drop_front(2);
903 size_t RegexMatchLength = S.find("}}");
904 assert(RegexMatchLength != StringRef::npos);
905 // Append the regex, enclosed in parentheses.
906 RegexStr += "(";
907 RegexStr.append(S.data(), RegexMatchLength);
908 RegexStr += ")";
909 S = S.drop_front(RegexMatchLength + 2);
910 } else {
911 size_t VerbatimMatchLength = S.find("{{");
912 if (VerbatimMatchLength == StringRef::npos)
913 VerbatimMatchLength = S.size();
914 // Escape and append the fixed string.
Hans Wennborge6a87752013-12-12 00:27:31 +0000915 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000916 S = S.drop_front(VerbatimMatchLength);
917 }
918 }
919
David Blaikie93106512014-08-29 16:30:23 +0000920 return llvm::make_unique<RegexDirective>(
921 DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr);
Chris Lattnere82411b2010-04-28 20:02:30 +0000922}