Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ---------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This is a concrete diagnostic client, which buffers the diagnostic messages.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Frontend/VerifyDiagnosticConsumer.h"
14
#include "clang/Basic/CharInfo.h"
15
#include "clang/Basic/Diagnostic.h"
16
#include "clang/Basic/DiagnosticOptions.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Basic/LLVM.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "clang/Basic/SourceManager.h"
21
#include "clang/Basic/TokenKinds.h"
22
#include "clang/Frontend/FrontendDiagnostic.h"
23
#include "clang/Frontend/TextDiagnosticBuffer.h"
24
#include "clang/Lex/HeaderSearch.h"
25
#include "clang/Lex/Lexer.h"
26
#include "clang/Lex/PPCallbacks.h"
27
#include "clang/Lex/Preprocessor.h"
28
#include "clang/Lex/Token.h"
29
#include "llvm/ADT/STLExtras.h"
30
#include "llvm/ADT/SmallPtrSet.h"
31
#include "llvm/ADT/SmallString.h"
32
#include "llvm/ADT/StringRef.h"
33
#include "llvm/ADT/Twine.h"
34
#include "llvm/Support/ErrorHandling.h"
35
#include "llvm/Support/Regex.h"
36
#include "llvm/Support/raw_ostream.h"
37
#include <algorithm>
38
#include <cassert>
39
#include <cstddef>
40
#include <cstring>
41
#include <iterator>
42
#include <memory>
43
#include <string>
44
#include <utility>
45
#include <vector>
46
47
using namespace clang;
48
49
using Directive = VerifyDiagnosticConsumer::Directive;
50
using DirectiveList = VerifyDiagnosticConsumer::DirectiveList;
51
using ExpectedData = VerifyDiagnosticConsumer::ExpectedData;
52
53
#ifndef NDEBUG
54
55
namespace {
56
57
class VerifyFileTracker : public PPCallbacks {
58
  VerifyDiagnosticConsumer &Verify;
59
  SourceManager &SM;
60
61
public:
62
  VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
63
16.5k
      : Verify(Verify), SM(SM) {}
64
65
  /// Hook into the preprocessor and update the list of parsed
66
  /// files when the preprocessor indicates a new file is entered.
67
  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
68
                   SrcMgr::CharacteristicKind FileType,
69
101k
                   FileID PrevFID) override {
70
101k
    Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
71
101k
                                  VerifyDiagnosticConsumer::IsParsed);
72
101k
  }
73
};
74
75
} // namespace
76
77
#endif
78
79
//===----------------------------------------------------------------------===//
80
// Checking diagnostics implementation.
81
//===----------------------------------------------------------------------===//
82
83
using DiagList = TextDiagnosticBuffer::DiagList;
84
using const_diag_iterator = TextDiagnosticBuffer::const_iterator;
85
86
namespace {
87
88
/// StandardDirective - Directive with string matching.
89
class StandardDirective : public Directive {
90
public:
91
  StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
92
                    bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
93
                    unsigned Min, unsigned Max)
94
      : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyFileAndLine,
95
291k
                  MatchAnyLine, Text, Min, Max) {}
96
97
291k
  bool isValid(std::string &Error) override {
98
    // all strings are considered valid; even empty ones
99
291k
    return true;
100
291k
  }
101
102
363k
  bool match(StringRef S) override {
103
363k
    return S.find(Text) != StringRef::npos;
104
363k
  }
105
};
106
107
/// RegexDirective - Directive with regular-expression matching.
108
class RegexDirective : public Directive {
109
public:
110
  RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
111
                 bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
112
                 unsigned Min, unsigned Max, StringRef RegexStr)
113
      : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyFileAndLine,
114
                  MatchAnyLine, Text, Min, Max),
115
4.41k
        Regex(RegexStr) {}
116
117
4.41k
  bool isValid(std::string &Error) override {
118
4.41k
    return Regex.isValid(Error);
119
4.41k
  }
120
121
4.56k
  bool match(StringRef S) override {
122
4.56k
    return Regex.match(S);
123
4.56k
  }
124
125
private:
126
  llvm::Regex Regex;
