Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/Diagnostic.h
Line
Count
Source (jump to first uncovered line)
1
//===- Diagnostic.h - C Language Family Diagnostic Handling -----*- C++ -*-===//
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
/// \file
10
/// Defines the Diagnostic-related interfaces.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_BASIC_DIAGNOSTIC_H
15
#define LLVM_CLANG_BASIC_DIAGNOSTIC_H
16
17
#include "clang/Basic/DiagnosticIDs.h"
18
#include "clang/Basic/DiagnosticOptions.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "clang/Basic/Specifiers.h"
21
#include "llvm/ADT/ArrayRef.h"
22
#include "llvm/ADT/DenseMap.h"
23
#include "llvm/ADT/IntrusiveRefCntPtr.h"
24
#include "llvm/ADT/Optional.h"
25
#include "llvm/ADT/SmallVector.h"
26
#include "llvm/ADT/StringRef.h"
27
#include "llvm/ADT/iterator_range.h"
28
#include "llvm/Support/Compiler.h"
29
#include <cassert>
30
#include <cstdint>
31
#include <limits>
32
#include <list>
33
#include <map>
34
#include <memory>
35
#include <string>
36
#include <type_traits>
37
#include <utility>
38
#include <vector>
39
40
namespace llvm {
41
class Error;
42
}
43
44
namespace clang {
45
46
class DeclContext;
47
class DiagnosticBuilder;
48
class DiagnosticConsumer;
49
class IdentifierInfo;
50
class LangOptions;
51
class Preprocessor;
52
class SourceManager;
53
class StoredDiagnostic;
54
55
namespace tok {
56
57
enum TokenKind : unsigned short;
58
59
} // namespace tok
60
61
/// Annotates a diagnostic with some code that should be
62
/// inserted, removed, or replaced to fix the problem.
63
///
64
/// This kind of hint should be used when we are certain that the
65
/// introduction, removal, or modification of a particular (small!)
66
/// amount of code will correct a compilation error. The compiler
67
/// should also provide full recovery from such errors, such that
68
/// suppressing the diagnostic output can still result in successful
69
/// compilation.
70
class FixItHint {
71
public:
72
  /// Code that should be replaced to correct the error. Empty for an
73
  /// insertion hint.
74
  CharSourceRange RemoveRange;
75
76
  /// Code in the specific range that should be inserted in the insertion
77
  /// location.
78
  CharSourceRange InsertFromRange;
79
80
  /// The actual code to insert at the insertion location, as a
81
  /// string.
82
  std::string CodeToInsert;
83
84
  bool BeforePreviousInsertions = false;
85
86
  /// Empty code modification hint, indicating that no code
87
  /// modification is known.
88
329k
  FixItHint() = default;
89
90
323k
  bool isNull() const {
91
323k
    return !RemoveRange.isValid();
92
323k
  }
93
94
  /// Create a code modification hint that inserts the given
95
  /// code string at a specific location.
96
  static FixItHint CreateInsertion(SourceLocation InsertionLoc,
97
                                   StringRef Code,
98
213k
                                   bool BeforePreviousInsertions = false) {
99
213k
    FixItHint Hint;
100
213k
    Hint.RemoveRange =
101
213k
      CharSourceRange::getCharRange(InsertionLoc, InsertionLoc);
102
213k
    Hint.CodeToInsert = std::string(Code);
103
213k
    Hint.BeforePreviousInsertions = BeforePreviousInsertions;
104
213k
    return Hint;
105
213k
  }
106
107
  /// Create a code modification hint that inserts the given
108
  /// code from \p FromRange at a specific location.
109
  static FixItHint CreateInsertionFromRange(SourceLocation InsertionLoc,
110
                                            CharSourceRange FromRange,
111
115
                                        bool BeforePreviousInsertions = false) {
112
115
    FixItHint Hint;
113
115
    Hint.RemoveRange =
114
115
      CharSourceRange::getCharRange(InsertionLoc, InsertionLoc);
115
115
    Hint.InsertFromRange = FromRange;
116
115
    Hint.BeforePreviousInsertions = BeforePreviousInsertions;
117
115
    return Hint;
118
115
  }
119
120
  /// Create a code modification hint that removes the given
121
  /// source range.
122
28.1k
  static FixItHint CreateRemoval(CharSourceRange RemoveRange) {
123
28.1k
    FixItHint Hint;
124
28.1k
    Hint.RemoveRange = RemoveRange;
125
28.1k
    return Hint;
126
28.1k
  }
127
27.5k
  static FixItHint CreateRemoval(SourceRange RemoveRange) {
128
27.5k
    return CreateRemoval(CharSourceRange::getTokenRange(RemoveRange));
129
27.5k
  }
130
131
  /// Create a code modification hint that replaces the given
132
  /// source range with the given code string.
133
  static FixItHint CreateReplacement(CharSourceRange RemoveRange,
134
36.8k
                                     StringRef Code) {
135
36.8k
    FixItHint Hint;
136
36.8k
    Hint.RemoveRange = RemoveRange;
137
36.8k
    Hint.CodeToInsert = std::string(Code);
138
36.8k
    return Hint;
139
36.8k
  }
140
141
  static FixItHint CreateReplacement(SourceRange RemoveRange,
142
11.0k
                                     StringRef Code) {
143
11.0k
    return CreateReplacement(CharSourceRange::getTokenRange(RemoveRange), Code);
144
11.0k
  }
145
};
146
147
/// Concrete class used by the front-end to report problems and issues.
148
///
149
/// This massages the diagnostics (e.g. handling things like "report warnings
150
/// as errors" and passes them off to the DiagnosticConsumer for reporting to
151
/// the user. DiagnosticsEngine is tied to one translation unit and one
152
/// SourceManager.
153
class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
154
public:
155
  /// The level of the diagnostic, after it has been through mapping.
156
  enum Level {
157
    Ignored = DiagnosticIDs::Ignored,
158
    Note = DiagnosticIDs::Note,
159
    Remark = DiagnosticIDs::Remark,
160
    Warning = DiagnosticIDs::Warning,
161
    Error = DiagnosticIDs::Error,
162
    Fatal = DiagnosticIDs::Fatal
163
  };
164
165
  enum ArgumentKind {
166
    /// std::string
167
    ak_std_string,
168
169
    /// const char *
170
    ak_c_string,
171
172
    /// int
173
    ak_sint,
174
175
    /// unsigned
176
    ak_uint,
177
178
    /// enum TokenKind : unsigned
179
    ak_tokenkind,
180
181
    /// IdentifierInfo
182
    ak_identifierinfo,
183
184
    /// address space
185
    ak_addrspace,
186
187
    /// Qualifiers
188
    ak_qual,
189
190
    /// QualType
191
    ak_qualtype,
192
193
    /// DeclarationName
194
    ak_declarationname,
195
196
    /// NamedDecl *
197
    ak_nameddecl,
198
199
    /// NestedNameSpecifier *
200
    ak_nestednamespec,
201
202
    /// DeclContext *
203
    ak_declcontext,
204
205
    /// pair<QualType, QualType>
206
    ak_qualtype_pair,
207
208
    /// Attr *
209
    ak_attr
210
  };
211
212
  /// Represents on argument value, which is a union discriminated
213
  /// by ArgumentKind, with a value.
214
  using ArgumentValue = std::pair<ArgumentKind, intptr_t>;
215
216
private:
217
  // Used by __extension__
218
  unsigned char AllExtensionsSilenced = 0;
219
220
  // Treat fatal errors like errors.
221
  bool FatalsAsError = false;
222
223
  // Suppress all diagnostics.
224
  bool SuppressAllDiagnostics = false;
225
226
  // Elide common types of templates.
227
  bool ElideType = true;
228
229
  // Print a tree when comparing templates.
230
  bool PrintTemplateTree = false;
231
232
  // Color printing is enabled.
233
  bool ShowColors = false;
234
235
  // Which overload candidates to show.
236
  OverloadsShown ShowOverloads = Ovl_All;
237
238
  // Cap of # errors emitted, 0 -> no limit.
239
  unsigned ErrorLimit = 0;
240
241
  // Cap on depth of template backtrace stack, 0 -> no limit.
242
  unsigned TemplateBacktraceLimit = 0;
243
244
  // Cap on depth of constexpr evaluation backtrace stack, 0 -> no limit.
245
  unsigned ConstexprBacktraceLimit = 0;
246
247
  IntrusiveRefCntPtr<DiagnosticIDs> Diags;
248
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
249
  DiagnosticConsumer *Client = nullptr;
250
  std::unique_ptr<DiagnosticConsumer> Owner;
251
  SourceManager *SourceMgr = nullptr;
252
253
  /// Mapping information for diagnostics.
254
  ///
255
  /// Mapping info is packed into four bits per diagnostic.  The low three
256
  /// bits are the mapping (an instance of diag::Severity), or zero if unset.
257
  /// The high bit is set when the mapping was established as a user mapping.
258
  /// If the high bit is clear, then the low bits are set to the default
259
  /// value, and should be mapped with -pedantic, -Werror, etc.
260
  ///
261
  /// A new DiagState is created and kept around when diagnostic pragmas modify
262
  /// the state so that we know what is the diagnostic state at any given
263
  /// source location.
264
  class DiagState {
265
    llvm::DenseMap<unsigned, DiagnosticMapping> DiagMap;
266
267
  public:
268
    // "Global" configuration state that can actually vary between modules.
269
270
    // Ignore all warnings: -w
271
    unsigned IgnoreAllWarnings : 1;
272
273
    // Enable all warnings.
274
    unsigned EnableAllWarnings : 1;
275
276
    // Treat warnings like errors.
277
    unsigned WarningsAsErrors : 1;
278
279
    // Treat errors like fatal errors.
280
    unsigned ErrorsAsFatal : 1;
281
282
    // Suppress warnings in system headers.
283
    unsigned SuppressSystemWarnings : 1;
284
285
    // Map extensions to warnings or errors?
286
    diag::Severity ExtBehavior = diag::Severity::Ignored;
287
288
    DiagState()
289
        : IgnoreAllWarnings(false), EnableAllWarnings(false),
290
          WarningsAsErrors(false), ErrorsAsFatal(false),
291
258k
          SuppressSystemWarnings(false) {}
292
293
    using iterator = llvm::DenseMap<unsigned, DiagnosticMapping>::iterator;
294
    using const_iterator =
295
        llvm::DenseMap<unsigned, DiagnosticMapping>::const_iterator;
296
297
2.01M
    void setMapping(diag::kind Diag, DiagnosticMapping Info) {
298
2.01M
      DiagMap[Diag] = Info;
299
2.01M
    }
300
301
0
    DiagnosticMapping lookupMapping(diag::kind Diag) const {
302
0
      return DiagMap.lookup(Diag);
303
0
    }
304
305
    DiagnosticMapping &getOrAddMapping(diag::kind Diag);
306
307
10.2k
    const_iterator begin() const { return DiagMap.begin(); }
308
10.2k
    const_iterator end() const { return DiagMap.end(); }
309
  };
310
311
  /// Keeps and automatically disposes all DiagStates that we create.
312
  std::list<DiagState> DiagStates;
313
314
  /// A mapping from files to the diagnostic states for those files. Lazily
315
  /// built on demand for files in which the diagnostic state has not changed.
316
  class DiagStateMap {
317
  public:
318
    /// Add an initial diagnostic state.
319
    void appendFirst(DiagState *State);
320
321
    /// Add a new latest state point.
322
    void append(SourceManager &SrcMgr, SourceLocation Loc, DiagState *State);
323
324
    /// Look up the diagnostic state at a given source location.
325
    DiagState *lookup(SourceManager &SrcMgr, SourceLocation Loc) const;
326
327
    /// Determine whether this map is empty.
328
161k
    bool empty() const { return Files.empty(); }
329
330
    /// Clear out this map.
331
257k
    void clear() {
332
257k
      Files.clear();
333
257k
      FirstDiagState = CurDiagState = nullptr;
334
257k
      CurDiagStateLoc = SourceLocation();
335
257k
    }
336
337
    /// Produce a debugging dump of the diagnostic state.
338
    LLVM_DUMP_METHOD void dump(SourceManager &SrcMgr,
339
                               StringRef DiagName = StringRef()) const;
340
341
    /// Grab the most-recently-added state point.
342
25.3M
    DiagState *getCurDiagState() const { return CurDiagState; }
343
344
    /// Get the location at which a diagnostic state was last added.
345
518k
    SourceLocation getCurDiagStateLoc() const { return CurDiagStateLoc; }
346
347
  private:
348
    friend class ASTReader;
349
    friend class ASTWriter;
350
351
    /// Represents a point in source where the diagnostic state was
352
    /// modified because of a pragma.
353
    ///
354
    /// 'Loc' can be null if the point represents the diagnostic state
355
    /// modifications done through the command-line.
356
    struct DiagStatePoint {
357
      DiagState *State;
358
      unsigned Offset;
359
360
      DiagStatePoint(DiagState *State, unsigned Offset)
361
3.58M
          : State(State), Offset(Offset) {}
362
    };
363
364
    /// Description of the diagnostic states and state transitions for a
365
    /// particular FileID.
366
    struct File {
367
      /// The diagnostic state for the parent file. This is strictly redundant,
368
      /// as looking up the DecomposedIncludedLoc for the FileID in the Files
369
      /// map would give us this, but we cache it here for performance.
370
      File *Parent = nullptr;
371
372
      /// The offset of this file within its parent.
373
      unsigned ParentOffset = 0;
374
375
      /// Whether this file has any local (not imported from an AST file)
376
      /// diagnostic state transitions.
377
      bool HasLocalTransitions = false;
378
379
      /// The points within the file where the state changes. There will always
380
      /// be at least one of these (the state on entry to the file).
381
      llvm::SmallVector<DiagStatePoint, 4> StateTransitions;
382
383
      DiagState *lookup(unsigned Offset) const;
384
    };
385
386
    /// The diagnostic states for each file.
387
    mutable std::map<FileID, File> Files;
388
389
    /// The initial diagnostic state.
390
    DiagState *FirstDiagState;
391
392
    /// The current diagnostic state.
393
    DiagState *CurDiagState;
394
395
    /// The location at which the current diagnostic state was established.
396
    SourceLocation CurDiagStateLoc;
397
398
    /// Get the diagnostic state information for a file.
399
    File *getFile(SourceManager &SrcMgr, FileID ID) const;
400
  };
401
402
  DiagStateMap DiagStatesByLoc;
403
404
  /// Keeps the DiagState that was active during each diagnostic 'push'
405
  /// so we can get back at it when we 'pop'.
406
  std::vector<DiagState *> DiagStateOnPushStack;
407
408
21.2M
  DiagState *GetCurDiagState() const {
409
21.2M
    return DiagStatesByLoc.getCurDiagState();
410
21.2M
  }
411
412
  void PushDiagStatePoint(DiagState *State, SourceLocation L);
413
414
  /// Finds the DiagStatePoint that contains the diagnostic state of
415
  /// the given source location.
416
147M
  DiagState *GetDiagStateForLoc(SourceLocation Loc) const {
417
147M
    return SourceMgr ? DiagStatesByLoc.lookup(*SourceMgr, Loc)
418
103k
                     : DiagStatesByLoc.getCurDiagState();
419
147M
  }
420
421
  /// Sticky flag set to \c true when an error is emitted.
422
  bool ErrorOccurred;
423
424
  /// Sticky flag set to \c true when an "uncompilable error" occurs.
425
  /// I.e. an error that was not upgraded from a warning by -Werror.
426
  bool UncompilableErrorOccurred;
427
428
  /// Sticky flag set to \c true when a fatal error is emitted.
429
  bool FatalErrorOccurred;
430
431
  /// Indicates that an unrecoverable error has occurred.
432
  bool UnrecoverableErrorOccurred;
433
434
  /// Counts for DiagnosticErrorTrap to check whether an error occurred
435
  /// during a parsing section, e.g. during parsing a function.
436
  unsigned TrapNumErrorsOccurred;
437
  unsigned TrapNumUnrecoverableErrorsOccurred;
438
439
  /// The level of the last diagnostic emitted.
440
  ///
441
  /// This is used to emit continuation diagnostics with the same level as the
442
  /// diagnostic that they follow.
443
  DiagnosticIDs::Level LastDiagLevel;
444
445
  /// Number of warnings reported
446
  unsigned NumWarnings;
447
448
  /// Number of errors reported
449
  unsigned NumErrors;
450
451
  /// A function pointer that converts an opaque diagnostic
452
  /// argument to a strings.
453
  ///
454
  /// This takes the modifiers and argument that was present in the diagnostic.
455
  ///
456
  /// The PrevArgs array indicates the previous arguments formatted for this
457
  /// diagnostic.  Implementations of this function can use this information to
458
  /// avoid redundancy across arguments.
459
  ///
460
  /// This is a hack to avoid a layering violation between libbasic and libsema.
461
  using ArgToStringFnTy = void (*)(
462
      ArgumentKind Kind, intptr_t Val,
463
      StringRef Modifier, StringRef Argument,
464
      ArrayRef<ArgumentValue> PrevArgs,
465
      SmallVectorImpl<char> &Output,
466
      void *Cookie,
467
      ArrayRef<intptr_t> QualTypeVals);
468
469
  void *ArgToStringCookie = nullptr;
470
  ArgToStringFnTy ArgToStringFn;
471
472
  /// ID of the "delayed" diagnostic, which is a (typically
473
  /// fatal) diagnostic that had to be delayed because it was found
474
  /// while emitting another diagnostic.
475
  unsigned DelayedDiagID;
476
477
  /// First string argument for the delayed diagnostic.
478
  std::string DelayedDiagArg1;
479
480
  /// Second string argument for the delayed diagnostic.
481
  std::string DelayedDiagArg2;
482
483
  /// Third string argument for the delayed diagnostic.
484
  std::string DelayedDiagArg3;
485
486
  /// Optional flag value.
487
  ///
488
  /// Some flags accept values, for instance: -Wframe-larger-than=<value> and
489
  /// -Rpass=<value>. The content of this string is emitted after the flag name
490
  /// and '='.
491
  std::string FlagValue;
492
493
public:
494
  explicit DiagnosticsEngine(IntrusiveRefCntPtr<DiagnosticIDs> Diags,
495
                             IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
496
                             DiagnosticConsumer *client = nullptr,
497
                             bool ShouldOwnClient = true);
498
  DiagnosticsEngine(const DiagnosticsEngine &) = delete;
499
  DiagnosticsEngine &operator=(const DiagnosticsEngine &) = delete;
500
  ~DiagnosticsEngine();
501
502
  LLVM_DUMP_METHOD void dump() const;
503
  LLVM_DUMP_METHOD void dump(StringRef DiagName) const;
504
505
515k
  const IntrusiveRefCntPtr<DiagnosticIDs> &getDiagnosticIDs() const {
506
515k
    return Diags;
507
515k
  }
508
509
  /// Retrieve the diagnostic options.
510
2.01M
  DiagnosticOptions &getDiagnosticOptions() const { return *DiagOpts; }
511
512
  using diag_mapping_range = llvm::iterator_range<DiagState::const_iterator>;
513
514
  /// Get the current set of diagnostic mappings.
515
3.39k
  diag_mapping_range getDiagnosticMappings() const {
516
3.39k
    const DiagState &DS = *GetCurDiagState();
517
3.39k
    return diag_mapping_range(DS.begin(), DS.end());
518
3.39k
  }
519
520
13.6M
  DiagnosticConsumer *getClient() { return Client; }
521
0
  const DiagnosticConsumer *getClient() const { return Client; }
522
523
  /// Determine whether this \c DiagnosticsEngine object own its client.
524
16.5k
  bool ownsClient() const { return Owner != nullptr; }
525
526
  /// Return the current diagnostic client along with ownership of that
527
  /// client.
528
51.4k
  std::unique_ptr<DiagnosticConsumer> takeClient() { return std::move(Owner); }
529
530
411k
  bool hasSourceManager() const { return SourceMgr != nullptr; }
531
532
18.7M
  SourceManager &getSourceManager() const {
533
18.7M
    assert(SourceMgr && "SourceManager not set!");
534
18.7M
    return *SourceMgr;
535
18.7M
  }
536
537
161k
  void setSourceManager(SourceManager *SrcMgr) {
538
161k
    assert(DiagStatesByLoc.empty() &&
539
161k
           "Leftover diag state from a different SourceManager.");
540
161k
    SourceMgr = SrcMgr;
541
161k
  }
542
543
  //===--------------------------------------------------------------------===//
544
  //  DiagnosticsEngine characterization methods, used by a client to customize
545
  //  how diagnostics are emitted.
546
  //
547
548
  /// Copies the current DiagMappings and pushes the new copy
549
  /// onto the top of the stack.
550
  void pushMappings(SourceLocation Loc);
551
552
  /// Pops the current DiagMappings off the top of the stack,
553
  /// causing the new top of the stack to be the active mappings.
554
  ///
555
  /// \returns \c true if the pop happens, \c false if there is only one
556
  /// DiagMapping on the stack.
557
  bool popMappings(SourceLocation Loc);
558
559
  /// Set the diagnostic client associated with this diagnostic object.
560
  ///
561
  /// \param ShouldOwnClient true if the diagnostic object should take
562
  /// ownership of \c client.
563
  void setClient(DiagnosticConsumer *client, bool ShouldOwnClient = true);
564
565
  /// Specify a limit for the number of errors we should
566
  /// emit before giving up.
567
  ///
568
  /// Zero disables the limit.
569
42.4k
  void setErrorLimit(unsigned Limit) { ErrorLimit = Limit; }
570
571
  /// Specify the maximum number of template instantiation
572
  /// notes to emit along with a given diagnostic.
573
113k
  void setTemplateBacktraceLimit(unsigned Limit) {
574
113k
    TemplateBacktraceLimit = Limit;
575
113k
  }
576
577
  /// Retrieve the maximum number of template instantiation
578
  /// notes to emit along with a given diagnostic.
579
5.56k
  unsigned getTemplateBacktraceLimit() const {
580
5.56k
    return TemplateBacktraceLimit;
581
5.56k
  }
582
583
  /// Specify the maximum number of constexpr evaluation
584
  /// notes to emit along with a given diagnostic.
585
113k
  void setConstexprBacktraceLimit(unsigned Limit) {
586
113k
    ConstexprBacktraceLimit = Limit;
587
113k
  }
588
589
  /// Retrieve the maximum number of constexpr evaluation
590
  /// notes to emit along with a given diagnostic.
591
605k
  unsigned getConstexprBacktraceLimit() const {
592
605k
    return ConstexprBacktraceLimit;
593
605k
  }
594
595
  /// When set to true, any unmapped warnings are ignored.
596
  ///
597
  /// If this and WarningsAsErrors are both set, then this one wins.
598
160k
  void setIgnoreAllWarnings(bool Val) {
599
160k
    GetCurDiagState()->IgnoreAllWarnings = Val;
600
160k
  }
601
2.83M
  bool getIgnoreAllWarnings() const {
602
2.83M
    return GetCurDiagState()->IgnoreAllWarnings;
603
2.83M
  }
604
605
  /// When set to true, any unmapped ignored warnings are no longer
606
  /// ignored.
607
  ///
608
  /// If this and IgnoreAllWarnings are both set, then that one wins.
609
45
  void setEnableAllWarnings(bool Val) {
610
45
    GetCurDiagState()->EnableAllWarnings = Val;
611
45
  }
612
38
  bool getEnableAllWarnings() const {
613
38
    return GetCurDiagState()->EnableAllWarnings;
614
38
  }
615
616
  /// When set to true, any warnings reported are issued as errors.
617
3.18k
  void setWarningsAsErrors(bool Val) {
618
3.18k
    GetCurDiagState()->WarningsAsErrors = Val;
619
3.18k
  }
620
3.48k
  bool getWarningsAsErrors() const {
621
3.48k
    return GetCurDiagState()->WarningsAsErrors;
622
3.48k
  }
623
624
  /// When set to true, any error reported is made a fatal error.
625
2
  void setErrorsAsFatal(bool Val) { GetCurDiagState()->ErrorsAsFatal = Val; }
626
0
  bool getErrorsAsFatal() const { return GetCurDiagState()->ErrorsAsFatal; }
627
628
  /// \brief When set to true, any fatal error reported is made an error.
629
  ///
630
  /// This setting takes precedence over the setErrorsAsFatal setting above.
631
7
  void setFatalsAsError(bool Val) { FatalsAsError = Val; }
632
0
  bool getFatalsAsError() const { return FatalsAsError; }
633
634
  /// When set to true mask warnings that come from system headers.
635
113k
  void setSuppressSystemWarnings(bool Val) {
636
113k
    GetCurDiagState()->SuppressSystemWarnings = Val;
637
113k
  }
638
17.5M
  bool getSuppressSystemWarnings() const {
639
17.5M
    return GetCurDiagState()->SuppressSystemWarnings;
640
17.5M
  }
641
642
  /// Suppress all diagnostics, to silence the front end when we
643
  /// know that we don't want any more diagnostics to be passed along to the
644
  /// client
645
1.35k
  void setSuppressAllDiagnostics(bool Val) { SuppressAllDiagnostics = Val; }
646
3.24M
  bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
647
648
  /// Set type eliding, to skip outputting same types occurring in
649
  /// template types.
650
113k
  void setElideType(bool Val) { ElideType = Val; }
651
0
  bool getElideType() { return ElideType; }
652
653
  /// Set tree printing, to outputting the template difference in a
654
  /// tree format.
655
113k
  void setPrintTemplateTree(bool Val) { PrintTemplateTree = Val; }
656
0
  bool getPrintTemplateTree() { return PrintTemplateTree; }
657
658
  /// Set color printing, so the type diffing will inject color markers
659
  /// into the output.
660
113k
  void setShowColors(bool Val) { ShowColors = Val; }
661
1.37k
  bool getShowColors() { return ShowColors; }
662
663
  /// Specify which overload candidates to show when overload resolution
664
  /// fails.
665
  ///
666
  /// By default, we show all candidates.
667
113k
  void setShowOverloads(OverloadsShown Val) {
668
113k
    ShowOverloads = Val;
669
113k
  }
670
10.2k
  OverloadsShown getShowOverloads() const { return ShowOverloads; }
671
672
  /// Pretend that the last diagnostic issued was ignored, so any
673
  /// subsequent notes will be suppressed, or restore a prior ignoring
674
  /// state after ignoring some diagnostics and their notes, possibly in
675
  /// the middle of another diagnostic.
676
  ///
677
  /// This can be used by clients who suppress diagnostics themselves.
678
2.02M
  void setLastDiagnosticIgnored(bool Ignored) {
679
2.02M
    if (LastDiagLevel == DiagnosticIDs::Fatal)
680
8
      FatalErrorOccurred = true;
681
1.94M
    LastDiagLevel = Ignored ? DiagnosticIDs::Ignored : 
DiagnosticIDs::Warning84.1k
;
682
2.02M
  }
683
684
  /// Determine whether the previous diagnostic was ignored. This can
685
  /// be used by clients that want to determine whether notes attached to a
686
  /// diagnostic will be suppressed.
687
1.90M
  bool isLastDiagnosticIgnored() const {
688
1.90M
    return LastDiagLevel == DiagnosticIDs::Ignored;
689
1.90M
  }
690
691
  /// Controls whether otherwise-unmapped extension diagnostics are
692
  /// mapped onto ignore/warning/error.
693
  ///
694
  /// This corresponds to the GCC -pedantic and -pedantic-errors option.
695
113k
  void setExtensionHandlingBehavior(diag::Severity H) {
696
113k
    GetCurDiagState()->ExtBehavior = H;
697
113k
  }
698
16.3k
  diag::Severity getExtensionHandlingBehavior() const {
699
16.3k
    return GetCurDiagState()->ExtBehavior;
700
16.3k
  }
701
702
  /// Counter bumped when an __extension__  block is/ encountered.
703
  ///
704
  /// When non-zero, all extension diagnostics are entirely silenced, no
705
  /// matter how they are mapped.
706
35.3k
  void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
707
35.3k
  void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
708
76.5M
  bool hasAllExtensionsSilenced() { return AllExtensionsSilenced != 0; }
709
710
  /// This allows the client to specify that certain warnings are
711
  /// ignored.
712
  ///
713
  /// Notes can never be mapped, errors can only be mapped to fatal, and
714
  /// WARNINGs and EXTENSIONs can be mapped arbitrarily.
715
  ///
716
  /// \param Loc The source location that this change of diagnostic state should
717
  /// take affect. It can be null if we are setting the latest state.
718
  void setSeverity(diag::kind Diag, diag::Severity Map, SourceLocation Loc);
719
720
  /// Change an entire diagnostic group (e.g. "unknown-pragmas") to
721
  /// have the specified mapping.
722
  ///
723
  /// \returns true (and ignores the request) if "Group" was unknown, false
724
  /// otherwise.
725
  ///
726
  /// \param Flavor The flavor of group to affect. -Rfoo does not affect the
727
  /// state of the -Wfoo group and vice versa.
728
  ///
729
  /// \param Loc The source location that this change of diagnostic state should
730
  /// take affect. It can be null if we are setting the state from command-line.
731
  bool setSeverityForGroup(diag::Flavor Flavor, StringRef Group,
732
                           diag::Severity Map,
733
                           SourceLocation Loc = SourceLocation());
734
735
  /// Set the warning-as-error flag for the given diagnostic group.
736
  ///
737
  /// This function always only operates on the current diagnostic state.
738
  ///
739
  /// \returns True if the given group is unknown, false otherwise.
740
  bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled);
