blob: ef5459c2339f4065d6e12e4699b38c00c04d7043 [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:
Anna Zaks2135ebb2011-12-15 02:28:16 +000085 /// Constant representing one or more matches aka regex "+".
86 static const unsigned OneOrMoreCount = UINT_MAX;
87
Chris Lattner60909e12010-04-28 20:02:30 +000088 SourceLocation Location;
89 const std::string Text;
90 unsigned Count;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000091
Chris Lattner60909e12010-04-28 20:02:30 +000092 virtual ~Directive() { }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000093
Chris Lattner60909e12010-04-28 20:02:30 +000094 // Returns true if directive text is valid.
95 // Otherwise returns false and populates E.
96 virtual bool isValid(std::string &Error) = 0;
97
98 // Returns true on match.
99 virtual bool Match(const std::string &S) = 0;
100
101protected:
102 Directive(const SourceLocation &Location, const std::string &Text,
103 unsigned Count)
104 : Location(Location), Text(Text), Count(Count) { }
105
106private:
Argyrios Kyrtzidisc83d2d72010-08-15 10:17:39 +0000107 Directive(const Directive&); // DO NOT IMPLEMENT
108 void operator=(const Directive&); // DO NOT IMPLEMENT
Chris Lattner60909e12010-04-28 20:02:30 +0000109};
110
111/// StandardDirective - Directive with string matching.
112///
113class StandardDirective : public Directive {
114public:
115 StandardDirective(const SourceLocation &Location, const std::string &Text,
116 unsigned Count)
117 : Directive(Location, Text, Count) { }
118
119 virtual bool isValid(std::string &Error) {
120 // all strings are considered valid; even empty ones
121 return true;
122 }
123
124 virtual bool Match(const std::string &S) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000125 return S.find(Text) != std::string::npos;
Chris Lattner60909e12010-04-28 20:02:30 +0000126 }
127};
128
129/// RegexDirective - Directive with regular-expression matching.
130///
131class RegexDirective : public Directive {
132public:
133 RegexDirective(const SourceLocation &Location, const std::string &Text,
134 unsigned Count)
135 : Directive(Location, Text, Count), Regex(Text) { }
136
137 virtual bool isValid(std::string &Error) {
138 if (Regex.isValid(Error))
139 return true;
140 return false;
141 }
142
143 virtual bool Match(const std::string &S) {
144 return Regex.match(S);
145 }
146
147private:
148 llvm::Regex Regex;
149};
150
151typedef std::vector<Directive*> DirectiveList;
152
153/// ExpectedData - owns directive objects and deletes on destructor.
154///
155struct ExpectedData {
156 DirectiveList Errors;
157 DirectiveList Warnings;
158 DirectiveList Notes;
159
160 ~ExpectedData() {
161 DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 };
162 for (DirectiveList **PL = Lists; *PL; ++PL) {
163 DirectiveList * const L = *PL;
164 for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I)
165 delete *I;
166 }
167 }
168};
169
170class ParseHelper
171{
172public:
173 ParseHelper(const char *Begin, const char *End)
174 : Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { }
175
176 // Return true if string literal is next.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 bool Next(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000178 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000179 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000180 if (PEnd > End)
181 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000182 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-04-28 20:02:30 +0000183 }
184
185 // Return true if number is next.
186 // Output N only if number is next.
187 bool Next(unsigned &N) {
188 unsigned TMP = 0;
189 P = C;
190 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
191 TMP *= 10;
192 TMP += P[0] - '0';
193 }
194 if (P == C)
195 return false;
196 PEnd = P;
197 N = TMP;
198 return true;
199 }
200
201 // Return true if string literal is found.
202 // When true, P marks begin-position of S in content.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 bool Search(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000204 P = std::search(C, End, S.begin(), S.end());
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000205 PEnd = P + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000206 return P != End;
207 }
208
209 // Advance 1-past previous next/search.
210 // Behavior is undefined if previous next/search failed.
211 bool Advance() {
212 C = PEnd;
213 return C < End;
214 }
215
216 // Skip zero or more whitespace.
217 void SkipWhitespace() {
218 for (; C < End && isspace(*C); ++C)
219 ;
220 }
221
222 // Return true if EOF reached.
223 bool Done() {
224 return !(C < End);
225 }
226
227 const char * const Begin; // beginning of expected content
228 const char * const End; // end of expected content (1-past)
229 const char *C; // position of next char in content
230 const char *P;
231
232private:
233 const char *PEnd; // previous next/search subject end (1-past)
234};
235
236} // namespace anonymous
237
238/// ParseDirective - Go through the comment and see if it indicates expected
239/// diagnostics. If so, then put them in the appropriate directive list.
240///
241static void ParseDirective(const char *CommentStart, unsigned CommentLen,
242 ExpectedData &ED, Preprocessor &PP,
243 SourceLocation Pos) {
244 // A single comment may contain multiple directives.
245 for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) {
246 // search for token: expected
247 if (!PH.Search("expected"))
248 break;
249 PH.Advance();
250
251 // next token: -
252 if (!PH.Next("-"))
253 continue;
254 PH.Advance();
255
256 // next token: { error | warning | note }
257 DirectiveList* DL = NULL;
258 if (PH.Next("error"))
259 DL = &ED.Errors;
260 else if (PH.Next("warning"))
261 DL = &ED.Warnings;
262 else if (PH.Next("note"))
263 DL = &ED.Notes;
264 else
265 continue;
266 PH.Advance();
267
268 // default directive kind
269 bool RegexKind = false;
270 const char* KindStr = "string";
271
272 // next optional token: -
273 if (PH.Next("-re")) {
274 PH.Advance();
275 RegexKind = true;
276 KindStr = "regex";
277 }
278
279 // skip optional whitespace
280 PH.SkipWhitespace();
281
Anna Zaks2135ebb2011-12-15 02:28:16 +0000282 // next optional token: positive integer or a '+'.
Chris Lattner60909e12010-04-28 20:02:30 +0000283 unsigned Count = 1;
284 if (PH.Next(Count))
285 PH.Advance();
Anna Zaks2135ebb2011-12-15 02:28:16 +0000286 else if (PH.Next("+")) {
287 Count = Directive::OneOrMoreCount;
288 PH.Advance();
289 }
Chris Lattner60909e12010-04-28 20:02:30 +0000290
291 // skip optional whitespace
292 PH.SkipWhitespace();
293
294 // next token: {{
295 if (!PH.Next("{{")) {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000296 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000297 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000298 continue;
299 }
Chris Lattner60909e12010-04-28 20:02:30 +0000300 PH.Advance();
301 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000302
Chris Lattner60909e12010-04-28 20:02:30 +0000303 // search for token: }}
304 if (!PH.Search("}}")) {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000305 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000306 diag::err_verify_missing_end) << KindStr;
307 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000308 }
Chris Lattner60909e12010-04-28 20:02:30 +0000309 const char* const ContentEnd = PH.P; // mark content end
310 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000311
Chris Lattner60909e12010-04-28 20:02:30 +0000312 // build directive text; convert \n to newlines
313 std::string Text;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000314 StringRef NewlineStr = "\\n";
315 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattner60909e12010-04-28 20:02:30 +0000316 size_t CPos = 0;
317 size_t FPos;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattner60909e12010-04-28 20:02:30 +0000319 Text += Content.substr(CPos, FPos-CPos);
320 Text += '\n';
321 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000322 }
Chris Lattner60909e12010-04-28 20:02:30 +0000323 if (Text.empty())
324 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000325
Chris Lattner60909e12010-04-28 20:02:30 +0000326 // construct new directive
327 Directive *D = Directive::Create(RegexKind, Pos, Text, Count);
328 std::string Error;
329 if (D->isValid(Error))
330 DL->push_back(D);
331 else {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000332 PP.Diag(Pos.getLocWithOffset(ContentBegin-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000333 diag::err_verify_invalid_content)
334 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000335 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000336 }
337}
338
339/// FindExpectedDiags - Lex the main source file to find all of the
340// expected errors and warnings.
Axel Naumann01231612011-07-25 19:18:12 +0000341static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED, FileID FID) {
342 // Create a raw lexer to pull all the comments out of FID.
343 if (FID.isInvalid())
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000344 return;
345
Axel Naumann01231612011-07-25 19:18:12 +0000346 SourceManager& SM = PP.getSourceManager();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000347 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000348 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
349 Lexer RawLex(FID, FromFile, SM, PP.getLangOptions());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000350
351 // Return comments as tokens, this is how we find expected diagnostics.
352 RawLex.SetCommentRetentionState(true);
353
354 Token Tok;
355 Tok.setKind(tok::comment);
356 while (Tok.isNot(tok::eof)) {
357 RawLex.Lex(Tok);
358 if (!Tok.is(tok::comment)) continue;
359
360 std::string Comment = PP.getSpelling(Tok);
361 if (Comment.empty()) continue;
362
Chris Lattner60909e12010-04-28 20:02:30 +0000363 // Find all expected errors/warnings/notes.
364 ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000365 };
366}
367
368/// PrintProblem - This takes a diagnostic map of the delta between expected and
369/// seen diagnostics. If there's anything in it, then something unexpected
370/// happened. Print the map out in a nice format and return "true". If the map
371/// is empty and we're not going to print things, then return "false".
372///
David Blaikied6471f72011-09-25 23:23:43 +0000373static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000374 const_diag_iterator diag_begin,
375 const_diag_iterator diag_end,
376 const char *Kind, bool Expected) {
377 if (diag_begin == diag_end) return 0;
378
379 llvm::SmallString<256> Fmt;
380 llvm::raw_svector_ostream OS(Fmt);
381 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000382 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000383 OS << "\n (frontend)";
384 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000385 OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000386 OS << ": " << I->second;
387 }
388
389 Diags.Report(diag::err_verify_inconsistent_diags)
Daniel Dunbar221c7212009-11-14 07:53:24 +0000390 << Kind << !Expected << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000391 return std::distance(diag_begin, diag_end);
392}
393
David Blaikied6471f72011-09-25 23:23:43 +0000394static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000395 DirectiveList &DL, const char *Kind,
396 bool Expected) {
397 if (DL.empty())
398 return 0;
399
400 llvm::SmallString<256> Fmt;
401 llvm::raw_svector_ostream OS(Fmt);
402 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
403 Directive& D = **I;
404 if (D.Location.isInvalid() || !SourceMgr)
405 OS << "\n (frontend)";
406 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000407 OS << "\n Line " << SourceMgr->getPresumedLineNumber(D.Location);
Chris Lattner60909e12010-04-28 20:02:30 +0000408 OS << ": " << D.Text;
409 }
410
411 Diags.Report(diag::err_verify_inconsistent_diags)
412 << Kind << !Expected << OS.str();
413 return DL.size();
414}
415
416/// CheckLists - Compare expected to seen diagnostic lists and return the
417/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000418///
David Blaikied6471f72011-09-25 23:23:43 +0000419static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000420 const char *Label,
421 DirectiveList &Left,
422 const_diag_iterator d2_begin,
423 const_diag_iterator d2_end) {
424 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000425 DiagList Right(d2_begin, d2_end);
426
Chris Lattner60909e12010-04-28 20:02:30 +0000427 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
428 Directive& D = **I;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000429 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.Location);
Anna Zaks2135ebb2011-12-15 02:28:16 +0000430 bool FoundOnce = false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000431
Chris Lattner60909e12010-04-28 20:02:30 +0000432 for (unsigned i = 0; i < D.Count; ++i) {
433 DiagList::iterator II, IE;
434 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000435 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattner60909e12010-04-28 20:02:30 +0000436 if (LineNo1 != LineNo2)
437 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000438
Chris Lattner60909e12010-04-28 20:02:30 +0000439 const std::string &RightText = II->second;
440 if (D.Match(RightText))
441 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000442 }
Chris Lattner60909e12010-04-28 20:02:30 +0000443 if (II == IE) {
Anna Zaks2135ebb2011-12-15 02:28:16 +0000444 if (D.Count == D.OneOrMoreCount && FoundOnce) {
445 // We are only interested in at least one match and we found one.
446 break;
447 }
Chris Lattner60909e12010-04-28 20:02:30 +0000448 // Not found.
449 LeftOnly.push_back(*I);
450 } else {
451 // Found. The same cannot be found twice.
452 Right.erase(II);
Anna Zaks2135ebb2011-12-15 02:28:16 +0000453 FoundOnce = true;
Chris Lattner60909e12010-04-28 20:02:30 +0000454 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000455 }
456 }
457 // Now all that's left in Right are those that were not matched.
458
Chris Lattner60909e12010-04-28 20:02:30 +0000459 return (PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true) +
460 PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(),
461 Label, false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000462}
463
464/// CheckResults - This compares the expected results to those that
465/// were actually reported. It emits any discrepencies. Return "true" if there
466/// were problems. Return "false" otherwise.
467///
David Blaikied6471f72011-09-25 23:23:43 +0000468static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000469 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000470 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000471 // We want to capture the delta between what was expected and what was
472 // seen.
473 //
474 // Expected \ Seen - set expected but not seen
475 // Seen \ Expected - set seen but not expected
476 unsigned NumProblems = 0;
477
478 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000479 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
480 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000481
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000482 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000483 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
484 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000485
486 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000487 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
488 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000489
490 return NumProblems;
491}
492
David Blaikie621bc692011-09-26 00:38:03 +0000493void VerifyDiagnosticConsumer::CheckDiagnostics() {
Chris Lattner60909e12010-04-28 20:02:30 +0000494 ExpectedData ED;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000495
496 // Ensure any diagnostics go to the primary client.
Douglas Gregor78243652011-09-13 01:26:44 +0000497 bool OwnsCurClient = Diags.ownsClient();
David Blaikie78ad0b92011-09-25 23:39:51 +0000498 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000499 Diags.setClient(PrimaryClient, false);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000500
501 // If we have a preprocessor, scan the source for expected diagnostic
502 // markers. If not then any diagnostics are unexpected.
503 if (CurrentPreprocessor) {
Axel Naumann01231612011-07-25 19:18:12 +0000504 SourceManager &SM = CurrentPreprocessor->getSourceManager();
505 // Extract expected-error strings from main file.
506 FindExpectedDiags(*CurrentPreprocessor, ED, SM.getMainFileID());
507 // Only check for expectations in other diagnostic locations
508 // if they are not the main file (via ID or FileEntry) - the main
509 // file has already been looked at, and its expectations must not
510 // be added twice.
511 if (!FirstErrorFID.isInvalid() && FirstErrorFID != SM.getMainFileID()
512 && (!SM.getFileEntryForID(FirstErrorFID)
513 || (SM.getFileEntryForID(FirstErrorFID) !=
Axel Naumann84c05e32011-08-24 13:36:19 +0000514 SM.getFileEntryForID(SM.getMainFileID())))) {
Axel Naumann01231612011-07-25 19:18:12 +0000515 FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID);
Axel Naumann84c05e32011-08-24 13:36:19 +0000516 FirstErrorFID = FileID();
517 }
Daniel Dunbar221c7212009-11-14 07:53:24 +0000518
519 // Check that the expected diagnostics occurred.
Axel Naumann01231612011-07-25 19:18:12 +0000520 NumErrors += CheckResults(Diags, SM, *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000521 } else {
522 NumErrors += (PrintProblem(Diags, 0,
523 Buffer->err_begin(), Buffer->err_end(),
524 "error", false) +
525 PrintProblem(Diags, 0,
526 Buffer->warn_begin(), Buffer->warn_end(),
527 "warn", false) +
528 PrintProblem(Diags, 0,
529 Buffer->note_begin(), Buffer->note_end(),
530 "note", false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000531 }
532
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000533 Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000534 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000535
536 // Reset the buffer, we have processed all the diagnostics in it.
537 Buffer.reset(new TextDiagnosticBuffer());
538}
Chris Lattner60909e12010-04-28 20:02:30 +0000539
Douglas Gregoraee526e2011-09-29 00:38:00 +0000540DiagnosticConsumer *
541VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
542 if (!Diags.getClient())
543 Diags.setClient(PrimaryClient->clone(Diags));
544
545 return new VerifyDiagnosticConsumer(Diags);
546}
547
Chris Lattner60909e12010-04-28 20:02:30 +0000548Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
549 const std::string &Text, unsigned Count) {
550 if (RegexKind)
551 return new RegexDirective(Location, Text, Count);
552 return new StandardDirective(Location, Text, Count);
553}