127
};
128
129
class ParseHelper
130
{
131
public:
132
  ParseHelper(StringRef S)
133
3.39M
      : Begin(S.begin()), End(S.end()), C(Begin), P(Begin) {}
134
135
  // Return true if string literal is next.
136
1.00M
  bool Next(StringRef S) {
137
1.00M
    P = C;
138
1.00M
    PEnd = C + S.size();
139
1.00M
    if (PEnd > End)
140
0
      return false;
141
1.00M
    return memcmp(P, S.data(), S.size()) == 0;
142
1.00M
  }
143
144
  // Return true if number is next.
145
  // Output N only if number is next.
146
358k
  bool Next(unsigned &N) {
147
358k
    unsigned TMP = 0;
148
358k
    P = C;
149
358k
    PEnd = P;
150
454k
    for (; PEnd < End && *PEnd >= '0' && 
*PEnd <= '9'367k
;
++PEnd96.3k
) {
151
96.3k
      TMP *= 10;
152
96.3k
      TMP += *PEnd - '0';
153
96.3k
    }
154
358k
    if (PEnd == C)
155
263k
      return false;
156
94.6k
    N = TMP;
157
94.6k
    return true;
158
94.6k
  }
159
160
  // Return true if a marker is next.
161
  // A marker is the longest match for /#[A-Za-z0-9_-]+/.
162
32.1k
  bool NextMarker() {
163
32.1k
    P = C;
164
32.1k
    if (P == End || *P != '#')
165
1.72k
      return false;
166
30.4k
    PEnd = P;
167
30.4k
    ++PEnd;
168
118k
    while ((isAlphanumeric(*PEnd) || 
*PEnd == '-'30.4k
||
*PEnd == '_'30.4k
) &&
169
88.1k
           PEnd < End)
170
88.1k
      ++PEnd;
171
30.4k
    return PEnd > P + 1;
172
30.4k
  }
173
174
  // Return true if string literal S is matched in content.
175
  // When true, P marks begin-position of the match, and calling Advance sets C
176
  // to end-position of the match.
177
  // If S is the empty string, then search for any letter instead (makes sense
178
  // with FinishDirectiveToken=true).
179
  // If EnsureStartOfWord, then skip matches that don't start a new word.
180
  // If FinishDirectiveToken, then assume the match is the start of a comment
181
  // directive for -verify, and extend the match to include the entire first
182
  // token of that directive.
183
  bool Search(StringRef S, bool EnsureStartOfWord = false,
184
3.57M
              bool FinishDirectiveToken = false) {
185
4.33M
    do {
186
4.33M
      if (!S.empty()) {
187
3.35M
        P = std::search(C, End, S.begin(), S.end());
188
3.35M
        PEnd = P + S.size();
189
3.35M
      }
190
980k
      else {
191
980k
        P = C;
192
1.85M
        while (P != End && 
!isLetter(*P)1.85M
)
193
877k
          ++P;
194
980k
        PEnd = P + 1;
195
980k
      }
196
4.33M
      if (P == End)
197
3.13M
        break;
198
      // If not start of word but required, skip and search again.
199
1.20M
      if (EnsureStartOfWord
200
               // Check if string literal starts a new word.
201
1.19M
          && !(P == Begin || isWhitespace(P[-1])
202
               // Or it could be preceded by the start of a comment.
203
767k
               || (P > (Begin + 1) && (P[-1] == '/' || 
P[-1] == '*'763k
)
204
4.45k
                                   &&  P[-2] == '/')))
205
763k
        continue;
206
436k
      if (FinishDirectiveToken) {
207
3.63M
        while (PEnd != End && 
(3.62M
isAlphanumeric(*PEnd)3.62M
208
717k
                               || *PEnd == '-' || 
*PEnd == '_'391k
))
209
3.23M
          ++PEnd;
210
        // Put back trailing digits and hyphens to be parsed later as a count
211
        // or count range.  Because -verify prefixes must start with letters,
212
        // we know the actual directive we found starts with a letter, so
213
        // we won't put back the entire directive word and thus record an empty
214
        // string.
215
405k
        assert(isLetter(*P) && "-verify prefix must start with a letter");
216
405k
        while (isDigit(PEnd[-1]) || 
PEnd[-1] == '-'405k
)
217
872
          --PEnd;
218
405k
      }
219
436k
      return true;
220
763k
    } while (Advance());
221
3.14M
    return false;
222
3.57M
  }
223
224
  // Return true if a CloseBrace that closes the OpenBrace at the current nest
225
  // level is found. When true, P marks begin-position of CloseBrace.
226
295k
  bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) {
227
295k
    unsigned Depth = 1;
228
295k
    P = C;
229
13.5M
    while (P < End) {
230
13.5M
      StringRef S(P, End - P);
231
13.5M
      if (S.startswith(OpenBrace)) {
232
5.52k
        ++Depth;
233
5.52k
        P += OpenBrace.size();
234
13.5M
      } else if (S.startswith(CloseBrace)) {
235
301k
        --Depth;
236
301k
        if (Depth == 0) {
237
295k
          PEnd = P + CloseBrace.size();
238
295k
          return true;
239
295k
        }
240
5.52k
        P += CloseBrace.size();
241
13.2M
      } else {
242
13.2M
        ++P;
243
13.2M
      }
244
13.5M
    }
245
0
    return false;
246
295k
  }
247
248
  // Advance 1-past previous next/search.
249
  // Behavior is undefined if previous next/search failed.
250
2.00M
  bool Advance() {
251
2.00M
    C = PEnd;
252
2.00M
    return C < End;
253
2.00M
  }
254
255
  // Return the text matched by the previous next/search.
256
  // Behavior is undefined if previous next/search failed.
257
435k
  StringRef Match() { return StringRef(P, PEnd - P); }
258
259
  // Skip zero or more whitespace.
260
591k
  void SkipWhitespace() {
261
883k
    for (; C < End && isWhitespace(*C); 
++C291k
)
262
291k
      ;
263
591k
  }
264
265
  // Return true if EOF reached.
266
3.82M
  bool Done() {
267
3.82M
    return !(C < End);
268
3.82M
  }
269
270
  // Beginning of expected content.
271
  const char * const Begin;
272
273
  // End of expected content (1-past).
274
  const char * const End;
275
276
  // Position of next char in content.
277
  const char *C;
278
279
  // Previous next/search subject start.
280
  const char *P;
281
282
private:
283
  // Previous next/search subject end (1-past).
284
  const char *PEnd = nullptr;