741
742
  /// Set the error-as-fatal flag for the given diagnostic group.
743
  ///
744
  /// This function always only operates on the current diagnostic state.
745
  ///
746
  /// \returns True if the given group is unknown, false otherwise.
747
  bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled);
748
749
  /// Add the specified mapping to all diagnostics of the specified
750
  /// flavor.
751
  ///
752
  /// Mainly to be used by -Wno-everything to disable all warnings but allow
753
  /// subsequent -W options to enable specific warnings.
754
  void setSeverityForAll(diag::Flavor Flavor, diag::Severity Map,
755
                         SourceLocation Loc = SourceLocation());
756
757
18.5M
  bool hasErrorOccurred() const { return ErrorOccurred; }
758
759
  /// Errors that actually prevent compilation, not those that are
760
  /// upgraded from a warning by -Werror.
761
10.1M
  bool hasUncompilableErrorOccurred() const {
762
10.1M
    return UncompilableErrorOccurred;
763
10.1M
  }
764
5.38M
  bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
765
766
  /// Determine whether any kind of unrecoverable error has occurred.
767
101
  bool hasUnrecoverableErrorOccurred() const {
768
101
    return FatalErrorOccurred || 
UnrecoverableErrorOccurred99
;
769
101
  }
770
771
271
  unsigned getNumWarnings() const { return NumWarnings; }
