blob: 40992fec3f6aafbd5c40573b0ababb705575768a [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
David Blaikie69609dc2011-09-26 00:38:03 +000030VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
Jordan Roseb00073d2012-08-10 01:06:16 +000031 : Diags(_Diags),
32 PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()),
33 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0),
Andy Gibbs0fea0452012-10-19 12:49:32 +000034 LangOpts(0), SrcManager(0), ActiveSourceFiles(0), Status(HasNoDirectives)
Douglas Gregor2b9b4642011-09-13 01:26:44 +000035{
36 Diags.takeClient();
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!");
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000044 SrcManager = 0;
Douglas Gregor2b9b4642011-09-13 01:26:44 +000045 CheckDiagnostics();
46 Diags.takeClient();
47 if (OwnsPrimaryClient)
48 delete PrimaryClient;
Daniel Dunbar34818552009-11-14 03:23:19 +000049}
50
Jordan Roseb00073d2012-08-10 01:06:16 +000051#ifndef NDEBUG
52namespace {
53class VerifyFileTracker : public PPCallbacks {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000054 VerifyDiagnosticConsumer &Verify;
Jordan Roseb00073d2012-08-10 01:06:16 +000055 SourceManager &SM;
56
57public:
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000058 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
59 : Verify(Verify), SM(SM) { }
Jordan Roseb00073d2012-08-10 01:06:16 +000060
61 /// \brief Hook into the preprocessor and update the list of parsed
62 /// files when the preprocessor indicates a new file is entered.
63 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
64 SrcMgr::CharacteristicKind FileType,
65 FileID PrevFID) {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000066 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
67 VerifyDiagnosticConsumer::IsParsed);
Jordan Roseb00073d2012-08-10 01:06:16 +000068 }
69};
70} // End anonymous namespace.
71#endif
72
David Blaikiee2eefae2011-09-25 23:39:51 +000073// DiagnosticConsumer interface.
Daniel Dunbar34818552009-11-14 03:23:19 +000074
David Blaikie69609dc2011-09-26 00:38:03 +000075void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor32fbe312012-01-20 16:28:04 +000076 const Preprocessor *PP) {
Jordan Roseb00073d2012-08-10 01:06:16 +000077 // Attach comment handler on first invocation.
78 if (++ActiveSourceFiles == 1) {
79 if (PP) {
80 CurrentPreprocessor = PP;
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000081 this->LangOpts = &LangOpts;
82 setSourceManager(PP->getSourceManager());
Jordan Roseb00073d2012-08-10 01:06:16 +000083 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
84#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000085 // Debug build tracks parsed files.
86 VerifyFileTracker *V = new VerifyFileTracker(*this, *SrcManager);
Jordan Roseb00073d2012-08-10 01:06:16 +000087 const_cast<Preprocessor*>(PP)->addPPCallbacks(V);
88#endif
89 }
90 }
Daniel Dunbar34818552009-11-14 03:23:19 +000091
Jordan Roseb00073d2012-08-10 01:06:16 +000092 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
Daniel Dunbar34818552009-11-14 03:23:19 +000093 PrimaryClient->BeginSourceFile(LangOpts, PP);
94}
95
David Blaikie69609dc2011-09-26 00:38:03 +000096void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Roseb00073d2012-08-10 01:06:16 +000097 assert(ActiveSourceFiles && "No active source files!");
Daniel Dunbar34818552009-11-14 03:23:19 +000098 PrimaryClient->EndSourceFile();
99
Jordan Roseb00073d2012-08-10 01:06:16 +0000100 // Detach comment handler once last active source file completed.
101 if (--ActiveSourceFiles == 0) {
102 if (CurrentPreprocessor)
103 const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this);
104
105 // Check diagnostics once last file completed.
106 CheckDiagnostics();
107 CurrentPreprocessor = 0;
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000108 LangOpts = 0;
Jordan Roseb00073d2012-08-10 01:06:16 +0000109 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000110}
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000111
David Blaikie69609dc2011-09-26 00:38:03 +0000112void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikieb5784322011-09-26 01:18:08 +0000113 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Douglas Gregor6b930962013-05-03 22:58:43 +0000114 if (Info.hasSourceManager()) {
115 // If this diagnostic is for a different source manager, ignore it.
116 if (SrcManager && &Info.getSourceManager() != SrcManager)
117 return;
118
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000119 setSourceManager(Info.getSourceManager());
Douglas Gregor6b930962013-05-03 22:58:43 +0000120 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000121
Jordan Roseb00073d2012-08-10 01:06:16 +0000122#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000123 // Debug build tracks unparsed files for possible
124 // unparsed expected-* directives.
125 if (SrcManager) {
126 SourceLocation Loc = Info.getLocation();
127 if (Loc.isValid()) {
128 ParsedStatus PS = IsUnparsed;
129
130 Loc = SrcManager->getExpansionLoc(Loc);
131 FileID FID = SrcManager->getFileID(Loc);
132
133 const FileEntry *FE = SrcManager->getFileEntryForID(FID);
134 if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
135 // If the file is a modules header file it shall not be parsed
136 // for expected-* directives.
137 HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
138 if (HS.findModuleForHeader(FE))
139 PS = IsUnparsedNoDirectives;
140 }
141
142 UpdateParsedFileStatus(*SrcManager, FID, PS);
143 }
Axel Naumannac50dcf2011-07-25 19:18:12 +0000144 }
Jordan Roseb00073d2012-08-10 01:06:16 +0000145#endif
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000146
Daniel Dunbar34818552009-11-14 03:23:19 +0000147 // Send the diagnostic to the buffer, we will check it once we reach the end
148 // of the source file (or are destructed).
149 Buffer->HandleDiagnostic(DiagLevel, Info);
150}
151
Daniel Dunbar34818552009-11-14 03:23:19 +0000152//===----------------------------------------------------------------------===//
153// Checking diagnostics implementation.
154//===----------------------------------------------------------------------===//
155
156typedef TextDiagnosticBuffer::DiagList DiagList;
157typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
158
Chris Lattnere82411b2010-04-28 20:02:30 +0000159namespace {
160
Chris Lattnere82411b2010-04-28 20:02:30 +0000161/// StandardDirective - Directive with string matching.
162///
163class StandardDirective : public Directive {
164public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000165 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000166 StringRef Text, unsigned Min, unsigned Max)
167 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000168
169 virtual bool isValid(std::string &Error) {
170 // all strings are considered valid; even empty ones
171 return true;
172 }
173
Jordan Rose6dae7612012-07-10 02:56:15 +0000174 virtual bool match(StringRef S) {
175 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,
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000184 StringRef Text, unsigned Min, unsigned Max, StringRef RegexStr)
185 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max), Regex(RegexStr) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000186
187 virtual bool isValid(std::string &Error) {
188 if (Regex.isValid(Error))
189 return true;
190 return false;
191 }
192
Jordan Rose6dae7612012-07-10 02:56:15 +0000193 virtual bool match(StringRef S) {
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)
205 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(NULL) { }
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
303} // namespace anonymous
304
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 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000328 DirectiveList* DL = NULL;
329 if (PH.Next("error"))
Jordan Roseb00073d2012-08-10 01:06:16 +0000330 DL = ED ? &ED->Errors : NULL;
Chris Lattnere82411b2010-04-28 20:02:30 +0000331 else if (PH.Next("warning"))
Jordan Roseb00073d2012-08-10 01:06:16 +0000332 DL = ED ? &ED->Warnings : NULL;
Chris Lattnere82411b2010-04-28 20:02:30 +0000333 else if (PH.Next("note"))
Jordan Roseb00073d2012-08-10 01:06:16 +0000334 DL = ED ? &ED->Notes : NULL;
Andy Gibbs0fea0452012-10-19 12:49:32 +0000335 else if (PH.Next("no-diagnostics")) {
336 if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
337 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
338 << /*IsExpectedNoDiagnostics=*/true;
339 else
340 Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
341 continue;
342 } else
Chris Lattnere82411b2010-04-28 20:02:30 +0000343 continue;
344 PH.Advance();
345
Andy Gibbs0fea0452012-10-19 12:49:32 +0000346 if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
347 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
348 << /*IsExpectedNoDiagnostics=*/false;
349 continue;
350 }
351 Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
352
Jordan Roseb00073d2012-08-10 01:06:16 +0000353 // If a directive has been found but we're not interested
354 // in storing the directive information, return now.
355 if (!DL)
356 return true;
357
Jordan Rosee1572eb2012-07-10 02:57:03 +0000358 // Default directive kind.
Chris Lattnere82411b2010-04-28 20:02:30 +0000359 bool RegexKind = false;
360 const char* KindStr = "string";
361
Jordan Rosee1572eb2012-07-10 02:57:03 +0000362 // Next optional token: -
Chris Lattnere82411b2010-04-28 20:02:30 +0000363 if (PH.Next("-re")) {
364 PH.Advance();
365 RegexKind = true;
366 KindStr = "regex";
367 }
368
Jordan Rosee1572eb2012-07-10 02:57:03 +0000369 // Next optional token: @
370 SourceLocation ExpectedLoc;
371 if (!PH.Next("@")) {
372 ExpectedLoc = Pos;
373 } else {
374 PH.Advance();
375 unsigned Line = 0;
376 bool FoundPlus = PH.Next("+");
377 if (FoundPlus || PH.Next("-")) {
378 // Relative to current line.
379 PH.Advance();
380 bool Invalid = false;
381 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
382 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
383 if (FoundPlus) ExpectedLine += Line;
384 else ExpectedLine -= Line;
385 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
386 }
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000387 } else if (PH.Next(Line)) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000388 // Absolute line number.
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000389 if (Line > 0)
Jordan Rosee1572eb2012-07-10 02:57:03 +0000390 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000391 } else if (PP && PH.Search(":")) {
392 // Specific source file.
393 StringRef Filename(PH.C, PH.P-PH.C);
394 PH.Advance();
395
396 // Lookup file via Preprocessor, like a #include.
397 const DirectoryLookup *CurDir;
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000398 const FileEntry *FE = PP->LookupFile(Pos, Filename, false, NULL, CurDir,
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000399 NULL, NULL, 0);
400 if (!FE) {
401 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
402 diag::err_verify_missing_file) << Filename << KindStr;
403 continue;
404 }
405
406 if (SM.translateFile(FE).isInvalid())
407 SM.createFileID(FE, Pos, SrcMgr::C_User);
408
409 if (PH.Next(Line) && Line > 0)
410 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
Jordan Rosee1572eb2012-07-10 02:57:03 +0000411 }
412
413 if (ExpectedLoc.isInvalid()) {
414 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
415 diag::err_verify_missing_line) << KindStr;
416 continue;
417 }
418 PH.Advance();
419 }
420
421 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000422 PH.SkipWhitespace();
423
Jordan Rosee1572eb2012-07-10 02:57:03 +0000424 // Next optional token: positive integer or a '+'.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000425 unsigned Min = 1;
426 unsigned Max = 1;
427 if (PH.Next(Min)) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000428 PH.Advance();
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000429 // A positive integer can be followed by a '+' meaning min
430 // or more, or by a '-' meaning a range from min to max.
431 if (PH.Next("+")) {
432 Max = Directive::MaxCount;
433 PH.Advance();
434 } else if (PH.Next("-")) {
435 PH.Advance();
436 if (!PH.Next(Max) || Max < Min) {
437 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
438 diag::err_verify_invalid_range) << KindStr;
439 continue;
440 }
441 PH.Advance();
442 } else {
443 Max = Min;
444 }
445 } else if (PH.Next("+")) {
446 // '+' on its own means "1 or more".
447 Max = Directive::MaxCount;
Anna Zaksa2510072011-12-15 02:28:16 +0000448 PH.Advance();
449 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000450
Jordan Rosee1572eb2012-07-10 02:57:03 +0000451 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000452 PH.SkipWhitespace();
453
Jordan Rosee1572eb2012-07-10 02:57:03 +0000454 // Next token: {{
Chris Lattnere82411b2010-04-28 20:02:30 +0000455 if (!PH.Next("{{")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000456 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
457 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar34818552009-11-14 03:23:19 +0000458 continue;
459 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000460 PH.Advance();
461 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar34818552009-11-14 03:23:19 +0000462
Jordan Rosee1572eb2012-07-10 02:57:03 +0000463 // Search for token: }}
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000464 if (!PH.SearchClosingBrace("{{", "}}")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000465 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
466 diag::err_verify_missing_end) << KindStr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000467 continue;
Daniel Dunbar34818552009-11-14 03:23:19 +0000468 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000469 const char* const ContentEnd = PH.P; // mark content end
470 PH.Advance();
Daniel Dunbar34818552009-11-14 03:23:19 +0000471
Jordan Rosee1572eb2012-07-10 02:57:03 +0000472 // Build directive text; convert \n to newlines.
Chris Lattnere82411b2010-04-28 20:02:30 +0000473 std::string Text;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000474 StringRef NewlineStr = "\\n";
475 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattnere82411b2010-04-28 20:02:30 +0000476 size_t CPos = 0;
477 size_t FPos;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000478 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000479 Text += Content.substr(CPos, FPos-CPos);
480 Text += '\n';
481 CPos = FPos + NewlineStr.size();
Daniel Dunbar34818552009-11-14 03:23:19 +0000482 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000483 if (Text.empty())
484 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar34818552009-11-14 03:23:19 +0000485
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000486 // Check that regex directives contain at least one regex.
487 if (RegexKind && Text.find("{{") == StringRef::npos) {
488 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
489 diag::err_verify_missing_regex) << Text;
490 return false;
491 }
492
Jordan Rosee1572eb2012-07-10 02:57:03 +0000493 // Construct new directive.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000494 Directive *D = Directive::create(RegexKind, Pos, ExpectedLoc, Text,
495 Min, Max);
Chris Lattnere82411b2010-04-28 20:02:30 +0000496 std::string Error;
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000497 if (D->isValid(Error)) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000498 DL->push_back(D);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000499 FoundDirective = true;
500 } else {
Jordan Rose6dae7612012-07-10 02:56:15 +0000501 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
502 diag::err_verify_invalid_content)
Chris Lattnere82411b2010-04-28 20:02:30 +0000503 << KindStr << Error;
Daniel Dunbar34818552009-11-14 03:23:19 +0000504 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000505 }
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000506
507 return FoundDirective;
508}
509
510/// HandleComment - Hook into the preprocessor and extract comments containing
511/// expected errors and warnings.
512bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
513 SourceRange Comment) {
514 SourceManager &SM = PP.getSourceManager();
Douglas Gregor6b930962013-05-03 22:58:43 +0000515
516 // If this comment is for a different source manager, ignore it.
517 if (SrcManager && &SM != SrcManager)
518 return false;
519
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000520 SourceLocation CommentBegin = Comment.getBegin();
521
522 const char *CommentRaw = SM.getCharacterData(CommentBegin);
523 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
524
525 if (C.empty())
526 return false;
527
528 // Fold any "\<EOL>" sequences
529 size_t loc = C.find('\\');
530 if (loc == StringRef::npos) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000531 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000532 return false;
533 }
534
535 std::string C2;
536 C2.reserve(C.size());
537
538 for (size_t last = 0;; loc = C.find('\\', last)) {
539 if (loc == StringRef::npos || loc == C.size()) {
540 C2 += C.substr(last);
541 break;
542 }
543 C2 += C.substr(last, loc-last);
544 last = loc + 1;
545
546 if (C[last] == '\n' || C[last] == '\r') {
547 ++last;
548
549 // Escape \r\n or \n\r, but not \n\n.
550 if (last < C.size())
551 if (C[last] == '\n' || C[last] == '\r')
552 if (C[last] != C[last-1])
553 ++last;
554 } else {
555 // This was just a normal backslash.
556 C2 += '\\';
557 }
558 }
559
560 if (!C2.empty())
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000561 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000562 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000563}
564
Jordan Roseb00073d2012-08-10 01:06:16 +0000565#ifndef NDEBUG
566/// \brief Lex the specified source file to determine whether it contains
567/// any expected-* directives. As a Lexer is used rather than a full-blown
568/// Preprocessor, directives inside skipped #if blocks will still be found.
569///
570/// \return true if any directives were found.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000571static bool findDirectives(SourceManager &SM, FileID FID,
572 const LangOptions &LangOpts) {
Axel Naumannac50dcf2011-07-25 19:18:12 +0000573 // Create a raw lexer to pull all the comments out of FID.
574 if (FID.isInvalid())
Jordan Roseb00073d2012-08-10 01:06:16 +0000575 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000576
577 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner710bb872009-11-30 04:18:44 +0000578 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000579 Lexer RawLex(FID, FromFile, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000580
581 // Return comments as tokens, this is how we find expected diagnostics.
582 RawLex.SetCommentRetentionState(true);
583
584 Token Tok;
585 Tok.setKind(tok::comment);
Andy Gibbs0fea0452012-10-19 12:49:32 +0000586 VerifyDiagnosticConsumer::DirectiveStatus Status =
587 VerifyDiagnosticConsumer::HasNoDirectives;
Daniel Dunbar34818552009-11-14 03:23:19 +0000588 while (Tok.isNot(tok::eof)) {
Eli Friedman0834a4b2013-09-19 00:41:32 +0000589 RawLex.LexFromRawLexer(Tok);
Daniel Dunbar34818552009-11-14 03:23:19 +0000590 if (!Tok.is(tok::comment)) continue;
591
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000592 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000593 if (Comment.empty()) continue;
594
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000595 // Find first directive.
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000596 if (ParseDirective(Comment, 0, SM, 0, Tok.getLocation(), Status))
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000597 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000598 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000599 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000600}
Jordan Roseb00073d2012-08-10 01:06:16 +0000601#endif // !NDEBUG
Daniel Dunbar34818552009-11-14 03:23:19 +0000602
Jordan Rosee1572eb2012-07-10 02:57:03 +0000603/// \brief Takes a list of diagnostics that have been generated but not matched
604/// by an expected-* directive and produces a diagnostic to the user from this.
605static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
606 const_diag_iterator diag_begin,
607 const_diag_iterator diag_end,
608 const char *Kind) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000609 if (diag_begin == diag_end) return 0;
610
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000611 SmallString<256> Fmt;
Daniel Dunbar34818552009-11-14 03:23:19 +0000612 llvm::raw_svector_ostream OS(Fmt);
613 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000614 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar34818552009-11-14 03:23:19 +0000615 OS << "\n (frontend)";
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000616 else {
617 OS << "\n ";
618 if (const FileEntry *File = SourceMgr->getFileEntryForID(
619 SourceMgr->getFileID(I->first)))
620 OS << " File " << File->getName();
621 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
622 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000623 OS << ": " << I->second;
624 }
625
Jordan Rose6f524ac2012-07-11 16:50:36 +0000626 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000627 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar34818552009-11-14 03:23:19 +0000628 return std::distance(diag_begin, diag_end);
629}
630
Jordan Rosee1572eb2012-07-10 02:57:03 +0000631/// \brief Takes a list of diagnostics that were expected to have been generated
632/// but were not and produces a diagnostic to the user from this.
633static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
634 DirectiveList &DL, const char *Kind) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000635 if (DL.empty())
636 return 0;
637
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000638 SmallString<256> Fmt;
Chris Lattnere82411b2010-04-28 20:02:30 +0000639 llvm::raw_svector_ostream OS(Fmt);
640 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000641 Directive &D = **I;
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000642 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc)
643 << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Jordan Rosee1572eb2012-07-10 02:57:03 +0000644 if (D.DirectiveLoc != D.DiagnosticLoc)
645 OS << " (directive at "
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000646 << SourceMgr.getFilename(D.DirectiveLoc) << ':'
647 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
Chris Lattnere82411b2010-04-28 20:02:30 +0000648 OS << ": " << D.Text;
649 }
650
Jordan Rose6f524ac2012-07-11 16:50:36 +0000651 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000652 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattnere82411b2010-04-28 20:02:30 +0000653 return DL.size();
654}
655
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000656/// \brief Determine whether two source locations come from the same file.
657static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
658 SourceLocation DiagnosticLoc) {
659 while (DiagnosticLoc.isMacroID())
660 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
661
Eli Friedman5ba37d52013-08-22 00:27:10 +0000662 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000663 return true;
664
665 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
Eli Friedman5ba37d52013-08-22 00:27:10 +0000666 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000667 return true;
668
669 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
670}
671
Chris Lattnere82411b2010-04-28 20:02:30 +0000672/// CheckLists - Compare expected to seen diagnostic lists and return the
673/// the difference between them.
Daniel Dunbar34818552009-11-14 03:23:19 +0000674///
David Blaikie9c902b52011-09-25 23:23:43 +0000675static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattnere82411b2010-04-28 20:02:30 +0000676 const char *Label,
677 DirectiveList &Left,
678 const_diag_iterator d2_begin,
679 const_diag_iterator d2_end) {
680 DirectiveList LeftOnly;
Daniel Dunbar34818552009-11-14 03:23:19 +0000681 DiagList Right(d2_begin, d2_end);
682
Chris Lattnere82411b2010-04-28 20:02:30 +0000683 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
684 Directive& D = **I;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000685 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar34818552009-11-14 03:23:19 +0000686
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000687 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000688 DiagList::iterator II, IE;
689 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth1aef0c52011-02-23 00:47:48 +0000690 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattnere82411b2010-04-28 20:02:30 +0000691 if (LineNo1 != LineNo2)
692 continue;
Daniel Dunbar34818552009-11-14 03:23:19 +0000693
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000694 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
695 continue;
696
Chris Lattnere82411b2010-04-28 20:02:30 +0000697 const std::string &RightText = II->second;
Jordan Rose6dae7612012-07-10 02:56:15 +0000698 if (D.match(RightText))
Chris Lattnere82411b2010-04-28 20:02:30 +0000699 break;
Daniel Dunbar34818552009-11-14 03:23:19 +0000700 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000701 if (II == IE) {
702 // Not found.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000703 if (i >= D.Min) break;
Chris Lattnere82411b2010-04-28 20:02:30 +0000704 LeftOnly.push_back(*I);
705 } else {
706 // Found. The same cannot be found twice.
707 Right.erase(II);
708 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000709 }
710 }
711 // Now all that's left in Right are those that were not matched.
Jordan Rosee1572eb2012-07-10 02:57:03 +0000712 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
713 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumi7bfba2f2011-12-17 13:00:31 +0000714 return num;
Daniel Dunbar34818552009-11-14 03:23:19 +0000715}
716
717/// CheckResults - This compares the expected results to those that
718/// were actually reported. It emits any discrepencies. Return "true" if there
719/// were problems. Return "false" otherwise.
720///
David Blaikie9c902b52011-09-25 23:23:43 +0000721static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar34818552009-11-14 03:23:19 +0000722 const TextDiagnosticBuffer &Buffer,
Chris Lattnere82411b2010-04-28 20:02:30 +0000723 ExpectedData &ED) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000724 // We want to capture the delta between what was expected and what was
725 // seen.
726 //
727 // Expected \ Seen - set expected but not seen
728 // Seen \ Expected - set seen but not expected
729 unsigned NumProblems = 0;
730
731 // See if there are error mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000732 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
733 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000734
Daniel Dunbar34818552009-11-14 03:23:19 +0000735 // See if there are warning mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000736 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
737 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar34818552009-11-14 03:23:19 +0000738
739 // See if there are note mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000740 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
741 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar34818552009-11-14 03:23:19 +0000742
743 return NumProblems;
744}
745
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000746void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
747 FileID FID,
748 ParsedStatus PS) {
749 // Check SourceManager hasn't changed.
750 setSourceManager(SM);
751
752#ifndef NDEBUG
753 if (FID.isInvalid())
754 return;
755
756 const FileEntry *FE = SM.getFileEntryForID(FID);
757
758 if (PS == IsParsed) {
759 // Move the FileID from the unparsed set to the parsed set.
760 UnparsedFiles.erase(FID);
761 ParsedFiles.insert(std::make_pair(FID, FE));
762 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
763 // Add the FileID to the unparsed set if we haven't seen it before.
764
765 // Check for directives.
766 bool FoundDirectives;
767 if (PS == IsUnparsedNoDirectives)
768 FoundDirectives = false;
769 else
770 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
771
772 // Add the FileID to the unparsed set.
773 UnparsedFiles.insert(std::make_pair(FID,
774 UnparsedFileStatus(FE, FoundDirectives)));
775 }
776#endif
777}
778
David Blaikie69609dc2011-09-26 00:38:03 +0000779void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar34818552009-11-14 03:23:19 +0000780 // Ensure any diagnostics go to the primary client.
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000781 bool OwnsCurClient = Diags.ownsClient();
David Blaikiee2eefae2011-09-25 23:39:51 +0000782 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000783 Diags.setClient(PrimaryClient, false);
Daniel Dunbar34818552009-11-14 03:23:19 +0000784
Jordan Roseb00073d2012-08-10 01:06:16 +0000785#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000786 // In a debug build, scan through any files that may have been missed
787 // during parsing and issue a fatal error if directives are contained
788 // within these files. If a fatal error occurs, this suggests that
789 // this file is being parsed separately from the main file, in which
790 // case consider moving the directives to the correct place, if this
791 // is applicable.
792 if (UnparsedFiles.size() > 0) {
793 // Generate a cache of parsed FileEntry pointers for alias lookups.
794 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
795 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
796 End = ParsedFiles.end(); I != End; ++I) {
797 if (const FileEntry *FE = I->second)
798 ParsedFileCache.insert(FE);
799 }
800
801 // Iterate through list of unparsed files.
802 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
803 End = UnparsedFiles.end(); I != End; ++I) {
804 const UnparsedFileStatus &Status = I->second;
805 const FileEntry *FE = Status.getFile();
806
807 // Skip files that have been parsed via an alias.
808 if (FE && ParsedFileCache.count(FE))
Jordan Roseb00073d2012-08-10 01:06:16 +0000809 continue;
810
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000811 // Report a fatal error if this file contained directives.
812 if (Status.foundDirectives()) {
Jordan Roseb00073d2012-08-10 01:06:16 +0000813 llvm::report_fatal_error(Twine("-verify directives found after rather"
814 " than during normal parsing of ",
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000815 StringRef(FE ? FE->getName() : "(unknown)")));
816 }
Axel Naumann744f1212011-08-24 13:36:19 +0000817 }
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000818
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000819 // UnparsedFiles has been processed now, so clear it.
820 UnparsedFiles.clear();
821 }
822#endif // !NDEBUG
823
824 if (SrcManager) {
Andy Gibbs0fea0452012-10-19 12:49:32 +0000825 // Produce an error if no expected-* directives could be found in the
826 // source file(s) processed.
827 if (Status == HasNoDirectives) {
828 Diags.Report(diag::err_verify_no_directives).setForceEmit();
829 ++NumErrors;
830 Status = HasNoDirectivesReported;
831 }
832
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000833 // Check that the expected diagnostics occurred.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000834 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000835 } else {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000836 NumErrors += (PrintUnexpected(Diags, 0, Buffer->err_begin(),
837 Buffer->err_end(), "error") +
838 PrintUnexpected(Diags, 0, Buffer->warn_begin(),
839 Buffer->warn_end(), "warn") +
840 PrintUnexpected(Diags, 0, Buffer->note_begin(),
841 Buffer->note_end(), "note"));
Daniel Dunbar34818552009-11-14 03:23:19 +0000842 }
843
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000844 Diags.takeClient();
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000845 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar34818552009-11-14 03:23:19 +0000846
847 // Reset the buffer, we have processed all the diagnostics in it.
848 Buffer.reset(new TextDiagnosticBuffer());
Axel Naumannb2f1a462012-07-10 16:24:07 +0000849 ED.Errors.clear();
850 ED.Warnings.clear();
851 ED.Notes.clear();
Daniel Dunbar34818552009-11-14 03:23:19 +0000852}
Chris Lattnere82411b2010-04-28 20:02:30 +0000853
Jordan Rosee1572eb2012-07-10 02:57:03 +0000854Directive *Directive::create(bool RegexKind, SourceLocation DirectiveLoc,
855 SourceLocation DiagnosticLoc, StringRef Text,
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000856 unsigned Min, unsigned Max) {
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000857 if (!RegexKind)
858 return new StandardDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
859
860 // Parse the directive into a regular expression.
861 std::string RegexStr;
862 StringRef S = Text;
863 while (!S.empty()) {
864 if (S.startswith("{{")) {
865 S = S.drop_front(2);
866 size_t RegexMatchLength = S.find("}}");
867 assert(RegexMatchLength != StringRef::npos);
868 // Append the regex, enclosed in parentheses.
869 RegexStr += "(";
870 RegexStr.append(S.data(), RegexMatchLength);
871 RegexStr += ")";
872 S = S.drop_front(RegexMatchLength + 2);
873 } else {
874 size_t VerbatimMatchLength = S.find("{{");
875 if (VerbatimMatchLength == StringRef::npos)
876 VerbatimMatchLength = S.size();
877 // Escape and append the fixed string.
Hans Wennborge6a87752013-12-12 00:27:31 +0000878 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000879 S = S.drop_front(VerbatimMatchLength);
880 }
881 }
882
883 return new RegexDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max, RegexStr);
Chris Lattnere82411b2010-04-28 20:02:30 +0000884}