285
};
286
287
// The information necessary to create a directive.
288
struct UnattachedDirective {
289
  DirectiveList *DL = nullptr;
290
  bool RegexKind = false;
291
  SourceLocation DirectivePos, ContentBegin;
292
  std::string Text;
293
  unsigned Min = 1, Max = 1;
294
};
295
296
// Attach the specified directive to the line of code indicated by
297
// \p ExpectedLoc.
298
void attachDirective(DiagnosticsEngine &Diags, const UnattachedDirective &UD,
299
                     SourceLocation ExpectedLoc,
300
                     bool MatchAnyFileAndLine = false,
301
295k
                     bool MatchAnyLine = false) {
302
  // Construct new directive.
303
295k
  std::unique_ptr<Directive> D = Directive::create(
304
295k
      UD.RegexKind, UD.DirectivePos, ExpectedLoc, MatchAnyFileAndLine,
305
295k
      MatchAnyLine, UD.Text, UD.Min, UD.Max);
306
295k
307
295k
  std::string Error;
308
295k
  if (!D->isValid(Error)) {
309
0
    Diags.Report(UD.ContentBegin, diag::err_verify_invalid_content)
310
0
      << (UD.RegexKind ? "regex" : "string") << Error;
311
0
  }
312
295k
313
295k
  UD.DL->push_back(std::move(D));
314
295k
}
315
316
} // anonymous
317
318
// Tracker for markers in the input files. A marker is a comment of the form
319
//
320
//   n = 123; // #123
321
//
322
// ... that can be referred to by a later expected-* directive:
323
//
324
//   // expected-error@#123 {{undeclared identifier 'n'}}
325
//
326
// Marker declarations must be at the start of a comment or preceded by
327
// whitespace to distinguish them from uses of markers in directives.
328
class VerifyDiagnosticConsumer::MarkerTracker {
329
  DiagnosticsEngine &Diags;
330
331
  struct Marker {
332
    SourceLocation DefLoc;
333
    SourceLocation RedefLoc;
334
    SourceLocation UseLoc;
335
  };
336
  llvm::StringMap<Marker> Markers;
337
338
  // Directives that couldn't be created yet because they name an unknown
339
  // marker.
340
  llvm::StringMap<llvm::SmallVector<UnattachedDirective, 2>> DeferredDirectives;
341
342
public:
343
17.1k
  MarkerTracker(DiagnosticsEngine &Diags) : Diags(Diags) {}
344
345
  // Register a marker.
346
29.9k
  void addMarker(StringRef MarkerName, SourceLocation Pos) {
347
29.9k
    auto InsertResult = Markers.insert(
348
29.9k
        {MarkerName, Marker{Pos, SourceLocation(), SourceLocation()}});
349
29.9k
350
29.9k
    Marker &M = InsertResult.first->second;
351
29.9k
    if (!InsertResult.second) {
352
      // Marker was redefined.
353
28.0k
      M.RedefLoc = Pos;
354
1.82k
    } else {
355
      // First definition: build any deferred directives.
356
1.82k
      auto Deferred = DeferredDirectives.find(MarkerName);
357
1.82k
      if (Deferred != DeferredDirectives.end()) {
358
30
        for (auto &UD : Deferred->second) {
359
30
          if (M.UseLoc.isInvalid())
360
14
            M.UseLoc = UD.DirectivePos;
361
30
          attachDirective(Diags, UD, Pos);
362
30
        }
363
14
        DeferredDirectives.erase(Deferred);
364
14
      }
365
1.82k
    }
366
29.9k
  }
367
368
  // Register a directive at the specified marker.
369
302
  void addDirective(StringRef MarkerName, const UnattachedDirective &UD) {
370
302
    auto MarkerIt = Markers.find(MarkerName);
371
302
    if (MarkerIt != Markers.end()) {
372
271
      Marker &M = MarkerIt->second;
373
271
      if (M.UseLoc.isInvalid())
374
154
        M.UseLoc = UD.DirectivePos;
375
271
      return attachDirective(Diags, UD, M.DefLoc);
376
271
    }
377
31
    DeferredDirectives[MarkerName].push_back(UD);
378
31
  }
379
380
  // Ensure we have no remaining deferred directives, and no
381
  // multiply-defined-and-used markers.
382
16.5k
  void finalize() {
383
1.81k
    for (auto &MarkerInfo : Markers) {
384
1.81k
      StringRef Name = MarkerInfo.first();
385
1.81k
      Marker &M = MarkerInfo.second;
386
1.81k
      if (M.RedefLoc.isValid() && 
M.UseLoc.isValid()895
) {
387
1
        Diags.Report(M.UseLoc, diag::err_verify_ambiguous_marker) << Name;
388
1
        Diags.Report(M.DefLoc, diag::note_verify_ambiguous_marker) << Name;
389
1
        Diags.Report(M.RedefLoc, diag::note_verify_ambiguous_marker) << Name;
390
1
      }
391
1.81k
    }
392
16.5k
393
1
    for (auto &DeferredPair : DeferredDirectives) {
394
1
      Diags.Report(DeferredPair.second.front().DirectivePos,
395
1
                   diag::err_verify_no_such_marker)
396
1
          << DeferredPair.first();
397
1
    }
398
16.5k
  }
399
};
400
401
/// ParseDirective - Go through the comment and see if it indicates expected
402
/// diagnostics. If so, then put them in the appropriate directive list.
403
///
404
/// Returns true if any valid directives were found.
405
static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
406
                           Preprocessor *PP, SourceLocation Pos,
407
                           VerifyDiagnosticConsumer::DirectiveStatus &Status,
408
1.69M
                           VerifyDiagnosticConsumer::MarkerTracker &Markers) {
409
1.69M
  DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : 
SM.getDiagnostics()616
;
410
1.69M
411
  // First, scan the comment looking for markers.
412
1.72M
  for (ParseHelper PH(S); !PH.Done();) {
413
1.72M
    if (!PH.Search("#", true))
414
1.69M
      break;
415
30.1k
    PH.C = PH.P;
416
30.1k
    if (!PH.NextMarker()) {
417
216
      PH.Next("#");
418
216
      PH.Advance();
419
216
      continue;
420
216
    }
421
29.9k
    PH.Advance();
422
29.9k
    Markers.addMarker(PH.Match(), Pos);
423
29.9k
  }
424
1.69M
425
  // A single comment may contain multiple directives.
426
1.69M
  bool FoundDirective = false;
427
2.10M
  for (ParseHelper PH(S); !PH.Done();) {
428
    // Search for the initial directive token.
429
    // If one prefix, save time by searching only for its directives.
430
    // Otherwise, search for any potential directive token and check it later.
431
1.85M
    const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes;
432
1.85M
    if (!(Prefixes.size() == 1 ? 
PH.Search(*Prefixes.begin(), true, true)1.61M
433
240k
                               : PH.Search("", true, true)))
434
1.44M
      break;
435
405k
436
405k
    StringRef DToken = PH.Match();
437
405k
    PH.Advance();
438
405k
439
    // Default directive kind.
440
405k
    UnattachedDirective D;
441
405k
    const char *KindStr = "string";
442
405k
443
    // Parse the initial directive token in reverse so we can easily determine
444
    // its exact actual prefix.  If we were to parse it from the front instead,
445
    // it would be harder to determine where the prefix ends because there
446
    // might be multiple matching -verify prefixes because some might prefix
447
    // others.
448
405k
449
    // Regex in initial directive token: -re
450
405k
    if (DToken.endswith("-re")) {
451
4.45k
      D.RegexKind = true;
452
4.45k
      KindStr = "regex";
453
4.45k
      DToken = DToken.substr(0, DToken.size()-3);
454
4.45k
    }
455
405k
456
    // Type in initial directive token: -{error|warning|note|no-diagnostics}
457
405k
    bool NoDiag = false;
458
405k
    StringRef DType;
459
405k
    if (DToken.endswith(DType="-error"))
460
164k
      D.DL = ED ? 
&ED->Errors164k
:
nullptr34
;
461
240k
    else if (DToken.endswith(DType="-warning"))
462
57.3k
      D.DL = ED ? 
&ED->Warnings57.3k
:
nullptr7
;
463
183k
    else if (DToken.endswith(DType="-remark"))
464
139
      D.DL = ED ? &ED->Remarks : 
nullptr0
;
465
182k
    else if (DToken.endswith(DType="-note"))
466
81.7k
      D.DL = ED ? 
&ED->Notes81.7k
:
nullptr10
;
467
101k
    else if (DToken.endswith(DType="-no-diagnostics")) {
468
7.87k
      NoDiag = true;
469
7.87k
      if (D.RegexKind)
470
0
        continue;
471
93.3k
    }
472
93.3k
    else
473
93.3k
      continue;
474
311k
    DToken = DToken.substr(0, DToken.size()-DType.size());
475
311k
476
    // What's left in DToken is the actual prefix.  That might not be a -verify
477
    // prefix even if there is only one -verify prefix (for example, the full
478
    // DToken is foo-bar-warning, but foo is the only -verify prefix).
479
311k
    if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken))
