blob: 5eeb696582019d3c743c5a1f7ff3b82643b9bc9e [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"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000015#include "clang/Frontend/FrontendDiagnostic.h"
16#include "clang/Frontend/TextDiagnosticBuffer.h"
17#include "clang/Lex/Preprocessor.h"
18#include "llvm/ADT/SmallString.h"
Chris Lattner60909e12010-04-28 20:02:30 +000019#include "llvm/Support/Regex.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000020#include "llvm/Support/raw_ostream.h"
21using namespace clang;
22
David Blaikie621bc692011-09-26 00:38:03 +000023VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
Douglas Gregor78243652011-09-13 01:26:44 +000024 : Diags(_Diags), PrimaryClient(Diags.getClient()),
25 OwnsPrimaryClient(Diags.ownsClient()),
26 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0)
27{
28 Diags.takeClient();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000029}
30
David Blaikie621bc692011-09-26 00:38:03 +000031VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Douglas Gregor78243652011-09-13 01:26:44 +000032 CheckDiagnostics();
33 Diags.takeClient();
34 if (OwnsPrimaryClient)
35 delete PrimaryClient;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000036}
37
David Blaikie78ad0b92011-09-25 23:39:51 +000038// DiagnosticConsumer interface.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000039
David Blaikie621bc692011-09-26 00:38:03 +000040void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000041 const Preprocessor *PP) {
42 // FIXME: Const hack, we screw up the preprocessor but in practice its ok
43 // because it doesn't get reused. It would be better if we could make a copy
44 // though.
45 CurrentPreprocessor = const_cast<Preprocessor*>(PP);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000046
47 PrimaryClient->BeginSourceFile(LangOpts, PP);
48}
49
David Blaikie621bc692011-09-26 00:38:03 +000050void VerifyDiagnosticConsumer::EndSourceFile() {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000051 CheckDiagnostics();
52
53 PrimaryClient->EndSourceFile();
54
55 CurrentPreprocessor = 0;
56}
Daniel Dunbar221c7212009-11-14 07:53:24 +000057
David Blaikie621bc692011-09-26 00:38:03 +000058void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikie40847cf2011-09-26 01:18:08 +000059 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Axel Naumann01231612011-07-25 19:18:12 +000060 if (FirstErrorFID.isInvalid() && Info.hasSourceManager()) {
61 const SourceManager &SM = Info.getSourceManager();
62 FirstErrorFID = SM.getFileID(Info.getLocation());
63 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000064 // Send the diagnostic to the buffer, we will check it once we reach the end
65 // of the source file (or are destructed).
66 Buffer->HandleDiagnostic(DiagLevel, Info);
67}
68
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000069//===----------------------------------------------------------------------===//
70// Checking diagnostics implementation.
71//===----------------------------------------------------------------------===//
72
73typedef TextDiagnosticBuffer::DiagList DiagList;
74typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
75
Chris Lattner60909e12010-04-28 20:02:30 +000076namespace {
77
78/// Directive - Abstract class representing a parsed verify directive.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000079///
Chris Lattner60909e12010-04-28 20:02:30 +000080class Directive {
81public:
82 static Directive* Create(bool RegexKind, const SourceLocation &Location,
83 const std::string &Text, unsigned Count);
84public:
85 SourceLocation Location;
86 const std::string Text;
87 unsigned Count;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000088
Chris Lattner60909e12010-04-28 20:02:30 +000089 virtual ~Directive() { }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000090
Chris Lattner60909e12010-04-28 20:02:30 +000091 // Returns true if directive text is valid.
92 // Otherwise returns false and populates E.
93 virtual bool isValid(std::string &Error) = 0;
94
95 // Returns true on match.
96 virtual bool Match(const std::string &S) = 0;
97
98protected:
99 Directive(const SourceLocation &Location, const std::string &Text,
100 unsigned Count)
101 : Location(Location), Text(Text), Count(Count) { }
102
103private:
Argyrios Kyrtzidisc83d2d72010-08-15 10:17:39 +0000104 Directive(const Directive&); // DO NOT IMPLEMENT
105 void operator=(const Directive&); // DO NOT IMPLEMENT
Chris Lattner60909e12010-04-28 20:02:30 +0000106};
107
108/// StandardDirective - Directive with string matching.
109///
110class StandardDirective : public Directive {
111public:
112 StandardDirective(const SourceLocation &Location, const std::string &Text,
113 unsigned Count)
114 : Directive(Location, Text, Count) { }
115
116 virtual bool isValid(std::string &Error) {
117 // all strings are considered valid; even empty ones
118 return true;
119 }
120
121 virtual bool Match(const std::string &S) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000122 return S.find(Text) != std::string::npos;
Chris Lattner60909e12010-04-28 20:02:30 +0000123 }
124};
125
126/// RegexDirective - Directive with regular-expression matching.
127///
128class RegexDirective : public Directive {
129public:
130 RegexDirective(const SourceLocation &Location, const std::string &Text,
131 unsigned Count)
132 : Directive(Location, Text, Count), Regex(Text) { }
133
134 virtual bool isValid(std::string &Error) {
135 if (Regex.isValid(Error))
136 return true;
137 return false;
138 }
139
140 virtual bool Match(const std::string &S) {
141 return Regex.match(S);
142 }
143
144private:
145 llvm::Regex Regex;
146};
147
148typedef std::vector<Directive*> DirectiveList;
149
150/// ExpectedData - owns directive objects and deletes on destructor.
151///
152struct ExpectedData {
153 DirectiveList Errors;
154 DirectiveList Warnings;
155 DirectiveList Notes;
156
157 ~ExpectedData() {
158 DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 };
159 for (DirectiveList **PL = Lists; *PL; ++PL) {
160 DirectiveList * const L = *PL;
161 for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I)
162 delete *I;
163 }
164 }
165};
166
167class ParseHelper
168{
169public:
170 ParseHelper(const char *Begin, const char *End)
171 : Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { }
172
173 // Return true if string literal is next.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 bool Next(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000175 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000176 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000177 if (PEnd > End)
178 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000179 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-04-28 20:02:30 +0000180 }
181
182 // Return true if number is next.
183 // Output N only if number is next.
184 bool Next(unsigned &N) {
185 unsigned TMP = 0;
186 P = C;
187 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
188 TMP *= 10;
189 TMP += P[0] - '0';
190 }
191 if (P == C)
192 return false;
193 PEnd = P;
194 N = TMP;
195 return true;
196 }
197
198 // Return true if string literal is found.
199 // When true, P marks begin-position of S in content.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 bool Search(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000201 P = std::search(C, End, S.begin(), S.end());
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000202 PEnd = P + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000203 return P != End;
204 }
205
206 // Advance 1-past previous next/search.
207 // Behavior is undefined if previous next/search failed.
208 bool Advance() {
209 C = PEnd;
210 return C < End;
211 }
212
213 // Skip zero or more whitespace.
214 void SkipWhitespace() {
215 for (; C < End && isspace(*C); ++C)
216 ;
217 }
218
219 // Return true if EOF reached.
220 bool Done() {
221 return !(C < End);
222 }
223
224 const char * const Begin; // beginning of expected content
225 const char * const End; // end of expected content (1-past)
226 const char *C; // position of next char in content
227 const char *P;
228
229private:
230 const char *PEnd; // previous next/search subject end (1-past)
231};
232
233} // namespace anonymous
234
235/// ParseDirective - Go through the comment and see if it indicates expected
236/// diagnostics. If so, then put them in the appropriate directive list.
237///
238static void ParseDirective(const char *CommentStart, unsigned CommentLen,
239 ExpectedData &ED, Preprocessor &PP,
240 SourceLocation Pos) {
241 // A single comment may contain multiple directives.
242 for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) {
243 // search for token: expected
244 if (!PH.Search("expected"))
245 break;
246 PH.Advance();
247
248 // next token: -
249 if (!PH.Next("-"))
250 continue;
251 PH.Advance();
252
253 // next token: { error | warning | note }
254 DirectiveList* DL = NULL;
255 if (PH.Next("error"))
256 DL = &ED.Errors;
257 else if (PH.Next("warning"))
258 DL = &ED.Warnings;
259 else if (PH.Next("note"))
260 DL = &ED.Notes;
261 else
262 continue;
263 PH.Advance();
264
265 // default directive kind
266 bool RegexKind = false;
267 const char* KindStr = "string";
268
269 // next optional token: -
270 if (PH.Next("-re")) {
271 PH.Advance();
272 RegexKind = true;
273 KindStr = "regex";
274 }
275
276 // skip optional whitespace
277 PH.SkipWhitespace();
278
279 // next optional token: positive integer
280 unsigned Count = 1;
281 if (PH.Next(Count))
282 PH.Advance();
283
284 // skip optional whitespace
285 PH.SkipWhitespace();
286
287 // next token: {{
288 if (!PH.Next("{{")) {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000289 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000290 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000291 continue;
292 }
Chris Lattner60909e12010-04-28 20:02:30 +0000293 PH.Advance();
294 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000295
Chris Lattner60909e12010-04-28 20:02:30 +0000296 // search for token: }}
297 if (!PH.Search("}}")) {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000298 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000299 diag::err_verify_missing_end) << KindStr;
300 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000301 }
Chris Lattner60909e12010-04-28 20:02:30 +0000302 const char* const ContentEnd = PH.P; // mark content end
303 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000304
Chris Lattner60909e12010-04-28 20:02:30 +0000305 // build directive text; convert \n to newlines
306 std::string Text;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000307 StringRef NewlineStr = "\\n";
308 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattner60909e12010-04-28 20:02:30 +0000309 size_t CPos = 0;
310 size_t FPos;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattner60909e12010-04-28 20:02:30 +0000312 Text += Content.substr(CPos, FPos-CPos);
313 Text += '\n';
314 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000315 }
Chris Lattner60909e12010-04-28 20:02:30 +0000316 if (Text.empty())
317 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000318
Chris Lattner60909e12010-04-28 20:02:30 +0000319 // construct new directive
320 Directive *D = Directive::Create(RegexKind, Pos, Text, Count);
321 std::string Error;
322 if (D->isValid(Error))
323 DL->push_back(D);
324 else {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000325 PP.Diag(Pos.getLocWithOffset(ContentBegin-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000326 diag::err_verify_invalid_content)
327 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000328 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000329 }
330}
331
332/// FindExpectedDiags - Lex the main source file to find all of the
333// expected errors and warnings.
Axel Naumann01231612011-07-25 19:18:12 +0000334static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED, FileID FID) {
335 // Create a raw lexer to pull all the comments out of FID.
336 if (FID.isInvalid())
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000337 return;
338
Axel Naumann01231612011-07-25 19:18:12 +0000339 SourceManager& SM = PP.getSourceManager();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000340 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000341 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
342 Lexer RawLex(FID, FromFile, SM, PP.getLangOptions());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000343
344 // Return comments as tokens, this is how we find expected diagnostics.
345 RawLex.SetCommentRetentionState(true);
346
347 Token Tok;
348 Tok.setKind(tok::comment);
349 while (Tok.isNot(tok::eof)) {
350 RawLex.Lex(Tok);
351 if (!Tok.is(tok::comment)) continue;
352
353 std::string Comment = PP.getSpelling(Tok);
354 if (Comment.empty()) continue;
355
Chris Lattner60909e12010-04-28 20:02:30 +0000356 // Find all expected errors/warnings/notes.
357 ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000358 };
359}
360
361/// PrintProblem - This takes a diagnostic map of the delta between expected and
362/// seen diagnostics. If there's anything in it, then something unexpected
363/// happened. Print the map out in a nice format and return "true". If the map
364/// is empty and we're not going to print things, then return "false".
365///
David Blaikied6471f72011-09-25 23:23:43 +0000366static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000367 const_diag_iterator diag_begin,
368 const_diag_iterator diag_end,
369 const char *Kind, bool Expected) {
370 if (diag_begin == diag_end) return 0;
371
372 llvm::SmallString<256> Fmt;
373 llvm::raw_svector_ostream OS(Fmt);
374 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000375 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000376 OS << "\n (frontend)";
377 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000378 OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000379 OS << ": " << I->second;
380 }
381
382 Diags.Report(diag::err_verify_inconsistent_diags)
Daniel Dunbar221c7212009-11-14 07:53:24 +0000383 << Kind << !Expected << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000384 return std::distance(diag_begin, diag_end);
385}
386
David Blaikied6471f72011-09-25 23:23:43 +0000387static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000388 DirectiveList &DL, const char *Kind,
389 bool Expected) {
390 if (DL.empty())
391 return 0;
392
393 llvm::SmallString<256> Fmt;
394 llvm::raw_svector_ostream OS(Fmt);
395 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
396 Directive& D = **I;
397 if (D.Location.isInvalid() || !SourceMgr)
398 OS << "\n (frontend)";
399 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000400 OS << "\n Line " << SourceMgr->getPresumedLineNumber(D.Location);
Chris Lattner60909e12010-04-28 20:02:30 +0000401 OS << ": " << D.Text;
402 }
403
404 Diags.Report(diag::err_verify_inconsistent_diags)
405 << Kind << !Expected << OS.str();
406 return DL.size();
407}
408
409/// CheckLists - Compare expected to seen diagnostic lists and return the
410/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000411///
David Blaikied6471f72011-09-25 23:23:43 +0000412static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000413 const char *Label,
414 DirectiveList &Left,
415 const_diag_iterator d2_begin,
416 const_diag_iterator d2_end) {
417 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000418 DiagList Right(d2_begin, d2_end);
419
Chris Lattner60909e12010-04-28 20:02:30 +0000420 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
421 Directive& D = **I;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000422 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.Location);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000423
Chris Lattner60909e12010-04-28 20:02:30 +0000424 for (unsigned i = 0; i < D.Count; ++i) {
425 DiagList::iterator II, IE;
426 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000427 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattner60909e12010-04-28 20:02:30 +0000428 if (LineNo1 != LineNo2)
429 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000430
Chris Lattner60909e12010-04-28 20:02:30 +0000431 const std::string &RightText = II->second;
432 if (D.Match(RightText))
433 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000434 }
Chris Lattner60909e12010-04-28 20:02:30 +0000435 if (II == IE) {
436 // Not found.
437 LeftOnly.push_back(*I);
438 } else {
439 // Found. The same cannot be found twice.
440 Right.erase(II);
441 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000442 }
443 }
444 // Now all that's left in Right are those that were not matched.
445
Chris Lattner60909e12010-04-28 20:02:30 +0000446 return (PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true) +
447 PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(),
448 Label, false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000449}
450
451/// CheckResults - This compares the expected results to those that
452/// were actually reported. It emits any discrepencies. Return "true" if there
453/// were problems. Return "false" otherwise.
454///
David Blaikied6471f72011-09-25 23:23:43 +0000455static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000456 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000457 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000458 // We want to capture the delta between what was expected and what was
459 // seen.
460 //
461 // Expected \ Seen - set expected but not seen
462 // Seen \ Expected - set seen but not expected
463 unsigned NumProblems = 0;
464
465 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000466 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
467 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000468
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000469 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000470 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
471 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000472
473 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000474 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
475 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000476
477 return NumProblems;
478}
479
David Blaikie621bc692011-09-26 00:38:03 +0000480void VerifyDiagnosticConsumer::CheckDiagnostics() {
Chris Lattner60909e12010-04-28 20:02:30 +0000481 ExpectedData ED;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000482
483 // Ensure any diagnostics go to the primary client.
Douglas Gregor78243652011-09-13 01:26:44 +0000484 bool OwnsCurClient = Diags.ownsClient();
David Blaikie78ad0b92011-09-25 23:39:51 +0000485 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000486 Diags.setClient(PrimaryClient, false);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000487
488 // If we have a preprocessor, scan the source for expected diagnostic
489 // markers. If not then any diagnostics are unexpected.
490 if (CurrentPreprocessor) {
Axel Naumann01231612011-07-25 19:18:12 +0000491 SourceManager &SM = CurrentPreprocessor->getSourceManager();
492 // Extract expected-error strings from main file.
493 FindExpectedDiags(*CurrentPreprocessor, ED, SM.getMainFileID());
494 // Only check for expectations in other diagnostic locations
495 // if they are not the main file (via ID or FileEntry) - the main
496 // file has already been looked at, and its expectations must not
497 // be added twice.
498 if (!FirstErrorFID.isInvalid() && FirstErrorFID != SM.getMainFileID()
499 && (!SM.getFileEntryForID(FirstErrorFID)
500 || (SM.getFileEntryForID(FirstErrorFID) !=
Axel Naumann84c05e32011-08-24 13:36:19 +0000501 SM.getFileEntryForID(SM.getMainFileID())))) {
Axel Naumann01231612011-07-25 19:18:12 +0000502 FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID);
Axel Naumann84c05e32011-08-24 13:36:19 +0000503 FirstErrorFID = FileID();
504 }
Daniel Dunbar221c7212009-11-14 07:53:24 +0000505
506 // Check that the expected diagnostics occurred.
Axel Naumann01231612011-07-25 19:18:12 +0000507 NumErrors += CheckResults(Diags, SM, *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000508 } else {
509 NumErrors += (PrintProblem(Diags, 0,
510 Buffer->err_begin(), Buffer->err_end(),
511 "error", false) +
512 PrintProblem(Diags, 0,
513 Buffer->warn_begin(), Buffer->warn_end(),
514 "warn", false) +
515 PrintProblem(Diags, 0,
516 Buffer->note_begin(), Buffer->note_end(),
517 "note", false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000518 }
519
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000520 Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000521 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000522
523 // Reset the buffer, we have processed all the diagnostics in it.
524 Buffer.reset(new TextDiagnosticBuffer());
525}
Chris Lattner60909e12010-04-28 20:02:30 +0000526
Douglas Gregoraee526e2011-09-29 00:38:00 +0000527DiagnosticConsumer *
528VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
529 if (!Diags.getClient())
530 Diags.setClient(PrimaryClient->clone(Diags));
531
532 return new VerifyDiagnosticConsumer(Diags);
533}
534
Chris Lattner60909e12010-04-28 20:02:30 +0000535Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
536 const std::string &Text, unsigned Count) {
537 if (RegexKind)
538 return new RegexDirective(Location, Text, Count);
539 return new StandardDirective(Location, Text, Count);
540}