Coverage Report

Created: 2020-02-25 14:32

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