blob: e0acd42ab2d1f85703c36bb8604fd9ca57f6af24 [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"
Jordan Rose7c304f52012-08-10 01:06:16 +000018#include "clang/Lex/HeaderSearch.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000019#include "clang/Lex/Preprocessor.h"
20#include "llvm/ADT/SmallString.h"
Chris Lattner60909e12010-04-28 20:02:30 +000021#include "llvm/Support/Regex.h"
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000022#include "llvm/Support/raw_ostream.h"
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000023#include <cctype>
Anna Zaksc035e092011-12-15 02:58:00 +000024
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000025using namespace clang;
Jordan Rose4313c012012-07-10 02:56:15 +000026typedef VerifyDiagnosticConsumer::Directive Directive;
27typedef VerifyDiagnosticConsumer::DirectiveList DirectiveList;
28typedef VerifyDiagnosticConsumer::ExpectedData ExpectedData;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000029
David Blaikie621bc692011-09-26 00:38:03 +000030VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
Jordan Rose7c304f52012-08-10 01:06:16 +000031 : Diags(_Diags),
32 PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()),
33 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0),
Jordan Rose7eaaa182012-08-18 16:58:52 +000034 LangOpts(0), SrcManager(0), ActiveSourceFiles(0)
Douglas Gregor78243652011-09-13 01:26:44 +000035{
36 Diags.takeClient();
Jordan Rose7eaaa182012-08-18 16:58:52 +000037 if (Diags.hasSourceManager())
38 setSourceManager(Diags.getSourceManager());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000039}
40
David Blaikie621bc692011-09-26 00:38:03 +000041VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Jordan Rose7c304f52012-08-10 01:06:16 +000042 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
43 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
Jordan Rose7eaaa182012-08-18 16:58:52 +000044 SrcManager = 0;
Douglas Gregor78243652011-09-13 01:26:44 +000045 CheckDiagnostics();
46 Diags.takeClient();
47 if (OwnsPrimaryClient)
48 delete PrimaryClient;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000049}
50
Jordan Rose7c304f52012-08-10 01:06:16 +000051#ifndef NDEBUG
52namespace {
53class VerifyFileTracker : public PPCallbacks {
Jordan Rose7eaaa182012-08-18 16:58:52 +000054 VerifyDiagnosticConsumer &Verify;
Jordan Rose7c304f52012-08-10 01:06:16 +000055 SourceManager &SM;
56
57public:
Jordan Rose7eaaa182012-08-18 16:58:52 +000058 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
59 : Verify(Verify), SM(SM) { }
Jordan Rose7c304f52012-08-10 01:06:16 +000060
61 /// \brief Hook into the preprocessor and update the list of parsed
62 /// files when the preprocessor indicates a new file is entered.
63 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
64 SrcMgr::CharacteristicKind FileType,
65 FileID PrevFID) {
Jordan Rose7eaaa182012-08-18 16:58:52 +000066 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
67 VerifyDiagnosticConsumer::IsParsed);
Jordan Rose7c304f52012-08-10 01:06:16 +000068 }
69};
70} // End anonymous namespace.
71#endif
72
David Blaikie78ad0b92011-09-25 23:39:51 +000073// DiagnosticConsumer interface.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000074
David Blaikie621bc692011-09-26 00:38:03 +000075void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +000076 const Preprocessor *PP) {
Jordan Rose7c304f52012-08-10 01:06:16 +000077 // Attach comment handler on first invocation.
78 if (++ActiveSourceFiles == 1) {
79 if (PP) {
80 CurrentPreprocessor = PP;
Jordan Rose7eaaa182012-08-18 16:58:52 +000081 this->LangOpts = &LangOpts;
82 setSourceManager(PP->getSourceManager());
Jordan Rose7c304f52012-08-10 01:06:16 +000083 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
84#ifndef NDEBUG
Jordan Rose7eaaa182012-08-18 16:58:52 +000085 // Debug build tracks parsed files.
86 VerifyFileTracker *V = new VerifyFileTracker(*this, *SrcManager);
Jordan Rose7c304f52012-08-10 01:06:16 +000087 const_cast<Preprocessor*>(PP)->addPPCallbacks(V);
88#endif
89 }
90 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000091
Jordan Rose7c304f52012-08-10 01:06:16 +000092 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000093 PrimaryClient->BeginSourceFile(LangOpts, PP);
94}
95
David Blaikie621bc692011-09-26 00:38:03 +000096void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Rose7c304f52012-08-10 01:06:16 +000097 assert(ActiveSourceFiles && "No active source files!");
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +000098 PrimaryClient->EndSourceFile();
99
Jordan Rose7c304f52012-08-10 01:06:16 +0000100 // Detach comment handler once last active source file completed.
101 if (--ActiveSourceFiles == 0) {
102 if (CurrentPreprocessor)
103 const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this);
104
105 // Check diagnostics once last file completed.
106 CheckDiagnostics();
107 CurrentPreprocessor = 0;
Jordan Rose7eaaa182012-08-18 16:58:52 +0000108 LangOpts = 0;
Jordan Rose7c304f52012-08-10 01:06:16 +0000109 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000110}
Daniel Dunbar221c7212009-11-14 07:53:24 +0000111
David Blaikie621bc692011-09-26 00:38:03 +0000112void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikie40847cf2011-09-26 01:18:08 +0000113 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Jordan Rose7eaaa182012-08-18 16:58:52 +0000114 if (Info.hasSourceManager())
115 setSourceManager(Info.getSourceManager());
116
Jordan Rose7c304f52012-08-10 01:06:16 +0000117#ifndef NDEBUG
Jordan Rose7eaaa182012-08-18 16:58:52 +0000118 // Debug build tracks unparsed files for possible
119 // unparsed expected-* directives.
120 if (SrcManager) {
121 SourceLocation Loc = Info.getLocation();
122 if (Loc.isValid()) {
123 ParsedStatus PS = IsUnparsed;
124
125 Loc = SrcManager->getExpansionLoc(Loc);
126 FileID FID = SrcManager->getFileID(Loc);
127
128 const FileEntry *FE = SrcManager->getFileEntryForID(FID);
129 if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
130 // If the file is a modules header file it shall not be parsed
131 // for expected-* directives.
132 HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
133 if (HS.findModuleForHeader(FE))
134 PS = IsUnparsedNoDirectives;
135 }
136
137 UpdateParsedFileStatus(*SrcManager, FID, PS);
138 }
Axel Naumann01231612011-07-25 19:18:12 +0000139 }
Jordan Rose7c304f52012-08-10 01:06:16 +0000140#endif
Jordan Rose7eaaa182012-08-18 16:58:52 +0000141
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000142 // Send the diagnostic to the buffer, we will check it once we reach the end
143 // of the source file (or are destructed).
144 Buffer->HandleDiagnostic(DiagLevel, Info);
145}
146
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000147//===----------------------------------------------------------------------===//
148// Checking diagnostics implementation.
149//===----------------------------------------------------------------------===//
150
151typedef TextDiagnosticBuffer::DiagList DiagList;
152typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
153
Chris Lattner60909e12010-04-28 20:02:30 +0000154namespace {
155
Chris Lattner60909e12010-04-28 20:02:30 +0000156/// StandardDirective - Directive with string matching.
157///
158class StandardDirective : public Directive {
159public:
Jordan Roseaa48fe82012-07-10 02:57:03 +0000160 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000161 StringRef Text, unsigned Min, unsigned Max)
162 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max) { }
Chris Lattner60909e12010-04-28 20:02:30 +0000163
164 virtual bool isValid(std::string &Error) {
165 // all strings are considered valid; even empty ones
166 return true;
167 }
168
Jordan Rose4313c012012-07-10 02:56:15 +0000169 virtual bool match(StringRef S) {
170 return S.find(Text) != StringRef::npos;
Chris Lattner60909e12010-04-28 20:02:30 +0000171 }
172};
173
174/// RegexDirective - Directive with regular-expression matching.
175///
176class RegexDirective : public Directive {
177public:
Jordan Roseaa48fe82012-07-10 02:57:03 +0000178 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000179 StringRef Text, unsigned Min, unsigned Max)
180 : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max), Regex(Text) { }
Chris Lattner60909e12010-04-28 20:02:30 +0000181
182 virtual bool isValid(std::string &Error) {
183 if (Regex.isValid(Error))
184 return true;
185 return false;
186 }
187
Jordan Rose4313c012012-07-10 02:56:15 +0000188 virtual bool match(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000189 return Regex.match(S);
190 }
191
192private:
193 llvm::Regex Regex;
194};
195
Chris Lattner60909e12010-04-28 20:02:30 +0000196class ParseHelper
197{
198public:
Jordan Rose78541c42012-07-11 19:58:23 +0000199 ParseHelper(StringRef S)
200 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(NULL) { }
Chris Lattner60909e12010-04-28 20:02:30 +0000201
202 // Return true if string literal is next.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 bool Next(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000204 P = C;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000205 PEnd = C + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000206 if (PEnd > End)
207 return false;
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000208 return !memcmp(P, S.data(), S.size());
Chris Lattner60909e12010-04-28 20:02:30 +0000209 }
210
211 // Return true if number is next.
212 // Output N only if number is next.
213 bool Next(unsigned &N) {
214 unsigned TMP = 0;
215 P = C;
216 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
217 TMP *= 10;
218 TMP += P[0] - '0';
219 }
220 if (P == C)
221 return false;
222 PEnd = P;
223 N = TMP;
224 return true;
225 }
226
227 // Return true if string literal is found.
228 // When true, P marks begin-position of S in content.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229 bool Search(StringRef S) {
Chris Lattner60909e12010-04-28 20:02:30 +0000230 P = std::search(C, End, S.begin(), S.end());
Benjamin Kramer0080f0c2010-09-01 17:28:48 +0000231 PEnd = P + S.size();
Chris Lattner60909e12010-04-28 20:02:30 +0000232 return P != End;
233 }
234
235 // Advance 1-past previous next/search.
236 // Behavior is undefined if previous next/search failed.
237 bool Advance() {
238 C = PEnd;
239 return C < End;
240 }
241
242 // Skip zero or more whitespace.
243 void SkipWhitespace() {
244 for (; C < End && isspace(*C); ++C)
245 ;
246 }
247
248 // Return true if EOF reached.
249 bool Done() {
250 return !(C < End);
251 }
252
253 const char * const Begin; // beginning of expected content
254 const char * const End; // end of expected content (1-past)
255 const char *C; // position of next char in content
256 const char *P;
257
258private:
259 const char *PEnd; // previous next/search subject end (1-past)
260};
261
262} // namespace anonymous
263
264/// ParseDirective - Go through the comment and see if it indicates expected
265/// diagnostics. If so, then put them in the appropriate directive list.
266///
Jordan Rose78541c42012-07-11 19:58:23 +0000267/// Returns true if any valid directives were found.
Jordan Rose7c304f52012-08-10 01:06:16 +0000268static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Jordan Rose4313c012012-07-10 02:56:15 +0000269 SourceLocation Pos, DiagnosticsEngine &Diags) {
Chris Lattner60909e12010-04-28 20:02:30 +0000270 // A single comment may contain multiple directives.
Jordan Rose78541c42012-07-11 19:58:23 +0000271 bool FoundDirective = false;
272 for (ParseHelper PH(S); !PH.Done();) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000273 // Search for token: expected
Chris Lattner60909e12010-04-28 20:02:30 +0000274 if (!PH.Search("expected"))
275 break;
276 PH.Advance();
277
Jordan Roseaa48fe82012-07-10 02:57:03 +0000278 // Next token: -
Chris Lattner60909e12010-04-28 20:02:30 +0000279 if (!PH.Next("-"))
280 continue;
281 PH.Advance();
282
Jordan Roseaa48fe82012-07-10 02:57:03 +0000283 // Next token: { error | warning | note }
Chris Lattner60909e12010-04-28 20:02:30 +0000284 DirectiveList* DL = NULL;
285 if (PH.Next("error"))
Jordan Rose7c304f52012-08-10 01:06:16 +0000286 DL = ED ? &ED->Errors : NULL;
Chris Lattner60909e12010-04-28 20:02:30 +0000287 else if (PH.Next("warning"))
Jordan Rose7c304f52012-08-10 01:06:16 +0000288 DL = ED ? &ED->Warnings : NULL;
Chris Lattner60909e12010-04-28 20:02:30 +0000289 else if (PH.Next("note"))
Jordan Rose7c304f52012-08-10 01:06:16 +0000290 DL = ED ? &ED->Notes : NULL;
Chris Lattner60909e12010-04-28 20:02:30 +0000291 else
292 continue;
293 PH.Advance();
294
Jordan Rose7c304f52012-08-10 01:06:16 +0000295 // If a directive has been found but we're not interested
296 // in storing the directive information, return now.
297 if (!DL)
298 return true;
299
Jordan Roseaa48fe82012-07-10 02:57:03 +0000300 // Default directive kind.
Chris Lattner60909e12010-04-28 20:02:30 +0000301 bool RegexKind = false;
302 const char* KindStr = "string";
303
Jordan Roseaa48fe82012-07-10 02:57:03 +0000304 // Next optional token: -
Chris Lattner60909e12010-04-28 20:02:30 +0000305 if (PH.Next("-re")) {
306 PH.Advance();
307 RegexKind = true;
308 KindStr = "regex";
309 }
310
Jordan Roseaa48fe82012-07-10 02:57:03 +0000311 // Next optional token: @
312 SourceLocation ExpectedLoc;
313 if (!PH.Next("@")) {
314 ExpectedLoc = Pos;
315 } else {
316 PH.Advance();
317 unsigned Line = 0;
318 bool FoundPlus = PH.Next("+");
319 if (FoundPlus || PH.Next("-")) {
320 // Relative to current line.
321 PH.Advance();
322 bool Invalid = false;
323 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
324 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
325 if (FoundPlus) ExpectedLine += Line;
326 else ExpectedLine -= Line;
327 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
328 }
329 } else {
330 // Absolute line number.
331 if (PH.Next(Line) && Line > 0)
332 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
333 }
334
335 if (ExpectedLoc.isInvalid()) {
336 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
337 diag::err_verify_missing_line) << KindStr;
338 continue;
339 }
340 PH.Advance();
341 }
342
343 // Skip optional whitespace.
Chris Lattner60909e12010-04-28 20:02:30 +0000344 PH.SkipWhitespace();
345
Jordan Roseaa48fe82012-07-10 02:57:03 +0000346 // Next optional token: positive integer or a '+'.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000347 unsigned Min = 1;
348 unsigned Max = 1;
349 if (PH.Next(Min)) {
Chris Lattner60909e12010-04-28 20:02:30 +0000350 PH.Advance();
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000351 // A positive integer can be followed by a '+' meaning min
352 // or more, or by a '-' meaning a range from min to max.
353 if (PH.Next("+")) {
354 Max = Directive::MaxCount;
355 PH.Advance();
356 } else if (PH.Next("-")) {
357 PH.Advance();
358 if (!PH.Next(Max) || Max < Min) {
359 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
360 diag::err_verify_invalid_range) << KindStr;
361 continue;
362 }
363 PH.Advance();
364 } else {
365 Max = Min;
366 }
367 } else if (PH.Next("+")) {
368 // '+' on its own means "1 or more".
369 Max = Directive::MaxCount;
Anna Zaks2135ebb2011-12-15 02:28:16 +0000370 PH.Advance();
371 }
Chris Lattner60909e12010-04-28 20:02:30 +0000372
Jordan Roseaa48fe82012-07-10 02:57:03 +0000373 // Skip optional whitespace.
Chris Lattner60909e12010-04-28 20:02:30 +0000374 PH.SkipWhitespace();
375
Jordan Roseaa48fe82012-07-10 02:57:03 +0000376 // Next token: {{
Chris Lattner60909e12010-04-28 20:02:30 +0000377 if (!PH.Next("{{")) {
Jordan Rose4313c012012-07-10 02:56:15 +0000378 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
379 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000380 continue;
381 }
Chris Lattner60909e12010-04-28 20:02:30 +0000382 PH.Advance();
383 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000384
Jordan Roseaa48fe82012-07-10 02:57:03 +0000385 // Search for token: }}
Chris Lattner60909e12010-04-28 20:02:30 +0000386 if (!PH.Search("}}")) {
Jordan Rose4313c012012-07-10 02:56:15 +0000387 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
388 diag::err_verify_missing_end) << KindStr;
Chris Lattner60909e12010-04-28 20:02:30 +0000389 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000390 }
Chris Lattner60909e12010-04-28 20:02:30 +0000391 const char* const ContentEnd = PH.P; // mark content end
392 PH.Advance();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000393
Jordan Roseaa48fe82012-07-10 02:57:03 +0000394 // Build directive text; convert \n to newlines.
Chris Lattner60909e12010-04-28 20:02:30 +0000395 std::string Text;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000396 StringRef NewlineStr = "\\n";
397 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattner60909e12010-04-28 20:02:30 +0000398 size_t CPos = 0;
399 size_t FPos;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000400 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattner60909e12010-04-28 20:02:30 +0000401 Text += Content.substr(CPos, FPos-CPos);
402 Text += '\n';
403 CPos = FPos + NewlineStr.size();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000404 }
Chris Lattner60909e12010-04-28 20:02:30 +0000405 if (Text.empty())
406 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000407
Jordan Roseaa48fe82012-07-10 02:57:03 +0000408 // Construct new directive.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000409 Directive *D = Directive::create(RegexKind, Pos, ExpectedLoc, Text,
410 Min, Max);
Chris Lattner60909e12010-04-28 20:02:30 +0000411 std::string Error;
Jordan Rose78541c42012-07-11 19:58:23 +0000412 if (D->isValid(Error)) {
Chris Lattner60909e12010-04-28 20:02:30 +0000413 DL->push_back(D);
Jordan Rose78541c42012-07-11 19:58:23 +0000414 FoundDirective = true;
415 } else {
Jordan Rose4313c012012-07-10 02:56:15 +0000416 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
417 diag::err_verify_invalid_content)
Chris Lattner60909e12010-04-28 20:02:30 +0000418 << KindStr << Error;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000419 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000420 }
Jordan Rose78541c42012-07-11 19:58:23 +0000421
422 return FoundDirective;
423}
424
425/// HandleComment - Hook into the preprocessor and extract comments containing
426/// expected errors and warnings.
427bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
428 SourceRange Comment) {
429 SourceManager &SM = PP.getSourceManager();
430 SourceLocation CommentBegin = Comment.getBegin();
431
432 const char *CommentRaw = SM.getCharacterData(CommentBegin);
433 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
434
435 if (C.empty())
436 return false;
437
438 // Fold any "\<EOL>" sequences
439 size_t loc = C.find('\\');
440 if (loc == StringRef::npos) {
Jordan Rose7c304f52012-08-10 01:06:16 +0000441 ParseDirective(C, &ED, SM, CommentBegin, PP.getDiagnostics());
Jordan Rose78541c42012-07-11 19:58:23 +0000442 return false;
443 }
444
445 std::string C2;
446 C2.reserve(C.size());
447
448 for (size_t last = 0;; loc = C.find('\\', last)) {
449 if (loc == StringRef::npos || loc == C.size()) {
450 C2 += C.substr(last);
451 break;
452 }
453 C2 += C.substr(last, loc-last);
454 last = loc + 1;
455
456 if (C[last] == '\n' || C[last] == '\r') {
457 ++last;
458
459 // Escape \r\n or \n\r, but not \n\n.
460 if (last < C.size())
461 if (C[last] == '\n' || C[last] == '\r')
462 if (C[last] != C[last-1])
463 ++last;
464 } else {
465 // This was just a normal backslash.
466 C2 += '\\';
467 }
468 }
469
470 if (!C2.empty())
Jordan Rose7c304f52012-08-10 01:06:16 +0000471 ParseDirective(C2, &ED, SM, CommentBegin, PP.getDiagnostics());
Jordan Rose78541c42012-07-11 19:58:23 +0000472 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000473}
474
Jordan Rose7c304f52012-08-10 01:06:16 +0000475#ifndef NDEBUG
476/// \brief Lex the specified source file to determine whether it contains
477/// any expected-* directives. As a Lexer is used rather than a full-blown
478/// Preprocessor, directives inside skipped #if blocks will still be found.
479///
480/// \return true if any directives were found.
Jordan Rose7eaaa182012-08-18 16:58:52 +0000481static bool findDirectives(SourceManager &SM, FileID FID,
482 const LangOptions &LangOpts) {
Axel Naumann01231612011-07-25 19:18:12 +0000483 // Create a raw lexer to pull all the comments out of FID.
484 if (FID.isInvalid())
Jordan Rose7c304f52012-08-10 01:06:16 +0000485 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000486
487 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000488 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Jordan Rose7eaaa182012-08-18 16:58:52 +0000489 Lexer RawLex(FID, FromFile, SM, LangOpts);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000490
491 // Return comments as tokens, this is how we find expected diagnostics.
492 RawLex.SetCommentRetentionState(true);
493
494 Token Tok;
495 Tok.setKind(tok::comment);
496 while (Tok.isNot(tok::eof)) {
497 RawLex.Lex(Tok);
498 if (!Tok.is(tok::comment)) continue;
499
Jordan Rose7eaaa182012-08-18 16:58:52 +0000500 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000501 if (Comment.empty()) continue;
502
Jordan Rose7eaaa182012-08-18 16:58:52 +0000503 // Find first directive.
504 if (ParseDirective(Comment, 0, SM, Tok.getLocation(),
505 SM.getDiagnostics()))
506 return true;
Jordan Rose7c304f52012-08-10 01:06:16 +0000507 }
Jordan Rose7eaaa182012-08-18 16:58:52 +0000508 return false;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000509}
Jordan Rose7c304f52012-08-10 01:06:16 +0000510#endif // !NDEBUG
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000511
Jordan Roseaa48fe82012-07-10 02:57:03 +0000512/// \brief Takes a list of diagnostics that have been generated but not matched
513/// by an expected-* directive and produces a diagnostic to the user from this.
514static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
515 const_diag_iterator diag_begin,
516 const_diag_iterator diag_end,
517 const char *Kind) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000518 if (diag_begin == diag_end) return 0;
519
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000520 SmallString<256> Fmt;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000521 llvm::raw_svector_ostream OS(Fmt);
522 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000523 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000524 OS << "\n (frontend)";
525 else
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000526 OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000527 OS << ": " << I->second;
528 }
529
Jordan Rosec6d64a22012-07-11 16:50:36 +0000530 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Roseaa48fe82012-07-10 02:57:03 +0000531 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000532 return std::distance(diag_begin, diag_end);
533}
534
Jordan Roseaa48fe82012-07-10 02:57:03 +0000535/// \brief Takes a list of diagnostics that were expected to have been generated
536/// but were not and produces a diagnostic to the user from this.
537static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
538 DirectiveList &DL, const char *Kind) {
Chris Lattner60909e12010-04-28 20:02:30 +0000539 if (DL.empty())
540 return 0;
541
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000542 SmallString<256> Fmt;
Chris Lattner60909e12010-04-28 20:02:30 +0000543 llvm::raw_svector_ostream OS(Fmt);
544 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000545 Directive &D = **I;
546 OS << "\n Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
547 if (D.DirectiveLoc != D.DiagnosticLoc)
548 OS << " (directive at "
549 << SourceMgr.getFilename(D.DirectiveLoc) << ":"
550 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ")";
Chris Lattner60909e12010-04-28 20:02:30 +0000551 OS << ": " << D.Text;
552 }
553
Jordan Rosec6d64a22012-07-11 16:50:36 +0000554 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Roseaa48fe82012-07-10 02:57:03 +0000555 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattner60909e12010-04-28 20:02:30 +0000556 return DL.size();
557}
558
559/// CheckLists - Compare expected to seen diagnostic lists and return the
560/// the difference between them.
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000561///
David Blaikied6471f72011-09-25 23:23:43 +0000562static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattner60909e12010-04-28 20:02:30 +0000563 const char *Label,
564 DirectiveList &Left,
565 const_diag_iterator d2_begin,
566 const_diag_iterator d2_end) {
567 DirectiveList LeftOnly;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000568 DiagList Right(d2_begin, d2_end);
569
Chris Lattner60909e12010-04-28 20:02:30 +0000570 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
571 Directive& D = **I;
Jordan Roseaa48fe82012-07-10 02:57:03 +0000572 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000573
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000574 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattner60909e12010-04-28 20:02:30 +0000575 DiagList::iterator II, IE;
576 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000577 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
Chris Lattner60909e12010-04-28 20:02:30 +0000578 if (LineNo1 != LineNo2)
579 continue;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000580
Chris Lattner60909e12010-04-28 20:02:30 +0000581 const std::string &RightText = II->second;
Jordan Rose4313c012012-07-10 02:56:15 +0000582 if (D.match(RightText))
Chris Lattner60909e12010-04-28 20:02:30 +0000583 break;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000584 }
Chris Lattner60909e12010-04-28 20:02:30 +0000585 if (II == IE) {
586 // Not found.
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000587 if (i >= D.Min) break;
Chris Lattner60909e12010-04-28 20:02:30 +0000588 LeftOnly.push_back(*I);
589 } else {
590 // Found. The same cannot be found twice.
591 Right.erase(II);
592 }
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000593 }
594 }
595 // Now all that's left in Right are those that were not matched.
Jordan Roseaa48fe82012-07-10 02:57:03 +0000596 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
597 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumiad646842011-12-17 13:00:31 +0000598 return num;
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000599}
600
601/// CheckResults - This compares the expected results to those that
602/// were actually reported. It emits any discrepencies. Return "true" if there
603/// were problems. Return "false" otherwise.
604///
David Blaikied6471f72011-09-25 23:23:43 +0000605static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000606 const TextDiagnosticBuffer &Buffer,
Chris Lattner60909e12010-04-28 20:02:30 +0000607 ExpectedData &ED) {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000608 // We want to capture the delta between what was expected and what was
609 // seen.
610 //
611 // Expected \ Seen - set expected but not seen
612 // Seen \ Expected - set seen but not expected
613 unsigned NumProblems = 0;
614
615 // See if there are error mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000616 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
617 Buffer.err_begin(), Buffer.err_end());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000618
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000619 // See if there are warning mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000620 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
621 Buffer.warn_begin(), Buffer.warn_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000622
623 // See if there are note mismatches.
Chris Lattner60909e12010-04-28 20:02:30 +0000624 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
625 Buffer.note_begin(), Buffer.note_end());
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000626
627 return NumProblems;
628}
629
Jordan Rose7eaaa182012-08-18 16:58:52 +0000630void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
631 FileID FID,
632 ParsedStatus PS) {
633 // Check SourceManager hasn't changed.
634 setSourceManager(SM);
635
636#ifndef NDEBUG
637 if (FID.isInvalid())
638 return;
639
640 const FileEntry *FE = SM.getFileEntryForID(FID);
641
642 if (PS == IsParsed) {
643 // Move the FileID from the unparsed set to the parsed set.
644 UnparsedFiles.erase(FID);
645 ParsedFiles.insert(std::make_pair(FID, FE));
646 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
647 // Add the FileID to the unparsed set if we haven't seen it before.
648
649 // Check for directives.
650 bool FoundDirectives;
651 if (PS == IsUnparsedNoDirectives)
652 FoundDirectives = false;
653 else
654 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
655
656 // Add the FileID to the unparsed set.
657 UnparsedFiles.insert(std::make_pair(FID,
658 UnparsedFileStatus(FE, FoundDirectives)));
659 }
660#endif
661}
662
David Blaikie621bc692011-09-26 00:38:03 +0000663void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000664 // Ensure any diagnostics go to the primary client.
Douglas Gregor78243652011-09-13 01:26:44 +0000665 bool OwnsCurClient = Diags.ownsClient();
David Blaikie78ad0b92011-09-25 23:39:51 +0000666 DiagnosticConsumer *CurClient = Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000667 Diags.setClient(PrimaryClient, false);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000668
Jordan Rose7c304f52012-08-10 01:06:16 +0000669#ifndef NDEBUG
Jordan Rose7eaaa182012-08-18 16:58:52 +0000670 // In a debug build, scan through any files that may have been missed
671 // during parsing and issue a fatal error if directives are contained
672 // within these files. If a fatal error occurs, this suggests that
673 // this file is being parsed separately from the main file, in which
674 // case consider moving the directives to the correct place, if this
675 // is applicable.
676 if (UnparsedFiles.size() > 0) {
677 // Generate a cache of parsed FileEntry pointers for alias lookups.
678 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
679 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
680 End = ParsedFiles.end(); I != End; ++I) {
681 if (const FileEntry *FE = I->second)
682 ParsedFileCache.insert(FE);
683 }
684
685 // Iterate through list of unparsed files.
686 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
687 End = UnparsedFiles.end(); I != End; ++I) {
688 const UnparsedFileStatus &Status = I->second;
689 const FileEntry *FE = Status.getFile();
690
691 // Skip files that have been parsed via an alias.
692 if (FE && ParsedFileCache.count(FE))
Jordan Rose7c304f52012-08-10 01:06:16 +0000693 continue;
694
Jordan Rose7eaaa182012-08-18 16:58:52 +0000695 // Report a fatal error if this file contained directives.
696 if (Status.foundDirectives()) {
Jordan Rose7c304f52012-08-10 01:06:16 +0000697 llvm::report_fatal_error(Twine("-verify directives found after rather"
698 " than during normal parsing of ",
Jordan Rose7eaaa182012-08-18 16:58:52 +0000699 StringRef(FE ? FE->getName() : "(unknown)")));
700 }
Axel Naumann84c05e32011-08-24 13:36:19 +0000701 }
Daniel Dunbar221c7212009-11-14 07:53:24 +0000702
Jordan Rose7eaaa182012-08-18 16:58:52 +0000703 // UnparsedFiles has been processed now, so clear it.
704 UnparsedFiles.clear();
705 }
706#endif // !NDEBUG
707
708 if (SrcManager) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000709 // Check that the expected diagnostics occurred.
Jordan Rose7eaaa182012-08-18 16:58:52 +0000710 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
Daniel Dunbar221c7212009-11-14 07:53:24 +0000711 } else {
Jordan Roseaa48fe82012-07-10 02:57:03 +0000712 NumErrors += (PrintUnexpected(Diags, 0, Buffer->err_begin(),
713 Buffer->err_end(), "error") +
714 PrintUnexpected(Diags, 0, Buffer->warn_begin(),
715 Buffer->warn_end(), "warn") +
716 PrintUnexpected(Diags, 0, Buffer->note_begin(),
717 Buffer->note_end(), "note"));
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000718 }
719
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000720 Diags.takeClient();
Douglas Gregor78243652011-09-13 01:26:44 +0000721 Diags.setClient(CurClient, OwnsCurClient);
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000722
723 // Reset the buffer, we have processed all the diagnostics in it.
724 Buffer.reset(new TextDiagnosticBuffer());
Axel Naumanne445e5d2012-07-10 16:24:07 +0000725 ED.Errors.clear();
726 ED.Warnings.clear();
727 ED.Notes.clear();
Daniel Dunbar81f5a1e2009-11-14 03:23:19 +0000728}
Chris Lattner60909e12010-04-28 20:02:30 +0000729
Douglas Gregoraee526e2011-09-29 00:38:00 +0000730DiagnosticConsumer *
731VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
732 if (!Diags.getClient())
733 Diags.setClient(PrimaryClient->clone(Diags));
734
735 return new VerifyDiagnosticConsumer(Diags);
736}
737
Jordan Roseaa48fe82012-07-10 02:57:03 +0000738Directive *Directive::create(bool RegexKind, SourceLocation DirectiveLoc,
739 SourceLocation DiagnosticLoc, StringRef Text,
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000740 unsigned Min, unsigned Max) {
Chris Lattner60909e12010-04-28 20:02:30 +0000741 if (RegexKind)
Jordan Rose3b81b7d2012-07-10 02:57:26 +0000742 return new RegexDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
743 return new StandardDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max);
Chris Lattner60909e12010-04-28 20:02:30 +0000744}