480
7.98k
      continue;
481
303k
482
303k
    if (NoDiag) {
483
7.77k
      if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
484
1
        Diags.Report(Pos, diag::err_verify_invalid_no_diags)
485
1
          << /*IsExpectedNoDiagnostics=*/true;
486
7.77k
      else
487
7.77k
        Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
488
7.77k
      continue;
489
7.77k
    }
490
296k
    if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
491
1
      Diags.Report(Pos, diag::err_verify_invalid_no_diags)
492
1
        << /*IsExpectedNoDiagnostics=*/false;
493
1
      continue;
494
1
    }
495
296k
    Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
496
296k
497
    // If a directive has been found but we're not interested
498
    // in storing the directive information, return now.
499
296k
    if (!D.DL)
500
51
      return true;
501
295k
502
    // Next optional token: @
503
295k
    SourceLocation ExpectedLoc;
504
295k
    StringRef Marker;
505
295k
    bool MatchAnyFileAndLine = false;
506
295k
    bool MatchAnyLine = false;
507
295k
    if (!PH.Next("@")) {
508
236k
      ExpectedLoc = Pos;
509
59.5k
    } else {
510
59.5k
      PH.Advance();
511
59.5k
      unsigned Line = 0;
512
59.5k
      bool FoundPlus = PH.Next("+");
513
59.5k
      if (FoundPlus || 
PH.Next("-")16.7k
) {
514
        // Relative to current line.
515
56.9k
        PH.Advance();
516
56.9k
        bool Invalid = false;
517
56.9k
        unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
518
56.9k
        if (!Invalid && PH.Next(Line) && (FoundPlus || 
Line < ExpectedLine14.1k
)) {
519
56.9k
          if (FoundPlus) 
ExpectedLine += Line42.7k
;
520
14.1k
          else ExpectedLine -= Line;
521
56.9k
          ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
522
56.9k
        }
523
2.59k
      } else if (PH.Next(Line)) {
524
        // Absolute line number.
525
568
        if (Line > 0)
526
562
          ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
527
2.02k
      } else if (PH.NextMarker()) {
528
302
        Marker = PH.Match();
529
1.72k
      } else if (PP && PH.Search(":")) {
530
        // Specific source file.
531
1.66k
        StringRef Filename(PH.C, PH.P-PH.C);
532
1.66k
        PH.Advance();
533
1.66k
534
1.66k
        if (Filename == "*") {
535
3
          MatchAnyFileAndLine = true;
536
3
          if (!PH.Next("*")) {
537
1
            Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin),
538
1
                         diag::err_verify_missing_line)
539
1
                << "'*'";
540
1
            continue;
541
1
          }
542
2
          MatchAnyLine = true;
543
2
          ExpectedLoc = SourceLocation();
544
1.65k
        } else {
545
          // Lookup file via Preprocessor, like a #include.
546
1.65k
          const DirectoryLookup *CurDir;
547
1.65k
          Optional<FileEntryRef> File =
548
1.65k
              PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
549
1.65k
                             nullptr, nullptr, nullptr, nullptr, nullptr);
550
1.65k
          if (!File) {
551
1
            Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin),
552
1
                         diag::err_verify_missing_file)
553
1
                << Filename << KindStr;
554
1
            continue;
555
1
          }
556
1.65k
557
1.65k
          const FileEntry *FE = &File->getFileEntry();
558
1.65k
          if (SM.translateFile(FE).isInvalid())
559
47
            SM.createFileID(FE, Pos, SrcMgr::C_User);
560
1.65k
561
1.65k
          if (PH.Next(Line) && 
Line > 0601
)
562
599
            ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
563
1.05k
          else if (PH.Next("*")) {
564
1.05k
            MatchAnyLine = true;
565
1.05k
            ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
566
1.05k
          }
567
1.65k
        }
568
67
      } else if (PH.Next("*")) {
569
61
        MatchAnyLine = true;
570
61
        ExpectedLoc = SourceLocation();
571
61
      }
572
59.5k
573
59.5k
      if (ExpectedLoc.isInvalid() && 
!MatchAnyLine386
&&
Marker.empty()323
) {
574
21
        Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
575
21
                     diag::err_verify_missing_line) << KindStr;
576
21
        continue;
577
21
      }
578
59.5k
      PH.Advance();
579
59.5k
    }
580
295k
581
    // Skip optional whitespace.
582
295k
    PH.SkipWhitespace();
583
295k
584
    // Next optional token: positive integer or a '+'.
585
295k
    if (PH.Next(D.Min)) {
586
35.2k
      PH.Advance();
587
      // A positive integer can be followed by a '+' meaning min
588
      // or more, or by a '-' meaning a range from min to max.
589
35.2k
      if (PH.Next("+")) {
590
463
        D.Max = Directive::MaxCount;
591
463
        PH.Advance();
592
34.8k
      } else if (PH.Next("-")) {
593
1.18k
        PH.Advance();
594
1.18k
        if (!PH.Next(D.Max) || D.Max < D.Min) {
595
0
          Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
596
0
                       diag::err_verify_invalid_range) << KindStr;
597
0
          continue;
598
0
        }
599
1.18k
        PH.Advance();
600
33.6k
      } else {
601
33.6k
        D.Max = D.Min;
602
33.6k
      }
603
260k
    } else if (PH.Next("+")) {
604
      // '+' on its own means "1 or more".
605
671
      D.Max = Directive::MaxCount;
606
671
      PH.Advance();
607
671
    }