772
773
487
  void setNumWarnings(unsigned NumWarnings) {
774
487
    this->NumWarnings = NumWarnings;
775
487
  }
776
777
  /// Return an ID for a diagnostic with the specified format string and
778
  /// level.
779
  ///
780
  /// If this is the first request for this diagnostic, it is registered and
781
  /// created, otherwise the existing ID is returned.
782
  ///
783
  /// \param FormatString A fixed diagnostic format string that will be hashed
784
  /// and mapped to a unique DiagID.
785
  template <unsigned N>
786
3.79k
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
3.79k
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
3.79k
                                  StringRef(FormatString, N - 1));
789
3.79k
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<27u>(clang::DiagnosticsEngine::Level, char const (&) [27u])
Line
Count
Source
786
35
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
35
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
35
                                  StringRef(FormatString, N - 1));
789
35
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<37u>(clang::DiagnosticsEngine::Level, char const (&) [37u])
Line
Count
Source
786
2
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
2
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
2
                                  StringRef(FormatString, N - 1));
789
2
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<58u>(clang::DiagnosticsEngine::Level, char const (&) [58u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<38u>(clang::DiagnosticsEngine::Level, char const (&) [38u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<63u>(clang::DiagnosticsEngine::Level, char const (&) [63u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<50u>(clang::DiagnosticsEngine::Level, char const (&) [50u])
Line
Count
Source
786
2
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
2
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
2
                                  StringRef(FormatString, N - 1));
789
2
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<62u>(clang::DiagnosticsEngine::Level, char const (&) [62u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<46u>(clang::DiagnosticsEngine::Level, char const (&) [46u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<40u>(clang::DiagnosticsEngine::Level, char const (&) [40u])
Line
Count
Source
786
1
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
1
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
1
                                  StringRef(FormatString, N - 1));
789
1
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<34u>(clang::DiagnosticsEngine::Level, char const (&) [34u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<35u>(clang::DiagnosticsEngine::Level, char const (&) [35u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<66u>(clang::DiagnosticsEngine::Level, char const (&) [66u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<52u>(clang::DiagnosticsEngine::Level, char const (&) [52u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<44u>(clang::DiagnosticsEngine::Level, char const (&) [44u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<43u>(clang::DiagnosticsEngine::Level, char const (&) [43u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<60u>(clang::DiagnosticsEngine::Level, char const (&) [60u])
Line
Count
Source
786
2
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
2
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
2
                                  StringRef(FormatString, N - 1));
789
2
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<51u>(clang::DiagnosticsEngine::Level, char const (&) [51u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<42u>(clang::DiagnosticsEngine::Level, char const (&) [42u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<36u>(clang::DiagnosticsEngine::Level, char const (&) [36u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<49u>(clang::DiagnosticsEngine::Level, char const (&) [49u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<61u>(clang::DiagnosticsEngine::Level, char const (&) [61u])
Line
Count
Source
786
164
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
164
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
164
                                  StringRef(FormatString, N - 1));
789
164
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<20u>(clang::DiagnosticsEngine::Level, char const (&) [20u])
Line
Count
Source
786
2
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
2
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
2
                                  StringRef(FormatString, N - 1));
789
2
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<3u>(clang::DiagnosticsEngine::Level, char const (&) [3u])
Line
Count
Source
786
3.04k
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
3.04k
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
3.04k
                                  StringRef(FormatString, N - 1));
789
3.04k
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<47u>(clang::DiagnosticsEngine::Level, char const (&) [47u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<26u>(clang::DiagnosticsEngine::Level, char const (&) [26u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<54u>(clang::DiagnosticsEngine::Level, char const (&) [54u])
Line
Count
Source
786
250
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
250
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
250
                                  StringRef(FormatString, N - 1));
789
250
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<73u>(clang::DiagnosticsEngine::Level, char const (&) [73u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<82u>(clang::DiagnosticsEngine::Level, char const (&) [82u])
Line
Count
Source
786
6
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
6
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
6
                                  StringRef(FormatString, N - 1));
789
6
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<141u>(clang::DiagnosticsEngine::Level, char const (&) [141u])
Line
Count
Source
786
22
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
22
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
22
                                  StringRef(FormatString, N - 1));
789
22
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<96u>(clang::DiagnosticsEngine::Level, char const (&) [96u])
Line
Count
Source
786
2
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
2
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
2
                                  StringRef(FormatString, N - 1));
789
2
  }
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<86u>(clang::DiagnosticsEngine::Level, char const (&) [86u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<83u>(clang::DiagnosticsEngine::Level, char const (&) [83u])
Unexecuted instantiation: unsigned int clang::DiagnosticsEngine::getCustomDiagID<39u>(clang::DiagnosticsEngine::Level, char const (&) [39u])
unsigned int clang::DiagnosticsEngine::getCustomDiagID<30u>(clang::DiagnosticsEngine::Level, char const (&) [30u])
Line
Count
Source
786
1
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
1
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
1
                                  StringRef(FormatString, N - 1));
789
1
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<124u>(clang::DiagnosticsEngine::Level, char const (&) [124u])
Line
Count
Source
786
10
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
10
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
10
                                  StringRef(FormatString, N - 1));
789
10
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<95u>(clang::DiagnosticsEngine::Level, char const (&) [95u])
Line
Count
Source
786
2
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
2
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
2
                                  StringRef(FormatString, N - 1));
789
2
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<68u>(clang::DiagnosticsEngine::Level, char const (&) [68u])
Line
Count
Source
786
78
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
78
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
78
                                  StringRef(FormatString, N - 1));
789
78
  }
unsigned int clang::DiagnosticsEngine::getCustomDiagID<113u>(clang::DiagnosticsEngine::Level, char const (&) [113u])
Line
Count
Source
786
164
  unsigned getCustomDiagID(Level L, const char (&FormatString)[N]) {
787
164
    return Diags->getCustomDiagID((DiagnosticIDs::Level)L,
788
164
                                  StringRef(FormatString, N - 1));
789
164
  }
790
791
  /// Converts a diagnostic argument (as an intptr_t) into the string
792
  /// that represents it.
793
  void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
794
                          StringRef Modifier, StringRef Argument,
795
                          ArrayRef<ArgumentValue> PrevArgs,
796
                          SmallVectorImpl<char> &Output,
797
161k
                          ArrayRef<intptr_t> QualTypeVals) const {
798
161k
    ArgToStringFn(Kind, Val, Modifier, Argument, PrevArgs, Output,
799
161k
                  ArgToStringCookie, QualTypeVals);
800
161k
  }
801
802
75.9k
  void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {
803
75.9k
    ArgToStringFn = Fn;
804
75.9k
    ArgToStringCookie = Cookie;
805
75.9k
  }
806
807
  /// Note that the prior diagnostic was emitted by some other
808
  /// \c DiagnosticsEngine, and we may be attaching a note to that diagnostic.
809
457
  void notePriorDiagnosticFrom(const DiagnosticsEngine &Other) {
810
457
    LastDiagLevel = Other.LastDiagLevel;
811
457
  }
812
813
  /// Reset the state of the diagnostic object to its initial
814
  /// configuration.
815
  void Reset();
816
817
  //===--------------------------------------------------------------------===//
818
  // DiagnosticsEngine classification and reporting interfaces.
819
  //
820
821
  /// Determine whether the diagnostic is known to be ignored.
822
  ///
823
  /// This can be used to opportunistically avoid expensive checks when it's
824
  /// known for certain that the diagnostic has been suppressed at the
825
  /// specified location \p Loc.
826
  ///
827
  /// \param Loc The source location we are interested in finding out the
828
  /// diagnostic state. Can be null in order to query the latest state.
829
141M
  bool isIgnored(unsigned DiagID, SourceLocation Loc) const {
830
141M
    return Diags->getDiagnosticSeverity(DiagID, Loc, *this) ==
831
141M
           diag::Severity::Ignored;
832
141M
  }
833
834
  /// Based on the way the client configured the DiagnosticsEngine
835
  /// object, classify the specified diagnostic ID into a Level, consumable by
836
  /// the DiagnosticConsumer.
837
  ///
838
  /// To preserve invariant assumptions, this function should not be used to
839
  /// influence parse or semantic analysis actions. Instead consider using
840
  /// \c isIgnored().
841
  ///
842
  /// \param Loc The source location we are interested in finding out the
843
  /// diagnostic state. Can be null in order to query the latest state.
844
56.3k
  Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc) const {
845
56.3k
    return (Level)Diags->getDiagnosticLevel(DiagID, Loc, *this);
846
56.3k
  }
847
848
  /// Issue the message to the client.
849
  ///
850
  /// This actually returns an instance of DiagnosticBuilder which emits the
851
  /// diagnostics (through @c ProcessDiag) when it is destroyed.
852
  ///
853
  /// \param DiagID A member of the @c diag::kind enum.
854
  /// \param Loc Represents the source location associated with the diagnostic,
855
  /// which can be an invalid location if no position information is available.
856
  inline DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID);
857
  inline DiagnosticBuilder Report(unsigned DiagID);
858
859
  void Report(const StoredDiagnostic &storedDiag);
860
861
  /// Determine whethere there is already a diagnostic in flight.
862
33
  bool isDiagnosticInFlight() const {
863
33
    return CurDiagID != std::numeric_limits<unsigned>::max();
864
33
  }
865
866
  /// Set the "delayed" diagnostic that will be emitted once
867
  /// the current diagnostic completes.
868
  ///
869
  ///  If a diagnostic is already in-flight but the front end must
870
  ///  report a problem (e.g., with an inconsistent file system
871
  ///  state), this routine sets a "delayed" diagnostic that will be
872
  ///  emitted after the current diagnostic completes. This should
873
  ///  only be used for fatal errors detected at inconvenient
874
  ///  times. If emitting a delayed diagnostic causes a second delayed
875
  ///  diagnostic to be introduced, that second delayed diagnostic
876
  ///  will be ignored.
877
  ///
878
  /// \param DiagID The ID of the diagnostic being delayed.
879
  ///
880
  /// \param Arg1 A string argument that will be provided to the
881
  /// diagnostic. A copy of this string will be stored in the
882
  /// DiagnosticsEngine object itself.
883
  ///
884
  /// \param Arg2 A string argument that will be provided to the
885
  /// diagnostic. A copy of this string will be stored in the
886
  /// DiagnosticsEngine object itself.
887
  ///
888
  /// \param Arg3 A string argument that will be provided to the
889
  /// diagnostic. A copy of this string will be stored in the
890
  /// DiagnosticsEngine object itself.
891
  void SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1 = "",
892
                            StringRef Arg2 = "", StringRef Arg3 = "");
893
894
  /// Clear out the current diagnostic.
895
6.64M
  void Clear() { CurDiagID = std::numeric_limits<unsigned>::max(); }
896
897
  /// Return the value associated with this diagnostic flag.
898
11.0k
  StringRef getFlagValue() const { return FlagValue; }
899
900
private:
901
  // This is private state used by DiagnosticBuilder.  We put it here instead of
902
  // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight
903
  // object.  This implementation choice means that we can only have one
904
  // diagnostic "in flight" at a time, but this seems to be a reasonable
905
  // tradeoff to keep these objects small.  Assertions verify that only one
906
  // diagnostic is in flight at a time.
907
  friend class Diagnostic;
908
  friend class DiagnosticBuilder;
909
  friend class DiagnosticErrorTrap;
910
  friend class DiagnosticIDs;
911
  friend class PartialDiagnostic;
912
913
  /// Report the delayed diagnostic.
914
  void ReportDelayed();
915
916
  /// The location of the current diagnostic that is in flight.
917
  SourceLocation CurDiagLoc;
918
919
  /// The ID of the current diagnostic that is in flight.
920
  ///
921
  /// This is set to std::numeric_limits<unsigned>::max() when there is no
922
  /// diagnostic in flight.
923
  unsigned CurDiagID;
924
925
  enum {
926
    /// The maximum number of arguments we can hold.
927
    ///
928
    /// We currently only support up to 10 arguments (%0-%9).  A single
929
    /// diagnostic with more than that almost certainly has to be simplified
930
    /// anyway.
931
    MaxArguments = 10,
932
  };
933
934
  /// The number of entries in Arguments.
935
  signed char NumDiagArgs;
936
937
  /// Specifies whether an argument is in DiagArgumentsStr or
938
  /// in DiagArguments.
939
  ///
940
  /// This is an array of ArgumentKind::ArgumentKind enum values, one for each
941
  /// argument.
942
  unsigned char DiagArgumentsKind[MaxArguments];
943
944
  /// Holds the values of each string argument for the current
945
  /// diagnostic.
946
  ///
947
  /// This is only used when the corresponding ArgumentKind is ak_std_string.
948
  std::string DiagArgumentsStr[MaxArguments];
949
950
  /// The values for the various substitution positions.
951
  ///
952
  /// This is used when the argument is not an std::string.  The specific
953
  /// value is mangled into an intptr_t and the interpretation depends on
954
  /// exactly what sort of argument kind it is.
955
  intptr_t DiagArgumentsVal[MaxArguments];
956
957
  /// The list of ranges added to this diagnostic.
958
  SmallVector<CharSourceRange, 8> DiagRanges;
959
960
  /// If valid, provides a hint with some code to insert, remove,
961
  /// or modify at a particular position.
962
  SmallVector<FixItHint, 8> DiagFixItHints;
963
964
2.01M
  DiagnosticMapping makeUserMapping(diag::Severity Map, SourceLocation L) {
965
2.01M
    bool isPragma = L.isValid();
966
2.01M
    DiagnosticMapping Mapping =
967
2.01M
        DiagnosticMapping::Make(Map, /*IsUser=*/true, isPragma);
968
2.01M
969
    // If this is a pragma mapping, then set the diagnostic mapping flags so
970
    // that we override command line options.
971
2.01M
    if (isPragma) {
972
518k
      Mapping.setNoWarningAsError(true);
973
518k
      Mapping.setNoErrorAsFatal(true);
974
518k
    }
975
2.01M
976
2.01M
    return Mapping;
977
2.01M
  }
978
979
  /// Used to report a diagnostic that is finally fully formed.
980
  ///
981
  /// \returns true if the diagnostic was emitted, false if it was suppressed.
982
6.51M
  bool ProcessDiag() {
983
6.51M
    return Diags->ProcessDiag(*this);
984
6.51M
  }
985
986
  /// @name Diagnostic Emission
987
  /// @{
988
protected:
989
  friend class ASTReader;
990
  friend class ASTWriter;
991
992
  // Sema requires access to the following functions because the current design
993
  // of SFINAE requires it to use its own SemaDiagnosticBuilder, which needs to
994
  // access us directly to ensure we minimize the emitted code for the common
995
  // Sema::Diag() patterns.
996
  friend class Sema;
997
998
  /// Emit the current diagnostic and clear the diagnostic state.
999
  ///
1000
  /// \param Force Emit the diagnostic regardless of suppression settings.
1001
  bool EmitCurrentDiagnostic(bool Force = false);
1002
1003
121k
  unsigned getCurrentDiagID() const { return CurDiagID; }
1004
1005
93
  SourceLocation getCurrentDiagLoc() const { return CurDiagLoc; }
1006
1007
  /// @}
1008
};
1009
1010
/// RAII class that determines when any errors have occurred
1011
/// between the time the instance was created and the time it was
1012
/// queried.
1013
///
1014
/// Note that you almost certainly do not want to use this. It's usually
1015
/// meaningless to ask whether a particular scope triggered an error message,
1016
/// because error messages outside that scope can mark things invalid (or cause
1017
/// us to reach an error limit), which can suppress errors within that scope.
1018
class DiagnosticErrorTrap {
1019
  DiagnosticsEngine &Diag;
1020
  unsigned NumErrors;
1021
  unsigned NumUnrecoverableErrors;
1022
1023
public:
1024
  explicit DiagnosticErrorTrap(DiagnosticsEngine &Diag)
1025
1.02M
      : Diag(Diag) { reset(); }
1026
1027
  /// Determine whether any errors have occurred since this
1028
  /// object instance was created.
1029
66
  bool hasErrorOccurred() const {
1030
66
    return Diag.TrapNumErrorsOccurred > NumErrors;
1031
66
  }
1032
1033
  /// Determine whether any unrecoverable errors have occurred since this
1034
  /// object instance was created.
1035
52.0M
  bool hasUnrecoverableErrorOccurred() const {
1036
52.0M
    return Diag.TrapNumUnrecoverableErrorsOccurred > NumUnrecoverableErrors;
1037
52.0M
  }
1038
1039
  /// Set to initial state of "no errors occurred".
1040
28.1M
  void reset() {
1041
28.1M
    NumErrors = Diag.TrapNumErrorsOccurred;
1042
28.1M
    NumUnrecoverableErrors = Diag.TrapNumUnrecoverableErrorsOccurred;
1043
28.1M
  }
1044
};
1045
1046
//===----------------------------------------------------------------------===//
1047
// DiagnosticBuilder
1048
//===----------------------------------------------------------------------===//
1049
1050
/// A little helper class used to produce diagnostics.
1051
///
1052
/// This is constructed by the DiagnosticsEngine::Report method, and
1053
/// allows insertion of extra information (arguments and source ranges) into
1054
/// the currently "in flight" diagnostic.  When the temporary for the builder
1055
/// is destroyed, the diagnostic is issued.
1056
///
1057
/// Note that many of these will be created as temporary objects (many call
1058
/// sites), so we want them to be small and we never want their address taken.
1059
/// This ensures that compilers with somewhat reasonable optimizers will promote
1060
/// the common fields to registers, eliminating increments of the NumArgs field,
1061
/// for example.
1062
class DiagnosticBuilder {
1063
  friend class DiagnosticsEngine;
1064
  friend class PartialDiagnostic;
1065
1066
  mutable DiagnosticsEngine *DiagObj = nullptr;
1067
  mutable unsigned NumArgs = 0;
1068
1069
  /// Status variable indicating if this diagnostic is still active.
1070
  ///
1071
  // NOTE: This field is redundant with DiagObj (IsActive iff (DiagObj == 0)),
1072
  // but LLVM is not currently smart enough to eliminate the null check that
1073
  // Emit() would end up with if we used that as our status variable.
1074
  mutable bool IsActive = false;
1075
1076
  /// Flag indicating that this diagnostic is being emitted via a
1077
  /// call to ForceEmit.
1078
  mutable bool IsForceEmit = false;
1079
1080
  DiagnosticBuilder() = default;
1081
1082
  explicit DiagnosticBuilder(DiagnosticsEngine *diagObj)
1083
6.63M
      : DiagObj(diagObj), IsActive(true) {
1084
6.63M
    assert(diagObj && "DiagnosticBuilder requires a valid DiagnosticsEngine!");
1085
6.63M
    diagObj->DiagRanges.clear();
1086
6.63M
    diagObj->DiagFixItHints.clear();
1087
6.63M
  }
1088
1089
protected:
1090
6.63M
  void FlushCounts() {
1091
6.63M
    DiagObj->NumDiagArgs = NumArgs;
1092
6.63M
  }
1093
1094
  /// Clear out the current diagnostic.
1095
9.33M
  void Clear() const {
1096
9.33M
    DiagObj = nullptr;
1097
9.33M
    IsActive = false;
1098
9.33M
    IsForceEmit = false;
1099
9.33M
  }
1100
1101
  /// Determine whether this diagnostic is still active.
1102
16.6M
  bool isActive() const { return IsActive; }
1103
1104
  /// Force the diagnostic builder to emit the diagnostic now.
1105
  ///
1106
  /// Once this function has been called, the DiagnosticBuilder object
1107
  /// should not be used again before it is destroyed.
1108
  ///
1109
  /// \returns true if a diagnostic was emitted, false if the
1110
  /// diagnostic was suppressed.
1111
9.33M
  bool Emit() {
1112
    // If this diagnostic is inactive, then its soul was stolen by the copy ctor
1113
    // (or by a subclass, as in SemaDiagnosticBuilder).
1114
9.33M
    if (!isActive()) 
return false5.38M
;
1115
3.95M
1116
    // When emitting diagnostics, we set the final argument count into
1117
    // the DiagnosticsEngine object.
1118
3.95M
    FlushCounts();
1119
3.95M
1120
    // Process the diagnostic.
1121
3.95M
    bool Result = DiagObj->EmitCurrentDiagnostic(IsForceEmit);
1122
3.95M
1123
    // This diagnostic is dead.
1124
3.95M
    Clear();
1125
3.95M
1126
3.95M
    return Result;
1127
3.95M
  }
1128
1129
public:
1130
  /// Copy constructor.  When copied, this "takes" the diagnostic info from the
1131
  /// input and neuters it.
1132
2.69M
  DiagnosticBuilder(const DiagnosticBuilder &D) {
1133
2.69M
    DiagObj = D.DiagObj;
1134
2.69M
    IsActive = D.IsActive;
1135
2.69M
    IsForceEmit = D.IsForceEmit;
1136
2.69M
    D.Clear();
1137
2.69M
    NumArgs = D.NumArgs;
1138
2.69M
  }
1139
1140
  DiagnosticBuilder &operator=(const DiagnosticBuilder &) = delete;
1141
1142
  /// Emits the diagnostic.
1143
9.33M
  ~DiagnosticBuilder() {
1144
9.33M
    Emit();
1145
9.33M
  }
1146
1147
  /// Forces the diagnostic to be emitted.
1148
125
  const DiagnosticBuilder &setForceEmit() const {
1149
125
    IsForceEmit = true;
1150
125
    return *this;
1151
125
  }
1152
1153
  /// Conversion of DiagnosticBuilder to bool always returns \c true.
1154
  ///
1155
  /// This allows is to be used in boolean error contexts (where \c true is
1156
  /// used to indicate that an error has occurred), like:
1157
  /// \code
1158
  /// return Diag(...);
1159
  /// \endcode
1160
6.50k
  operator bool() const { return true; }
1161
1162
424k
  void AddString(StringRef S) const {
1163
424k
    assert(isActive() && "Clients must not add to cleared diagnostic!");
1164
424k
    assert(NumArgs < DiagnosticsEngine::MaxArguments &&
1165
424k
           "Too many arguments to diagnostic!");
1166
424k
    DiagObj->DiagArgumentsKind[NumArgs] = DiagnosticsEngine::ak_std_string;
1167
424k
    DiagObj->DiagArgumentsStr[NumArgs++] = std::string(S);
1168
424k
  }
1169
1170
2.53M
  void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
1171
2.53M
    assert(isActive() && "Clients must not add to cleared diagnostic!");
1172
2.53M
    assert(NumArgs < DiagnosticsEngine::MaxArguments &&
1173
2.53M
           "Too many arguments to diagnostic!");
1174
2.53M
    DiagObj->DiagArgumentsKind[NumArgs] = Kind;
1175
2.53M
    DiagObj->DiagArgumentsVal[NumArgs++] = V;
1176
2.53M
  }
1177
1178
1.35M
  void AddSourceRange(const CharSourceRange &R) const {
1179
1.35M
    assert(isActive() && "Clients must not add to cleared diagnostic!");
1180
1.35M
    DiagObj->DiagRanges.push_back(R);
1181
1.35M
  }
1182
1183
323k
  void AddFixItHint(const FixItHint &Hint) const {
1184
323k
    assert(isActive() && "Clients must not add to cleared diagnostic!");
1185
323k
    if (!Hint.isNull())
1186
274k
      DiagObj->DiagFixItHints.push_back(Hint);
1187
323k
  }
1188
1189
4.66k
  void addFlagValue(StringRef V) const { DiagObj->FlagValue = std::string(V); }
1190
};
1191
1192
struct AddFlagValue {
1193
  StringRef Val;
1194
1195
4.66k
  explicit AddFlagValue(StringRef V) : Val(V) {}
1196
};
1197
1198
/// Register a value for the flag in the current diagnostic. This
1199
/// value will be shown as the suffix "=value" after the flag name. It is
1200
/// useful in cases where the diagnostic flag accepts values (e.g.,
1201
/// -Rpass or -Wframe-larger-than).
1202
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1203
4.66k
                                           const AddFlagValue V) {
1204
4.66k
  DB.addFlagValue(V.Val);
1205
4.66k
  return DB;
1206
4.66k
}
1207
1208
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1209
398k
                                           StringRef S) {
1210
398k
  DB.AddString(S);
1211
398k
  return DB;
1212
398k
}
1213
1214
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1215
55.6k
                                           const char *Str) {
1216
55.6k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str),
1217
55.6k
                  DiagnosticsEngine::ak_c_string);
1218
55.6k
  return DB;
1219
55.6k
}
1220
1221
310k
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) {
1222
310k
  DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
1223
310k
  return DB;
1224
310k
}
1225
1226
// We use enable_if here to prevent that this overload is selected for
1227
// pointers or other arguments that are implicitly convertible to bool.
1228
template <typename T>
1229
inline std::enable_if_t<std::is_same<T, bool>::value, const DiagnosticBuilder &>
1230
142k
operator<<(const DiagnosticBuilder &DB, T I) {
1231
142k
  DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
1232
142k
  return DB;
1233
142k
}
1234
1235
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1236
126k
                                           unsigned I) {
1237
126k
  DB.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
1238
126k
  return DB;
1239
126k
}
1240
1241
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1242
42.9k
                                           tok::TokenKind I) {
1243
42.9k
  DB.AddTaggedVal(static_cast<unsigned>(I), DiagnosticsEngine::ak_tokenkind);
1244
42.9k
  return DB;
1245
42.9k
}
1246
1247
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1248
509k
                                           const IdentifierInfo *II) {
1249
509k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(II),
1250
509k
                  DiagnosticsEngine::ak_identifierinfo);
1251
509k
  return DB;
1252
509k
}
1253
1254
// Adds a DeclContext to the diagnostic. The enable_if template magic is here
1255
// so that we only match those arguments that are (statically) DeclContexts;
1256
// other arguments that derive from DeclContext (e.g., RecordDecls) will not
1257
// match.
1258
template <typename T>
1259
inline std::enable_if_t<
1260
    std::is_same<std::remove_const_t<T>, DeclContext>::value,
1261
    const DiagnosticBuilder &>
1262
34.2k
operator<<(const DiagnosticBuilder &DB, T *DC) {
1263
34.2k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
1264
34.2k
                  DiagnosticsEngine::ak_declcontext);
1265
34.2k
  return DB;
1266
34.2k
}
std::__1::enable_if<std::is_same<std::__1::remove_const<clang::DeclContext>::type, clang::DeclContext>::value, clang::DiagnosticBuilder const&>::type clang::operator<<<clang::DeclContext>(clang::DiagnosticBuilder const&, clang::DeclContext*)
Line
Count
Source
1262
34.1k
operator<<(const DiagnosticBuilder &DB, T *DC) {
1263
34.1k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
1264
34.1k
                  DiagnosticsEngine::ak_declcontext);
1265
34.1k
  return DB;
1266
34.1k
}
std::__1::enable_if<std::is_same<std::__1::remove_const<clang::DeclContext const>::type, clang::DeclContext>::value, clang::DiagnosticBuilder const&>::type clang::operator<<<clang::DeclContext const>(clang::DiagnosticBuilder const&, clang::DeclContext const*)
Line
Count
Source
1262
52
operator<<(const DiagnosticBuilder &DB, T *DC) {
1263
52
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
1264
52
                  DiagnosticsEngine::ak_declcontext);
1265
52
  return DB;
1266
52
}
1267
1268
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1269
1.10M
                                           SourceRange R) {
1270
1.10M
  DB.AddSourceRange(CharSourceRange::getTokenRange(R));
1271
1.10M
  return DB;
1272
1.10M
}
1273
1274
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1275
19.3k
                                           ArrayRef<SourceRange> Ranges) {
1276
19.3k
  for (SourceRange R : Ranges)
1277
29.5k
    DB.AddSourceRange(CharSourceRange::getTokenRange(R));
1278
19.3k
  return DB;
1279
19.3k
}
1280
1281
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1282
5.83k
                                           const CharSourceRange &R) {
1283
5.83k
  DB.AddSourceRange(R);
1284
5.83k
  return DB;
1285
5.83k
}
1286
1287
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1288
320k
                                           const FixItHint &Hint) {
1289
320k
  DB.AddFixItHint(Hint);
1290
320k
  return DB;
1291
320k
}
1292
1293
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1294
65.9k
                                           ArrayRef<FixItHint> Hints) {
1295
65.9k
  for (const FixItHint &Hint : Hints)
1296
1.33k
    DB.AddFixItHint(Hint);
1297
65.9k
  return DB;
1298
65.9k
}
1299
1300
inline const DiagnosticBuilder &
1301
operator<<(const DiagnosticBuilder &DB,
1302
0
           const llvm::Optional<SourceRange> &Opt) {
1303
0
  if (Opt)
1304
0
    DB << *Opt;
1305
0
  return DB;
1306
0
}
1307
1308
inline const DiagnosticBuilder &
1309
operator<<(const DiagnosticBuilder &DB,
1310
0
           const llvm::Optional<CharSourceRange> &Opt) {
1311
0
  if (Opt)
1312
0
    DB << *Opt;
1313
0
  return DB;
1314
0
}
1315
1316
inline const DiagnosticBuilder &
1317
0
operator<<(const DiagnosticBuilder &DB, const llvm::Optional<FixItHint> &Opt) {
1318
0
  if (Opt)
1319
0
    DB << *Opt;
1320
0
  return DB;
1321
0
}
1322
1323
/// A nullability kind paired with a bit indicating whether it used a
1324
/// context-sensitive keyword.
1325
using DiagNullabilityKind = std::pair<NullabilityKind, bool>;
1326
1327
const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1328
                                    DiagNullabilityKind nullability);
