blob: 8aa65f1f1fc5fc6a30a5cd2fb7cef70cd2e0eeac [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
Jordan Rose78541c42012-07-11 19:58:23 +000014#include "clang/Basic/FileManager.h"
David Blaikie621bc692011-09-26 00:38:03 +000015#include "clang/Frontend/VerifyDiagnosticConsumer.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000016#include "clang/Frontend/FrontendDiagnostic.h"
17#include "clang/Frontend/TextDiagnosticBuffer.h"
18#include "clang/Lex/Preprocessor.h"
19#include "llvm/ADT/SmallString.h"
Chris Lattner60909e12010-04-28 20:02:30 +000020#include "llvm/Support/Regex.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000021#include "llvm/Support/raw_ostream.h"
Anna Zaksc035e092011-12-15 02:58:00 +000022
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000023using namespace clang;
Jordan Rose4313c012012-07-10 02:56:15 +000024typedef VerifyDiagnosticConsumer::Directive Directive;
25typedef VerifyDiagnosticConsumer::DirectiveList DirectiveList;
26typedef VerifyDiagnosticConsumer::ExpectedData ExpectedData;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000027
David Blaikie621bc692011-09-26 00:38:03 +000028VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
Douglas Gregor78243652011-09-13 01:26:44 +000029 : Diags(_Diags), PrimaryClient(Diags.getClient()),
30 OwnsPrimaryClient(Diags.ownsClient()),
31 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0)
32{
33 Diags.takeClient();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000034}
35
David Blaikie621bc692011-09-26 00:38:03 +000036VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Douglas Gregor78243652011-09-13 01:26:44 +000037 CheckDiagnostics();
38 Diags.takeClient();
39 if (OwnsPrimaryClient)
40 delete PrimaryClient;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000041}
42
David Blaikie78ad0b92011-09-25 23:39:51 +000043// DiagnosticConsumer interface.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000044
David Blaikie621bc692011-09-26 00:38:03 +000045void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +000046 const Preprocessor *PP) {
Jordan Rose78541c42012-07-11 19:58:23 +000047 CurrentPreprocessor = PP;
48 if (PP) const_cast<Preprocessor*>(PP)->addCommentHandler(this);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000049
50 PrimaryClient->BeginSourceFile(LangOpts, PP);
51}
52
David Blaikie621bc692011-09-26 00:38:03 +000053void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Rose78541c42012-07-11 19:58:23 +000054 if (CurrentPreprocessor)
55 const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000056 CheckDiagnostics();
57
58 PrimaryClient->EndSourceFile();
59
60 CurrentPreprocessor = 0;
61}
Daniel Dunbar221c7212009-11-14 07:53:24 +000062
David Blaikie621bc692011-09-26 00:38:03 +000063void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikie40847cf2011-09-26 01:18:08 +000064 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Jordan Rose78541c42012-07-11 19:58:23 +000065 if (Info.hasSourceManager()) {
Axel Naumann01231612011-07-25 19:18:12 +000066 const SourceManager &SM = Info.getSourceManager();
Jordan Rose78541c42012-07-11 19:58:23 +000067 FilesWithDiagnostics.insert(SM.getFileID(Info.getLocation()));
Axel Naumann01231612011-07-25 19:18:12 +000068 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000069 // Send the diagnostic to the buffer, we will check it once we reach the end
70 // of the source file (or are destructed).
71 Buffer->HandleDiagnostic(DiagLevel, Info);
72}
73
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000074//===----------------------------------------------------------------------===//
75// Checking diagnostics implementation.
76//===----------------------------------------------------------------------===//
77
78typedef TextDiagnosticBuffer::DiagList DiagList;
79typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
80
Chris Lattner60909e12010-04-28 20:02:30 +000081namespace {
82
Chris Lattner60909e12010-04-28 20:02:30 +000083/// StandardDirective - Directive with string matching.
84///
85class StandardDirective : public Directive {
86public:
Jordan Roseaa48fe82012-07-10 02:57:03 +000087 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Rose3b81b7d2012-07-10 02:57:26 +000088 StringRef Text, unsigned Min, unsigned Max)
89 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max) { }
Chris Lattner60909e12010-04-28 20:02:30 +000090
91 virtual bool isValid(std::string &Error) {
92 // all strings are considered valid; even empty ones
93 return true;
94 }
95
Jordan Rose4313c012012-07-10 02:56:15 +000096 virtual bool match(StringRef S) {
97 return S.find(Text) != StringRef::npos;
Chris Lattner60909e12010-04-28 20:02:30 +000098 }
99};
100
101/// RegexDirective - Directive with regular-expression matching.
102///
103class RegexDirective : public Directive {
104public:
Jordan Roseaa48fe82012-07-10 02:57:03 +0000105 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000106 StringRef Text, unsigned Min, unsigned Max)
107 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max), Regex(Text) { }
Chris Lattner60909e12010-04-28 20:02:30 +0000108
109 virtual bool isValid(std::string &Error) {
110 if (Regex.isValid(Error))
111 return true;
112 return false;
113 }
114
Jordan Rose4313c012012-07-10 02:56:15 +0000115 virtual bool match(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000116 return Regex.match(S);
117 }
118
119private:
120 llvm::Regex Regex;
121};
122
Chris Lattner60909e12010-04-28 20:02:30 +0000123class ParseHelper
124{
125public:
Jordan Rose78541c42012-07-11 19:58:23 +0000126 ParseHelper(StringRef S)
127 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(NULL) { }
Chris Lattner60909e12010-04-28 20:02:30 +0000128
129 // Return true if string literal is next.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 bool Next(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000131 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000132 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000133 if (PEnd > End)
134 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000135 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-04-28 20:02:30 +0000136 }
137
138 // Return true if number is next.
139 // Output N only if number is next.
140 bool Next(unsigned &N) {
141 unsigned TMP = 0;
142 P = C;
143 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
144 TMP *= 10;
145 TMP += P[0] - '0';
146 }
147 if (P == C)
148 return false;
149 PEnd = P;
150 N = TMP;
151 return true;
152 }
153
154 // Return true if string literal is found.
155 // When true, P marks begin-position of S in content.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156 bool Search(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000157 P = std::search(C, End, S.begin(), S.end());
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000158 PEnd = P + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000159 return P != End;
160 }
161
162 // Advance 1-past previous next/search.
163 // Behavior is undefined if previous next/search failed.
164 bool Advance() {
165 C = PEnd;
166 return C < End;
167 }
168
169 // Skip zero or more whitespace.
170 void SkipWhitespace() {
171 for (; C < End && isspace(*C); ++C)
172 ;
173 }
174
175 // Return true if EOF reached.
176 bool Done() {
177 return !(C < End);
178 }
179
180 const char * const Begin; // beginning of expected content
181 const char * const End; // end of expected content (1-past)
182 const char *C; // position of next char in content
183 const char *P;
184
185private:
186 const char *PEnd; // previous next/search subject end (1-past)
187};
188
189} // namespace anonymous
190
191/// ParseDirective - Go through the comment and see if it indicates expected
192/// diagnostics. If so, then put them in the appropriate directive list.
193///
Jordan Rose78541c42012-07-11 19:58:23 +0000194/// Returns true if any valid directives were found.
195static bool ParseDirective(StringRef S, ExpectedData &ED, SourceManager &SM,
Jordan Rose4313c012012-07-10 02:56:15 +0000196 SourceLocation Pos, DiagnosticsEngine &Diags) {
Chris Lattner60909e12010-04-28 20:02:30 +0000197 // A single comment may contain multiple directives.
Jordan Rose78541c42012-07-11 19:58:23 +0000198 bool FoundDirective = false;
199 for (ParseHelper PH(S); !PH.Done();) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000200 // Search for token: expected
Chris Lattner60909e12010-04-28 20:02:30 +0000201 if (!PH.Search("expected"))
202 break;
203 PH.Advance();
204
Jordan Roseaa48fe82012-07-10 02:57:03 +0000205 // Next token: -
Chris Lattner60909e12010-04-28 20:02:30 +0000206 if (!PH.Next("-"))
207 continue;
208 PH.Advance();
209
Jordan Roseaa48fe82012-07-10 02:57:03 +0000210 // Next token: { error | warning | note }
Chris Lattner60909e12010-04-28 20:02:30 +0000211 DirectiveList* DL = NULL;
212 if (PH.Next("error"))
213 DL = &ED.Errors;
214 else if (PH.Next("warning"))
215 DL = &ED.Warnings;
216 else if (PH.Next("note"))
217 DL = &ED.Notes;
218 else
219 continue;
220 PH.Advance();
221
Jordan Roseaa48fe82012-07-10 02:57:03 +0000222 // Default directive kind.
Chris Lattner60909e12010-04-28 20:02:30 +0000223 bool RegexKind = false;
224 const char* KindStr = "string";
225
Jordan Roseaa48fe82012-07-10 02:57:03 +0000226 // Next optional token: -
Chris Lattner60909e12010-04-28 20:02:30 +0000227 if (PH.Next("-re")) {
228 PH.Advance();
229 RegexKind = true;
230 KindStr = "regex";
231 }
232
Jordan Roseaa48fe82012-07-10 02:57:03 +0000233 // Next optional token: @
234 SourceLocation ExpectedLoc;
235 if (!PH.Next("@")) {
236 ExpectedLoc = Pos;
237 } else {
238 PH.Advance();
239 unsigned Line = 0;
240 bool FoundPlus = PH.Next("+");
241 if (FoundPlus || PH.Next("-")) {
242 // Relative to current line.
243 PH.Advance();
244 bool Invalid = false;
245 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
246 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
247 if (FoundPlus) ExpectedLine += Line;
248 else ExpectedLine -= Line;
249 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
250 }
251 } else {
252 // Absolute line number.
253 if (PH.Next(Line) && Line > 0)
254 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
255 }
256
257 if (ExpectedLoc.isInvalid()) {
258 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
259 diag::err_verify_missing_line) << KindStr;
260 continue;
261 }
262 PH.Advance();
263 }
264
265 // Skip optional whitespace.
Chris Lattner60909e12010-04-28 20:02:30 +0000266 PH.SkipWhitespace();
267
Jordan Roseaa48fe82012-07-10 02:57:03 +0000268 // Next optional token: positive integer or a '+'.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000269 unsigned Min = 1;
270 unsigned Max = 1;
271 if (PH.Next(Min)) {
Chris Lattner60909e12010-04-28 20:02:30 +0000272 PH.Advance();
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000273 // A positive integer can be followed by a '+' meaning min
274 // or more, or by a '-' meaning a range from min to max.
275 if (PH.Next("+")) {
276 Max = Directive::MaxCount;
277 PH.Advance();
278 } else if (PH.Next("-")) {
279 PH.Advance();
280 if (!PH.Next(Max) || Max < Min) {
281 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
282 diag::err_verify_invalid_range) << KindStr;
283 continue;
284 }
285 PH.Advance();
286 } else {
287 Max = Min;
288 }
289 } else if (PH.Next("+")) {
290 // '+' on its own means "1 or more".
291 Max = Directive::MaxCount;
Anna Zaks2135ebb2011-12-15 02:28:16 +0000292 PH.Advance();
293 }
Chris Lattner60909e12010-04-28 20:02:30 +0000294
Jordan Roseaa48fe82012-07-10 02:57:03 +0000295 // Skip optional whitespace.
Chris Lattner60909e12010-04-28 20:02:30 +0000296 PH.SkipWhitespace();
297
Jordan Roseaa48fe82012-07-10 02:57:03 +0000298 // Next token: {{
Chris Lattner60909e12010-04-28 20:02:30 +0000299 if (!PH.Next("{{")) {
Jordan Rose4313c012012-07-10 02:56:15 +0000300 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
301 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000302 continue;
303 }
Chris Lattner60909e12010-04-28 20:02:30 +0000304 PH.Advance();
305 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000306
Jordan Roseaa48fe82012-07-10 02:57:03 +0000307 // Search for token: }}
Chris Lattner60909e12010-04-28 20:02:30 +0000308 if (!PH.Search("}}")) {
Jordan Rose4313c012012-07-10 02:56:15 +0000309 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
310 diag::err_verify_missing_end) << KindStr;
Chris Lattner60909e12010-04-28 20:02:30 +0000311 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000312 }
Chris Lattner60909e12010-04-28 20:02:30 +0000313 const char* const ContentEnd = PH.P; // mark content end
314 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000315
Jordan Roseaa48fe82012-07-10 02:57:03 +0000316 // Build directive text; convert \n to newlines.
Chris Lattner60909e12010-04-28 20:02:30 +0000317 std::string Text;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 StringRef NewlineStr = "\\n";
319 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattner60909e12010-04-28 20:02:30 +0000320 size_t CPos = 0;
321 size_t FPos;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000322 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattner60909e12010-04-28 20:02:30 +0000323 Text += Content.substr(CPos, FPos-CPos);
324 Text += '\n';
325 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000326 }
Chris Lattner60909e12010-04-28 20:02:30 +0000327 if (Text.empty())
328 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000329
Jordan Roseaa48fe82012-07-10 02:57:03 +0000330 // Construct new directive.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000331 Directive *D = Directive::create(RegexKind, Pos, ExpectedLoc, Text,
332 Min, Max);
Chris Lattner60909e12010-04-28 20:02:30 +0000333 std::string Error;
Jordan Rose78541c42012-07-11 19:58:23 +0000334 if (D->isValid(Error)) {
Chris Lattner60909e12010-04-28 20:02:30 +0000335 DL->push_back(D);
Jordan Rose78541c42012-07-11 19:58:23 +0000336 FoundDirective = true;
337 } else {
Jordan Rose4313c012012-07-10 02:56:15 +0000338 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
339 diag::err_verify_invalid_content)
Chris Lattner60909e12010-04-28 20:02:30 +0000340 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000341 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000342 }
Jordan Rose78541c42012-07-11 19:58:23 +0000343
344 return FoundDirective;
345}
346
347/// HandleComment - Hook into the preprocessor and extract comments containing
348/// expected errors and warnings.
349bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
350 SourceRange Comment) {
351 SourceManager &SM = PP.getSourceManager();
352 SourceLocation CommentBegin = Comment.getBegin();
353
354 const char *CommentRaw = SM.getCharacterData(CommentBegin);
355 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
356
357 if (C.empty())
358 return false;
359
360 // Fold any "\<EOL>" sequences
361 size_t loc = C.find('\\');
362 if (loc == StringRef::npos) {
363 if (ParseDirective(C, ED, SM, CommentBegin, PP.getDiagnostics()))
364 if (const FileEntry *E = SM.getFileEntryForID(SM.getFileID(CommentBegin)))
365 FilesWithDirectives.insert(E);
366 return false;
367 }
368
369 std::string C2;
370 C2.reserve(C.size());
371
372 for (size_t last = 0;; loc = C.find('\\', last)) {
373 if (loc == StringRef::npos || loc == C.size()) {
374 C2 += C.substr(last);
375 break;
376 }
377 C2 += C.substr(last, loc-last);
378 last = loc + 1;
379
380 if (C[last] == '\n' || C[last] == '\r') {
381 ++last;
382
383 // Escape \r\n or \n\r, but not \n\n.
384 if (last < C.size())
385 if (C[last] == '\n' || C[last] == '\r')
386 if (C[last] != C[last-1])
387 ++last;
388 } else {
389 // This was just a normal backslash.
390 C2 += '\\';
391 }
392 }
393
394 if (!C2.empty())
395 if (ParseDirective(C2, ED, SM, CommentBegin, PP.getDiagnostics()))
396 if (const FileEntry *E = SM.getFileEntryForID(SM.getFileID(CommentBegin)))
397 FilesWithDirectives.insert(E);
398 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000399}
400
401/// FindExpectedDiags - Lex the main source file to find all of the
402// expected errors and warnings.
Jordan Rose78541c42012-07-11 19:58:23 +0000403static void FindExpectedDiags(const Preprocessor &PP, ExpectedData &ED,
404 FileID FID) {
Axel Naumann01231612011-07-25 19:18:12 +0000405 // Create a raw lexer to pull all the comments out of FID.
406 if (FID.isInvalid())
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000407 return;
408
Axel Naumann01231612011-07-25 19:18:12 +0000409 SourceManager& SM = PP.getSourceManager();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000410 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000411 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
David Blaikie4e4d0842012-03-11 07:00:24 +0000412 Lexer RawLex(FID, FromFile, SM, PP.getLangOpts());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000413
414 // Return comments as tokens, this is how we find expected diagnostics.
415 RawLex.SetCommentRetentionState(true);
416
417 Token Tok;
418 Tok.setKind(tok::comment);
419 while (Tok.isNot(tok::eof)) {
420 RawLex.Lex(Tok);
421 if (!Tok.is(tok::comment)) continue;
422
423 std::string Comment = PP.getSpelling(Tok);
424 if (Comment.empty()) continue;
425
Chris Lattner60909e12010-04-28 20:02:30 +0000426 // Find all expected errors/warnings/notes.
Jordan Rose78541c42012-07-11 19:58:23 +0000427 ParseDirective(Comment, ED, SM, Tok.getLocation(), PP.getDiagnostics());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000428 };
429}
430
Jordan Roseaa48fe82012-07-10 02:57:03 +0000431/// \brief Takes a list of diagnostics that have been generated but not matched
432/// by an expected-* directive and produces a diagnostic to the user from this.
433static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
434 const_diag_iterator diag_begin,
435 const_diag_iterator diag_end,
436 const char *Kind) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000437 if (diag_begin == diag_end) return 0;
438
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000439 SmallString<256> Fmt;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000440 llvm::raw_svector_ostream OS(Fmt);
441 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000442 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000443 OS << "\n (frontend)";
444 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000445 OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000446 OS << ": " << I->second;
447 }
448
Jordan Rosec6d64a22012-07-11 16:50:36 +0000449 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Roseaa48fe82012-07-10 02:57:03 +0000450 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000451 return std::distance(diag_begin, diag_end);
452}
453
Jordan Roseaa48fe82012-07-10 02:57:03 +0000454/// \brief Takes a list of diagnostics that were expected to have been generated
455/// but were not and produces a diagnostic to the user from this.
456static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
457 DirectiveList &DL, const char *Kind) {
Chris Lattner60909e12010-04-28 20:02:30 +0000458 if (DL.empty())
459 return 0;
460
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000461 SmallString<256> Fmt;
Chris Lattner60909e12010-04-28 20:02:30 +0000462 llvm::raw_svector_ostream OS(Fmt);
463 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000464 Directive &D = **I;
465 OS << "\n Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
466 if (D.DirectiveLoc != D.DiagnosticLoc)
467 OS << " (directive at "
468 << SourceMgr.getFilename(D.DirectiveLoc) << ":"
469 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ")";
Chris Lattner60909e12010-04-28 20:02:30 +0000470 OS << ": " << D.Text;
471 }
472
Jordan Rosec6d64a22012-07-11 16:50:36 +0000473 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Roseaa48fe82012-07-10 02:57:03 +0000474 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattner60909e12010-04-28 20:02:30 +0000475 return DL.size();
476}
477
478/// CheckLists - Compare expected to seen diagnostic lists and return the
479/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000480///
David Blaikied6471f72011-09-25 23:23:43 +0000481static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000482 const char *Label,
483 DirectiveList &Left,
484 const_diag_iterator d2_begin,
485 const_diag_iterator d2_end) {
486 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000487 DiagList Right(d2_begin, d2_end);
488
Chris Lattner60909e12010-04-28 20:02:30 +0000489 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
490 Directive& D = **I;
Jordan Roseaa48fe82012-07-10 02:57:03 +0000491 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000492
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000493 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattner60909e12010-04-28 20:02:30 +0000494 DiagList::iterator II, IE;
495 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000496 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattner60909e12010-04-28 20:02:30 +0000497 if (LineNo1 != LineNo2)
498 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000499
Chris Lattner60909e12010-04-28 20:02:30 +0000500 const std::string &RightText = II->second;
Jordan Rose4313c012012-07-10 02:56:15 +0000501 if (D.match(RightText))
Chris Lattner60909e12010-04-28 20:02:30 +0000502 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000503 }
Chris Lattner60909e12010-04-28 20:02:30 +0000504 if (II == IE) {
505 // Not found.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000506 if (i >= D.Min) break;
Chris Lattner60909e12010-04-28 20:02:30 +0000507 LeftOnly.push_back(*I);
508 } else {
509 // Found. The same cannot be found twice.
510 Right.erase(II);
511 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000512 }
513 }
514 // Now all that's left in Right are those that were not matched.
Jordan Roseaa48fe82012-07-10 02:57:03 +0000515 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
516 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumiad646842011-12-17 13:00:31 +0000517 return num;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000518}
519
520/// CheckResults - This compares the expected results to those that
521/// were actually reported. It emits any discrepencies. Return "true" if there
522/// were problems. Return "false" otherwise.
523///
David Blaikied6471f72011-09-25 23:23:43 +0000524static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000525 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000526 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000527 // We want to capture the delta between what was expected and what was
528 // seen.
529 //
530 // Expected \ Seen - set expected but not seen
531 // Seen \ Expected - set seen but not expected
532 unsigned NumProblems = 0;
533
534 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000535 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
536 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000537
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000538 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000539 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
540 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000541
542 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000543 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
544 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000545
546 return NumProblems;
547}
548
David Blaikie621bc692011-09-26 00:38:03 +0000549void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000550 // Ensure any diagnostics go to the primary client.
Douglas Gregor78243652011-09-13 01:26:44 +0000551 bool OwnsCurClient = Diags.ownsClient();
David Blaikie78ad0b92011-09-25 23:39:51 +0000552 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000553 Diags.setClient(PrimaryClient, false);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000554
555 // If we have a preprocessor, scan the source for expected diagnostic
556 // markers. If not then any diagnostics are unexpected.
557 if (CurrentPreprocessor) {
Axel Naumann01231612011-07-25 19:18:12 +0000558 SourceManager &SM = CurrentPreprocessor->getSourceManager();
Jordan Rose78541c42012-07-11 19:58:23 +0000559 // Only check for expectations in other diagnostic locations not
560 // captured during normal parsing.
561 // FIXME: This check is currently necessary while synthesized files may
562 // not have their expected-* directives captured during parsing. These
563 // cases should be fixed and the following loop replaced with one which
564 // checks only during a debug build and asserts on a mismatch.
565 for (FilesWithDiagnosticsSet::iterator I = FilesWithDiagnostics.begin(),
566 End = FilesWithDiagnostics.end();
567 I != End; ++I) {
568 const FileEntry *E = SM.getFileEntryForID(*I);
569 if (!E || !FilesWithDirectives.count(E)) {
570 if (E)
571 FilesWithDirectives.insert(E);
572 FindExpectedDiags(*CurrentPreprocessor, ED, *I);
573 }
Axel Naumann84c05e32011-08-24 13:36:19 +0000574 }
Daniel Dunbar221c7212009-11-14 07:53:24 +0000575
576 // Check that the expected diagnostics occurred.
Axel Naumann01231612011-07-25 19:18:12 +0000577 NumErrors += CheckResults(Diags, SM, *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000578 } else {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000579 NumErrors += (PrintUnexpected(Diags, 0, Buffer->err_begin(),
580 Buffer->err_end(), "error") +
581 PrintUnexpected(Diags, 0, Buffer->warn_begin(),
582 Buffer->warn_end(), "warn") +
583 PrintUnexpected(Diags, 0, Buffer->note_begin(),
584 Buffer->note_end(), "note"));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000585 }
586
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000587 Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000588 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000589
590 // Reset the buffer, we have processed all the diagnostics in it.
591 Buffer.reset(new TextDiagnosticBuffer());
Axel Naumanne445e5d2012-07-10 16:24:07 +0000592 ED.Errors.clear();
593 ED.Warnings.clear();
594 ED.Notes.clear();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000595}
Chris Lattner60909e12010-04-28 20:02:30 +0000596
Douglas Gregoraee526e2011-09-29 00:38:00 +0000597DiagnosticConsumer *
598VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
599 if (!Diags.getClient())
600 Diags.setClient(PrimaryClient->clone(Diags));
601
602 return new VerifyDiagnosticConsumer(Diags);
603}
604
Jordan Roseaa48fe82012-07-10 02:57:03 +0000605Directive *Directive::create(bool RegexKind, SourceLocation DirectiveLoc,
606 SourceLocation DiagnosticLoc, StringRef Text,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000607 unsigned Min, unsigned Max) {
Chris Lattner60909e12010-04-28 20:02:30 +0000608 if (RegexKind)
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000609 return new RegexDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
610 return new StandardDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
Chris Lattner60909e12010-04-28 20:02:30 +0000611}