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