1329
1330
inline DiagnosticBuilder DiagnosticsEngine::Report(SourceLocation Loc,
1331
6.63M
                                                   unsigned DiagID) {
1332
6.63M
  assert(CurDiagID == std::numeric_limits<unsigned>::max() &&
1333
6.63M
         "Multiple diagnostics in flight at once!");
1334
6.63M
  CurDiagLoc = Loc;
1335
6.63M
  CurDiagID = DiagID;
1336
6.63M
  FlagValue.clear();
1337
6.63M
  return DiagnosticBuilder(this);
1338
6.63M
}
1339
1340
const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1341
                                    llvm::Error &&E);
1342
1343
9.15k
inline DiagnosticBuilder DiagnosticsEngine::Report(unsigned DiagID) {
1344
9.15k
  return Report(SourceLocation(), DiagID);
1345
9.15k
}
1346
1347
//===----------------------------------------------------------------------===//
1348
// Diagnostic
1349
//===----------------------------------------------------------------------===//
1350
1351
/// A little helper class (which is basically a smart pointer that forwards
1352
/// info from DiagnosticsEngine) that allows clients to enquire about the
1353
/// currently in-flight diagnostic.
1354
class Diagnostic {
1355
  const DiagnosticsEngine *DiagObj;
1356
  StringRef StoredDiagMessage;
1357
1358
public:
1359
7.02M
  explicit Diagnostic(const DiagnosticsEngine *DO) : DiagObj(DO) {}
1360
  Diagnostic(const DiagnosticsEngine *DO, StringRef storedDiagMessage)
1361
150
      : DiagObj(DO), StoredDiagMessage(storedDiagMessage) {}
1362
1363
615k
  const DiagnosticsEngine *getDiags() const { return DiagObj; }
1364
7.10M
  unsigned getID() const { return DiagObj->CurDiagID; }
1365
7.41M
  const SourceLocation &getLocation() const { return DiagObj->CurDiagLoc; }
1366
394k
  bool hasSourceManager() const { return DiagObj->hasSourceManager(); }
1367
743k
  SourceManager &getSourceManager() const { return DiagObj->getSourceManager();}
1368
1369
4.67M
  unsigned getNumArgs() const { return DiagObj->NumDiagArgs; }
1370
1371
  /// Return the kind of the specified index.
1372
  ///
1373
  /// Based on the kind of argument, the accessors below can be used to get
1374
  /// the value.
1375
  ///
1376
  /// \pre Idx < getNumArgs()
1377
3.95M
  DiagnosticsEngine::ArgumentKind getArgKind(unsigned Idx) const {
1378
3.95M
    assert(Idx < getNumArgs() && "Argument index out of range!");
1379
3.95M
    return (DiagnosticsEngine::ArgumentKind)DiagObj->DiagArgumentsKind[Idx];
1380
3.95M
  }
1381
1382
  /// Return the provided argument string specified by \p Idx.
1383
  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_std_string
1384
289k
  const std::string &getArgStdStr(unsigned Idx) const {
1385
289k
    assert(getArgKind(Idx) == DiagnosticsEngine::ak_std_string &&
1386
289k
           "invalid argument accessor!");
1387
289k
    return DiagObj->DiagArgumentsStr[Idx];
1388
289k
  }
1389
1390
  /// Return the specified C string argument.
1391
  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_c_string
1392
15.2k
  const char *getArgCStr(unsigned Idx) const {
1393
15.2k
    assert(getArgKind(Idx) == DiagnosticsEngine::ak_c_string &&
1394
15.2k
           "invalid argument accessor!");
1395
15.2k
    return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]);
