1 //===--- DiagnosticIDs.h - Diagnostic IDs Handling --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the Diagnostic IDs-related interfaces.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_DIAGNOSTICIDS_H
16 #define LLVM_CLANG_DIAGNOSTICIDS_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/StringRef.h"
23 class DiagnosticsEngine;
27 // Import the diagnostic enums themselves.
29 // Start position for diagnostics.
31 DIAG_START_DRIVER = 300,
32 DIAG_START_FRONTEND = DIAG_START_DRIVER + 100,
33 DIAG_START_SERIALIZATION = DIAG_START_FRONTEND + 100,
34 DIAG_START_LEX = DIAG_START_SERIALIZATION + 120,
35 DIAG_START_PARSE = DIAG_START_LEX + 300,
36 DIAG_START_AST = DIAG_START_PARSE + 400,
37 DIAG_START_COMMENT = DIAG_START_AST + 100,
38 DIAG_START_SEMA = DIAG_START_COMMENT + 100,
39 DIAG_START_ANALYSIS = DIAG_START_SEMA + 3000,
40 DIAG_UPPER_LIMIT = DIAG_START_ANALYSIS + 100
45 /// \brief All of the diagnostics that can be emitted by the frontend.
46 typedef unsigned kind;
48 // Get typedefs for common diagnostics.
50 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,\
51 SFINAE,ACCESS,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
52 #include "clang/Basic/DiagnosticCommonKinds.inc"
53 NUM_BUILTIN_COMMON_DIAGNOSTICS
57 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
58 /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR
59 /// (emit as an error). It allows clients to map errors to
60 /// MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop emitting diagnostics after this
63 // NOTE: 0 means "uncomputed".
64 MAP_IGNORE = 1, ///< Map this diagnostic to nothing, ignore it.
65 MAP_WARNING = 2, ///< Map this diagnostic to a warning.
66 MAP_ERROR = 3, ///< Map this diagnostic to an error.
67 MAP_FATAL = 4 ///< Map this diagnostic to a fatal error.
71 class DiagnosticMappingInfo {
74 unsigned IsPragma : 1;
75 unsigned HasShowInSystemHeader : 1;
76 unsigned HasNoWarningAsError : 1;
77 unsigned HasNoErrorAsFatal : 1;
80 static DiagnosticMappingInfo Make(diag::Mapping Mapping, bool IsUser,
82 DiagnosticMappingInfo Result;
83 Result.Mapping = Mapping;
84 Result.IsUser = IsUser;
85 Result.IsPragma = IsPragma;
86 Result.HasShowInSystemHeader = 0;
87 Result.HasNoWarningAsError = 0;
88 Result.HasNoErrorAsFatal = 0;
92 diag::Mapping getMapping() const { return diag::Mapping(Mapping); }
93 void setMapping(diag::Mapping Value) { Mapping = Value; }
95 bool isUser() const { return IsUser; }
96 bool isPragma() const { return IsPragma; }
98 bool hasShowInSystemHeader() const { return HasShowInSystemHeader; }
99 void setShowInSystemHeader(bool Value) { HasShowInSystemHeader = Value; }
101 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
102 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
104 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
105 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
108 /// \brief Used for handling and querying diagnostic IDs. Can be used and shared
109 /// by multiple Diagnostics for multiple translation units.
110 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
112 /// Level The level of the diagnostic, after it has been through mapping.
114 Ignored, Note, Warning, Error, Fatal
118 /// \brief Information for uniquing and looking up custom diags.
119 diag::CustomDiagInfo *CustomDiagInfo;
125 /// \brief Return an ID for a diagnostic with the specified message and level.
127 /// If this is the first request for this diagnostic, it is registered and
128 /// created, otherwise the existing ID is returned.
129 unsigned getCustomDiagID(Level L, StringRef Message);
131 //===--------------------------------------------------------------------===//
132 // Diagnostic classification and reporting interfaces.
135 /// \brief Given a diagnostic ID, return a description of the issue.
136 StringRef getDescription(unsigned DiagID) const;
138 /// \brief Return true if the unmapped diagnostic levelof the specified
139 /// diagnostic ID is a Warning or Extension.
141 /// This only works on builtin diagnostics, not custom ones, and is not
142 /// legal to call on NOTEs.
143 static bool isBuiltinWarningOrExtension(unsigned DiagID);
145 /// \brief Return true if the specified diagnostic is mapped to errors by
147 static bool isDefaultMappingAsError(unsigned DiagID);
149 /// \brief Determine whether the given built-in diagnostic ID is a Note.
150 static bool isBuiltinNote(unsigned DiagID);
152 /// \brief Determine whether the given built-in diagnostic ID is for an
153 /// extension of some sort.
154 static bool isBuiltinExtensionDiag(unsigned DiagID) {
156 return isBuiltinExtensionDiag(DiagID, ignored);
159 /// \brief Determine whether the given built-in diagnostic ID is for an
160 /// extension of some sort, and whether it is enabled by default.
162 /// This also returns EnabledByDefault, which is set to indicate whether the
163 /// diagnostic is ignored by default (in which case -pedantic enables it) or
164 /// treated as a warning/error by default.
166 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
169 /// \brief Return the lowest-level warning option that enables the specified
172 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
173 static StringRef getWarningOptionForDiag(unsigned DiagID);
175 /// \brief Return the category number that a specified \p DiagID belongs to,
176 /// or 0 if no category.
177 static unsigned getCategoryNumberForDiag(unsigned DiagID);
179 /// \brief Return the number of diagnostic categories.
180 static unsigned getNumberOfCategories();
182 /// \brief Given a category ID, return the name of the category.
183 static StringRef getCategoryNameFromID(unsigned CategoryID);
185 /// \brief Return true if a given diagnostic falls into an ARC diagnostic
187 static bool isARCDiagnostic(unsigned DiagID);
189 /// \brief Enumeration describing how the emission of a diagnostic should
190 /// be treated when it occurs during C++ template argument deduction.
191 enum SFINAEResponse {
192 /// \brief The diagnostic should not be reported, but it should cause
193 /// template argument deduction to fail.
195 /// The vast majority of errors that occur during template argument
196 /// deduction fall into this category.
197 SFINAE_SubstitutionFailure,
199 /// \brief The diagnostic should be suppressed entirely.
201 /// Warnings generally fall into this category.
204 /// \brief The diagnostic should be reported.
206 /// The diagnostic should be reported. Various fatal errors (e.g.,
207 /// template instantiation depth exceeded) fall into this category.
210 /// \brief The diagnostic is an access-control diagnostic, which will be
211 /// substitution failures in some contexts and reported in others.
215 /// \brief Determines whether the given built-in diagnostic ID is
216 /// for an error that is suppressed if it occurs during C++ template
217 /// argument deduction.
219 /// When an error is suppressed due to SFINAE, the template argument
220 /// deduction fails but no diagnostic is emitted. Certain classes of
221 /// errors, such as those errors that involve C++ access control,
222 /// are not SFINAE errors.
223 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
225 /// \brief Get the set of all diagnostic IDs in the group with the given name.
227 /// \param Diags [out] - On return, the diagnostics in the group.
228 /// \returns True if the given group is unknown, false otherwise.
229 bool getDiagnosticsInGroup(StringRef Group,
230 SmallVectorImpl<diag::kind> &Diags) const;
232 /// \brief Get the set of all diagnostic IDs.
233 void getAllDiagnostics(SmallVectorImpl<diag::kind> &Diags) const;
235 /// \brief Get the warning option with the closest edit distance to the given
237 static StringRef getNearestWarningOption(StringRef Group);
240 /// \brief Get the set of all diagnostic IDs in the given group.
242 /// \param Diags [out] - On return, the diagnostics in the group.
243 void getDiagnosticsInGroup(const WarningOption *Group,
244 SmallVectorImpl<diag::kind> &Diags) const;
246 /// \brief Based on the way the client configured the DiagnosticsEngine
247 /// object, classify the specified diagnostic ID into a Level, consumable by
248 /// the DiagnosticClient.
250 /// \param Loc The source location we are interested in finding out the
251 /// diagnostic state. Can be null in order to query the latest state.
252 DiagnosticIDs::Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
253 const DiagnosticsEngine &Diag) const;
255 /// \brief An internal implementation helper used when \p DiagClass is
257 DiagnosticIDs::Level getDiagnosticLevel(unsigned DiagID,
260 const DiagnosticsEngine &Diag) const;
262 /// \brief Used to report a diagnostic that is finally fully formed.
264 /// \returns \c true if the diagnostic was emitted, \c false if it was
266 bool ProcessDiag(DiagnosticsEngine &Diag) const;
268 /// \brief Used to emit a diagnostic that is finally fully formed,
269 /// ignoring suppression.
270 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
272 /// \brief Whether the diagnostic may leave the AST in a state where some
273 /// invariants can break.
274 bool isUnrecoverable(unsigned DiagID) const;
276 friend class DiagnosticsEngine;
279 } // end namespace clang