608
295k
609
    // Skip optional whitespace.
610
295k
    PH.SkipWhitespace();
611
295k
612
    // Next token: {{
613
295k
    if (!PH.Next("{{")) {
614
7
      Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
615
7
                   diag::err_verify_missing_start) << KindStr;
616
7
      continue;
617
7
    }
618
295k
    PH.Advance();
619
295k
    const char* const ContentBegin = PH.C; // mark content begin
620
    // Search for token: }}
621
295k
    if (!PH.SearchClosingBrace("{{", "}}")) {
622
0
      Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
623
0
                   diag::err_verify_missing_end) << KindStr;
624
0
      continue;
625
0
    }
626
295k
    const char* const ContentEnd = PH.P; // mark content end
627
295k
    PH.Advance();
628
295k
629
295k
    D.DirectivePos = Pos;
630
295k
    D.ContentBegin = Pos.getLocWithOffset(ContentBegin - PH.Begin);
631
295k
632
    // Build directive text; convert \n to newlines.
633
295k
    StringRef NewlineStr = "\\n";
634
295k
    StringRef Content(ContentBegin, ContentEnd-ContentBegin);
635
295k
    size_t CPos = 0;
636
295k
    size_t FPos;
637
296k
    while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
638
86
      D.Text += Content.substr(CPos, FPos-CPos);
639
86
      D.Text += '\n';
640
86
      CPos = FPos + NewlineStr.size();
641
86
    }
642
295k
    if (D.Text.empty())
643
295k
      D.Text.assign(ContentBegin, ContentEnd);
644
295k
645
    // Check that regex directives contain at least one regex.
646
295k
    if (D.RegexKind && 
D.Text.find("{{") == StringRef::npos4.41k
) {
647
0
      Diags.Report(D.ContentBegin, diag::err_verify_missing_regex) << D.Text;
648
0
      return false;
649
0
    }
650
295k
651
295k
    if (Marker.empty())
652
295k
      attachDirective(Diags, D, ExpectedLoc, MatchAnyFileAndLine, MatchAnyLine);
653
302
    else
654
302
      Markers.addDirective(Marker, D);
655
295k
    FoundDirective = true;
656
295k
  }
657
1.69M
658
1.69M
  return FoundDirective;
659
1.69M
}
660
661
VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_)
662
    : Diags(Diags_), PrimaryClient(Diags.getClient()),
663
      PrimaryClientOwner(Diags.takeClient()),
664
      Buffer(new TextDiagnosticBuffer()), Markers(new MarkerTracker(Diags)),
665
16.5k
      Status(HasNoDirectives) {
666
16.5k
  if (Diags.hasSourceManager())
667
0
    setSourceManager(Diags.getSourceManager());
668
16.5k
}
669
670
16.4k
VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
671
16.4k
  assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
672
16.4k
  assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
673
16.4k
  SrcManager = nullptr;
674
16.4k
  CheckDiagnostics();
675
16.4k
  assert(!Diags.ownsClient() &&
676
16.4k
         "The VerifyDiagnosticConsumer takes over ownership of the client!");
677
16.4k
}
678
679
// DiagnosticConsumer interface.
680
681
void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
682
16.5k
                                               const Preprocessor *PP) {
683
  // Attach comment handler on first invocation.
684
16.5k
  if (++ActiveSourceFiles == 1) {
685
16.5k
    if (PP) {
686
16.5k
      CurrentPreprocessor = PP;
687
16.5k
      this->LangOpts = &LangOpts;
688
16.5k
      setSourceManager(PP->getSourceManager());
689
16.5k
      const_cast<Preprocessor *>(PP)->addCommentHandler(this);
690
16.5k
#ifndef NDEBUG
691
      // Debug build tracks parsed files.
692
16.5k
      const_cast<Preprocessor *>(PP)->addPPCallbacks(
693
16.5k
                      std::make_unique<VerifyFileTracker>(*this, *SrcManager));
694
16.5k
#endif
695
16.5k
    }
696
16.5k
  }
697
16.5k
698
16.5k
  assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
699
16.5k
  PrimaryClient->BeginSourceFile(LangOpts, PP);
700
16.5k
}
701
702
16.5k
void VerifyDiagnosticConsumer::EndSourceFile() {
703
16.5k
  assert(ActiveSourceFiles && "No active source files!");
704
16.5k
  PrimaryClient->EndSourceFile();
705
16.5k
706
  // Detach comment handler once last active source file completed.
707
16.5k
  if (--ActiveSourceFiles == 0) {
708
16.5k
    if (CurrentPreprocessor)
709
16.5k
      const_cast<Preprocessor *>(CurrentPreprocessor)->
710
16.5k
          removeCommentHandler(this);
711
16.5k
712
    // Diagnose any used-but-not-defined markers.
713
16.5k
    Markers->finalize();
714
16.5k
715
    // Check diagnostics once last file completed.
716
16.5k
    CheckDiagnostics();
717
16.5k
    CurrentPreprocessor = nullptr;
718
16.5k
    LangOpts = nullptr;
719
16.5k
  }
720
16.5k
}
721
722
void VerifyDiagnosticConsumer::HandleDiagnostic(
723
348k
      DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
724
348k
  if (Info.hasSourceManager()) {
725
    // If this diagnostic is for a different source manager, ignore it.
726
348k
    if (SrcManager && &Info.getSourceManager() != SrcManager)
727
272
      return;
728
348k
729
348k
    setSourceManager(Info.getSourceManager());
730
348k
  }
731
348k
732
348k
#ifndef NDEBUG
733
  // Debug build tracks unparsed files for possible
734
  // unparsed expected-* directives.
735
348k
  if (SrcManager) {
736
348k
    SourceLocation Loc = Info.getLocation();
737
348k
    if (Loc.isValid()) {
738
348k
      ParsedStatus PS = IsUnparsed;
739
348k
740
348k
      Loc = SrcManager->getExpansionLoc(Loc);
741
348k
      FileID FID = SrcManager->getFileID(Loc);
742
348k
743
348k
      const FileEntry *FE = SrcManager->getFileEntryForID(FID);
744
348k
      if (FE && 
CurrentPreprocessor348k
&&
SrcManager->isLoadedFileID(FID)348k
) {
745
        // If the file is a modules header file it shall not be parsed
746
        // for expected-* directives.
747
1.48k
        HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
748
1.48k
        if (HS.findModuleForHeader(FE))
749
1.09k
          PS = IsUnparsedNoDirectives;
750
1.48k
      }
751
348k
752
348k
      UpdateParsedFileStatus(*SrcManager, FID, PS);
753
348k
    }
754
348k
  }
755
348k
#endif
756
348k
757
  // Send the diagnostic to the buffer, we will check it once we reach the end
758
  // of the source file (or are destructed).
759
348k
  Buffer->HandleDiagnostic(DiagLevel, Info);
760
348k
}
761
762
/// HandleComment - Hook into the preprocessor and extract comments containing
763
///  expected errors and warnings.
764
bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
765
1.69M
                                             SourceRange Comment) {
766
1.69M
  SourceManager &SM = PP.getSourceManager();
767
1.69M
768
  // If this comment is for a different source manager, ignore it.
769
1.69M
  if (SrcManager && &SM != SrcManager)
770
0
    return false;
771
1.69M
772
1.69M
  SourceLocation CommentBegin = Comment.getBegin();
773
1.69M
774
1.69M
  const char *CommentRaw = SM.getCharacterData(CommentBegin);
775
1.69M
  StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
776
1.69M
777
1.69M
  if (C.empty())
778
2
    return false;
779
1.69M
780
  // Fold any "\<EOL>" sequences
781
1.69M
  size_t loc = C.find('\\');
782
1.69M
  if (loc == StringRef::npos) {
783
1.56M
    ParseDirective(C, &ED, SM, &PP, CommentBegin, Status, *Markers);
784
1.56M
    return false;
785
1.56M
  }
786
133k
787
133k
  std::string C2;
788
133k
  C2.reserve(C.size());
789
133k
790
281k
  for (size_t last = 0;; 
loc = C.find('\\', last)148k
) {
791
281k
    if (loc == StringRef::npos || 
loc == C.size()148k
) {
792
133k
      C2 += C.substr(last);
793
133k
      break;
794
133k
    }
795
148k
    C2 += C.substr(last, loc-last);
796
148k
    last = loc + 1;
797
148k
798
148k
    if (C[last] == '\n' || 
C[last] == '\r'136k
) {
799
11.6k
      ++last;
800
11.6k
801
      // Escape \r\n  or \n\r, but not \n\n.
802
11.6k
      if (last < C.size())
803
11.6k
        if (C[last] == '\n' || C[last] == '\r')
804
0
          if (C[last] != C[last-1])
805
0
            ++last;
806
136k
    } else {
807
      // This was just a normal backslash.
808
136k
      C2 += '\\';
809
136k
    }
810
148k
  }
811
133k
812
133k
  if (!C2.empty())
813
133k
    ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status, *Markers);