1396
15.2k
  }
1397
1398
  /// Return the specified signed integer argument.
1399
  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_sint
1400
120k
  int getArgSInt(unsigned Idx) const {
1401
120k
    assert(getArgKind(Idx) == DiagnosticsEngine::ak_sint &&
1402
120k
           "invalid argument accessor!");
1403
120k
    return (int)DiagObj->DiagArgumentsVal[Idx];
1404
120k
  }
1405
1406
  /// Return the specified unsigned integer argument.
1407
  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_uint
1408
88.3k
  unsigned getArgUInt(unsigned Idx) const {
1409
88.3k
    assert(getArgKind(Idx) == DiagnosticsEngine::ak_uint &&
1410
88.3k
           "invalid argument accessor!");
1411
88.3k
    return (unsigned)DiagObj->DiagArgumentsVal[Idx];
1412
88.3k
  }
1413
1414
  /// Return the specified IdentifierInfo argument.
1415
  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_identifierinfo
1416
14.2k
  const IdentifierInfo *getArgIdentifier(unsigned Idx) const {
1417
14.2k
    assert(getArgKind(Idx) == DiagnosticsEngine::ak_identifierinfo &&
1418
14.2k
           "invalid argument accessor!");
1419
14.2k
    return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]);
1420
14.2k
  }
