blob: 57ea9a0f8be7515c26f6fe949126003ab0f858e3 [file] [log] [blame]
David Blaikie69609dc2011-09-26 00:38:03 +00001//===---- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ------===//
Daniel Dunbar34818552009-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 Blaikie69609dc2011-09-26 00:38:03 +000014#include "clang/Frontend/VerifyDiagnosticConsumer.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000015#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/FileManager.h"
Daniel Dunbar34818552009-11-14 03:23:19 +000017#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/TextDiagnosticBuffer.h"
Jordan Roseb00073d2012-08-10 01:06:16 +000019#include "clang/Lex/HeaderSearch.h"
Daniel Dunbar34818552009-11-14 03:23:19 +000020#include "clang/Lex/Preprocessor.h"
21#include "llvm/ADT/SmallString.h"
Chris Lattnere82411b2010-04-28 20:02:30 +000022#include "llvm/Support/Regex.h"
Daniel Dunbar34818552009-11-14 03:23:19 +000023#include "llvm/Support/raw_ostream.h"
Anna Zaks2c74eed2011-12-15 02:58:00 +000024
Daniel Dunbar34818552009-11-14 03:23:19 +000025using namespace clang;
Jordan Rose6dae7612012-07-10 02:56:15 +000026typedef VerifyDiagnosticConsumer::Directive Directive;
27typedef VerifyDiagnosticConsumer::DirectiveList DirectiveList;
28typedef VerifyDiagnosticConsumer::ExpectedData ExpectedData;
Daniel Dunbar34818552009-11-14 03:23:19 +000029
Justin Bognerba7add32014-10-16 06:00:55 +000030VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_)
31 : Diags(Diags_),
Alexander Kornienko41c247a2014-11-17 23:46:02 +000032 PrimaryClient(Diags.getClient()), PrimaryClientOwner(Diags.takeClient()),
Craig Topper49a27902014-05-22 04:46:25 +000033 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(nullptr),
34 LangOpts(nullptr), SrcManager(nullptr), ActiveSourceFiles(0),
35 Status(HasNoDirectives)
Douglas Gregor2b9b4642011-09-13 01:26:44 +000036{
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000037 if (Diags.hasSourceManager())
38 setSourceManager(Diags.getSourceManager());
Daniel Dunbar34818552009-11-14 03:23:19 +000039}
40
David Blaikie69609dc2011-09-26 00:38:03 +000041VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
Jordan Roseb00073d2012-08-10 01:06:16 +000042 assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
43 assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
Craig Topper49a27902014-05-22 04:46:25 +000044 SrcManager = nullptr;
Alexander Kornienko41c247a2014-11-17 23:46:02 +000045 CheckDiagnostics();
Chandler Carruth0349f262016-11-03 17:42:32 +000046 Diags.takeClient().reset();
Daniel Dunbar34818552009-11-14 03:23:19 +000047}
48
Jordan Roseb00073d2012-08-10 01:06:16 +000049#ifndef NDEBUG
50namespace {
51class VerifyFileTracker : public PPCallbacks {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000052 VerifyDiagnosticConsumer &Verify;
Jordan Roseb00073d2012-08-10 01:06:16 +000053 SourceManager &SM;
54
55public:
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000056 VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
57 : Verify(Verify), SM(SM) { }
Jordan Roseb00073d2012-08-10 01:06:16 +000058
59 /// \brief Hook into the preprocessor and update the list of parsed
60 /// files when the preprocessor indicates a new file is entered.
Alexander Kornienko34eb2072015-04-11 02:00:23 +000061 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
62 SrcMgr::CharacteristicKind FileType,
63 FileID PrevFID) override {
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000064 Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
65 VerifyDiagnosticConsumer::IsParsed);
Jordan Roseb00073d2012-08-10 01:06:16 +000066 }
67};
68} // End anonymous namespace.
69#endif
70
David Blaikiee2eefae2011-09-25 23:39:51 +000071// DiagnosticConsumer interface.
Daniel Dunbar34818552009-11-14 03:23:19 +000072
David Blaikie69609dc2011-09-26 00:38:03 +000073void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
Douglas Gregor32fbe312012-01-20 16:28:04 +000074 const Preprocessor *PP) {
Jordan Roseb00073d2012-08-10 01:06:16 +000075 // Attach comment handler on first invocation.
76 if (++ActiveSourceFiles == 1) {
77 if (PP) {
78 CurrentPreprocessor = PP;
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000079 this->LangOpts = &LangOpts;
80 setSourceManager(PP->getSourceManager());
Jordan Roseb00073d2012-08-10 01:06:16 +000081 const_cast<Preprocessor*>(PP)->addCommentHandler(this);
82#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +000083 // Debug build tracks parsed files.
Craig Topperb8a70532014-09-10 04:53:53 +000084 const_cast<Preprocessor*>(PP)->addPPCallbacks(
85 llvm::make_unique<VerifyFileTracker>(*this, *SrcManager));
Jordan Roseb00073d2012-08-10 01:06:16 +000086#endif
87 }
88 }
Daniel Dunbar34818552009-11-14 03:23:19 +000089
Jordan Roseb00073d2012-08-10 01:06:16 +000090 assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
Daniel Dunbar34818552009-11-14 03:23:19 +000091 PrimaryClient->BeginSourceFile(LangOpts, PP);
92}
93
David Blaikie69609dc2011-09-26 00:38:03 +000094void VerifyDiagnosticConsumer::EndSourceFile() {
Jordan Roseb00073d2012-08-10 01:06:16 +000095 assert(ActiveSourceFiles && "No active source files!");
Daniel Dunbar34818552009-11-14 03:23:19 +000096 PrimaryClient->EndSourceFile();
97
Jordan Roseb00073d2012-08-10 01:06:16 +000098 // Detach comment handler once last active source file completed.
99 if (--ActiveSourceFiles == 0) {
100 if (CurrentPreprocessor)
101 const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this);
102
103 // Check diagnostics once last file completed.
104 CheckDiagnostics();
Craig Topper49a27902014-05-22 04:46:25 +0000105 CurrentPreprocessor = nullptr;
106 LangOpts = nullptr;
Jordan Roseb00073d2012-08-10 01:06:16 +0000107 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000108}
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000109
David Blaikie69609dc2011-09-26 00:38:03 +0000110void VerifyDiagnosticConsumer::HandleDiagnostic(
David Blaikieb5784322011-09-26 01:18:08 +0000111 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
Douglas Gregor6b930962013-05-03 22:58:43 +0000112 if (Info.hasSourceManager()) {
113 // If this diagnostic is for a different source manager, ignore it.
114 if (SrcManager && &Info.getSourceManager() != SrcManager)
115 return;
116
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000117 setSourceManager(Info.getSourceManager());
Douglas Gregor6b930962013-05-03 22:58:43 +0000118 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000119
Jordan Roseb00073d2012-08-10 01:06:16 +0000120#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000121 // Debug build tracks unparsed files for possible
122 // unparsed expected-* directives.
123 if (SrcManager) {
124 SourceLocation Loc = Info.getLocation();
125 if (Loc.isValid()) {
126 ParsedStatus PS = IsUnparsed;
127
128 Loc = SrcManager->getExpansionLoc(Loc);
129 FileID FID = SrcManager->getFileID(Loc);
130
131 const FileEntry *FE = SrcManager->getFileEntryForID(FID);
132 if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
133 // If the file is a modules header file it shall not be parsed
134 // for expected-* directives.
135 HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
136 if (HS.findModuleForHeader(FE))
137 PS = IsUnparsedNoDirectives;
138 }
139
140 UpdateParsedFileStatus(*SrcManager, FID, PS);
141 }
Axel Naumannac50dcf2011-07-25 19:18:12 +0000142 }
Jordan Roseb00073d2012-08-10 01:06:16 +0000143#endif
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000144
Daniel Dunbar34818552009-11-14 03:23:19 +0000145 // Send the diagnostic to the buffer, we will check it once we reach the end
146 // of the source file (or are destructed).
147 Buffer->HandleDiagnostic(DiagLevel, Info);
148}
149
Daniel Dunbar34818552009-11-14 03:23:19 +0000150//===----------------------------------------------------------------------===//
151// Checking diagnostics implementation.
152//===----------------------------------------------------------------------===//
153
154typedef TextDiagnosticBuffer::DiagList DiagList;
155typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
156
Chris Lattnere82411b2010-04-28 20:02:30 +0000157namespace {
158
Chris Lattnere82411b2010-04-28 20:02:30 +0000159/// StandardDirective - Directive with string matching.
160///
161class StandardDirective : public Directive {
162public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000163 StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000164 bool MatchAnyLine, StringRef Text, unsigned Min,
165 unsigned Max)
166 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000167
Craig Topperafa7cb32014-03-13 06:07:04 +0000168 bool isValid(std::string &Error) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000169 // all strings are considered valid; even empty ones
170 return true;
171 }
172
Craig Topperafa7cb32014-03-13 06:07:04 +0000173 bool match(StringRef S) override {
Jordan Rose6dae7612012-07-10 02:56:15 +0000174 return S.find(Text) != StringRef::npos;
Chris Lattnere82411b2010-04-28 20:02:30 +0000175 }
176};
177
178/// RegexDirective - Directive with regular-expression matching.
179///
180class RegexDirective : public Directive {
181public:
Jordan Rosee1572eb2012-07-10 02:57:03 +0000182 RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000183 bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max,
184 StringRef RegexStr)
185 : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max),
186 Regex(RegexStr) { }
Chris Lattnere82411b2010-04-28 20:02:30 +0000187
Craig Topperafa7cb32014-03-13 06:07:04 +0000188 bool isValid(std::string &Error) override {
Alexander Kornienko5583e6f2015-12-28 15:15:16 +0000189 return Regex.isValid(Error);
Chris Lattnere82411b2010-04-28 20:02:30 +0000190 }
191
Craig Topperafa7cb32014-03-13 06:07:04 +0000192 bool match(StringRef S) override {
Chris Lattnere82411b2010-04-28 20:02:30 +0000193 return Regex.match(S);
194 }
195
196private:
197 llvm::Regex Regex;
198};
199
Chris Lattnere82411b2010-04-28 20:02:30 +0000200class ParseHelper
201{
202public:
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000203 ParseHelper(StringRef S)
Craig Topper49a27902014-05-22 04:46:25 +0000204 : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(nullptr) {}
Chris Lattnere82411b2010-04-28 20:02:30 +0000205
206 // Return true if string literal is next.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000207 bool Next(StringRef S) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000208 P = C;
Benjamin Kramer2bd4cee2010-09-01 17:28:48 +0000209 PEnd = C + S.size();
Chris Lattnere82411b2010-04-28 20:02:30 +0000210 if (PEnd > End)
211 return false;
Benjamin Kramer2bd4cee2010-09-01 17:28:48 +0000212 return !memcmp(P, S.data(), S.size());
Chris Lattnere82411b2010-04-28 20:02:30 +0000213 }
214
215 // Return true if number is next.
216 // Output N only if number is next.
217 bool Next(unsigned &N) {
218 unsigned TMP = 0;
219 P = C;
220 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
221 TMP *= 10;
222 TMP += P[0] - '0';
223 }
224 if (P == C)
225 return false;
226 PEnd = P;
227 N = TMP;
228 return true;
229 }
230
231 // Return true if string literal is found.
232 // When true, P marks begin-position of S in content.
Andy Gibbsac51de62012-10-19 12:36:49 +0000233 bool Search(StringRef S, bool EnsureStartOfWord = false) {
234 do {
235 P = std::search(C, End, S.begin(), S.end());
236 PEnd = P + S.size();
237 if (P == End)
238 break;
239 if (!EnsureStartOfWord
240 // Check if string literal starts a new word.
Jordan Rosea7d03842013-02-08 22:30:41 +0000241 || P == Begin || isWhitespace(P[-1])
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000242 // Or it could be preceded by the start of a comment.
Andy Gibbsac51de62012-10-19 12:36:49 +0000243 || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*')
244 && P[-2] == '/'))
245 return true;
246 // Otherwise, skip and search again.
247 } while (Advance());
248 return false;
Chris Lattnere82411b2010-04-28 20:02:30 +0000249 }
250
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000251 // Return true if a CloseBrace that closes the OpenBrace at the current nest
252 // level is found. When true, P marks begin-position of CloseBrace.
253 bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) {
254 unsigned Depth = 1;
255 P = C;
256 while (P < End) {
257 StringRef S(P, End - P);
258 if (S.startswith(OpenBrace)) {
259 ++Depth;
260 P += OpenBrace.size();
261 } else if (S.startswith(CloseBrace)) {
262 --Depth;
263 if (Depth == 0) {
264 PEnd = P + CloseBrace.size();
265 return true;
266 }
267 P += CloseBrace.size();
268 } else {
269 ++P;
270 }
271 }
272 return false;
273 }
274
Chris Lattnere82411b2010-04-28 20:02:30 +0000275 // Advance 1-past previous next/search.
276 // Behavior is undefined if previous next/search failed.
277 bool Advance() {
278 C = PEnd;
279 return C < End;
280 }
281
282 // Skip zero or more whitespace.
283 void SkipWhitespace() {
Jordan Rosea7d03842013-02-08 22:30:41 +0000284 for (; C < End && isWhitespace(*C); ++C)
Chris Lattnere82411b2010-04-28 20:02:30 +0000285 ;
286 }
287
288 // Return true if EOF reached.
289 bool Done() {
290 return !(C < End);
291 }
292
293 const char * const Begin; // beginning of expected content
294 const char * const End; // end of expected content (1-past)
295 const char *C; // position of next char in content
296 const char *P;
297
298private:
299 const char *PEnd; // previous next/search subject end (1-past)
300};
301
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000302} // namespace anonymous
Chris Lattnere82411b2010-04-28 20:02:30 +0000303
304/// ParseDirective - Go through the comment and see if it indicates expected
305/// diagnostics. If so, then put them in the appropriate directive list.
306///
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000307/// Returns true if any valid directives were found.
Jordan Roseb00073d2012-08-10 01:06:16 +0000308static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000309 Preprocessor *PP, SourceLocation Pos,
Andy Gibbs0fea0452012-10-19 12:49:32 +0000310 VerifyDiagnosticConsumer::DirectiveStatus &Status) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000311 DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
312
Chris Lattnere82411b2010-04-28 20:02:30 +0000313 // A single comment may contain multiple directives.
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000314 bool FoundDirective = false;
315 for (ParseHelper PH(S); !PH.Done();) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000316 // Search for token: expected
Andy Gibbsac51de62012-10-19 12:36:49 +0000317 if (!PH.Search("expected", true))
Chris Lattnere82411b2010-04-28 20:02:30 +0000318 break;
319 PH.Advance();
320
Jordan Rosee1572eb2012-07-10 02:57:03 +0000321 // Next token: -
Chris Lattnere82411b2010-04-28 20:02:30 +0000322 if (!PH.Next("-"))
323 continue;
324 PH.Advance();
325
Jordan Rosee1572eb2012-07-10 02:57:03 +0000326 // Next token: { error | warning | note }
Craig Topper49a27902014-05-22 04:46:25 +0000327 DirectiveList *DL = nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000328 if (PH.Next("error"))
Craig Topper49a27902014-05-22 04:46:25 +0000329 DL = ED ? &ED->Errors : nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000330 else if (PH.Next("warning"))
Craig Topper49a27902014-05-22 04:46:25 +0000331 DL = ED ? &ED->Warnings : nullptr;
Tobias Grosser86a85672014-05-01 14:06:01 +0000332 else if (PH.Next("remark"))
Craig Topper49a27902014-05-22 04:46:25 +0000333 DL = ED ? &ED->Remarks : nullptr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000334 else if (PH.Next("note"))
Craig Topper49a27902014-05-22 04:46:25 +0000335 DL = ED ? &ED->Notes : nullptr;
Andy Gibbs0fea0452012-10-19 12:49:32 +0000336 else if (PH.Next("no-diagnostics")) {
337 if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
338 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
339 << /*IsExpectedNoDiagnostics=*/true;
340 else
341 Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
342 continue;
343 } else
Chris Lattnere82411b2010-04-28 20:02:30 +0000344 continue;
345 PH.Advance();
346
Andy Gibbs0fea0452012-10-19 12:49:32 +0000347 if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
348 Diags.Report(Pos, diag::err_verify_invalid_no_diags)
349 << /*IsExpectedNoDiagnostics=*/false;
350 continue;
351 }
352 Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
353
Jordan Roseb00073d2012-08-10 01:06:16 +0000354 // If a directive has been found but we're not interested
355 // in storing the directive information, return now.
356 if (!DL)
357 return true;
358
Jordan Rosee1572eb2012-07-10 02:57:03 +0000359 // Default directive kind.
Alp Toker6ed72512013-12-14 01:07:05 +0000360 bool RegexKind = false;
Chris Lattnere82411b2010-04-28 20:02:30 +0000361 const char* KindStr = "string";
362
Alp Toker6ed72512013-12-14 01:07:05 +0000363 // Next optional token: -
364 if (PH.Next("-re")) {
365 PH.Advance();
366 RegexKind = true;
367 KindStr = "regex";
368 }
369
Jordan Rosee1572eb2012-07-10 02:57:03 +0000370 // Next optional token: @
371 SourceLocation ExpectedLoc;
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000372 bool MatchAnyLine = false;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000373 if (!PH.Next("@")) {
374 ExpectedLoc = Pos;
375 } else {
376 PH.Advance();
377 unsigned Line = 0;
378 bool FoundPlus = PH.Next("+");
379 if (FoundPlus || PH.Next("-")) {
380 // Relative to current line.
381 PH.Advance();
382 bool Invalid = false;
383 unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
384 if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
385 if (FoundPlus) ExpectedLine += Line;
386 else ExpectedLine -= Line;
387 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
388 }
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000389 } else if (PH.Next(Line)) {
Jordan Rosee1572eb2012-07-10 02:57:03 +0000390 // Absolute line number.
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000391 if (Line > 0)
Jordan Rosee1572eb2012-07-10 02:57:03 +0000392 ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000393 } else if (PP && PH.Search(":")) {
394 // Specific source file.
395 StringRef Filename(PH.C, PH.P-PH.C);
396 PH.Advance();
397
398 // Lookup file via Preprocessor, like a #include.
399 const DirectoryLookup *CurDir;
Richard Smith25d50752014-10-20 00:15:49 +0000400 const FileEntry *FE =
401 PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
402 nullptr, nullptr, nullptr);
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000403 if (!FE) {
404 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
405 diag::err_verify_missing_file) << Filename << KindStr;
406 continue;
407 }
408
409 if (SM.translateFile(FE).isInvalid())
410 SM.createFileID(FE, Pos, SrcMgr::C_User);
411
412 if (PH.Next(Line) && Line > 0)
413 ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000414 else if (PH.Next("*")) {
415 MatchAnyLine = true;
416 ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
417 }
Jordan Rosee1572eb2012-07-10 02:57:03 +0000418 }
419
420 if (ExpectedLoc.isInvalid()) {
421 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
422 diag::err_verify_missing_line) << KindStr;
423 continue;
424 }
425 PH.Advance();
426 }
427
428 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000429 PH.SkipWhitespace();
430
Jordan Rosee1572eb2012-07-10 02:57:03 +0000431 // Next optional token: positive integer or a '+'.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000432 unsigned Min = 1;
433 unsigned Max = 1;
434 if (PH.Next(Min)) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000435 PH.Advance();
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000436 // A positive integer can be followed by a '+' meaning min
437 // or more, or by a '-' meaning a range from min to max.
438 if (PH.Next("+")) {
439 Max = Directive::MaxCount;
440 PH.Advance();
441 } else if (PH.Next("-")) {
442 PH.Advance();
443 if (!PH.Next(Max) || Max < Min) {
444 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
445 diag::err_verify_invalid_range) << KindStr;
446 continue;
447 }
448 PH.Advance();
449 } else {
450 Max = Min;
451 }
452 } else if (PH.Next("+")) {
453 // '+' on its own means "1 or more".
454 Max = Directive::MaxCount;
Anna Zaksa2510072011-12-15 02:28:16 +0000455 PH.Advance();
456 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000457
Jordan Rosee1572eb2012-07-10 02:57:03 +0000458 // Skip optional whitespace.
Chris Lattnere82411b2010-04-28 20:02:30 +0000459 PH.SkipWhitespace();
460
Jordan Rosee1572eb2012-07-10 02:57:03 +0000461 // Next token: {{
Chris Lattnere82411b2010-04-28 20:02:30 +0000462 if (!PH.Next("{{")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000463 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
464 diag::err_verify_missing_start) << KindStr;
Daniel Dunbar34818552009-11-14 03:23:19 +0000465 continue;
466 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000467 PH.Advance();
468 const char* const ContentBegin = PH.C; // mark content begin
Daniel Dunbar34818552009-11-14 03:23:19 +0000469
Jordan Rosee1572eb2012-07-10 02:57:03 +0000470 // Search for token: }}
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000471 if (!PH.SearchClosingBrace("{{", "}}")) {
Jordan Rose6dae7612012-07-10 02:56:15 +0000472 Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
473 diag::err_verify_missing_end) << KindStr;
Chris Lattnere82411b2010-04-28 20:02:30 +0000474 continue;
Daniel Dunbar34818552009-11-14 03:23:19 +0000475 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000476 const char* const ContentEnd = PH.P; // mark content end
477 PH.Advance();
Daniel Dunbar34818552009-11-14 03:23:19 +0000478
Jordan Rosee1572eb2012-07-10 02:57:03 +0000479 // Build directive text; convert \n to newlines.
Chris Lattnere82411b2010-04-28 20:02:30 +0000480 std::string Text;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000481 StringRef NewlineStr = "\\n";
482 StringRef Content(ContentBegin, ContentEnd-ContentBegin);
Chris Lattnere82411b2010-04-28 20:02:30 +0000483 size_t CPos = 0;
484 size_t FPos;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000485 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000486 Text += Content.substr(CPos, FPos-CPos);
487 Text += '\n';
488 CPos = FPos + NewlineStr.size();
Daniel Dunbar34818552009-11-14 03:23:19 +0000489 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000490 if (Text.empty())
491 Text.assign(ContentBegin, ContentEnd);
Daniel Dunbar34818552009-11-14 03:23:19 +0000492
Alp Toker6ed72512013-12-14 01:07:05 +0000493 // Check that regex directives contain at least one regex.
494 if (RegexKind && Text.find("{{") == StringRef::npos) {
495 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
496 diag::err_verify_missing_regex) << Text;
497 return false;
498 }
499
Jordan Rosee1572eb2012-07-10 02:57:03 +0000500 // Construct new directive.
David Blaikie93106512014-08-29 16:30:23 +0000501 std::unique_ptr<Directive> D = Directive::create(
502 RegexKind, Pos, ExpectedLoc, MatchAnyLine, Text, Min, Max);
Nico Weber568dacc2014-04-24 05:32:03 +0000503
Chris Lattnere82411b2010-04-28 20:02:30 +0000504 std::string Error;
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000505 if (D->isValid(Error)) {
David Blaikie93106512014-08-29 16:30:23 +0000506 DL->push_back(std::move(D));
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000507 FoundDirective = true;
508 } else {
Jordan Rose6dae7612012-07-10 02:56:15 +0000509 Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin),
510 diag::err_verify_invalid_content)
Chris Lattnere82411b2010-04-28 20:02:30 +0000511 << KindStr << Error;
Daniel Dunbar34818552009-11-14 03:23:19 +0000512 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000513 }
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000514
515 return FoundDirective;
516}
517
518/// HandleComment - Hook into the preprocessor and extract comments containing
519/// expected errors and warnings.
520bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
521 SourceRange Comment) {
522 SourceManager &SM = PP.getSourceManager();
Douglas Gregor6b930962013-05-03 22:58:43 +0000523
524 // If this comment is for a different source manager, ignore it.
525 if (SrcManager && &SM != SrcManager)
526 return false;
527
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000528 SourceLocation CommentBegin = Comment.getBegin();
529
530 const char *CommentRaw = SM.getCharacterData(CommentBegin);
531 StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
532
533 if (C.empty())
534 return false;
535
536 // Fold any "\<EOL>" sequences
537 size_t loc = C.find('\\');
538 if (loc == StringRef::npos) {
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000539 ParseDirective(C, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000540 return false;
541 }
542
543 std::string C2;
544 C2.reserve(C.size());
545
546 for (size_t last = 0;; loc = C.find('\\', last)) {
547 if (loc == StringRef::npos || loc == C.size()) {
548 C2 += C.substr(last);
549 break;
550 }
551 C2 += C.substr(last, loc-last);
552 last = loc + 1;
553
554 if (C[last] == '\n' || C[last] == '\r') {
555 ++last;
556
557 // Escape \r\n or \n\r, but not \n\n.
558 if (last < C.size())
559 if (C[last] == '\n' || C[last] == '\r')
560 if (C[last] != C[last-1])
561 ++last;
562 } else {
563 // This was just a normal backslash.
564 C2 += '\\';
565 }
566 }
567
568 if (!C2.empty())
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000569 ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status);
Jordan Roseb13eb8d2012-07-11 19:58:23 +0000570 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000571}
572
Jordan Roseb00073d2012-08-10 01:06:16 +0000573#ifndef NDEBUG
574/// \brief Lex the specified source file to determine whether it contains
575/// any expected-* directives. As a Lexer is used rather than a full-blown
576/// Preprocessor, directives inside skipped #if blocks will still be found.
577///
578/// \return true if any directives were found.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000579static bool findDirectives(SourceManager &SM, FileID FID,
580 const LangOptions &LangOpts) {
Axel Naumannac50dcf2011-07-25 19:18:12 +0000581 // Create a raw lexer to pull all the comments out of FID.
582 if (FID.isInvalid())
Jordan Roseb00073d2012-08-10 01:06:16 +0000583 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000584
585 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner710bb872009-11-30 04:18:44 +0000586 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000587 Lexer RawLex(FID, FromFile, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000588
589 // Return comments as tokens, this is how we find expected diagnostics.
590 RawLex.SetCommentRetentionState(true);
591
592 Token Tok;
593 Tok.setKind(tok::comment);
Andy Gibbs0fea0452012-10-19 12:49:32 +0000594 VerifyDiagnosticConsumer::DirectiveStatus Status =
595 VerifyDiagnosticConsumer::HasNoDirectives;
Daniel Dunbar34818552009-11-14 03:23:19 +0000596 while (Tok.isNot(tok::eof)) {
Eli Friedman0834a4b2013-09-19 00:41:32 +0000597 RawLex.LexFromRawLexer(Tok);
Daniel Dunbar34818552009-11-14 03:23:19 +0000598 if (!Tok.is(tok::comment)) continue;
599
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000600 std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
Daniel Dunbar34818552009-11-14 03:23:19 +0000601 if (Comment.empty()) continue;
602
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000603 // Find first directive.
Craig Topper49a27902014-05-22 04:46:25 +0000604 if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
605 Status))
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000606 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000607 }
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000608 return false;
Daniel Dunbar34818552009-11-14 03:23:19 +0000609}
Jordan Roseb00073d2012-08-10 01:06:16 +0000610#endif // !NDEBUG
Daniel Dunbar34818552009-11-14 03:23:19 +0000611
Jordan Rosee1572eb2012-07-10 02:57:03 +0000612/// \brief Takes a list of diagnostics that have been generated but not matched
613/// by an expected-* directive and produces a diagnostic to the user from this.
614static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
615 const_diag_iterator diag_begin,
616 const_diag_iterator diag_end,
617 const char *Kind) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000618 if (diag_begin == diag_end) return 0;
619
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000620 SmallString<256> Fmt;
Daniel Dunbar34818552009-11-14 03:23:19 +0000621 llvm::raw_svector_ostream OS(Fmt);
622 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000623 if (I->first.isInvalid() || !SourceMgr)
Daniel Dunbar34818552009-11-14 03:23:19 +0000624 OS << "\n (frontend)";
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000625 else {
626 OS << "\n ";
627 if (const FileEntry *File = SourceMgr->getFileEntryForID(
628 SourceMgr->getFileID(I->first)))
629 OS << " File " << File->getName();
630 OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
631 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000632 OS << ": " << I->second;
633 }
634
Jordan Rose6f524ac2012-07-11 16:50:36 +0000635 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000636 << Kind << /*Unexpected=*/true << OS.str();
Daniel Dunbar34818552009-11-14 03:23:19 +0000637 return std::distance(diag_begin, diag_end);
638}
639
Jordan Rosee1572eb2012-07-10 02:57:03 +0000640/// \brief Takes a list of diagnostics that were expected to have been generated
641/// but were not and produces a diagnostic to the user from this.
David Blaikie93106512014-08-29 16:30:23 +0000642static unsigned PrintExpected(DiagnosticsEngine &Diags,
643 SourceManager &SourceMgr,
644 std::vector<Directive *> &DL, const char *Kind) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000645 if (DL.empty())
646 return 0;
647
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000648 SmallString<256> Fmt;
Chris Lattnere82411b2010-04-28 20:02:30 +0000649 llvm::raw_svector_ostream OS(Fmt);
David Blaikie93106512014-08-29 16:30:23 +0000650 for (auto *DirPtr : DL) {
651 Directive &D = *DirPtr;
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000652 OS << "\n File " << SourceMgr.getFilename(D.DiagnosticLoc);
653 if (D.MatchAnyLine)
654 OS << " Line *";
655 else
656 OS << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Jordan Rosee1572eb2012-07-10 02:57:03 +0000657 if (D.DirectiveLoc != D.DiagnosticLoc)
658 OS << " (directive at "
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000659 << SourceMgr.getFilename(D.DirectiveLoc) << ':'
660 << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
Chris Lattnere82411b2010-04-28 20:02:30 +0000661 OS << ": " << D.Text;
662 }
663
Jordan Rose6f524ac2012-07-11 16:50:36 +0000664 Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
Jordan Rosee1572eb2012-07-10 02:57:03 +0000665 << Kind << /*Unexpected=*/false << OS.str();
Chris Lattnere82411b2010-04-28 20:02:30 +0000666 return DL.size();
667}
668
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000669/// \brief Determine whether two source locations come from the same file.
670static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
671 SourceLocation DiagnosticLoc) {
672 while (DiagnosticLoc.isMacroID())
673 DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
674
Eli Friedman5ba37d52013-08-22 00:27:10 +0000675 if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000676 return true;
677
678 const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
Eli Friedman5ba37d52013-08-22 00:27:10 +0000679 if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000680 return true;
681
682 return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
683}
684
Chris Lattnere82411b2010-04-28 20:02:30 +0000685/// CheckLists - Compare expected to seen diagnostic lists and return the
686/// the difference between them.
Daniel Dunbar34818552009-11-14 03:23:19 +0000687///
David Blaikie9c902b52011-09-25 23:23:43 +0000688static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Chris Lattnere82411b2010-04-28 20:02:30 +0000689 const char *Label,
690 DirectiveList &Left,
691 const_diag_iterator d2_begin,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000692 const_diag_iterator d2_end,
693 bool IgnoreUnexpected) {
David Blaikie93106512014-08-29 16:30:23 +0000694 std::vector<Directive *> LeftOnly;
Daniel Dunbar34818552009-11-14 03:23:19 +0000695 DiagList Right(d2_begin, d2_end);
696
David Blaikie93106512014-08-29 16:30:23 +0000697 for (auto &Owner : Left) {
698 Directive &D = *Owner;
Jordan Rosee1572eb2012-07-10 02:57:03 +0000699 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
Daniel Dunbar34818552009-11-14 03:23:19 +0000700
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000701 for (unsigned i = 0; i < D.Max; ++i) {
Chris Lattnere82411b2010-04-28 20:02:30 +0000702 DiagList::iterator II, IE;
703 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Andy Gibbsc1f152e2014-07-10 16:43:29 +0000704 if (!D.MatchAnyLine) {
705 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
706 if (LineNo1 != LineNo2)
707 continue;
708 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000709
Andy Gibbsfcc699a2013-04-17 08:06:46 +0000710 if (!IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
711 continue;
712
Chris Lattnere82411b2010-04-28 20:02:30 +0000713 const std::string &RightText = II->second;
Jordan Rose6dae7612012-07-10 02:56:15 +0000714 if (D.match(RightText))
Chris Lattnere82411b2010-04-28 20:02:30 +0000715 break;
Daniel Dunbar34818552009-11-14 03:23:19 +0000716 }
Chris Lattnere82411b2010-04-28 20:02:30 +0000717 if (II == IE) {
718 // Not found.
Jordan Roseb8b2ca62012-07-10 02:57:26 +0000719 if (i >= D.Min) break;
David Blaikie93106512014-08-29 16:30:23 +0000720 LeftOnly.push_back(&D);
Chris Lattnere82411b2010-04-28 20:02:30 +0000721 } else {
722 // Found. The same cannot be found twice.
723 Right.erase(II);
724 }
Daniel Dunbar34818552009-11-14 03:23:19 +0000725 }
726 }
727 // Now all that's left in Right are those that were not matched.
Jordan Rosee1572eb2012-07-10 02:57:03 +0000728 unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
Eric Fiselier098e6de2015-06-13 07:11:40 +0000729 if (!IgnoreUnexpected)
730 num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
NAKAMURA Takumi7bfba2f2011-12-17 13:00:31 +0000731 return num;
Daniel Dunbar34818552009-11-14 03:23:19 +0000732}
733
734/// CheckResults - This compares the expected results to those that
735/// were actually reported. It emits any discrepencies. Return "true" if there
736/// were problems. Return "false" otherwise.
737///
David Blaikie9c902b52011-09-25 23:23:43 +0000738static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Daniel Dunbar34818552009-11-14 03:23:19 +0000739 const TextDiagnosticBuffer &Buffer,
Chris Lattnere82411b2010-04-28 20:02:30 +0000740 ExpectedData &ED) {
Daniel Dunbar34818552009-11-14 03:23:19 +0000741 // We want to capture the delta between what was expected and what was
742 // seen.
743 //
744 // Expected \ Seen - set expected but not seen
745 // Seen \ Expected - set seen but not expected
746 unsigned NumProblems = 0;
747
Eric Fiselier098e6de2015-06-13 07:11:40 +0000748 const DiagnosticLevelMask DiagMask =
749 Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
750
Daniel Dunbar34818552009-11-14 03:23:19 +0000751 // See if there are error mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000752 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000753 Buffer.err_begin(), Buffer.err_end(),
754 bool(DiagnosticLevelMask::Error & DiagMask));
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000755
Daniel Dunbar34818552009-11-14 03:23:19 +0000756 // See if there are warning mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000757 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000758 Buffer.warn_begin(), Buffer.warn_end(),
759 bool(DiagnosticLevelMask::Warning & DiagMask));
Daniel Dunbar34818552009-11-14 03:23:19 +0000760
Tobias Grosser86a85672014-05-01 14:06:01 +0000761 // See if there are remark mismatches.
762 NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000763 Buffer.remark_begin(), Buffer.remark_end(),
764 bool(DiagnosticLevelMask::Remark & DiagMask));
Tobias Grosser86a85672014-05-01 14:06:01 +0000765
Daniel Dunbar34818552009-11-14 03:23:19 +0000766 // See if there are note mismatches.
Chris Lattnere82411b2010-04-28 20:02:30 +0000767 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
Eric Fiselier098e6de2015-06-13 07:11:40 +0000768 Buffer.note_begin(), Buffer.note_end(),
769 bool(DiagnosticLevelMask::Note & DiagMask));
Daniel Dunbar34818552009-11-14 03:23:19 +0000770
771 return NumProblems;
772}
773
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000774void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
775 FileID FID,
776 ParsedStatus PS) {
777 // Check SourceManager hasn't changed.
778 setSourceManager(SM);
779
780#ifndef NDEBUG
781 if (FID.isInvalid())
782 return;
783
784 const FileEntry *FE = SM.getFileEntryForID(FID);
785
786 if (PS == IsParsed) {
787 // Move the FileID from the unparsed set to the parsed set.
788 UnparsedFiles.erase(FID);
789 ParsedFiles.insert(std::make_pair(FID, FE));
790 } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
791 // Add the FileID to the unparsed set if we haven't seen it before.
792
793 // Check for directives.
794 bool FoundDirectives;
795 if (PS == IsUnparsedNoDirectives)
796 FoundDirectives = false;
797 else
798 FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
799
800 // Add the FileID to the unparsed set.
801 UnparsedFiles.insert(std::make_pair(FID,
802 UnparsedFileStatus(FE, FoundDirectives)));
803 }
804#endif
805}
806
David Blaikie69609dc2011-09-26 00:38:03 +0000807void VerifyDiagnosticConsumer::CheckDiagnostics() {
Daniel Dunbar34818552009-11-14 03:23:19 +0000808 // Ensure any diagnostics go to the primary client.
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000809 DiagnosticConsumer *CurClient = Diags.getClient();
810 std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient();
Douglas Gregor2b9b4642011-09-13 01:26:44 +0000811 Diags.setClient(PrimaryClient, false);
Daniel Dunbar34818552009-11-14 03:23:19 +0000812
Jordan Roseb00073d2012-08-10 01:06:16 +0000813#ifndef NDEBUG
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000814 // In a debug build, scan through any files that may have been missed
815 // during parsing and issue a fatal error if directives are contained
816 // within these files. If a fatal error occurs, this suggests that
817 // this file is being parsed separately from the main file, in which
818 // case consider moving the directives to the correct place, if this
819 // is applicable.
820 if (UnparsedFiles.size() > 0) {
821 // Generate a cache of parsed FileEntry pointers for alias lookups.
822 llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
823 for (ParsedFilesMap::iterator I = ParsedFiles.begin(),
824 End = ParsedFiles.end(); I != End; ++I) {
825 if (const FileEntry *FE = I->second)
826 ParsedFileCache.insert(FE);
827 }
828
829 // Iterate through list of unparsed files.
830 for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(),
831 End = UnparsedFiles.end(); I != End; ++I) {
832 const UnparsedFileStatus &Status = I->second;
833 const FileEntry *FE = Status.getFile();
834
835 // Skip files that have been parsed via an alias.
836 if (FE && ParsedFileCache.count(FE))
Jordan Roseb00073d2012-08-10 01:06:16 +0000837 continue;
838
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000839 // Report a fatal error if this file contained directives.
840 if (Status.foundDirectives()) {
Jordan Roseb00073d2012-08-10 01:06:16 +0000841 llvm::report_fatal_error(Twine("-verify directives found after rather"
842 " than during normal parsing of ",
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000843 StringRef(FE ? FE->getName() : "(unknown)")));
844 }
Axel Naumann744f1212011-08-24 13:36:19 +0000845 }
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000846
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000847 // UnparsedFiles has been processed now, so clear it.
848 UnparsedFiles.clear();
849 }
850#endif // !NDEBUG
851
852 if (SrcManager) {
Andy Gibbs0fea0452012-10-19 12:49:32 +0000853 // Produce an error if no expected-* directives could be found in the
854 // source file(s) processed.
855 if (Status == HasNoDirectives) {
856 Diags.Report(diag::err_verify_no_directives).setForceEmit();
857 ++NumErrors;
858 Status = HasNoDirectivesReported;
859 }
860
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000861 // Check that the expected diagnostics occurred.
Jordan Rose8c1ac0c2012-08-18 16:58:52 +0000862 NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000863 } else {
Eric Fiselier098e6de2015-06-13 07:11:40 +0000864 const DiagnosticLevelMask DiagMask =
865 ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
866 if (bool(DiagnosticLevelMask::Error & DiagMask))
867 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
868 Buffer->err_end(), "error");
869 if (bool(DiagnosticLevelMask::Warning & DiagMask))
870 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
871 Buffer->warn_end(), "warn");
872 if (bool(DiagnosticLevelMask::Remark & DiagMask))
873 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(),
874 Buffer->remark_end(), "remark");
875 if (bool(DiagnosticLevelMask::Note & DiagMask))
876 NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
877 Buffer->note_end(), "note");
Daniel Dunbar34818552009-11-14 03:23:19 +0000878 }
879
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000880 Diags.setClient(CurClient, Owner.release() != nullptr);
Daniel Dunbar34818552009-11-14 03:23:19 +0000881
882 // Reset the buffer, we have processed all the diagnostics in it.
883 Buffer.reset(new TextDiagnosticBuffer());
Nico Weberdc493cf2014-04-24 05:39:55 +0000884 ED.Reset();
Daniel Dunbar34818552009-11-14 03:23:19 +0000885}
Chris Lattnere82411b2010-04-28 20:02:30 +0000886
David Blaikie93106512014-08-29 16:30:23 +0000887std::unique_ptr<Directive> Directive::create(bool RegexKind,
888 SourceLocation DirectiveLoc,
889 SourceLocation DiagnosticLoc,
890 bool MatchAnyLine, StringRef Text,
891 unsigned Min, unsigned Max) {
Alp Toker6ed72512013-12-14 01:07:05 +0000892 if (!RegexKind)
David Blaikie93106512014-08-29 16:30:23 +0000893 return llvm::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc,
894 MatchAnyLine, Text, Min, Max);
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000895
896 // Parse the directive into a regular expression.
897 std::string RegexStr;
898 StringRef S = Text;
899 while (!S.empty()) {
900 if (S.startswith("{{")) {
901 S = S.drop_front(2);
902 size_t RegexMatchLength = S.find("}}");
903 assert(RegexMatchLength != StringRef::npos);
904 // Append the regex, enclosed in parentheses.
905 RegexStr += "(";
906 RegexStr.append(S.data(), RegexMatchLength);
907 RegexStr += ")";
908 S = S.drop_front(RegexMatchLength + 2);
909 } else {
910 size_t VerbatimMatchLength = S.find("{{");
911 if (VerbatimMatchLength == StringRef::npos)
912 VerbatimMatchLength = S.size();
913 // Escape and append the fixed string.
Hans Wennborge6a87752013-12-12 00:27:31 +0000914 RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
Hans Wennborgcda4b6d2013-12-11 23:40:50 +0000915 S = S.drop_front(VerbatimMatchLength);
916 }
917 }
918
David Blaikie93106512014-08-29 16:30:23 +0000919 return llvm::make_unique<RegexDirective>(
920 DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr);
Chris Lattnere82411b2010-04-28 20:02:30 +0000921}