blob: 045e60add1fc97069b42849600ceb14efd297438 [file] [log] [blame]
David Blaikie621bc692011-09-26 00:38:03 +00001//===---- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ------===//
Daniel Dunbar81f5a1e2009-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 Blaikie621bc692011-09-26 00:38:03 +000014#include "clang/Frontend/VerifyDiagnosticConsumer.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000015#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/Basic/FileManager.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000017#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/TextDiagnosticBuffer.h"
Jordan Rose7c304f52012-08-10 01:06:16 +000019#include "clang/Lex/HeaderSearch.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000020#include "clang/Lex/Preprocessor.h"
21#include "llvm/ADT/SmallString.h"
Chris Lattner60909e12010-04-28 20:02:30 +000022#include "llvm/Support/Regex.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000023#include "llvm/Support/raw_ostream.h"
Anna Zaksc035e092011-12-15 02:58:00 +000024
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000025using namespace clang;
Jordan Rose4313c012012-07-10 02:56:15 +000026typedef VerifyDiagnosticConsumer::Directive Directive;
27typedef VerifyDiagnosticConsumer::DirectiveList DirectiveList;
28typedef VerifyDiagnosticConsumer::ExpectedData ExpectedData;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000029
David Blaikie621bc692011-09-26 00:38:03 +000030VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
Jordan Rose7c304f52012-08-10 01:06:16 +000031 : Diags(_Diags),
32 PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()),
33 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0),
Andy Gibbs266dba32012-10-19 12:49:32 +000034 LangOpts(0), SrcManager(0), ActiveSourceFiles(0), Status(HasNoDirectives)
Douglas Gregor78243652011-09-13 01:26:44 +000035{
36 Diags.takeClient();
Jordan Rose7eaaa182012-08-18 16:58:52 +000037 if (Diags.hasSourceManager())
38 setSourceManager(Diags.getSourceManager());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000039}
40
David Blaikie621bc692011-09-26 00:38:03 +000041VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Jordan Rose7c304f52012-08-10 01:06:16 +000042 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
43 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
Jordan Rose7eaaa182012-08-18 16:58:52 +000044 SrcManager = 0;
Douglas Gregor78243652011-09-13 01:26:44 +000045 CheckDiagnostics();
46 Diags.takeClient();
47 if (OwnsPrimaryClient)
48 delete PrimaryClient;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000049}
50
Jordan Rose7c304f52012-08-10 01:06:16 +000051#ifndef NDEBUG
52namespace {
53class VerifyFileTracker : public PPCallbacks {
Jordan Rose7eaaa182012-08-18 16:58:52 +000054 VerifyDiagnosticConsumer &Verify;
Jordan Rose7c304f52012-08-10 01:06:16 +000055 SourceManager &SM;
56
57public:
Jordan Rose7eaaa182012-08-18 16:58:52 +000058 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
59 : Verify(Verify), SM(SM) { }
Jordan Rose7c304f52012-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 Rose7eaaa182012-08-18 16:58:52 +000066 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
67 VerifyDiagnosticConsumer::IsParsed);
Jordan Rose7c304f52012-08-10 01:06:16 +000068 }
69};
70} // End anonymous namespace.
71#endif
72
David Blaikie78ad0b92011-09-25 23:39:51 +000073// DiagnosticConsumer interface.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000074
David Blaikie621bc692011-09-26 00:38:03 +000075void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +000076 const Preprocessor *PP) {
Jordan Rose7c304f52012-08-10 01:06:16 +000077 // Attach comment handler on first invocation.
78 if (++ActiveSourceFiles == 1) {
79 if (PP) {
80 CurrentPreprocessor = PP;
Jordan Rose7eaaa182012-08-18 16:58:52 +000081 this->LangOpts = &LangOpts;
82 setSourceManager(PP->getSourceManager());
Jordan Rose7c304f52012-08-10 01:06:16 +000083 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
84#ifndef NDEBUG
Jordan Rose7eaaa182012-08-18 16:58:52 +000085 // Debug build tracks parsed files.
86 VerifyFileTracker *V = new VerifyFileTracker(*this, *SrcManager);
Jordan Rose7c304f52012-08-10 01:06:16 +000087 const_cast<Preprocessor*>(PP)->addPPCallbacks(V);
88#endif
89 }
90 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000091
Jordan Rose7c304f52012-08-10 01:06:16 +000092 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000093 PrimaryClient->BeginSourceFile(LangOpts, PP);
94}
95
David Blaikie621bc692011-09-26 00:38:03 +000096void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Rose7c304f52012-08-10 01:06:16 +000097 assert(ActiveSourceFiles && "No active source files!");
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000098 PrimaryClient->EndSourceFile();
99
Jordan Rose7c304f52012-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 Rose7eaaa182012-08-18 16:58:52 +0000108 LangOpts = 0;
Jordan Rose7c304f52012-08-10 01:06:16 +0000109 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000110}
Daniel Dunbar221c7212009-11-14 07:53:24 +0000111
David Blaikie621bc692011-09-26 00:38:03 +0000112void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikie40847cf2011-09-26 01:18:08 +0000113 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Douglas Gregora4a90ca2013-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 Rose7eaaa182012-08-18 16:58:52 +0000119 setSourceManager(Info.getSourceManager());
Douglas Gregora4a90ca2013-05-03 22:58:43 +0000120 }
Jordan Rose7eaaa182012-08-18 16:58:52 +0000121
Jordan Rose7c304f52012-08-10 01:06:16 +0000122#ifndef NDEBUG
Jordan Rose7eaaa182012-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 Naumann01231612011-07-25 19:18:12 +0000144 }
Jordan Rose7c304f52012-08-10 01:06:16 +0000145#endif
Jordan Rose7eaaa182012-08-18 16:58:52 +0000146
Daniel Dunbar81f5a1e2009-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 Dunbar81f5a1e2009-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 Lattner60909e12010-04-28 20:02:30 +0000159namespace {
160
Chris Lattner60909e12010-04-28 20:02:30 +0000161/// StandardDirective - Directive with string matching.
162///
163class StandardDirective : public Directive {
164public:
Jordan Roseaa48fe82012-07-10 02:57:03 +0000165 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000166 StringRef Text, unsigned Min, unsigned Max)
167 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max) { }
Chris Lattner60909e12010-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 Rose4313c012012-07-10 02:56:15 +0000174 virtual bool match(StringRef S) {
175 return S.find(Text) != StringRef::npos;
Chris Lattner60909e12010-04-28 20:02:30 +0000176 }
177};
178
179/// RegexDirective - Directive with regular-expression matching.
180///
181class RegexDirective : public Directive {
182public:
Jordan Roseaa48fe82012-07-10 02:57:03 +0000183 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000184 StringRef Text, unsigned Min, unsigned Max)
185 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max), Regex(Text) { }
Chris Lattner60909e12010-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 Rose4313c012012-07-10 02:56:15 +0000193 virtual bool match(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000194 return Regex.match(S);
195 }
196
197private:
198 llvm::Regex Regex;
199};
200
Chris Lattner60909e12010-04-28 20:02:30 +0000201class ParseHelper
202{
203public:
Jordan Rose78541c42012-07-11 19:58:23 +0000204 ParseHelper(StringRef S)
205 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(NULL) { }
Chris Lattner60909e12010-04-28 20:02:30 +0000206
207 // Return true if string literal is next.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 bool Next(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000209 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000210 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000211 if (PEnd > End)
212 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000213 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-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 Gibbs4a529d22012-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 Rose3f6f51e2013-02-08 22:30:41 +0000242 || P == Begin || isWhitespace(P[-1])
Andy Gibbs4a529d22012-10-19 12:36:49 +0000243 // Or it could be preceeded by the start of a comment.
244 || (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 Lattner60909e12010-04-28 20:02:30 +0000250 }
251
252 // Advance 1-past previous next/search.
253 // Behavior is undefined if previous next/search failed.
254 bool Advance() {
255 C = PEnd;
256 return C < End;
257 }
258
259 // Skip zero or more whitespace.
260 void SkipWhitespace() {
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000261 for (; C < End && isWhitespace(*C); ++C)
Chris Lattner60909e12010-04-28 20:02:30 +0000262 ;
263 }
264
265 // Return true if EOF reached.
266 bool Done() {
267 return !(C < End);
268 }
269
270 const char * const Begin; // beginning of expected content
271 const char * const End; // end of expected content (1-past)
272 const char *C; // position of next char in content
273 const char *P;
274
275private:
276 const char *PEnd; // previous next/search subject end (1-past)
277};
278
279} // namespace anonymous
280
281/// ParseDirective - Go through the comment and see if it indicates expected
282/// diagnostics. If so, then put them in the appropriate directive list.
283///
Jordan Rose78541c42012-07-11 19:58:23 +0000284/// Returns true if any valid directives were found.
Jordan Rose7c304f52012-08-10 01:06:16 +0000285static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Andy Gibbsb42f2002013-04-17 08:06:46 +0000286 Preprocessor *PP, SourceLocation Pos,
Andy Gibbs266dba32012-10-19 12:49:32 +0000287 VerifyDiagnosticConsumer::DirectiveStatus &Status) {
Andy Gibbsb42f2002013-04-17 08:06:46 +0000288 DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
289
Chris Lattner60909e12010-04-28 20:02:30 +0000290 // A single comment may contain multiple directives.
Jordan Rose78541c42012-07-11 19:58:23 +0000291 bool FoundDirective = false;
292 for (ParseHelper PH(S); !PH.Done();) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000293 // Search for token: expected
Andy Gibbs4a529d22012-10-19 12:36:49 +0000294 if (!PH.Search("expected", true))
Chris Lattner60909e12010-04-28 20:02:30 +0000295 break;
296 PH.Advance();
297
Jordan Roseaa48fe82012-07-10 02:57:03 +0000298 // Next token: -
Chris Lattner60909e12010-04-28 20:02:30 +0000299 if (!PH.Next("-"))
300 continue;
301 PH.Advance();
302
Jordan Roseaa48fe82012-07-10 02:57:03 +0000303 // Next token: { error | warning | note }
Chris Lattner60909e12010-04-28 20:02:30 +0000304 DirectiveList* DL = NULL;
305 if (PH.Next("error"))
Jordan Rose7c304f52012-08-10 01:06:16 +0000306 DL = ED ? &ED->Errors : NULL;
Chris Lattner60909e12010-04-28 20:02:30 +0000307 else if (PH.Next("warning"))
Jordan Rose7c304f52012-08-10 01:06:16 +0000308 DL = ED ? &ED->Warnings : NULL;
Chris Lattner60909e12010-04-28 20:02:30 +0000309 else if (PH.Next("note"))
Jordan Rose7c304f52012-08-10 01:06:16 +0000310 DL = ED ? &ED->Notes : NULL;
Andy Gibbs266dba32012-10-19 12:49:32 +0000311 else if (PH.Next("no-diagnostics")) {
312 if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
313 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
314 << /*IsExpectedNoDiagnostics=*/true;
315 else
316 Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
317 continue;
318 } else
Chris Lattner60909e12010-04-28 20:02:30 +0000319 continue;
320 PH.Advance();
321
Andy Gibbs266dba32012-10-19 12:49:32 +0000322 if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
323 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
324 << /*IsExpectedNoDiagnostics=*/false;
325 continue;
326 }
327 Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
328
Jordan Rose7c304f52012-08-10 01:06:16 +0000329 // If a directive has been found but we're not interested
330 // in storing the directive information, return now.
331 if (!DL)
332 return true;
333
Jordan Roseaa48fe82012-07-10 02:57:03 +0000334 // Default directive kind.
Chris Lattner60909e12010-04-28 20:02:30 +0000335 bool RegexKind = false;
336 const char* KindStr = "string";
337
Jordan Roseaa48fe82012-07-10 02:57:03 +0000338 // Next optional token: -
Chris Lattner60909e12010-04-28 20:02:30 +0000339 if (PH.Next("-re")) {
340 PH.Advance();
341 RegexKind = true;
342 KindStr = "regex";
343 }
344
Jordan Roseaa48fe82012-07-10 02:57:03 +0000345 // Next optional token: @
346 SourceLocation ExpectedLoc;
347 if (!PH.Next("@")) {
348 ExpectedLoc = Pos;
349 } else {
350 PH.Advance();
351 unsigned Line = 0;
352 bool FoundPlus = PH.Next("+");
353 if (FoundPlus || PH.Next("-")) {
354 // Relative to current line.
355 PH.Advance();
356 bool Invalid = false;
357 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
358 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
359 if (FoundPlus) ExpectedLine += Line;
360 else ExpectedLine -= Line;
361 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
362 }
Andy Gibbsb42f2002013-04-17 08:06:46 +0000363 } else if (PH.Next(Line)) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000364 // Absolute line number.
Andy Gibbsb42f2002013-04-17 08:06:46 +0000365 if (Line > 0)
Jordan Roseaa48fe82012-07-10 02:57:03 +0000366 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
Andy Gibbsb42f2002013-04-17 08:06:46 +0000367 } else if (PP && PH.Search(":")) {
368 // Specific source file.
369 StringRef Filename(PH.C, PH.P-PH.C);
370 PH.Advance();
371
372 // Lookup file via Preprocessor, like a #include.
373 const DirectoryLookup *CurDir;
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000374 const FileEntry *FE = PP->LookupFile(Pos, Filename, false, NULL, CurDir,
Andy Gibbsb42f2002013-04-17 08:06:46 +0000375 NULL, NULL, 0);
376 if (!FE) {
377 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
378 diag::err_verify_missing_file) << Filename << KindStr;
379 continue;
380 }
381
382 if (SM.translateFile(FE).isInvalid())
383 SM.createFileID(FE, Pos, SrcMgr::C_User);
384
385 if (PH.Next(Line) && Line > 0)
386 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
Jordan Roseaa48fe82012-07-10 02:57:03 +0000387 }
388
389 if (ExpectedLoc.isInvalid()) {
390 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
391 diag::err_verify_missing_line) << KindStr;
392 continue;
393 }
394 PH.Advance();
395 }
396
397 // Skip optional whitespace.
Chris Lattner60909e12010-04-28 20:02:30 +0000398 PH.SkipWhitespace();
399
Jordan Roseaa48fe82012-07-10 02:57:03 +0000400 // Next optional token: positive integer or a '+'.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000401 unsigned Min = 1;
402 unsigned Max = 1;
403 if (PH.Next(Min)) {
Chris Lattner60909e12010-04-28 20:02:30 +0000404 PH.Advance();
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000405 // A positive integer can be followed by a '+' meaning min
406 // or more, or by a '-' meaning a range from min to max.
407 if (PH.Next("+")) {
408 Max = Directive::MaxCount;
409 PH.Advance();
410 } else if (PH.Next("-")) {
411 PH.Advance();
412 if (!PH.Next(Max) || Max < Min) {
413 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
414 diag::err_verify_invalid_range) << KindStr;
415 continue;
416 }
417 PH.Advance();
418 } else {
419 Max = Min;
420 }
421 } else if (PH.Next("+")) {
422 // '+' on its own means "1 or more".
423 Max = Directive::MaxCount;
Anna Zaks2135ebb2011-12-15 02:28:16 +0000424 PH.Advance();
425 }
Chris Lattner60909e12010-04-28 20:02:30 +0000426
Jordan Roseaa48fe82012-07-10 02:57:03 +0000427 // Skip optional whitespace.
Chris Lattner60909e12010-04-28 20:02:30 +0000428 PH.SkipWhitespace();
429
Jordan Roseaa48fe82012-07-10 02:57:03 +0000430 // Next token: {{
Chris Lattner60909e12010-04-28 20:02:30 +0000431 if (!PH.Next("{{")) {
Jordan Rose4313c012012-07-10 02:56:15 +0000432 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
433 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000434 continue;
435 }
Chris Lattner60909e12010-04-28 20:02:30 +0000436 PH.Advance();
437 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000438
Jordan Roseaa48fe82012-07-10 02:57:03 +0000439 // Search for token: }}
Chris Lattner60909e12010-04-28 20:02:30 +0000440 if (!PH.Search("}}")) {
Jordan Rose4313c012012-07-10 02:56:15 +0000441 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
442 diag::err_verify_missing_end) << KindStr;
Chris Lattner60909e12010-04-28 20:02:30 +0000443 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000444 }
Chris Lattner60909e12010-04-28 20:02:30 +0000445 const char* const ContentEnd = PH.P; // mark content end
446 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000447
Jordan Roseaa48fe82012-07-10 02:57:03 +0000448 // Build directive text; convert \n to newlines.
Chris Lattner60909e12010-04-28 20:02:30 +0000449 std::string Text;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000450 StringRef NewlineStr = "\\n";
451 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattner60909e12010-04-28 20:02:30 +0000452 size_t CPos = 0;
453 size_t FPos;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000454 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattner60909e12010-04-28 20:02:30 +0000455 Text += Content.substr(CPos, FPos-CPos);
456 Text += '\n';
457 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000458 }
Chris Lattner60909e12010-04-28 20:02:30 +0000459 if (Text.empty())
460 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000461
Jordan Roseaa48fe82012-07-10 02:57:03 +0000462 // Construct new directive.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000463 Directive *D = Directive::create(RegexKind, Pos, ExpectedLoc, Text,
464 Min, Max);
Chris Lattner60909e12010-04-28 20:02:30 +0000465 std::string Error;
Jordan Rose78541c42012-07-11 19:58:23 +0000466 if (D->isValid(Error)) {
Chris Lattner60909e12010-04-28 20:02:30 +0000467 DL->push_back(D);
Jordan Rose78541c42012-07-11 19:58:23 +0000468 FoundDirective = true;
469 } else {
Jordan Rose4313c012012-07-10 02:56:15 +0000470 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
471 diag::err_verify_invalid_content)
Chris Lattner60909e12010-04-28 20:02:30 +0000472 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000473 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000474 }
Jordan Rose78541c42012-07-11 19:58:23 +0000475
476 return FoundDirective;
477}
478
479/// HandleComment - Hook into the preprocessor and extract comments containing
480/// expected errors and warnings.
481bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
482 SourceRange Comment) {
483 SourceManager &SM = PP.getSourceManager();
Douglas Gregora4a90ca2013-05-03 22:58:43 +0000484
485 // If this comment is for a different source manager, ignore it.
486 if (SrcManager && &SM != SrcManager)
487 return false;
488
Jordan Rose78541c42012-07-11 19:58:23 +0000489 SourceLocation CommentBegin = Comment.getBegin();
490
491 const char *CommentRaw = SM.getCharacterData(CommentBegin);
492 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
493
494 if (C.empty())
495 return false;
496
497 // Fold any "\<EOL>" sequences
498 size_t loc = C.find('\\');
499 if (loc == StringRef::npos) {
Andy Gibbsb42f2002013-04-17 08:06:46 +0000500 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status);
Jordan Rose78541c42012-07-11 19:58:23 +0000501 return false;
502 }
503
504 std::string C2;
505 C2.reserve(C.size());
506
507 for (size_t last = 0;; loc = C.find('\\', last)) {
508 if (loc == StringRef::npos || loc == C.size()) {
509 C2 += C.substr(last);
510 break;
511 }
512 C2 += C.substr(last, loc-last);
513 last = loc + 1;
514
515 if (C[last] == '\n' || C[last] == '\r') {
516 ++last;
517
518 // Escape \r\n or \n\r, but not \n\n.
519 if (last < C.size())
520 if (C[last] == '\n' || C[last] == '\r')
521 if (C[last] != C[last-1])
522 ++last;
523 } else {
524 // This was just a normal backslash.
525 C2 += '\\';
526 }
527 }
528
529 if (!C2.empty())
Andy Gibbsb42f2002013-04-17 08:06:46 +0000530 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status);
Jordan Rose78541c42012-07-11 19:58:23 +0000531 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000532}
533
Jordan Rose7c304f52012-08-10 01:06:16 +0000534#ifndef NDEBUG
535/// \brief Lex the specified source file to determine whether it contains
536/// any expected-* directives. As a Lexer is used rather than a full-blown
537/// Preprocessor, directives inside skipped #if blocks will still be found.
538///
539/// \return true if any directives were found.
Jordan Rose7eaaa182012-08-18 16:58:52 +0000540static bool findDirectives(SourceManager &SM, FileID FID,
541 const LangOptions &LangOpts) {
Axel Naumann01231612011-07-25 19:18:12 +0000542 // Create a raw lexer to pull all the comments out of FID.
543 if (FID.isInvalid())
Jordan Rose7c304f52012-08-10 01:06:16 +0000544 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000545
546 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000547 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Jordan Rose7eaaa182012-08-18 16:58:52 +0000548 Lexer RawLex(FID, FromFile, SM, LangOpts);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000549
550 // Return comments as tokens, this is how we find expected diagnostics.
551 RawLex.SetCommentRetentionState(true);
552
553 Token Tok;
554 Tok.setKind(tok::comment);
Andy Gibbs266dba32012-10-19 12:49:32 +0000555 VerifyDiagnosticConsumer::DirectiveStatus Status =
556 VerifyDiagnosticConsumer::HasNoDirectives;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000557 while (Tok.isNot(tok::eof)) {
Eli Friedmand2f93082013-09-19 00:41:32 +0000558 RawLex.LexFromRawLexer(Tok);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000559 if (!Tok.is(tok::comment)) continue;
560
Jordan Rose7eaaa182012-08-18 16:58:52 +0000561 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000562 if (Comment.empty()) continue;
563
Jordan Rose7eaaa182012-08-18 16:58:52 +0000564 // Find first directive.
Andy Gibbsb42f2002013-04-17 08:06:46 +0000565 if (ParseDirective(Comment, 0, SM, 0, Tok.getLocation(), Status))
Jordan Rose7eaaa182012-08-18 16:58:52 +0000566 return true;
Jordan Rose7c304f52012-08-10 01:06:16 +0000567 }
Jordan Rose7eaaa182012-08-18 16:58:52 +0000568 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000569}
Jordan Rose7c304f52012-08-10 01:06:16 +0000570#endif // !NDEBUG
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000571
Jordan Roseaa48fe82012-07-10 02:57:03 +0000572/// \brief Takes a list of diagnostics that have been generated but not matched
573/// by an expected-* directive and produces a diagnostic to the user from this.
574static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
575 const_diag_iterator diag_begin,
576 const_diag_iterator diag_end,
577 const char *Kind) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000578 if (diag_begin == diag_end) return 0;
579
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000580 SmallString<256> Fmt;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000581 llvm::raw_svector_ostream OS(Fmt);
582 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000583 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000584 OS << "\n (frontend)";
Andy Gibbsb42f2002013-04-17 08:06:46 +0000585 else {
586 OS << "\n ";
587 if (const FileEntry *File = SourceMgr->getFileEntryForID(
588 SourceMgr->getFileID(I->first)))
589 OS << " File " << File->getName();
590 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
591 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000592 OS << ": " << I->second;
593 }
594
Jordan Rosec6d64a22012-07-11 16:50:36 +0000595 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Roseaa48fe82012-07-10 02:57:03 +0000596 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000597 return std::distance(diag_begin, diag_end);
598}
599
Jordan Roseaa48fe82012-07-10 02:57:03 +0000600/// \brief Takes a list of diagnostics that were expected to have been generated
601/// but were not and produces a diagnostic to the user from this.
602static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
603 DirectiveList &DL, const char *Kind) {
Chris Lattner60909e12010-04-28 20:02:30 +0000604 if (DL.empty())
605 return 0;
606
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000607 SmallString<256> Fmt;
Chris Lattner60909e12010-04-28 20:02:30 +0000608 llvm::raw_svector_ostream OS(Fmt);
609 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000610 Directive &D = **I;
Andy Gibbsb42f2002013-04-17 08:06:46 +0000611 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc)
612 << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Jordan Roseaa48fe82012-07-10 02:57:03 +0000613 if (D.DirectiveLoc != D.DiagnosticLoc)
614 OS << " (directive at "
Andy Gibbsb42f2002013-04-17 08:06:46 +0000615 << SourceMgr.getFilename(D.DirectiveLoc) << ':'
616 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
Chris Lattner60909e12010-04-28 20:02:30 +0000617 OS << ": " << D.Text;
618 }
619
Jordan Rosec6d64a22012-07-11 16:50:36 +0000620 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Roseaa48fe82012-07-10 02:57:03 +0000621 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattner60909e12010-04-28 20:02:30 +0000622 return DL.size();
623}
624
Andy Gibbsb42f2002013-04-17 08:06:46 +0000625/// \brief Determine whether two source locations come from the same file.
626static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
627 SourceLocation DiagnosticLoc) {
628 while (DiagnosticLoc.isMacroID())
629 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
630
Eli Friedman24146972013-08-22 00:27:10 +0000631 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
Andy Gibbsb42f2002013-04-17 08:06:46 +0000632 return true;
633
634 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
Eli Friedman24146972013-08-22 00:27:10 +0000635 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
Andy Gibbsb42f2002013-04-17 08:06:46 +0000636 return true;
637
638 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
639}
640
Chris Lattner60909e12010-04-28 20:02:30 +0000641/// CheckLists - Compare expected to seen diagnostic lists and return the
642/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000643///
David Blaikied6471f72011-09-25 23:23:43 +0000644static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000645 const char *Label,
646 DirectiveList &Left,
647 const_diag_iterator d2_begin,
648 const_diag_iterator d2_end) {
649 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000650 DiagList Right(d2_begin, d2_end);
651
Chris Lattner60909e12010-04-28 20:02:30 +0000652 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
653 Directive& D = **I;
Jordan Roseaa48fe82012-07-10 02:57:03 +0000654 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000655
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000656 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattner60909e12010-04-28 20:02:30 +0000657 DiagList::iterator II, IE;
658 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000659 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattner60909e12010-04-28 20:02:30 +0000660 if (LineNo1 != LineNo2)
661 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000662
Andy Gibbsb42f2002013-04-17 08:06:46 +0000663 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
664 continue;
665
Chris Lattner60909e12010-04-28 20:02:30 +0000666 const std::string &RightText = II->second;
Jordan Rose4313c012012-07-10 02:56:15 +0000667 if (D.match(RightText))
Chris Lattner60909e12010-04-28 20:02:30 +0000668 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000669 }
Chris Lattner60909e12010-04-28 20:02:30 +0000670 if (II == IE) {
671 // Not found.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000672 if (i >= D.Min) break;
Chris Lattner60909e12010-04-28 20:02:30 +0000673 LeftOnly.push_back(*I);
674 } else {
675 // Found. The same cannot be found twice.
676 Right.erase(II);
677 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000678 }
679 }
680 // Now all that's left in Right are those that were not matched.
Jordan Roseaa48fe82012-07-10 02:57:03 +0000681 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
682 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumiad646842011-12-17 13:00:31 +0000683 return num;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000684}
685
686/// CheckResults - This compares the expected results to those that
687/// were actually reported. It emits any discrepencies. Return "true" if there
688/// were problems. Return "false" otherwise.
689///
David Blaikied6471f72011-09-25 23:23:43 +0000690static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000691 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000692 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000693 // We want to capture the delta between what was expected and what was
694 // seen.
695 //
696 // Expected \ Seen - set expected but not seen
697 // Seen \ Expected - set seen but not expected
698 unsigned NumProblems = 0;
699
700 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000701 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
702 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000703
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000704 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000705 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
706 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000707
708 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000709 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
710 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000711
712 return NumProblems;
713}
714
Jordan Rose7eaaa182012-08-18 16:58:52 +0000715void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
716 FileID FID,
717 ParsedStatus PS) {
718 // Check SourceManager hasn't changed.
719 setSourceManager(SM);
720
721#ifndef NDEBUG
722 if (FID.isInvalid())
723 return;
724
725 const FileEntry *FE = SM.getFileEntryForID(FID);
726
727 if (PS == IsParsed) {
728 // Move the FileID from the unparsed set to the parsed set.
729 UnparsedFiles.erase(FID);
730 ParsedFiles.insert(std::make_pair(FID, FE));
731 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
732 // Add the FileID to the unparsed set if we haven't seen it before.
733
734 // Check for directives.
735 bool FoundDirectives;
736 if (PS == IsUnparsedNoDirectives)
737 FoundDirectives = false;
738 else
739 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
740
741 // Add the FileID to the unparsed set.
742 UnparsedFiles.insert(std::make_pair(FID,
743 UnparsedFileStatus(FE, FoundDirectives)));
744 }
745#endif
746}
747
David Blaikie621bc692011-09-26 00:38:03 +0000748void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000749 // Ensure any diagnostics go to the primary client.
Douglas Gregor78243652011-09-13 01:26:44 +0000750 bool OwnsCurClient = Diags.ownsClient();
David Blaikie78ad0b92011-09-25 23:39:51 +0000751 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000752 Diags.setClient(PrimaryClient, false);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000753
Jordan Rose7c304f52012-08-10 01:06:16 +0000754#ifndef NDEBUG
Jordan Rose7eaaa182012-08-18 16:58:52 +0000755 // In a debug build, scan through any files that may have been missed
756 // during parsing and issue a fatal error if directives are contained
757 // within these files. If a fatal error occurs, this suggests that
758 // this file is being parsed separately from the main file, in which
759 // case consider moving the directives to the correct place, if this
760 // is applicable.
761 if (UnparsedFiles.size() > 0) {
762 // Generate a cache of parsed FileEntry pointers for alias lookups.
763 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
764 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
765 End = ParsedFiles.end(); I != End; ++I) {
766 if (const FileEntry *FE = I->second)
767 ParsedFileCache.insert(FE);
768 }
769
770 // Iterate through list of unparsed files.
771 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
772 End = UnparsedFiles.end(); I != End; ++I) {
773 const UnparsedFileStatus &Status = I->second;
774 const FileEntry *FE = Status.getFile();
775
776 // Skip files that have been parsed via an alias.
777 if (FE && ParsedFileCache.count(FE))
Jordan Rose7c304f52012-08-10 01:06:16 +0000778 continue;
779
Jordan Rose7eaaa182012-08-18 16:58:52 +0000780 // Report a fatal error if this file contained directives.
781 if (Status.foundDirectives()) {
Jordan Rose7c304f52012-08-10 01:06:16 +0000782 llvm::report_fatal_error(Twine("-verify directives found after rather"
783 " than during normal parsing of ",
Jordan Rose7eaaa182012-08-18 16:58:52 +0000784 StringRef(FE ? FE->getName() : "(unknown)")));
785 }
Axel Naumann84c05e32011-08-24 13:36:19 +0000786 }
Daniel Dunbar221c7212009-11-14 07:53:24 +0000787
Jordan Rose7eaaa182012-08-18 16:58:52 +0000788 // UnparsedFiles has been processed now, so clear it.
789 UnparsedFiles.clear();
790 }
791#endif // !NDEBUG
792
793 if (SrcManager) {
Andy Gibbs266dba32012-10-19 12:49:32 +0000794 // Produce an error if no expected-* directives could be found in the
795 // source file(s) processed.
796 if (Status == HasNoDirectives) {
797 Diags.Report(diag::err_verify_no_directives).setForceEmit();
798 ++NumErrors;
799 Status = HasNoDirectivesReported;
800 }
801
Daniel Dunbar221c7212009-11-14 07:53:24 +0000802 // Check that the expected diagnostics occurred.
Jordan Rose7eaaa182012-08-18 16:58:52 +0000803 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000804 } else {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000805 NumErrors += (PrintUnexpected(Diags, 0, Buffer->err_begin(),
806 Buffer->err_end(), "error") +
807 PrintUnexpected(Diags, 0, Buffer->warn_begin(),
808 Buffer->warn_end(), "warn") +
809 PrintUnexpected(Diags, 0, Buffer->note_begin(),
810 Buffer->note_end(), "note"));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000811 }
812
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000813 Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000814 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000815
816 // Reset the buffer, we have processed all the diagnostics in it.
817 Buffer.reset(new TextDiagnosticBuffer());
Axel Naumanne445e5d2012-07-10 16:24:07 +0000818 ED.Errors.clear();
819 ED.Warnings.clear();
820 ED.Notes.clear();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000821}
Chris Lattner60909e12010-04-28 20:02:30 +0000822
Jordan Roseaa48fe82012-07-10 02:57:03 +0000823Directive *Directive::create(bool RegexKind, SourceLocation DirectiveLoc,
824 SourceLocation DiagnosticLoc, StringRef Text,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000825 unsigned Min, unsigned Max) {
Chris Lattner60909e12010-04-28 20:02:30 +0000826 if (RegexKind)
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000827 return new RegexDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
828 return new StandardDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
Chris Lattner60909e12010-04-28 20:02:30 +0000829}