1421
1422
  /// Return the specified non-string argument in an opaque form.
1423
  /// \pre getArgKind(Idx) != DiagnosticsEngine::ak_std_string
1424
1.01M
  intptr_t getRawArg(unsigned Idx) const {
1425
1.01M
    assert(getArgKind(Idx) != DiagnosticsEngine::ak_std_string &&
1426
1.01M
           "invalid argument accessor!");
1427
1.01M
    return DiagObj->DiagArgumentsVal[Idx];
1428
1.01M
  }
1429
1430
  /// Return the number of source ranges associated with this diagnostic.
1431
220k
  unsigned getNumRanges() const {
1432
220k
    return DiagObj->DiagRanges.size();
1433
220k
  }
1434
1435
  /// \pre Idx < getNumRanges()
1436
107k
  const CharSourceRange &getRange(unsigned Idx) const {
1437
107k
    assert(Idx < getNumRanges() && "Invalid diagnostic range index!");
1438
107k
    return DiagObj->DiagRanges[Idx];
1439
107k
  }
1440
1441
  /// Return an array reference for this diagnostic's ranges.
1442
47.1k
  ArrayRef<CharSourceRange> getRanges() const {
1443
47.1k
    return DiagObj->DiagRanges;
1444
47.1k
  }
1445
1446
116k
  unsigned getNumFixItHints() const {
1447
116k
    return DiagObj->DiagFixItHints.size();
1448
116k
  }
