blob: 531deb017714ae36402fe355bf5205662854ad6c [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_),
Jordan Roseb00073d2012-08-10 01:06:16 +000032 PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()),
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{
37 Diags.takeClient();
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000038 if (Diags.hasSourceManager())
39 setSourceManager(Diags.getSourceManager());
Daniel Dunbar34818552009-11-14 03:23:19 +000040}
41
David Blaikie69609dc2011-09-26 00:38:03 +000042VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Jordan Roseb00073d2012-08-10 01:06:16 +000043 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
44 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
Craig Topper49a27902014-05-22 04:46:25 +000045 SrcManager = nullptr;
Douglas Gregor2b9b4642011-09-13 01:26:44 +000046 CheckDiagnostics();
47 Diags.takeClient();
48 if (OwnsPrimaryClient)
49 delete PrimaryClient;
Daniel Dunbar34818552009-11-14 03:23:19 +000050}
51
Jordan Roseb00073d2012-08-10 01:06:16 +000052#ifndef NDEBUG
53namespace {
54class VerifyFileTracker : public PPCallbacks {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000055 VerifyDiagnosticConsumer &Verify;
Jordan Roseb00073d2012-08-10 01:06:16 +000056 SourceManager &SM;
57
58public:
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000059 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
60 : Verify(Verify), SM(SM) { }
Jordan Roseb00073d2012-08-10 01:06:16 +000061
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) {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000067 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
68 VerifyDiagnosticConsumer::IsParsed);
Jordan Roseb00073d2012-08-10 01:06:16 +000069 }
70};
71} // End anonymous namespace.
72#endif
73
David Blaikiee2eefae2011-09-25 23:39:51 +000074// DiagnosticConsumer interface.
Daniel Dunbar34818552009-11-14 03:23:19 +000075
David Blaikie69609dc2011-09-26 00:38:03 +000076void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor32fbe312012-01-20 16:28:04 +000077 const Preprocessor *PP) {
Jordan Roseb00073d2012-08-10 01:06:16 +000078 // Attach comment handler on first invocation.
79 if (++ActiveSourceFiles == 1) {
80 if (PP) {
81 CurrentPreprocessor = PP;
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000082 this->LangOpts = &LangOpts;
83 setSourceManager(PP->getSourceManager());
Jordan Roseb00073d2012-08-10 01:06:16 +000084 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
85#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000086 // Debug build tracks parsed files.
Craig Topperb8a70532014-09-10 04:53:53 +000087 const_cast<Preprocessor*>(PP)->addPPCallbacks(
88 llvm::make_unique<VerifyFileTracker>(*this, *SrcManager));
Jordan Roseb00073d2012-08-10 01:06:16 +000089#endif
90 }
91 }
Daniel Dunbar34818552009-11-14 03:23:19 +000092
Jordan Roseb00073d2012-08-10 01:06:16 +000093 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
Daniel Dunbar34818552009-11-14 03:23:19 +000094 PrimaryClient->BeginSourceFile(LangOpts, PP);
95}
96
David Blaikie69609dc2011-09-26 00:38:03 +000097void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Roseb00073d2012-08-10 01:06:16 +000098 assert(ActiveSourceFiles && "No active source files!");
Daniel Dunbar34818552009-11-14 03:23:19 +000099 PrimaryClient->EndSourceFile();
100
Jordan Roseb00073d2012-08-10 01:06:16 +0000101 // 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();
Craig Topper49a27902014-05-22 04:46:25 +0000108 CurrentPreprocessor = nullptr;
109 LangOpts = nullptr;
Jordan Roseb00073d2012-08-10 01:06:16 +0000110 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000111}
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000112
David Blaikie69609dc2011-09-26 00:38:03 +0000113void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikieb5784322011-09-26 01:18:08 +0000114 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Douglas Gregor6b930962013-05-03 22:58:43 +0000115 if (Info.hasSourceManager()) {
116 // If this diagnostic is for a different source manager, ignore it.
117 if (SrcManager && &Info.getSourceManager() != SrcManager)
118 return;
119
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000120 setSourceManager(Info.getSourceManager());
Douglas Gregor6b930962013-05-03 22:58:43 +0000121 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000122
Jordan Roseb00073d2012-08-10 01:06:16 +0000123#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000124 // 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 }
Axel Naumannac50dcf2011-07-25 19:18:12 +0000145 }
Jordan Roseb00073d2012-08-10 01:06:16 +0000146#endif
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000147
Daniel Dunbar34818552009-11-14 03:23:19 +0000148 // 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
Daniel Dunbar34818552009-11-14 03:23:19 +0000153//===----------------------------------------------------------------------===//
154// Checking diagnostics implementation.
155//===----------------------------------------------------------------------===//
156
157typedef TextDiagnosticBuffer::DiagList DiagList;
158typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
159
Chris Lattnere82411b2010-04-28 20:02:30 +0000160namespace {
161
Chris Lattnere82411b2010-04-28 20:02:30 +0000162/// StandardDirective - Directive with string matching.
163///
164class StandardDirective : public Directive {
165public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000166 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000167 bool MatchAnyLine, StringRef Text, unsigned Min,
168 unsigned Max)
169 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000170
Craig Topperafa7cb32014-03-13 06:07:04 +0000171 bool isValid(std::string &Error) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000172 // all strings are considered valid; even empty ones
173 return true;
174 }
175
Craig Topperafa7cb32014-03-13 06:07:04 +0000176 bool match(StringRef S) override {
Jordan Rose6dae7612012-07-10 02:56:15 +0000177 return S.find(Text) != StringRef::npos;
Chris Lattnere82411b2010-04-28 20:02:30 +0000178 }
179};
180
181/// RegexDirective - Directive with regular-expression matching.
182///
183class RegexDirective : public Directive {
184public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000185 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000186 bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max,
187 StringRef RegexStr)
188 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max),
189 Regex(RegexStr) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000190
Craig Topperafa7cb32014-03-13 06:07:04 +0000191 bool isValid(std::string &Error) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000192 if (Regex.isValid(Error))
193 return true;
194 return false;
195 }
196
Craig Topperafa7cb32014-03-13 06:07:04 +0000197 bool match(StringRef S) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000198 return Regex.match(S);
199 }
200
201private:
202 llvm::Regex Regex;
203};
204
Chris Lattnere82411b2010-04-28 20:02:30 +0000205class ParseHelper
206{
207public:
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000208 ParseHelper(StringRef S)
Craig Topper49a27902014-05-22 04:46:25 +0000209 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(nullptr) {}
Chris Lattnere82411b2010-04-28 20:02:30 +0000210
211 // Return true if string literal is next.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000212 bool Next(StringRef S) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000213 P = C;
Benjamin Kramer2bd4cee2010-09-01 17:28:48 +0000214 PEnd = C + S.size();
Chris Lattnere82411b2010-04-28 20:02:30 +0000215 if (PEnd > End)
216 return false;
Benjamin Kramer2bd4cee2010-09-01 17:28:48 +0000217 return !memcmp(P, S.data(), S.size());
Chris Lattnere82411b2010-04-28 20:02:30 +0000218 }
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.
Andy Gibbsac51de62012-10-19 12:36:49 +0000238 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.
Jordan Rosea7d03842013-02-08 22:30:41 +0000246 || P == Begin || isWhitespace(P[-1])
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000247 // Or it could be preceded by the start of a comment.
Andy Gibbsac51de62012-10-19 12:36:49 +0000248 || (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;
Chris Lattnere82411b2010-04-28 20:02:30 +0000254 }
255
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000256 // 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
Chris Lattnere82411b2010-04-28 20:02:30 +0000280 // 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() {
Jordan Rosea7d03842013-02-08 22:30:41 +0000289 for (; C < End && isWhitespace(*C); ++C)
Chris Lattnere82411b2010-04-28 20:02:30 +0000290 ;
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
303private:
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///
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000312/// Returns true if any valid directives were found.
Jordan Roseb00073d2012-08-10 01:06:16 +0000313static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000314 Preprocessor *PP, SourceLocation Pos,
Andy Gibbs0fea0452012-10-19 12:49:32 +0000315 VerifyDiagnosticConsumer::DirectiveStatus &Status) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000316 DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
317
Chris Lattnere82411b2010-04-28 20:02:30 +0000318 // A single comment may contain multiple directives.
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000319 bool FoundDirective = false;
320 for (ParseHelper PH(S); !PH.Done();) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000321 // Search for token: expected
Andy Gibbsac51de62012-10-19 12:36:49 +0000322 if (!PH.Search("expected", true))
Chris Lattnere82411b2010-04-28 20:02:30 +0000323 break;
324 PH.Advance();
325
Jordan Rosee1572eb2012-07-10 02:57:03 +0000326 // Next token: -
Chris Lattnere82411b2010-04-28 20:02:30 +0000327 if (!PH.Next("-"))
328 continue;
329 PH.Advance();
330
Jordan Rosee1572eb2012-07-10 02:57:03 +0000331 // Next token: { error | warning | note }
Craig Topper49a27902014-05-22 04:46:25 +0000332 DirectiveList *DL = nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000333 if (PH.Next("error"))
Craig Topper49a27902014-05-22 04:46:25 +0000334 DL = ED ? &ED->Errors : nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000335 else if (PH.Next("warning"))
Craig Topper49a27902014-05-22 04:46:25 +0000336 DL = ED ? &ED->Warnings : nullptr;
Tobias Grosser86a85672014-05-01 14:06:01 +0000337 else if (PH.Next("remark"))
Craig Topper49a27902014-05-22 04:46:25 +0000338 DL = ED ? &ED->Remarks : nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000339 else if (PH.Next("note"))
Craig Topper49a27902014-05-22 04:46:25 +0000340 DL = ED ? &ED->Notes : nullptr;
Andy Gibbs0fea0452012-10-19 12:49:32 +0000341 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
Chris Lattnere82411b2010-04-28 20:02:30 +0000349 continue;
350 PH.Advance();
351
Andy Gibbs0fea0452012-10-19 12:49:32 +0000352 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
Jordan Roseb00073d2012-08-10 01:06:16 +0000359 // 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
Jordan Rosee1572eb2012-07-10 02:57:03 +0000364 // Default directive kind.
Alp Toker6ed72512013-12-14 01:07:05 +0000365 bool RegexKind = false;
Chris Lattnere82411b2010-04-28 20:02:30 +0000366 const char* KindStr = "string";
367
Alp Toker6ed72512013-12-14 01:07:05 +0000368 // Next optional token: -
369 if (PH.Next("-re")) {
370 PH.Advance();
371 RegexKind = true;
372 KindStr = "regex";
373 }
374
Jordan Rosee1572eb2012-07-10 02:57:03 +0000375 // Next optional token: @
376 SourceLocation ExpectedLoc;
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000377 bool MatchAnyLine = false;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000378 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 }
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000394 } else if (PH.Next(Line)) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000395 // Absolute line number.
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000396 if (Line > 0)
Jordan Rosee1572eb2012-07-10 02:57:03 +0000397 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000398 } 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;
Richard Smith25d50752014-10-20 00:15:49 +0000405 const FileEntry *FE =
406 PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
407 nullptr, nullptr, nullptr);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000408 if (!FE) {
409 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
410 diag::err_verify_missing_file) << Filename << KindStr;
411 continue;
412 }
413
414 if (SM.translateFile(FE).isInvalid())
415 SM.createFileID(FE, Pos, SrcMgr::C_User);
416
417 if (PH.Next(Line) && Line > 0)
418 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000419 else if (PH.Next("*")) {
420 MatchAnyLine = true;
421 ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
422 }
Jordan Rosee1572eb2012-07-10 02:57:03 +0000423 }
424
425 if (ExpectedLoc.isInvalid()) {
426 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
427 diag::err_verify_missing_line) << KindStr;
428 continue;
429 }
430 PH.Advance();
431 }
432
433 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000434 PH.SkipWhitespace();
435
Jordan Rosee1572eb2012-07-10 02:57:03 +0000436 // Next optional token: positive integer or a '+'.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000437 unsigned Min = 1;
438 unsigned Max = 1;
439 if (PH.Next(Min)) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000440 PH.Advance();
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000441 // A positive integer can be followed by a '+' meaning min
442 // or more, or by a '-' meaning a range from min to max.
443 if (PH.Next("+")) {
444 Max = Directive::MaxCount;
445 PH.Advance();
446 } else if (PH.Next("-")) {
447 PH.Advance();
448 if (!PH.Next(Max) || Max < Min) {
449 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
450 diag::err_verify_invalid_range) << KindStr;
451 continue;
452 }
453 PH.Advance();
454 } else {
455 Max = Min;
456 }
457 } else if (PH.Next("+")) {
458 // '+' on its own means "1 or more".
459 Max = Directive::MaxCount;
Anna Zaksa2510072011-12-15 02:28:16 +0000460 PH.Advance();
461 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000462
Jordan Rosee1572eb2012-07-10 02:57:03 +0000463 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000464 PH.SkipWhitespace();
465
Jordan Rosee1572eb2012-07-10 02:57:03 +0000466 // Next token: {{
Chris Lattnere82411b2010-04-28 20:02:30 +0000467 if (!PH.Next("{{")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000468 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
469 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar34818552009-11-14 03:23:19 +0000470 continue;
471 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000472 PH.Advance();
473 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar34818552009-11-14 03:23:19 +0000474
Jordan Rosee1572eb2012-07-10 02:57:03 +0000475 // Search for token: }}
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000476 if (!PH.SearchClosingBrace("{{", "}}")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000477 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
478 diag::err_verify_missing_end) << KindStr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000479 continue;
Daniel Dunbar34818552009-11-14 03:23:19 +0000480 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000481 const char* const ContentEnd = PH.P; // mark content end
482 PH.Advance();
Daniel Dunbar34818552009-11-14 03:23:19 +0000483
Jordan Rosee1572eb2012-07-10 02:57:03 +0000484 // Build directive text; convert \n to newlines.
Chris Lattnere82411b2010-04-28 20:02:30 +0000485 std::string Text;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000486 StringRef NewlineStr = "\\n";
487 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattnere82411b2010-04-28 20:02:30 +0000488 size_t CPos = 0;
489 size_t FPos;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000490 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000491 Text += Content.substr(CPos, FPos-CPos);
492 Text += '\n';
493 CPos = FPos + NewlineStr.size();
Daniel Dunbar34818552009-11-14 03:23:19 +0000494 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000495 if (Text.empty())
496 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar34818552009-11-14 03:23:19 +0000497
Alp Toker6ed72512013-12-14 01:07:05 +0000498 // Check that regex directives contain at least one regex.
499 if (RegexKind && Text.find("{{") == StringRef::npos) {
500 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
501 diag::err_verify_missing_regex) << Text;
502 return false;
503 }
504
Jordan Rosee1572eb2012-07-10 02:57:03 +0000505 // Construct new directive.
David Blaikie93106512014-08-29 16:30:23 +0000506 std::unique_ptr<Directive> D = Directive::create(
507 RegexKind, Pos, ExpectedLoc, MatchAnyLine, Text, Min, Max);
Nico Weber568dacc2014-04-24 05:32:03 +0000508
Chris Lattnere82411b2010-04-28 20:02:30 +0000509 std::string Error;
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000510 if (D->isValid(Error)) {
David Blaikie93106512014-08-29 16:30:23 +0000511 DL->push_back(std::move(D));
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000512 FoundDirective = true;
513 } else {
Jordan Rose6dae7612012-07-10 02:56:15 +0000514 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
515 diag::err_verify_invalid_content)
Chris Lattnere82411b2010-04-28 20:02:30 +0000516 << KindStr << Error;
Daniel Dunbar34818552009-11-14 03:23:19 +0000517 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000518 }
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000519
520 return FoundDirective;
521}
522
523/// HandleComment - Hook into the preprocessor and extract comments containing
524/// expected errors and warnings.
525bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
526 SourceRange Comment) {
527 SourceManager &SM = PP.getSourceManager();
Douglas Gregor6b930962013-05-03 22:58:43 +0000528
529 // If this comment is for a different source manager, ignore it.
530 if (SrcManager && &SM != SrcManager)
531 return false;
532
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000533 SourceLocation CommentBegin = Comment.getBegin();
534
535 const char *CommentRaw = SM.getCharacterData(CommentBegin);
536 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
537
538 if (C.empty())
539 return false;
540
541 // Fold any "\<EOL>" sequences
542 size_t loc = C.find('\\');
543 if (loc == StringRef::npos) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000544 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000545 return false;
546 }
547
548 std::string C2;
549 C2.reserve(C.size());
550
551 for (size_t last = 0;; loc = C.find('\\', last)) {
552 if (loc == StringRef::npos || loc == C.size()) {
553 C2 += C.substr(last);
554 break;
555 }
556 C2 += C.substr(last, loc-last);
557 last = loc + 1;
558
559 if (C[last] == '\n' || C[last] == '\r') {
560 ++last;
561
562 // Escape \r\n or \n\r, but not \n\n.
563 if (last < C.size())
564 if (C[last] == '\n' || C[last] == '\r')
565 if (C[last] != C[last-1])
566 ++last;
567 } else {
568 // This was just a normal backslash.
569 C2 += '\\';
570 }
571 }
572
573 if (!C2.empty())
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000574 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000575 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000576}
577
Jordan Roseb00073d2012-08-10 01:06:16 +0000578#ifndef NDEBUG
579/// \brief Lex the specified source file to determine whether it contains
580/// any expected-* directives. As a Lexer is used rather than a full-blown
581/// Preprocessor, directives inside skipped #if blocks will still be found.
582///
583/// \return true if any directives were found.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000584static bool findDirectives(SourceManager &SM, FileID FID,
585 const LangOptions &LangOpts) {
Axel Naumannac50dcf2011-07-25 19:18:12 +0000586 // Create a raw lexer to pull all the comments out of FID.
587 if (FID.isInvalid())
Jordan Roseb00073d2012-08-10 01:06:16 +0000588 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000589
590 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner710bb872009-11-30 04:18:44 +0000591 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000592 Lexer RawLex(FID, FromFile, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000593
594 // Return comments as tokens, this is how we find expected diagnostics.
595 RawLex.SetCommentRetentionState(true);
596
597 Token Tok;
598 Tok.setKind(tok::comment);
Andy Gibbs0fea0452012-10-19 12:49:32 +0000599 VerifyDiagnosticConsumer::DirectiveStatus Status =
600 VerifyDiagnosticConsumer::HasNoDirectives;
Daniel Dunbar34818552009-11-14 03:23:19 +0000601 while (Tok.isNot(tok::eof)) {
Eli Friedman0834a4b2013-09-19 00:41:32 +0000602 RawLex.LexFromRawLexer(Tok);
Daniel Dunbar34818552009-11-14 03:23:19 +0000603 if (!Tok.is(tok::comment)) continue;
604
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000605 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000606 if (Comment.empty()) continue;
607
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000608 // Find first directive.
Craig Topper49a27902014-05-22 04:46:25 +0000609 if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
610 Status))
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000611 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000612 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000613 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000614}
Jordan Roseb00073d2012-08-10 01:06:16 +0000615#endif // !NDEBUG
Daniel Dunbar34818552009-11-14 03:23:19 +0000616
Jordan Rosee1572eb2012-07-10 02:57:03 +0000617/// \brief Takes a list of diagnostics that have been generated but not matched
618/// by an expected-* directive and produces a diagnostic to the user from this.
619static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
620 const_diag_iterator diag_begin,
621 const_diag_iterator diag_end,
622 const char *Kind) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000623 if (diag_begin == diag_end) return 0;
624
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000625 SmallString<256> Fmt;
Daniel Dunbar34818552009-11-14 03:23:19 +0000626 llvm::raw_svector_ostream OS(Fmt);
627 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000628 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar34818552009-11-14 03:23:19 +0000629 OS << "\n (frontend)";
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000630 else {
631 OS << "\n ";
632 if (const FileEntry *File = SourceMgr->getFileEntryForID(
633 SourceMgr->getFileID(I->first)))
634 OS << " File " << File->getName();
635 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
636 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000637 OS << ": " << I->second;
638 }
639
Jordan Rose6f524ac2012-07-11 16:50:36 +0000640 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000641 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar34818552009-11-14 03:23:19 +0000642 return std::distance(diag_begin, diag_end);
643}
644
Jordan Rosee1572eb2012-07-10 02:57:03 +0000645/// \brief Takes a list of diagnostics that were expected to have been generated
646/// but were not and produces a diagnostic to the user from this.
David Blaikie93106512014-08-29 16:30:23 +0000647static unsigned PrintExpected(DiagnosticsEngine &Diags,
648 SourceManager &SourceMgr,
649 std::vector<Directive *> &DL, const char *Kind) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000650 if (DL.empty())
651 return 0;
652
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000653 SmallString<256> Fmt;
Chris Lattnere82411b2010-04-28 20:02:30 +0000654 llvm::raw_svector_ostream OS(Fmt);
David Blaikie93106512014-08-29 16:30:23 +0000655 for (auto *DirPtr : DL) {
656 Directive &D = *DirPtr;
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000657 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc);
658 if (D.MatchAnyLine)
659 OS << " Line *";
660 else
661 OS << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Jordan Rosee1572eb2012-07-10 02:57:03 +0000662 if (D.DirectiveLoc != D.DiagnosticLoc)
663 OS << " (directive at "
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000664 << SourceMgr.getFilename(D.DirectiveLoc) << ':'
665 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
Chris Lattnere82411b2010-04-28 20:02:30 +0000666 OS << ": " << D.Text;
667 }
668
Jordan Rose6f524ac2012-07-11 16:50:36 +0000669 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000670 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattnere82411b2010-04-28 20:02:30 +0000671 return DL.size();
672}
673
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000674/// \brief Determine whether two source locations come from the same file.
675static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
676 SourceLocation DiagnosticLoc) {
677 while (DiagnosticLoc.isMacroID())
678 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
679
Eli Friedman5ba37d52013-08-22 00:27:10 +0000680 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000681 return true;
682
683 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
Eli Friedman5ba37d52013-08-22 00:27:10 +0000684 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000685 return true;
686
687 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
688}
689
Chris Lattnere82411b2010-04-28 20:02:30 +0000690/// CheckLists - Compare expected to seen diagnostic lists and return the
691/// the difference between them.
Daniel Dunbar34818552009-11-14 03:23:19 +0000692///
David Blaikie9c902b52011-09-25 23:23:43 +0000693static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattnere82411b2010-04-28 20:02:30 +0000694 const char *Label,
695 DirectiveList &Left,
696 const_diag_iterator d2_begin,
697 const_diag_iterator d2_end) {
David Blaikie93106512014-08-29 16:30:23 +0000698 std::vector<Directive *> LeftOnly;
Daniel Dunbar34818552009-11-14 03:23:19 +0000699 DiagList Right(d2_begin, d2_end);
700
David Blaikie93106512014-08-29 16:30:23 +0000701 for (auto &Owner : Left) {
702 Directive &D = *Owner;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000703 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar34818552009-11-14 03:23:19 +0000704
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000705 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000706 DiagList::iterator II, IE;
707 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000708 if (!D.MatchAnyLine) {
709 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
710 if (LineNo1 != LineNo2)
711 continue;
712 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000713
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000714 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
715 continue;
716
Chris Lattnere82411b2010-04-28 20:02:30 +0000717 const std::string &RightText = II->second;
Jordan Rose6dae7612012-07-10 02:56:15 +0000718 if (D.match(RightText))
Chris Lattnere82411b2010-04-28 20:02:30 +0000719 break;
Daniel Dunbar34818552009-11-14 03:23:19 +0000720 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000721 if (II == IE) {
722 // Not found.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000723 if (i >= D.Min) break;
David Blaikie93106512014-08-29 16:30:23 +0000724 LeftOnly.push_back(&D);
Chris Lattnere82411b2010-04-28 20:02:30 +0000725 } else {
726 // Found. The same cannot be found twice.
727 Right.erase(II);
728 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000729 }
730 }
731 // Now all that's left in Right are those that were not matched.
Jordan Rosee1572eb2012-07-10 02:57:03 +0000732 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
733 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumi7bfba2f2011-12-17 13:00:31 +0000734 return num;
Daniel Dunbar34818552009-11-14 03:23:19 +0000735}
736
737/// CheckResults - This compares the expected results to those that
738/// were actually reported. It emits any discrepencies. Return "true" if there
739/// were problems. Return "false" otherwise.
740///
David Blaikie9c902b52011-09-25 23:23:43 +0000741static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar34818552009-11-14 03:23:19 +0000742 const TextDiagnosticBuffer &Buffer,
Chris Lattnere82411b2010-04-28 20:02:30 +0000743 ExpectedData &ED) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000744 // We want to capture the delta between what was expected and what was
745 // seen.
746 //
747 // Expected \ Seen - set expected but not seen
748 // Seen \ Expected - set seen but not expected
749 unsigned NumProblems = 0;
750
751 // See if there are error mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000752 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
753 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000754
Daniel Dunbar34818552009-11-14 03:23:19 +0000755 // See if there are warning mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000756 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
757 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar34818552009-11-14 03:23:19 +0000758
Tobias Grosser86a85672014-05-01 14:06:01 +0000759 // See if there are remark mismatches.
760 NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
761 Buffer.remark_begin(), Buffer.remark_end());
762
Daniel Dunbar34818552009-11-14 03:23:19 +0000763 // See if there are note mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000764 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
765 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar34818552009-11-14 03:23:19 +0000766
767 return NumProblems;
768}
769
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000770void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
771 FileID FID,
772 ParsedStatus PS) {
773 // Check SourceManager hasn't changed.
774 setSourceManager(SM);
775
776#ifndef NDEBUG
777 if (FID.isInvalid())
778 return;
779
780 const FileEntry *FE = SM.getFileEntryForID(FID);
781
782 if (PS == IsParsed) {
783 // Move the FileID from the unparsed set to the parsed set.
784 UnparsedFiles.erase(FID);
785 ParsedFiles.insert(std::make_pair(FID, FE));
786 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
787 // Add the FileID to the unparsed set if we haven't seen it before.
788
789 // Check for directives.
790 bool FoundDirectives;
791 if (PS == IsUnparsedNoDirectives)
792 FoundDirectives = false;
793 else
794 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
795
796 // Add the FileID to the unparsed set.
797 UnparsedFiles.insert(std::make_pair(FID,
798 UnparsedFileStatus(FE, FoundDirectives)));
799 }
800#endif
801}
802
David Blaikie69609dc2011-09-26 00:38:03 +0000803void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar34818552009-11-14 03:23:19 +0000804 // Ensure any diagnostics go to the primary client.
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000805 bool OwnsCurClient = Diags.ownsClient();
David Blaikiee2eefae2011-09-25 23:39:51 +0000806 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000807 Diags.setClient(PrimaryClient, false);
Daniel Dunbar34818552009-11-14 03:23:19 +0000808
Jordan Roseb00073d2012-08-10 01:06:16 +0000809#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000810 // In a debug build, scan through any files that may have been missed
811 // during parsing and issue a fatal error if directives are contained
812 // within these files. If a fatal error occurs, this suggests that
813 // this file is being parsed separately from the main file, in which
814 // case consider moving the directives to the correct place, if this
815 // is applicable.
816 if (UnparsedFiles.size() > 0) {
817 // Generate a cache of parsed FileEntry pointers for alias lookups.
818 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
819 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
820 End = ParsedFiles.end(); I != End; ++I) {
821 if (const FileEntry *FE = I->second)
822 ParsedFileCache.insert(FE);
823 }
824
825 // Iterate through list of unparsed files.
826 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
827 End = UnparsedFiles.end(); I != End; ++I) {
828 const UnparsedFileStatus &Status = I->second;
829 const FileEntry *FE = Status.getFile();
830
831 // Skip files that have been parsed via an alias.
832 if (FE && ParsedFileCache.count(FE))
Jordan Roseb00073d2012-08-10 01:06:16 +0000833 continue;
834
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000835 // Report a fatal error if this file contained directives.
836 if (Status.foundDirectives()) {
Jordan Roseb00073d2012-08-10 01:06:16 +0000837 llvm::report_fatal_error(Twine("-verify directives found after rather"
838 " than during normal parsing of ",
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000839 StringRef(FE ? FE->getName() : "(unknown)")));
840 }
Axel Naumann744f1212011-08-24 13:36:19 +0000841 }
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000842
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000843 // UnparsedFiles has been processed now, so clear it.
844 UnparsedFiles.clear();
845 }
846#endif // !NDEBUG
847
848 if (SrcManager) {
Andy Gibbs0fea0452012-10-19 12:49:32 +0000849 // Produce an error if no expected-* directives could be found in the
850 // source file(s) processed.
851 if (Status == HasNoDirectives) {
852 Diags.Report(diag::err_verify_no_directives).setForceEmit();
853 ++NumErrors;
854 Status = HasNoDirectivesReported;
855 }
856
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000857 // Check that the expected diagnostics occurred.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000858 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000859 } else {
Craig Topper49a27902014-05-22 04:46:25 +0000860 NumErrors += (PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
Jordan Rosee1572eb2012-07-10 02:57:03 +0000861 Buffer->err_end(), "error") +
Craig Topper49a27902014-05-22 04:46:25 +0000862 PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
Jordan Rosee1572eb2012-07-10 02:57:03 +0000863 Buffer->warn_end(), "warn") +
Craig Topper49a27902014-05-22 04:46:25 +0000864 PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
Jordan Rosee1572eb2012-07-10 02:57:03 +0000865 Buffer->note_end(), "note"));
Daniel Dunbar34818552009-11-14 03:23:19 +0000866 }
867
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000868 Diags.takeClient();
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000869 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar34818552009-11-14 03:23:19 +0000870
871 // Reset the buffer, we have processed all the diagnostics in it.
872 Buffer.reset(new TextDiagnosticBuffer());
Nico Weberdc493cf2014-04-24 05:39:55 +0000873 ED.Reset();
Daniel Dunbar34818552009-11-14 03:23:19 +0000874}
Chris Lattnere82411b2010-04-28 20:02:30 +0000875
David Blaikie93106512014-08-29 16:30:23 +0000876std::unique_ptr<Directive> Directive::create(bool RegexKind,
877 SourceLocation DirectiveLoc,
878 SourceLocation DiagnosticLoc,
879 bool MatchAnyLine, StringRef Text,
880 unsigned Min, unsigned Max) {
Alp Toker6ed72512013-12-14 01:07:05 +0000881 if (!RegexKind)
David Blaikie93106512014-08-29 16:30:23 +0000882 return llvm::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc,
883 MatchAnyLine, Text, Min, Max);
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000884
885 // Parse the directive into a regular expression.
886 std::string RegexStr;
887 StringRef S = Text;
888 while (!S.empty()) {
889 if (S.startswith("{{")) {
890 S = S.drop_front(2);
891 size_t RegexMatchLength = S.find("}}");
892 assert(RegexMatchLength != StringRef::npos);
893 // Append the regex, enclosed in parentheses.
894 RegexStr += "(";
895 RegexStr.append(S.data(), RegexMatchLength);
896 RegexStr += ")";
897 S = S.drop_front(RegexMatchLength + 2);
898 } else {
899 size_t VerbatimMatchLength = S.find("{{");
900 if (VerbatimMatchLength == StringRef::npos)
901 VerbatimMatchLength = S.size();
902 // Escape and append the fixed string.
Hans Wennborge6a87752013-12-12 00:27:31 +0000903 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000904 S = S.drop_front(VerbatimMatchLength);
905 }
906 }
907
David Blaikie93106512014-08-29 16:30:23 +0000908 return llvm::make_unique<RegexDirective>(
909 DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr);
Chris Lattnere82411b2010-04-28 20:02:30 +0000910}