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;
26 // Import the diagnostic enums themselves.
28 // Start position for diagnostics.
30 DIAG_START_COMMON = 0,
31 DIAG_START_DRIVER = DIAG_START_COMMON + 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,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
53 #include "clang/Basic/DiagnosticCommonKinds.inc"
54 NUM_BUILTIN_COMMON_DIAGNOSTICS
58 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
59 /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR
60 /// (emit as an error). It allows clients to map errors to
61 /// MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop emitting diagnostics after this
64 // NOTE: 0 means "uncomputed".
65 MAP_IGNORE = 1, ///< Map this diagnostic to nothing, ignore it.
66 MAP_WARNING = 2, ///< Map this diagnostic to a warning.
67 MAP_ERROR = 3, ///< Map this diagnostic to an error.
68 MAP_FATAL = 4 ///< Map this diagnostic to a fatal error.
72 class DiagnosticMappingInfo {
75 unsigned IsPragma : 1;
76 unsigned HasShowInSystemHeader : 1;
77 unsigned HasNoWarningAsError : 1;
78 unsigned HasNoErrorAsFatal : 1;
81 static DiagnosticMappingInfo Make(diag::Mapping Mapping, bool IsUser,
83 DiagnosticMappingInfo Result;
84 Result.Mapping = Mapping;
85 Result.IsUser = IsUser;
86 Result.IsPragma = IsPragma;
87 Result.HasShowInSystemHeader = 0;
88 Result.HasNoWarningAsError = 0;
89 Result.HasNoErrorAsFatal = 0;
93 diag::Mapping getMapping() const { return diag::Mapping(Mapping); }
94 void setMapping(diag::Mapping Value) { Mapping = Value; }
96 bool isUser() const { return IsUser; }
97 bool isPragma() const { return IsPragma; }
99 bool hasShowInSystemHeader() const { return HasShowInSystemHeader; }
100 void setShowInSystemHeader(bool Value) { HasShowInSystemHeader = Value; }
102 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
103 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
105 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
106 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
109 /// \brief Used for handling and querying diagnostic IDs.
111 /// Can be used and shared by multiple Diagnostics for multiple translation units.
112 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
114 /// \brief The level of the diagnostic, after it has been through mapping.
116 Ignored, Note, Warning, Error, Fatal
120 /// \brief Information for uniquing and looking up custom diags.
121 diag::CustomDiagInfo *CustomDiagInfo;
127 /// \brief Return an ID for a diagnostic with the specified message and level.
129 /// If this is the first request for this diagnostic, it is registered and
130 /// created, otherwise the existing ID is returned.
131 unsigned getCustomDiagID(Level L, StringRef Message);
133 //===--------------------------------------------------------------------===//
134 // Diagnostic classification and reporting interfaces.
137 /// \brief Given a diagnostic ID, return a description of the issue.
138 StringRef getDescription(unsigned DiagID) const;
140 /// \brief Return true if the unmapped diagnostic levelof the specified
141 /// diagnostic ID is a Warning or Extension.
143 /// This only works on builtin diagnostics, not custom ones, and is not
144 /// legal to call on NOTEs.
145 static bool isBuiltinWarningOrExtension(unsigned DiagID);
147 /// \brief Return true if the specified diagnostic is mapped to errors by
149 static bool isDefaultMappingAsError(unsigned DiagID);
151 /// \brief Determine whether the given built-in diagnostic ID is a Note.
152 static bool isBuiltinNote(unsigned DiagID);
154 /// \brief Determine whether the given built-in diagnostic ID is for an
155 /// extension of some sort.
156 static bool isBuiltinExtensionDiag(unsigned DiagID) {
158 return isBuiltinExtensionDiag(DiagID, ignored);
161 /// \brief Determine whether the given built-in diagnostic ID is for an
162 /// extension of some sort, and whether it is enabled by default.
164 /// This also returns EnabledByDefault, which is set to indicate whether the
165 /// diagnostic is ignored by default (in which case -pedantic enables it) or
166 /// treated as a warning/error by default.
168 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
171 /// \brief Return the lowest-level warning option that enables the specified
174 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
175 static StringRef getWarningOptionForDiag(unsigned DiagID);
177 /// \brief Return the category number that a specified \p DiagID belongs to,
178 /// or 0 if no category.
179 static unsigned getCategoryNumberForDiag(unsigned DiagID);
181 /// \brief Return the number of diagnostic categories.
182 static unsigned getNumberOfCategories();
184 /// \brief Given a category ID, return the name of the category.
185 static StringRef getCategoryNameFromID(unsigned CategoryID);
187 /// \brief Return true if a given diagnostic falls into an ARC diagnostic
189 static bool isARCDiagnostic(unsigned DiagID);
191 /// \brief Enumeration describing how the emission of a diagnostic should
192 /// be treated when it occurs during C++ template argument deduction.
193 enum SFINAEResponse {
194 /// \brief The diagnostic should not be reported, but it should cause
195 /// template argument deduction to fail.
197 /// The vast majority of errors that occur during template argument
198 /// deduction fall into this category.
199 SFINAE_SubstitutionFailure,
201 /// \brief The diagnostic should be suppressed entirely.
203 /// Warnings generally fall into this category.
206 /// \brief The diagnostic should be reported.
208 /// The diagnostic should be reported. Various fatal errors (e.g.,
209 /// template instantiation depth exceeded) fall into this category.
212 /// \brief The diagnostic is an access-control diagnostic, which will be
213 /// substitution failures in some contexts and reported in others.
217 /// \brief Determines whether the given built-in diagnostic ID is
218 /// for an error that is suppressed if it occurs during C++ template
219 /// argument deduction.
221 /// When an error is suppressed due to SFINAE, the template argument
222 /// deduction fails but no diagnostic is emitted. Certain classes of
223 /// errors, such as those errors that involve C++ access control,
224 /// are not SFINAE errors.
225 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
227 /// \brief Get the set of all diagnostic IDs in the group with the given name.
229 /// \param[out] Diags - On return, the diagnostics in the group.
230 /// \returns \c true if the given group is unknown, \c false otherwise.
231 bool getDiagnosticsInGroup(StringRef Group,
232 SmallVectorImpl<diag::kind> &Diags) const;
234 /// \brief Get the set of all diagnostic IDs.
235 void getAllDiagnostics(SmallVectorImpl<diag::kind> &Diags) const;
237 /// \brief Get the warning option with the closest edit distance to the given
239 static StringRef getNearestWarningOption(StringRef Group);
242 /// \brief Classify the specified diagnostic ID into a Level, consumable by
243 /// the DiagnosticClient.
245 /// The classification is based on the way the client configured the
246 /// DiagnosticsEngine object.
248 /// \param Loc The source location for which we are interested in finding out
249 /// the diagnostic state. Can be null in order to query the latest state.
250 DiagnosticIDs::Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
251 const DiagnosticsEngine &Diag) const;
253 /// \brief An internal implementation helper used when \p DiagClass is
255 DiagnosticIDs::Level getDiagnosticLevel(unsigned DiagID,
258 const DiagnosticsEngine &Diag) const;
260 /// \brief Used to report a diagnostic that is finally fully formed.
262 /// \returns \c true if the diagnostic was emitted, \c false if it was
264 bool ProcessDiag(DiagnosticsEngine &Diag) const;
266 /// \brief Used to emit a diagnostic that is finally fully formed,
267 /// ignoring suppression.
268 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
270 /// \brief Whether the diagnostic may leave the AST in a state where some
271 /// invariants can break.
272 bool isUnrecoverable(unsigned DiagID) const;
274 friend class DiagnosticsEngine;
277 } // end namespace clang