blob: 51b351f4a923dca0ea233fd27749183dbcfc0243 [file] [log] [blame]
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +00001//===--- VerifyDiagnosticsClient.cpp - Verifying Diagnostic Client --------===//
2//
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
14#include "clang/Frontend/VerifyDiagnosticsClient.h"
15#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
Daniel Dunbar221c7212009-11-14 07:53:24 +000023VerifyDiagnosticsClient::VerifyDiagnosticsClient(Diagnostic &_Diags,
24 DiagnosticClient *_Primary)
25 : Diags(_Diags), PrimaryClient(_Primary),
Argyrios Kyrtzidisab41b972010-11-18 21:13:57 +000026 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000027}
28
29VerifyDiagnosticsClient::~VerifyDiagnosticsClient() {
30 CheckDiagnostics();
31}
32
33// DiagnosticClient interface.
34
35void VerifyDiagnosticsClient::BeginSourceFile(const LangOptions &LangOpts,
36 const Preprocessor *PP) {
37 // FIXME: Const hack, we screw up the preprocessor but in practice its ok
38 // because it doesn't get reused. It would be better if we could make a copy
39 // though.
40 CurrentPreprocessor = const_cast<Preprocessor*>(PP);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000041
42 PrimaryClient->BeginSourceFile(LangOpts, PP);
43}
44
45void VerifyDiagnosticsClient::EndSourceFile() {
46 CheckDiagnostics();
47
48 PrimaryClient->EndSourceFile();
49
50 CurrentPreprocessor = 0;
51}
Daniel Dunbar221c7212009-11-14 07:53:24 +000052
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000053void VerifyDiagnosticsClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
54 const DiagnosticInfo &Info) {
55 // Send the diagnostic to the buffer, we will check it once we reach the end
56 // of the source file (or are destructed).
57 Buffer->HandleDiagnostic(DiagLevel, Info);
58}
59
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000060//===----------------------------------------------------------------------===//
61// Checking diagnostics implementation.
62//===----------------------------------------------------------------------===//
63
64typedef TextDiagnosticBuffer::DiagList DiagList;
65typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
66
Chris Lattner60909e12010-04-28 20:02:30 +000067namespace {
68
69/// Directive - Abstract class representing a parsed verify directive.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000070///
Chris Lattner60909e12010-04-28 20:02:30 +000071class Directive {
72public:
73 static Directive* Create(bool RegexKind, const SourceLocation &Location,
74 const std::string &Text, unsigned Count);
75public:
76 SourceLocation Location;
77 const std::string Text;
78 unsigned Count;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000079
Chris Lattner60909e12010-04-28 20:02:30 +000080 virtual ~Directive() { }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000081
Chris Lattner60909e12010-04-28 20:02:30 +000082 // Returns true if directive text is valid.
83 // Otherwise returns false and populates E.
84 virtual bool isValid(std::string &Error) = 0;
85
86 // Returns true on match.
87 virtual bool Match(const std::string &S) = 0;
88
89protected:
90 Directive(const SourceLocation &Location, const std::string &Text,
91 unsigned Count)
92 : Location(Location), Text(Text), Count(Count) { }
93
94private:
Argyrios Kyrtzidisc83d2d72010-08-15 10:17:39 +000095 Directive(const Directive&); // DO NOT IMPLEMENT
96 void operator=(const Directive&); // DO NOT IMPLEMENT
Chris Lattner60909e12010-04-28 20:02:30 +000097};
98
99/// StandardDirective - Directive with string matching.
100///
101class StandardDirective : public Directive {
102public:
103 StandardDirective(const SourceLocation &Location, const std::string &Text,
104 unsigned Count)
105 : Directive(Location, Text, Count) { }
106
107 virtual bool isValid(std::string &Error) {
108 // all strings are considered valid; even empty ones
109 return true;
110 }
111
112 virtual bool Match(const std::string &S) {
113 return S.find(Text) != std::string::npos ||
114 Text.find(S) != std::string::npos;
115 }
116};
117
118/// RegexDirective - Directive with regular-expression matching.
119///
120class RegexDirective : public Directive {
121public:
122 RegexDirective(const SourceLocation &Location, const std::string &Text,
123 unsigned Count)
124 : Directive(Location, Text, Count), Regex(Text) { }
125
126 virtual bool isValid(std::string &Error) {
127 if (Regex.isValid(Error))
128 return true;
129 return false;
130 }
131
132 virtual bool Match(const std::string &S) {
133 return Regex.match(S);
134 }
135
136private:
137 llvm::Regex Regex;
138};
139
140typedef std::vector<Directive*> DirectiveList;
141
142/// ExpectedData - owns directive objects and deletes on destructor.
143///
144struct ExpectedData {
145 DirectiveList Errors;
146 DirectiveList Warnings;
147 DirectiveList Notes;
148
149 ~ExpectedData() {
150 DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 };
151 for (DirectiveList **PL = Lists; *PL; ++PL) {
152 DirectiveList * const L = *PL;
153 for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I)
154 delete *I;
155 }
156 }
157};
158
159class ParseHelper
160{
161public:
162 ParseHelper(const char *Begin, const char *End)
163 : Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { }
164
165 // Return true if string literal is next.
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000166 bool Next(llvm::StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000167 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000168 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000169 if (PEnd > End)
170 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000171 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-04-28 20:02:30 +0000172 }
173
174 // Return true if number is next.
175 // Output N only if number is next.
176 bool Next(unsigned &N) {
177 unsigned TMP = 0;
178 P = C;
179 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
180 TMP *= 10;
181 TMP += P[0] - '0';
182 }
183 if (P == C)
184 return false;
185 PEnd = P;
186 N = TMP;
187 return true;
188 }
189
190 // Return true if string literal is found.
191 // When true, P marks begin-position of S in content.
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000192 bool Search(llvm::StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000193 P = std::search(C, End, S.begin(), S.end());
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000194 PEnd = P + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000195 return P != End;
196 }
197
198 // Advance 1-past previous next/search.
199 // Behavior is undefined if previous next/search failed.
200 bool Advance() {
201 C = PEnd;
202 return C < End;
203 }
204
205 // Skip zero or more whitespace.
206 void SkipWhitespace() {
207 for (; C < End && isspace(*C); ++C)
208 ;
209 }
210
211 // Return true if EOF reached.
212 bool Done() {
213 return !(C < End);
214 }
215
216 const char * const Begin; // beginning of expected content
217 const char * const End; // end of expected content (1-past)
218 const char *C; // position of next char in content
219 const char *P;
220
221private:
222 const char *PEnd; // previous next/search subject end (1-past)
223};
224
225} // namespace anonymous
226
227/// ParseDirective - Go through the comment and see if it indicates expected
228/// diagnostics. If so, then put them in the appropriate directive list.
229///
230static void ParseDirective(const char *CommentStart, unsigned CommentLen,
231 ExpectedData &ED, Preprocessor &PP,
232 SourceLocation Pos) {
233 // A single comment may contain multiple directives.
234 for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) {
235 // search for token: expected
236 if (!PH.Search("expected"))
237 break;
238 PH.Advance();
239
240 // next token: -
241 if (!PH.Next("-"))
242 continue;
243 PH.Advance();
244
245 // next token: { error | warning | note }
246 DirectiveList* DL = NULL;
247 if (PH.Next("error"))
248 DL = &ED.Errors;
249 else if (PH.Next("warning"))
250 DL = &ED.Warnings;
251 else if (PH.Next("note"))
252 DL = &ED.Notes;
253 else
254 continue;
255 PH.Advance();
256
257 // default directive kind
258 bool RegexKind = false;
259 const char* KindStr = "string";
260
261 // next optional token: -
262 if (PH.Next("-re")) {
263 PH.Advance();
264 RegexKind = true;
265 KindStr = "regex";
266 }
267
268 // skip optional whitespace
269 PH.SkipWhitespace();
270
271 // next optional token: positive integer
272 unsigned Count = 1;
273 if (PH.Next(Count))
274 PH.Advance();
275
276 // skip optional whitespace
277 PH.SkipWhitespace();
278
279 // next token: {{
280 if (!PH.Next("{{")) {
281 PP.Diag(Pos.getFileLocWithOffset(PH.C-PH.Begin),
282 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000283 continue;
284 }
Chris Lattner60909e12010-04-28 20:02:30 +0000285 PH.Advance();
286 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000287
Chris Lattner60909e12010-04-28 20:02:30 +0000288 // search for token: }}
289 if (!PH.Search("}}")) {
290 PP.Diag(Pos.getFileLocWithOffset(PH.C-PH.Begin),
291 diag::err_verify_missing_end) << KindStr;
292 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000293 }
Chris Lattner60909e12010-04-28 20:02:30 +0000294 const char* const ContentEnd = PH.P; // mark content end
295 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000296
Chris Lattner60909e12010-04-28 20:02:30 +0000297 // build directive text; convert \n to newlines
298 std::string Text;
299 llvm::StringRef NewlineStr = "\\n";
300 llvm::StringRef Content(ContentBegin, ContentEnd-ContentBegin);
301 size_t CPos = 0;
302 size_t FPos;
303 while ((FPos = Content.find(NewlineStr, CPos)) != llvm::StringRef::npos) {
304 Text += Content.substr(CPos, FPos-CPos);
305 Text += '\n';
306 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000307 }
Chris Lattner60909e12010-04-28 20:02:30 +0000308 if (Text.empty())
309 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000310
Chris Lattner60909e12010-04-28 20:02:30 +0000311 // construct new directive
312 Directive *D = Directive::Create(RegexKind, Pos, Text, Count);
313 std::string Error;
314 if (D->isValid(Error))
315 DL->push_back(D);
316 else {
317 PP.Diag(Pos.getFileLocWithOffset(ContentBegin-PH.Begin),
318 diag::err_verify_invalid_content)
319 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000320 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000321 }
322}
323
324/// FindExpectedDiags - Lex the main source file to find all of the
325// expected errors and warnings.
Chris Lattner60909e12010-04-28 20:02:30 +0000326static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000327 // Create a raw lexer to pull all the comments out of the main file. We don't
328 // want to look in #include'd headers for expected-error strings.
Chris Lattner6e290142009-11-30 04:18:44 +0000329 SourceManager &SM = PP.getSourceManager();
330 FileID FID = SM.getMainFileID();
331 if (SM.getMainFileID().isInvalid())
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000332 return;
333
334 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000335 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
336 Lexer RawLex(FID, FromFile, SM, PP.getLangOptions());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000337
338 // Return comments as tokens, this is how we find expected diagnostics.
339 RawLex.SetCommentRetentionState(true);
340
341 Token Tok;
342 Tok.setKind(tok::comment);
343 while (Tok.isNot(tok::eof)) {
344 RawLex.Lex(Tok);
345 if (!Tok.is(tok::comment)) continue;
346
347 std::string Comment = PP.getSpelling(Tok);
348 if (Comment.empty()) continue;
349
Chris Lattner60909e12010-04-28 20:02:30 +0000350 // Find all expected errors/warnings/notes.
351 ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000352 };
353}
354
355/// PrintProblem - This takes a diagnostic map of the delta between expected and
356/// seen diagnostics. If there's anything in it, then something unexpected
357/// happened. Print the map out in a nice format and return "true". If the map
358/// is empty and we're not going to print things, then return "false".
359///
Daniel Dunbar221c7212009-11-14 07:53:24 +0000360static unsigned PrintProblem(Diagnostic &Diags, SourceManager *SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000361 const_diag_iterator diag_begin,
362 const_diag_iterator diag_end,
363 const char *Kind, bool Expected) {
364 if (diag_begin == diag_end) return 0;
365
366 llvm::SmallString<256> Fmt;
367 llvm::raw_svector_ostream OS(Fmt);
368 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000369 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000370 OS << "\n (frontend)";
371 else
Daniel Dunbar221c7212009-11-14 07:53:24 +0000372 OS << "\n Line " << SourceMgr->getInstantiationLineNumber(I->first);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000373 OS << ": " << I->second;
374 }
375
376 Diags.Report(diag::err_verify_inconsistent_diags)
Daniel Dunbar221c7212009-11-14 07:53:24 +0000377 << Kind << !Expected << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000378 return std::distance(diag_begin, diag_end);
379}
380
Chris Lattner60909e12010-04-28 20:02:30 +0000381static unsigned PrintProblem(Diagnostic &Diags, SourceManager *SourceMgr,
382 DirectiveList &DL, const char *Kind,
383 bool Expected) {
384 if (DL.empty())
385 return 0;
386
387 llvm::SmallString<256> Fmt;
388 llvm::raw_svector_ostream OS(Fmt);
389 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
390 Directive& D = **I;
391 if (D.Location.isInvalid() || !SourceMgr)
392 OS << "\n (frontend)";
393 else
394 OS << "\n Line " << SourceMgr->getInstantiationLineNumber(D.Location);
395 OS << ": " << D.Text;
396 }
397
398 Diags.Report(diag::err_verify_inconsistent_diags)
399 << Kind << !Expected << OS.str();
400 return DL.size();
401}
402
403/// CheckLists - Compare expected to seen diagnostic lists and return the
404/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000405///
Chris Lattner60909e12010-04-28 20:02:30 +0000406static unsigned CheckLists(Diagnostic &Diags, SourceManager &SourceMgr,
407 const char *Label,
408 DirectiveList &Left,
409 const_diag_iterator d2_begin,
410 const_diag_iterator d2_end) {
411 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000412 DiagList Right(d2_begin, d2_end);
413
Chris Lattner60909e12010-04-28 20:02:30 +0000414 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
415 Directive& D = **I;
416 unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(D.Location);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000417
Chris Lattner60909e12010-04-28 20:02:30 +0000418 for (unsigned i = 0; i < D.Count; ++i) {
419 DiagList::iterator II, IE;
420 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
421 unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
422 if (LineNo1 != LineNo2)
423 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000424
Chris Lattner60909e12010-04-28 20:02:30 +0000425 const std::string &RightText = II->second;
426 if (D.Match(RightText))
427 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000428 }
Chris Lattner60909e12010-04-28 20:02:30 +0000429 if (II == IE) {
430 // Not found.
431 LeftOnly.push_back(*I);
432 } else {
433 // Found. The same cannot be found twice.
434 Right.erase(II);
435 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000436 }
437 }
438 // Now all that's left in Right are those that were not matched.
439
Chris Lattner60909e12010-04-28 20:02:30 +0000440 return (PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true) +
441 PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(),
442 Label, false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000443}
444
445/// CheckResults - This compares the expected results to those that
446/// were actually reported. It emits any discrepencies. Return "true" if there
447/// were problems. Return "false" otherwise.
448///
449static unsigned CheckResults(Diagnostic &Diags, SourceManager &SourceMgr,
450 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000451 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000452 // We want to capture the delta between what was expected and what was
453 // seen.
454 //
455 // Expected \ Seen - set expected but not seen
456 // Seen \ Expected - set seen but not expected
457 unsigned NumProblems = 0;
458
459 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000460 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
461 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000462
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000463 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000464 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
465 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000466
467 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000468 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
469 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000470
471 return NumProblems;
472}
473
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000474void VerifyDiagnosticsClient::CheckDiagnostics() {
Chris Lattner60909e12010-04-28 20:02:30 +0000475 ExpectedData ED;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000476
477 // Ensure any diagnostics go to the primary client.
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000478 DiagnosticClient *CurClient = Diags.takeClient();
Daniel Dunbar221c7212009-11-14 07:53:24 +0000479 Diags.setClient(PrimaryClient.get());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000480
481 // If we have a preprocessor, scan the source for expected diagnostic
482 // markers. If not then any diagnostics are unexpected.
483 if (CurrentPreprocessor) {
Chris Lattner60909e12010-04-28 20:02:30 +0000484 FindExpectedDiags(*CurrentPreprocessor, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000485
486 // Check that the expected diagnostics occurred.
487 NumErrors += CheckResults(Diags, CurrentPreprocessor->getSourceManager(),
Chris Lattner60909e12010-04-28 20:02:30 +0000488 *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000489 } else {
490 NumErrors += (PrintProblem(Diags, 0,
491 Buffer->err_begin(), Buffer->err_end(),
492 "error", false) +
493 PrintProblem(Diags, 0,
494 Buffer->warn_begin(), Buffer->warn_end(),
495 "warn", false) +
496 PrintProblem(Diags, 0,
497 Buffer->note_begin(), Buffer->note_end(),
498 "note", false));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000499 }
500
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000501 Diags.takeClient();
Daniel Dunbar221c7212009-11-14 07:53:24 +0000502 Diags.setClient(CurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000503
504 // Reset the buffer, we have processed all the diagnostics in it.
505 Buffer.reset(new TextDiagnosticBuffer());
506}
Chris Lattner60909e12010-04-28 20:02:30 +0000507
508Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
509 const std::string &Text, unsigned Count) {
510 if (RegexKind)
511 return new RegexDirective(Location, Text, Count);
512 return new StandardDirective(Location, Text, Count);
513}