1449
1450
1.02k
  const FixItHint &getFixItHint(unsigned Idx) const {
1451
1.02k
    assert(Idx < getNumFixItHints() && "Invalid index!");
1452
1.02k
    return DiagObj->DiagFixItHints[Idx];
1453
1.02k
  }
1454
1455
47.9k
  ArrayRef<FixItHint> getFixItHints() const {
1456
47.9k
    return DiagObj->DiagFixItHints;
1457
47.9k
  }
1458
1459
  /// Format this diagnostic into a string, substituting the
1460
  /// formal arguments into the %0 slots.
1461
  ///
1462
  /// The result is appended onto the \p OutStr array.
1463
  void FormatDiagnostic(SmallVectorImpl<char> &OutStr) const;
1464
1465
  /// Format the given format-string into the output buffer using the
1466
  /// arguments stored in this diagnostic.
1467
  void FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
1468
                        SmallVectorImpl<char> &OutStr) const;
1469
};
1470
1471
/**
1472
 * Represents a diagnostic in a form that can be retained until its
1473
 * corresponding source manager is destroyed.
1474
 */
1475
class StoredDiagnostic {
1476
  unsigned ID;
1477
  DiagnosticsEngine::Level Level;
1478
  FullSourceLoc Loc;
1479
  std::string Message;
1480
  std::vector<CharSourceRange> Ranges;
1481
  std::vector<FixItHint> FixIts;
1482
1483
public:
1484
  StoredDiagnostic() = default;
1485
  StoredDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info);