814
133k
  return false;
815
133k
}
816
817
#ifndef NDEBUG
818
/// Lex the specified source file to determine whether it contains
819
/// any expected-* directives.  As a Lexer is used rather than a full-blown
820
/// Preprocessor, directives inside skipped #if blocks will still be found.
821
///
822
/// \return true if any directives were found.
823
static bool findDirectives(SourceManager &SM, FileID FID,
824
126
                           const LangOptions &LangOpts) {
825
  // Create a raw lexer to pull all the comments out of FID.
826
126
  if (FID.isInvalid())
827
0
    return false;
828
126
829
  // Create a lexer to lex all the tokens of the main file in raw mode.
830
126
  const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
831
126
  Lexer RawLex(FID, FromFile, SM, LangOpts);
832
126
833
  // Return comments as tokens, this is how we find expected diagnostics.
834
126
  RawLex.SetCommentRetentionState(true);
835
126
836
126
  Token Tok;
837
126
  Tok.setKind(tok::comment);
838
126
  VerifyDiagnosticConsumer::DirectiveStatus Status =
839
126
    VerifyDiagnosticConsumer::HasNoDirectives;
840
28.8k
  while (Tok.isNot(tok::eof)) {
841
28.8k
    RawLex.LexFromRawLexer(Tok);
842
28.8k
    if (!Tok.is(tok::comment)) 
continue28.2k
;
843
616
844
616
    std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
845
616
    if (Comment.empty()) 
continue0
;
846
616
847
    // We don't care about tracking markers for this phase.
848
616
    VerifyDiagnosticConsumer::MarkerTracker Markers(SM.getDiagnostics());
849
616
850
    // Find first directive.
851
616
    if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
852
616
                       Status, Markers))
853
51
      return true;
854
616
  }
855
75
  return false;
856
126
}
857
#endif // !NDEBUG
858
859
/// Takes a list of diagnostics that have been generated but not matched
860
/// by an expected-* directive and produces a diagnostic to the user from this.
861
static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
862
                                const_diag_iterator diag_begin,
863
                                const_diag_iterator diag_end,
864
131k
                                const char *Kind) {
865
131k
  if (diag_begin == diag_end) 
return 0131k
;
866
77
867
77
  SmallString<256> Fmt;
868
77
  llvm::raw_svector_ostream OS(Fmt);
869
195
  for (const_diag_iterator I = diag_begin, E = diag_end; I != E; 
++I118
) {
870
118
    if (I->first.isInvalid() || 
!SourceMgr84
)
871
34
      OS << "\n  (frontend)";
872
84
    else {
873
84
      OS << "\n ";
874
84
      if (const FileEntry *File = SourceMgr->getFileEntryForID(
875
84
                                                SourceMgr->getFileID(I->first)))
876
84
        OS << " File " << File->getName();
877
84
      OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
878
84
    }
879
118
    OS << ": " << I->second;
880
118
  }
881
77
882
77
  Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
883
77
    << Kind << /*Unexpected=*/true << OS.str();
884
77
  return std::distance(diag_begin, diag_end);
885
77
}
886
887
/// Takes a list of diagnostics that were expected to have been generated
888
/// but were not and produces a diagnostic to the user from this.
889
static unsigned PrintExpected(DiagnosticsEngine &Diags,
890
                              SourceManager &SourceMgr,
891
66.1k
                              std::vector<Directive *> &DL, const char *Kind) {
892
66.1k
  if (DL.empty())
893
66.1k
    return 0;
894
25
895
25
  SmallString<256> Fmt;
896
25
  llvm::raw_svector_ostream OS(Fmt);
897
175
  for (const auto *D : DL) {
898
175
    if (D->DiagnosticLoc.isInvalid() || 
D->MatchAnyFileAndLine174
)
899
1
      OS << "\n  File *";
900
174
    else
901
174
      OS << "\n  File " << SourceMgr.getFilename(D->DiagnosticLoc);
902
175
    if (D->MatchAnyLine)
903
2
      OS << " Line *";
904
173
    else
905
173
      OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc);
906
175
    if (D->DirectiveLoc != D->DiagnosticLoc)
907
20
      OS << " (directive at "
908
20
         << SourceMgr.getFilename(D->DirectiveLoc) << ':'
909
20
         << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')';
910
175
    OS << ": " << D->Text;
911
175
  }
