blob: 67bc5efa1a563f84af719fa3033de0d77faacea6 [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"
Eli Friedman4bf34d12011-12-15 04:24:37 +000021#include <climits>
Anna Zaksc035e092011-12-15 02:58:00 +000022
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000023using namespace clang;
24
David Blaikie621bc692011-09-26 00:38:03 +000025VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
Douglas Gregor78243652011-09-13 01:26:44 +000026 : Diags(_Diags), PrimaryClient(Diags.getClient()),
27 OwnsPrimaryClient(Diags.ownsClient()),
28 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0)
29{
30 Diags.takeClient();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000031}
32
David Blaikie621bc692011-09-26 00:38:03 +000033VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Douglas Gregor78243652011-09-13 01:26:44 +000034 CheckDiagnostics();
35 Diags.takeClient();
36 if (OwnsPrimaryClient)
37 delete PrimaryClient;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000038}
39
David Blaikie78ad0b92011-09-25 23:39:51 +000040// DiagnosticConsumer interface.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000041
David Blaikie621bc692011-09-26 00:38:03 +000042void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +000043 const Preprocessor *PP) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000044 // FIXME: Const hack, we screw up the preprocessor but in practice its ok
45 // because it doesn't get reused. It would be better if we could make a copy
46 // though.
47 CurrentPreprocessor = const_cast<Preprocessor*>(PP);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000048
49 PrimaryClient->BeginSourceFile(LangOpts, PP);
50}
51
David Blaikie621bc692011-09-26 00:38:03 +000052void VerifyDiagnosticConsumer::EndSourceFile() {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000053 CheckDiagnostics();
54
55 PrimaryClient->EndSourceFile();
56
57 CurrentPreprocessor = 0;
58}
Daniel Dunbar221c7212009-11-14 07:53:24 +000059
David Blaikie621bc692011-09-26 00:38:03 +000060void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikie40847cf2011-09-26 01:18:08 +000061 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Axel Naumann01231612011-07-25 19:18:12 +000062 if (FirstErrorFID.isInvalid() && Info.hasSourceManager()) {
63 const SourceManager &SM = Info.getSourceManager();
64 FirstErrorFID = SM.getFileID(Info.getLocation());
65 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000066 // Send the diagnostic to the buffer, we will check it once we reach the end
67 // of the source file (or are destructed).
68 Buffer->HandleDiagnostic(DiagLevel, Info);
69}
70
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000071//===----------------------------------------------------------------------===//
72// Checking diagnostics implementation.
73//===----------------------------------------------------------------------===//
74
75typedef TextDiagnosticBuffer::DiagList DiagList;
76typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
77
Chris Lattner60909e12010-04-28 20:02:30 +000078namespace {
79
80/// Directive - Abstract class representing a parsed verify directive.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000081///
Chris Lattner60909e12010-04-28 20:02:30 +000082class Directive {
83public:
84 static Directive* Create(bool RegexKind, const SourceLocation &Location,
85 const std::string &Text, unsigned Count);
86public:
Anna Zaks2135ebb2011-12-15 02:28:16 +000087 /// Constant representing one or more matches aka regex "+".
88 static const unsigned OneOrMoreCount = UINT_MAX;
89
Chris Lattner60909e12010-04-28 20:02:30 +000090 SourceLocation Location;
91 const std::string Text;
92 unsigned Count;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000093
Chris Lattner60909e12010-04-28 20:02:30 +000094 virtual ~Directive() { }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000095
Chris Lattner60909e12010-04-28 20:02:30 +000096 // Returns true if directive text is valid.
97 // Otherwise returns false and populates E.
98 virtual bool isValid(std::string &Error) = 0;
99
100 // Returns true on match.
101 virtual bool Match(const std::string &S) = 0;
102
103protected:
104 Directive(const SourceLocation &Location, const std::string &Text,
105 unsigned Count)
106 : Location(Location), Text(Text), Count(Count) { }
107
108private:
Argyrios Kyrtzidisc83d2d72010-08-15 10:17:39 +0000109 Directive(const Directive&); // DO NOT IMPLEMENT
110 void operator=(const Directive&); // DO NOT IMPLEMENT
Chris Lattner60909e12010-04-28 20:02:30 +0000111};
112
113/// StandardDirective - Directive with string matching.
114///
115class StandardDirective : public Directive {
116public:
117 StandardDirective(const SourceLocation &Location, const std::string &Text,
118 unsigned Count)
119 : Directive(Location, Text, Count) { }
120
121 virtual bool isValid(std::string &Error) {
122 // all strings are considered valid; even empty ones
123 return true;
124 }
125
126 virtual bool Match(const std::string &S) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000127 return S.find(Text) != std::string::npos;
Chris Lattner60909e12010-04-28 20:02:30 +0000128 }
129};
130
131/// RegexDirective - Directive with regular-expression matching.
132///
133class RegexDirective : public Directive {
134public:
135 RegexDirective(const SourceLocation &Location, const std::string &Text,
136 unsigned Count)
137 : Directive(Location, Text, Count), Regex(Text) { }
138
139 virtual bool isValid(std::string &Error) {
140 if (Regex.isValid(Error))
141 return true;
142 return false;
143 }
144
145 virtual bool Match(const std::string &S) {
146 return Regex.match(S);
147 }
148
149private:
150 llvm::Regex Regex;
151};
152
153typedef std::vector<Directive*> DirectiveList;
154
155/// ExpectedData - owns directive objects and deletes on destructor.
156///
157struct ExpectedData {
158 DirectiveList Errors;
159 DirectiveList Warnings;
160 DirectiveList Notes;
161
162 ~ExpectedData() {
163 DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 };
164 for (DirectiveList **PL = Lists; *PL; ++PL) {
165 DirectiveList * const L = *PL;
166 for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I)
167 delete *I;
168 }
169 }
170};
171
172class ParseHelper
173{
174public:
175 ParseHelper(const char *Begin, const char *End)
176 : Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { }
177
178 // Return true if string literal is next.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179 bool Next(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000180 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000181 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000182 if (PEnd > End)
183 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000184 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-04-28 20:02:30 +0000185 }
186
187 // Return true if number is next.
188 // Output N only if number is next.
189 bool Next(unsigned &N) {
190 unsigned TMP = 0;
191 P = C;
192 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
193 TMP *= 10;
194 TMP += P[0] - '0';
195 }
196 if (P == C)
197 return false;
198 PEnd = P;
199 N = TMP;
200 return true;
201 }
202
203 // Return true if string literal is found.
204 // When true, P marks begin-position of S in content.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000205 bool Search(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000206 P = std::search(C, End, S.begin(), S.end());
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000207 PEnd = P + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000208 return P != End;
209 }
210
211 // Advance 1-past previous next/search.
212 // Behavior is undefined if previous next/search failed.
213 bool Advance() {
214 C = PEnd;
215 return C < End;
216 }
217
218 // Skip zero or more whitespace.
219 void SkipWhitespace() {
220 for (; C < End && isspace(*C); ++C)
221 ;
222 }
223
224 // Return true if EOF reached.
225 bool Done() {
226 return !(C < End);
227 }
228
229 const char * const Begin; // beginning of expected content
230 const char * const End; // end of expected content (1-past)
231 const char *C; // position of next char in content
232 const char *P;
233
234private:
235 const char *PEnd; // previous next/search subject end (1-past)
236};
237
238} // namespace anonymous
239
240/// ParseDirective - Go through the comment and see if it indicates expected
241/// diagnostics. If so, then put them in the appropriate directive list.
242///
243static void ParseDirective(const char *CommentStart, unsigned CommentLen,
244 ExpectedData &ED, Preprocessor &PP,
245 SourceLocation Pos) {
246 // A single comment may contain multiple directives.
247 for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) {
248 // search for token: expected
249 if (!PH.Search("expected"))
250 break;
251 PH.Advance();
252
253 // next token: -
254 if (!PH.Next("-"))
255 continue;
256 PH.Advance();
257
258 // next token: { error | warning | note }
259 DirectiveList* DL = NULL;
260 if (PH.Next("error"))
261 DL = &ED.Errors;
262 else if (PH.Next("warning"))
263 DL = &ED.Warnings;
264 else if (PH.Next("note"))
265 DL = &ED.Notes;
266 else
267 continue;
268 PH.Advance();
269
270 // default directive kind
271 bool RegexKind = false;
272 const char* KindStr = "string";
273
274 // next optional token: -
275 if (PH.Next("-re")) {
276 PH.Advance();
277 RegexKind = true;
278 KindStr = "regex";
279 }
280
281 // skip optional whitespace
282 PH.SkipWhitespace();
283
Anna Zaks2135ebb2011-12-15 02:28:16 +0000284 // next optional token: positive integer or a '+'.
Chris Lattner60909e12010-04-28 20:02:30 +0000285 unsigned Count = 1;
286 if (PH.Next(Count))
287 PH.Advance();
Anna Zaks2135ebb2011-12-15 02:28:16 +0000288 else if (PH.Next("+")) {
289 Count = Directive::OneOrMoreCount;
290 PH.Advance();
291 }
Chris Lattner60909e12010-04-28 20:02:30 +0000292
293 // skip optional whitespace
294 PH.SkipWhitespace();
295
296 // next token: {{
297 if (!PH.Next("{{")) {
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_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000300 continue;
301 }
Chris Lattner60909e12010-04-28 20:02:30 +0000302 PH.Advance();
303 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000304
Chris Lattner60909e12010-04-28 20:02:30 +0000305 // search for token: }}
306 if (!PH.Search("}}")) {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000307 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000308 diag::err_verify_missing_end) << KindStr;
309 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000310 }
Chris Lattner60909e12010-04-28 20:02:30 +0000311 const char* const ContentEnd = PH.P; // mark content end
312 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000313
Chris Lattner60909e12010-04-28 20:02:30 +0000314 // build directive text; convert \n to newlines
315 std::string Text;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000316 StringRef NewlineStr = "\\n";
317 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattner60909e12010-04-28 20:02:30 +0000318 size_t CPos = 0;
319 size_t FPos;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000320 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattner60909e12010-04-28 20:02:30 +0000321 Text += Content.substr(CPos, FPos-CPos);
322 Text += '\n';
323 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000324 }
Chris Lattner60909e12010-04-28 20:02:30 +0000325 if (Text.empty())
326 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000327
Chris Lattner60909e12010-04-28 20:02:30 +0000328 // construct new directive
329 Directive *D = Directive::Create(RegexKind, Pos, Text, Count);
330 std::string Error;
331 if (D->isValid(Error))
332 DL->push_back(D);
333 else {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000334 PP.Diag(Pos.getLocWithOffset(ContentBegin-PH.Begin),
Chris Lattner60909e12010-04-28 20:02:30 +0000335 diag::err_verify_invalid_content)
336 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000337 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000338 }
339}
340
341/// FindExpectedDiags - Lex the main source file to find all of the
342// expected errors and warnings.
Axel Naumann01231612011-07-25 19:18:12 +0000343static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED, FileID FID) {
344 // Create a raw lexer to pull all the comments out of FID.
345 if (FID.isInvalid())
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000346 return;
347
Axel Naumann01231612011-07-25 19:18:12 +0000348 SourceManager& SM = PP.getSourceManager();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000349 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000350 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
351 Lexer RawLex(FID, FromFile, SM, PP.getLangOptions());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000352
353 // Return comments as tokens, this is how we find expected diagnostics.
354 RawLex.SetCommentRetentionState(true);
355
356 Token Tok;
357 Tok.setKind(tok::comment);
358 while (Tok.isNot(tok::eof)) {
359 RawLex.Lex(Tok);
360 if (!Tok.is(tok::comment)) continue;
361
362 std::string Comment = PP.getSpelling(Tok);
363 if (Comment.empty()) continue;
364
Chris Lattner60909e12010-04-28 20:02:30 +0000365 // Find all expected errors/warnings/notes.
366 ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000367 };
368}
369
370/// PrintProblem - This takes a diagnostic map of the delta between expected and
371/// seen diagnostics. If there's anything in it, then something unexpected
372/// happened. Print the map out in a nice format and return "true". If the map
373/// is empty and we're not going to print things, then return "false".
374///
David Blaikied6471f72011-09-25 23:23:43 +0000375static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000376 const_diag_iterator diag_begin,
377 const_diag_iterator diag_end,
378 const char *Kind, bool Expected) {
379 if (diag_begin == diag_end) return 0;
380
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000381 SmallString<256> Fmt;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000382 llvm::raw_svector_ostream OS(Fmt);
383 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000384 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000385 OS << "\n (frontend)";
386 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000387 OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000388 OS << ": " << I->second;
389 }
390
391 Diags.Report(diag::err_verify_inconsistent_diags)
Daniel Dunbar221c7212009-11-14 07:53:24 +0000392 << Kind << !Expected << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000393 return std::distance(diag_begin, diag_end);
394}
395
David Blaikied6471f72011-09-25 23:23:43 +0000396static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000397 DirectiveList &DL, const char *Kind,
398 bool Expected) {
399 if (DL.empty())
400 return 0;
401
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000402 SmallString<256> Fmt;
Chris Lattner60909e12010-04-28 20:02:30 +0000403 llvm::raw_svector_ostream OS(Fmt);
404 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
405 Directive& D = **I;
406 if (D.Location.isInvalid() || !SourceMgr)
407 OS << "\n (frontend)";
408 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000409 OS << "\n Line " << SourceMgr->getPresumedLineNumber(D.Location);
Chris Lattner60909e12010-04-28 20:02:30 +0000410 OS << ": " << D.Text;
411 }
412
413 Diags.Report(diag::err_verify_inconsistent_diags)
414 << Kind << !Expected << OS.str();
415 return DL.size();
416}
417
418/// CheckLists - Compare expected to seen diagnostic lists and return the
419/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000420///
David Blaikied6471f72011-09-25 23:23:43 +0000421static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000422 const char *Label,
423 DirectiveList &Left,
424 const_diag_iterator d2_begin,
425 const_diag_iterator d2_end) {
426 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000427 DiagList Right(d2_begin, d2_end);
428
Chris Lattner60909e12010-04-28 20:02:30 +0000429 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
430 Directive& D = **I;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000431 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.Location);
Anna Zaks2135ebb2011-12-15 02:28:16 +0000432 bool FoundOnce = false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000433
Chris Lattner60909e12010-04-28 20:02:30 +0000434 for (unsigned i = 0; i < D.Count; ++i) {
435 DiagList::iterator II, IE;
436 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000437 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattner60909e12010-04-28 20:02:30 +0000438 if (LineNo1 != LineNo2)
439 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000440
Chris Lattner60909e12010-04-28 20:02:30 +0000441 const std::string &RightText = II->second;
442 if (D.Match(RightText))
443 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000444 }
Chris Lattner60909e12010-04-28 20:02:30 +0000445 if (II == IE) {
Anna Zaks0e818a42011-12-16 18:28:45 +0000446 if (D.Count == D.OneOrMoreCount) {
447 if (!FoundOnce)
448 LeftOnly.push_back(*I);
449 // We are only interested in at least one match, so exit the loop.
Anna Zaks2135ebb2011-12-15 02:28:16 +0000450 break;
451 }
Chris Lattner60909e12010-04-28 20:02:30 +0000452 // Not found.
453 LeftOnly.push_back(*I);
454 } else {
455 // Found. The same cannot be found twice.
456 Right.erase(II);
Anna Zaks2135ebb2011-12-15 02:28:16 +0000457 FoundOnce = true;
Chris Lattner60909e12010-04-28 20:02:30 +0000458 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000459 }
460 }
461 // Now all that's left in Right are those that were not matched.
NAKAMURA Takumiad646842011-12-17 13:00:31 +0000462 unsigned num = PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true);
463 num += PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(),
464 Label, false);
465 return num;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000466}
467
468/// CheckResults - This compares the expected results to those that
469/// were actually reported. It emits any discrepencies. Return "true" if there
470/// were problems. Return "false" otherwise.
471///
David Blaikied6471f72011-09-25 23:23:43 +0000472static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000473 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000474 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000475 // We want to capture the delta between what was expected and what was
476 // seen.
477 //
478 // Expected \ Seen - set expected but not seen
479 // Seen \ Expected - set seen but not expected
480 unsigned NumProblems = 0;
481
482 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000483 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
484 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000485
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000486 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000487 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
488 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000489
490 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000491 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
492 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000493
494 return NumProblems;
495}
496
David Blaikie621bc692011-09-26 00:38:03 +0000497void VerifyDiagnosticConsumer::CheckDiagnostics() {
Chris Lattner60909e12010-04-28 20:02:30 +0000498 ExpectedData ED;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000499
500 // Ensure any diagnostics go to the primary client.
Douglas Gregor78243652011-09-13 01:26:44 +0000501 bool OwnsCurClient = Diags.ownsClient();
David Blaikie78ad0b92011-09-25 23:39:51 +0000502 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000503 Diags.setClient(PrimaryClient, false);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000504
505 // If we have a preprocessor, scan the source for expected diagnostic
506 // markers. If not then any diagnostics are unexpected.
507 if (CurrentPreprocessor) {
Axel Naumann01231612011-07-25 19:18:12 +0000508 SourceManager &SM = CurrentPreprocessor->getSourceManager();
509 // Extract expected-error strings from main file.
510 FindExpectedDiags(*CurrentPreprocessor, ED, SM.getMainFileID());
511 // Only check for expectations in other diagnostic locations
512 // if they are not the main file (via ID or FileEntry) - the main
513 // file has already been looked at, and its expectations must not
514 // be added twice.
515 if (!FirstErrorFID.isInvalid() && FirstErrorFID != SM.getMainFileID()
516 && (!SM.getFileEntryForID(FirstErrorFID)
517 || (SM.getFileEntryForID(FirstErrorFID) !=
Axel Naumann84c05e32011-08-24 13:36:19 +0000518 SM.getFileEntryForID(SM.getMainFileID())))) {
Axel Naumann01231612011-07-25 19:18:12 +0000519 FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID);
Axel Naumann84c05e32011-08-24 13:36:19 +0000520 FirstErrorFID = FileID();
521 }
Daniel Dunbar221c7212009-11-14 07:53:24 +0000522
523 // Check that the expected diagnostics occurred.
Axel Naumann01231612011-07-25 19:18:12 +0000524 NumErrors += CheckResults(Diags, SM, *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000525 } else {
526 NumErrors += (PrintProblem(Diags, 0,
527 Buffer->err_begin(), Buffer->err_end(),
528 "error", false) +
529 PrintProblem(Diags, 0,
530 Buffer->warn_begin(), Buffer->warn_end(),
531 "warn", false) +
532 PrintProblem(Diags, 0,
533 Buffer->note_begin(), Buffer->note_end(),
534 "note", false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000535 }
536
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000537 Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000538 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000539
540 // Reset the buffer, we have processed all the diagnostics in it.
541 Buffer.reset(new TextDiagnosticBuffer());
542}
Chris Lattner60909e12010-04-28 20:02:30 +0000543
Douglas Gregoraee526e2011-09-29 00:38:00 +0000544DiagnosticConsumer *
545VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
546 if (!Diags.getClient())
547 Diags.setClient(PrimaryClient->clone(Diags));
548
549 return new VerifyDiagnosticConsumer(Diags);
550}
551
Chris Lattner60909e12010-04-28 20:02:30 +0000552Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
553 const std::string &Text, unsigned Count) {
554 if (RegexKind)
555 return new RegexDirective(Location, Text, Count);
556 return new StandardDirective(Location, Text, Count);
557}