1486
  StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1487
                   StringRef Message);
1488
  StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1489
                   StringRef Message, FullSourceLoc Loc,
1490
                   ArrayRef<CharSourceRange> Ranges,
1491
                   ArrayRef<FixItHint> Fixits);
1492
1493
  /// Evaluates true when this object stores a diagnostic.
1494
0
  explicit operator bool() const { return !Message.empty(); }
1495
1496
22.6k
  unsigned getID() const { return ID; }
1497
9.53k
  DiagnosticsEngine::Level getLevel() const { return Level; }
1498
27.5k
  const FullSourceLoc &getLocation() const { return Loc; }
1499
2.07k
  StringRef getMessage() const { return Message; }
1500
1501
95
  void setLocation(FullSourceLoc Loc) { this->Loc = Loc; }
1502
1503
  using range_iterator = std::vector<CharSourceRange>::const_iterator;
1504
1505
251
  range_iterator range_begin() const { return Ranges.begin(); }
1506
151
  range_iterator range_end() const { return Ranges.end(); }
1507
879
  unsigned range_size() const { return Ranges.size(); }
1508
1509
665
  ArrayRef<CharSourceRange> getRanges() const {
1510
665
    return llvm::makeArrayRef(Ranges);
1511
665
  }
1512
1513
  using fixit_iterator = std::vector<FixItHint>::const_iterator;
1514
1515
187
  fixit_iterator fixit_begin() const { return FixIts.begin(); }
1516
150
  fixit_iterator fixit_end() const { return FixIts.end(); }
1517
816
  unsigned fixit_size() const { return FixIts.size(); }
1518
1519
663
  ArrayRef<FixItHint> getFixIts() const {
1520
663
    return llvm::makeArrayRef(FixIts);
1521
663
  }
1522
};
1523
1524
/// Abstract interface, implemented by clients of the front-end, which
1525
/// formats and prints fully processed diagnostics.
1526
class DiagnosticConsumer {
1527
protected:
1528
  unsigned NumWarnings = 0;       ///< Number of warnings reported
1529
  unsigned NumErrors = 0;         ///< Number of errors reported
1530
1531
public:
1532
263k
  DiagnosticConsumer() = default;
1533
  virtual ~DiagnosticConsumer();
1534
1535
133k
  unsigned getNumErrors() const { return NumErrors; }
1536
58.5k
  unsigned getNumWarnings() const { return NumWarnings; }
1537
148
  virtual void clear() { NumWarnings = NumErrors = 0; }
1538
1539
  /// Callback to inform the diagnostic client that processing
1540
  /// of a source file is beginning.
1541
  ///
1542
  /// Note that diagnostics may be emitted outside the processing of a source
1543
  /// file, for example during the parsing of command line options. However,
1544
  /// diagnostics with source range information are required to only be emitted
1545
  /// in between BeginSourceFile() and EndSourceFile().
1546
  ///
1547
  /// \param LangOpts The language options for the source file being processed.
1548
  /// \param PP The preprocessor object being used for the source; this is
1549
  /// optional, e.g., it may not be present when processing AST source files.
1550
  virtual void BeginSourceFile(const LangOptions &LangOpts,
1551
1.91k
                               const Preprocessor *PP = nullptr) {}
1552
1553
  /// Callback to inform the diagnostic client that processing
1554
  /// of a source file has ended.
1555
  ///
1556
  /// The diagnostic client should assume that any objects made available via
1557
  /// BeginSourceFile() are inaccessible.
1558
4.79k
  virtual void EndSourceFile() {}
1559
1560
  /// Callback to inform the diagnostic client that processing of all
1561
  /// source files has ended.
1562
78.6k
  virtual void finish() {}
1563
1564
  /// Indicates whether the diagnostics handled by this
1565
  /// DiagnosticConsumer should be included in the number of diagnostics
1566
  /// reported by DiagnosticsEngine.
1567
  ///
1568
  /// The default implementation returns true.
1569
  virtual bool IncludeInDiagnosticCounts() const;
1570
1571
  /// Handle this diagnostic, reporting it to the user or
1572
  /// capturing it to a log as needed.
1573
  ///
1574
  /// The default implementation just keeps track of the total number of
1575
  /// warnings and errors.
1576
  virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
1577
                                const Diagnostic &Info);
1578
};
1579
1580
/// A diagnostic client that ignores all diagnostics.
1581
class IgnoringDiagConsumer : public DiagnosticConsumer {
1582
  virtual void anchor();
1583
1584
  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
1585
58
                        const Diagnostic &Info) override {
1586
    // Just ignore it.
1587
58
  }
1588
};
1589
1590
/// Diagnostic consumer that forwards diagnostics along to an
1591
/// existing, already-initialized diagnostic consumer.
1592
///
1593
class ForwardingDiagnosticConsumer : public DiagnosticConsumer {
1594
  DiagnosticConsumer &Target;
1595
1596
public:
1597
1.72k
  ForwardingDiagnosticConsumer(DiagnosticConsumer &Target) : Target(Target) {}
1598
  ~ForwardingDiagnosticConsumer() override;
1599
1600
  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
1601
                        const Diagnostic &Info) override;
1602
  void clear() override;
1603
1604
  bool IncludeInDiagnosticCounts() const override;
1605
};
1606
1607
// Struct used for sending info about how a type should be printed.
1608
struct TemplateDiffTypes {
1609
  intptr_t FromType;
1610
  intptr_t ToType;
1611
  unsigned PrintTree : 1;
1612
  unsigned PrintFromType : 1;
1613
  unsigned ElideType : 1;
1614
  unsigned ShowColors : 1;
1615
1616
  // The printer sets this variable to true if the template diff was used.
1617
  unsigned TemplateDiffUsed : 1;
1618
};
1619
1620
/// Special character that the diagnostic printer will use to toggle the bold
1621
/// attribute.  The character itself will be not be printed.
1622
const char ToggleHighlight = 127;
1623
1624
/// ProcessWarningOptions - Initialize the diagnostic client and process the
1625
/// warning options specified on the command line.
1626
void ProcessWarningOptions(DiagnosticsEngine &Diags,
1627
                           const DiagnosticOptions &Opts,
1628
                           bool ReportDiags = true);
1629
1630
} // namespace clang
1631
1632
#endif // LLVM_CLANG_BASIC_DIAGNOSTIC_H