912
25
913
25
  Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
914
25
    << Kind << /*Unexpected=*/false << OS.str();
915
25
  return DL.size();
916
25
}
917
918
/// Determine whether two source locations come from the same file.
919
static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
920
370k
                           SourceLocation DiagnosticLoc) {
921
385k
  while (DiagnosticLoc.isMacroID())
922
14.8k
    DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
923
370k
924
370k
  if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
925
367k
    return true;
926
2.56k
927
2.56k
  const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
928
2.56k
  if (!DiagFile && 
SM.isWrittenInMainFile(DirectiveLoc)13
)
929
13
    return true;
930
2.55k
931
2.55k
  return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
932
2.55k
}
933
934
/// CheckLists - Compare expected to seen diagnostic lists and return the
935
/// the difference between them.
936
static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
937
                           const char *Label,
938
                           DirectiveList &Left,
939
                           const_diag_iterator d2_begin,
940
                           const_diag_iterator d2_end,
941
66.1k
                           bool IgnoreUnexpected) {
942
66.1k
  std::vector<Directive *> LeftOnly;
943
66.1k
  DiagList Right(d2_begin, d2_end);
944
66.1k
945
295k
  for (auto &Owner : Left) {
946
295k
    Directive &D = *Owner;
947
295k
    unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
948
295k
949
639k
    for (unsigned i = 0; i < D.Max; 
++i343k
) {
950
345k
      DiagList::iterator II, IE;
951
2.23M
      for (II = Right.begin(), IE = Right.end(); II != IE; 
++II1.88M
) {
952
2.23M
        if (!D.MatchAnyLine) {
953
2.21M
          unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
954
2.21M
          if (LineNo1 != LineNo2)
955
1.86M
            continue;
956
370k
        }
957
370k
958
370k
        if (!D.DiagnosticLoc.isInvalid() && 
!D.MatchAnyFileAndLine370k
&&
959
370k
            !IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
960
2.26k
          continue;
961
368k
962
368k
        const std::string &RightText = II->second;
963
368k
        if (D.match(RightText))
964
343k
          break;
965
368k
      }
966
345k
      if (II == IE) {
967
        // Not found.
968
2.05k
        if (i >= D.Min) 
break1.88k
;
969
175
        LeftOnly.push_back(&D);
970
343k
      } else {
971
        // Found. The same cannot be found twice.
972
343k
        Right.erase(II);
973
343k
      }
974
345k
    }
975
295k
  }
976
  // Now all that's left in Right are those that were not matched.
977
66.1k
  unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
978
66.1k
  if (!IgnoreUnexpected)
979
65.6k
    num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
980
66.1k
  return num;
981
66.1k
}
982
983
/// CheckResults - This compares the expected results to those that
984
/// were actually reported. It emits any discrepencies. Return "true" if there
985
/// were problems. Return "false" otherwise.
986
static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
987
                             const TextDiagnosticBuffer &Buffer,
988
16.5k
                             ExpectedData &ED) {
989
  // We want to capture the delta between what was expected and what was
990
  // seen.
991
  //
992
  //   Expected \ Seen - set expected but not seen
993
  //   Seen \ Expected - set seen but not expected
994
16.5k
  unsigned NumProblems = 0;
995
16.5k
996
16.5k
  const DiagnosticLevelMask DiagMask =
997
16.5k
    Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
998
16.5k
999
  // See if there are error mismatches.
1000
16.5k
  NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
1001
16.5k
                            Buffer.err_begin(), Buffer.err_end(),
1002
16.5k
                            bool(DiagnosticLevelMask::Error & DiagMask));
1003
16.5k
1004
  // See if there are warning mismatches.
1005
16.5k
  NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
1006
16.5k
                            Buffer.warn_begin(), Buffer.warn_end(),
1007
16.5k
                            bool(DiagnosticLevelMask::Warning & DiagMask));
1008
16.5k
1009
  // See if there are remark mismatches.
1010
16.5k
  NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
1011
16.5k
                            Buffer.remark_begin(), Buffer.remark_end(),
1012
16.5k
                            bool(DiagnosticLevelMask::Remark & DiagMask));
1013
16.5k
1014
  // See if there are note mismatches.
1015
16.5k
  NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
1016
16.5k
                            Buffer.note_begin(), Buffer.note_end(),
1017
16.5k
                            bool(DiagnosticLevelMask::Note & DiagMask));
1018
16.5k
1019
16.5k
  return NumProblems;
1020
16.5k
}
1021
1022
void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
1023
                                                      FileID FID,
1024
450k
                                                      ParsedStatus PS) {
1025
  // Check SourceManager hasn't changed.
1026
450k
  setSourceManager(SM);
1027
450k
1028
450k
#ifndef NDEBUG
1029
450k
  if (FID.isInvalid())
1030
0
    return;
1031
450k
1032
450k
  const FileEntry *FE = SM.getFileEntryForID(FID);
1033
450k
1034
450k
  if (PS == IsParsed) {
1035
    // Move the FileID from the unparsed set to the parsed set.
1036
101k
    UnparsedFiles.erase(FID);
1037
101k
    ParsedFiles.insert(std::make_pair(FID, FE));
1038
348k
  } else if (!ParsedFiles.count(FID) && 
!UnparsedFiles.count(FID)1.54k
) {
1039
    // Add the FileID to the unparsed set if we haven't seen it before.
1040
308
1041
    // Check for directives.
1042
308
    bool FoundDirectives;
1043
308
    if (PS == IsUnparsedNoDirectives)
1044
182
      FoundDirectives = false;
1045
126
    else
1046
126
      FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
1047
308
1048
    // Add the FileID to the unparsed set.
1049
308
    UnparsedFiles.insert(std::make_pair(FID,
1050
308
                                      UnparsedFileStatus(FE, FoundDirectives)));
1051
308
  }
1052
450k
#endif
1053
450k
}
1054
1055
33.0k
void VerifyDiagnosticConsumer::CheckDiagnostics() {
1056
  // Ensure any diagnostics go to the primary client.
1057
33.0k
  DiagnosticConsumer *CurClient = Diags.getClient();
1058
33.0k
  std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient();
1059
33.0k
  Diags.setClient(PrimaryClient, false);
1060
33.0k
1061
33.0k
#ifndef NDEBUG
1062
  // In a debug build, scan through any files that may have been missed
1063
  // during parsing and issue a fatal error if directives are contained
1064
  // within these files.  If a fatal error occurs, this suggests that
1065
  // this file is being parsed separately from the main file, in which
1066
  // case consider moving the directives to the correct place, if this
1067
  // is applicable.
1068
33.0k
  if (!UnparsedFiles.empty()) {
1069
    // Generate a cache of parsed FileEntry pointers for alias lookups.
1070
219
    llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
1071
219
    for (const auto &I : ParsedFiles)
1072
462
      if (const FileEntry *FE = I.second)
1073
243
        ParsedFileCache.insert(FE);
1074
219
1075
    // Iterate through list of unparsed files.
1076
305
    for (const auto &I : UnparsedFiles) {
1077
305
      const UnparsedFileStatus &Status = I.second;
1078
305
      const FileEntry *FE = Status.getFile();
1079
305
1080
      // Skip files that have been parsed via an alias.
1081
305
      if (FE && 
ParsedFileCache.count(FE)301
)
1082
56
        continue;
1083
249
1084
      // Report a fatal error if this file contained directives.
1085
249
      if (Status.foundDirectives()) {
1086
0
        llvm::report_fatal_error(Twine("-verify directives found after rather"
1087
0
                                       " than during normal parsing of ",
1088
0
                                 StringRef(FE ? FE->getName() : "(unknown)")));
1089
0
      }
1090
249
    }
1091
219
1092
    // UnparsedFiles has been processed now, so clear it.
1093
219
    UnparsedFiles.clear();
1094
219
  }
1095
33.0k
#endif // !NDEBUG
1096
33.0k
1097
33.0k
  if (SrcManager) {
1098
    // Produce an error if no expected-* directives could be found in the
1099
    // source file(s) processed.
1100
16.5k
    if (Status == HasNoDirectives) {
1101
23
      Diags.Report(diag::err_verify_no_directives).setForceEmit();
1102
23
      ++NumErrors;
1103
23
      Status = HasNoDirectivesReported;
1104
23
    }
1105
16.5k
1106
    // Check that the expected diagnostics occurred.
1107
16.5k
    NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
1108
16.4k
  } else {
1109
16.4k
    const DiagnosticLevelMask DiagMask =
1110
16.4k
        ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
1111
16.4k
    if (bool(DiagnosticLevelMask::Error & DiagMask))
1112
16.1k
      NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
1113
16.1k
                                   Buffer->err_end(), "error");
1114
16.4k
    if (bool(DiagnosticLevelMask::Warning & DiagMask))
1115
16.4k
      NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
1116
16.4k
                                   Buffer->warn_end(), "warn");
1117
16.4k
    if (bool(DiagnosticLevelMask::Remark & DiagMask))
1118
16.4k
      NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(),
1119
16.4k
                                   Buffer->remark_end(), "remark");
1120
16.4k
    if (bool(DiagnosticLevelMask::Note & DiagMask))
1121
16.3k
      NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
1122
16.3k
                                   Buffer->note_end(), "note");
1123
16.4k
  }
1124
33.0k
1125
33.0k
  Diags.setClient(CurClient, Owner.release() != nullptr);
1126
33.0k
1127
  // Reset the buffer, we have processed all the diagnostics in it.
1128
33.0k
  Buffer.reset(new TextDiagnosticBuffer());
1129
33.0k
  ED.Reset();
1130
33.0k
}
1131
1132
std::unique_ptr<Directive> Directive::create(bool RegexKind,
1133
                                             SourceLocation DirectiveLoc,
1134
                                             SourceLocation DiagnosticLoc,
1135
                                             bool MatchAnyFileAndLine,
1136
                                             bool MatchAnyLine, StringRef Text,
1137
295k
                                             unsigned Min, unsigned Max) {
1138
295k
  if (!RegexKind)
1139
291k
    return std::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc,
1140
291k
                                               MatchAnyFileAndLine,
1141
291k
                                               MatchAnyLine, Text, Min, Max);
1142
4.41k
1143
  // Parse the directive into a regular expression.
1144
4.41k
  std::string RegexStr;
1145
4.41k
  StringRef S = Text;
1146
17.0k
  while (!S.empty()) {
1147
12.6k
    if (S.startswith("{{")) {
1148
5.51k
      S = S.drop_front(2);
1149
5.51k
      size_t RegexMatchLength = S.find("}}");
1150
5.51k
      assert(RegexMatchLength != StringRef::npos);
1151
      // Append the regex, enclosed in parentheses.
1152
5.51k
      RegexStr += "(";
1153
5.51k
      RegexStr.append(S.data(), RegexMatchLength);
1154
5.51k
      RegexStr += ")";
1155
5.51k
      S = S.drop_front(RegexMatchLength + 2);
1156
7.10k
    } else {
1157
7.10k
      size_t VerbatimMatchLength = S.find("{{");
1158
7.10k
      if (VerbatimMatchLength == StringRef::npos)
1159
2.39k
        VerbatimMatchLength = S.size();
1160
      // Escape and append the fixed string.
1161
7.10k
      RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
1162
7.10k
      S = S.drop_front(VerbatimMatchLength);
1163
7.10k
    }
1164
12.6k
  }
1165
4.41k
1166
4.41k
  return std::make_unique<RegexDirective>(DirectiveLoc, DiagnosticLoc,
1167
4.41k
                                          MatchAnyFileAndLine, MatchAnyLine,
1168
4.41k
                                          Text, Min, Max, RegexStr);
1169
4.41k
}