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_BASIC_DIAGNOSTICIDS_H
16 #define LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/StringRef.h"
24 class DiagnosticsEngine;
27 // Import the diagnostic enums themselves.
29 // Start position for diagnostics.
31 DIAG_START_COMMON = 0,
32 DIAG_START_DRIVER = DIAG_START_COMMON + 300,
33 DIAG_START_FRONTEND = DIAG_START_DRIVER + 200,
34 DIAG_START_SERIALIZATION = DIAG_START_FRONTEND + 100,
35 DIAG_START_LEX = DIAG_START_SERIALIZATION + 120,
36 DIAG_START_PARSE = DIAG_START_LEX + 400,
37 DIAG_START_AST = DIAG_START_PARSE + 500,
38 DIAG_START_COMMENT = DIAG_START_AST + 110,
39 DIAG_START_SEMA = DIAG_START_COMMENT + 100,
40 DIAG_START_ANALYSIS = DIAG_START_SEMA + 3500,
41 DIAG_UPPER_LIMIT = DIAG_START_ANALYSIS + 100
46 /// \brief All of the diagnostics that can be emitted by the frontend.
47 typedef unsigned kind;
49 // Get typedefs for common diagnostics.
51 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,\
52 SFINAE,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
54 #include "clang/Basic/DiagnosticCommonKinds.inc"
55 NUM_BUILTIN_COMMON_DIAGNOSTICS
59 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
60 /// to either Ignore (nothing), Remark (emit a remark), Warning
61 /// (emit a warning) or Error (emit as an error). It allows clients to
62 /// map ERRORs to Error or Fatal (stop emitting diagnostics after this one).
64 // NOTE: 0 means "uncomputed".
65 Ignored = 1, ///< Do not present this diagnostic, ignore it.
66 Remark = 2, ///< Present this diagnostic as a remark.
67 Warning = 3, ///< Present this diagnostic as a warning.
68 Error = 4, ///< Present this diagnostic as an error.
69 Fatal = 5 ///< Present this diagnostic as a fatal error.
72 /// Flavors of diagnostics we can emit. Used to filter for a particular
73 /// kind of diagnostic (for instance, for -W/-R flags).
75 WarningOrError, ///< A diagnostic that indicates a problem or potential
76 ///< problem. Can be made fatal by -Werror.
77 Remark ///< A diagnostic that indicates normal progress through
82 class DiagnosticMapping {
83 unsigned Severity : 3;
85 unsigned IsPragma : 1;
86 unsigned HasNoWarningAsError : 1;
87 unsigned HasNoErrorAsFatal : 1;
88 unsigned WasUpgradedFromWarning : 1;
91 static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
93 DiagnosticMapping Result;
94 Result.Severity = (unsigned)Severity;
95 Result.IsUser = IsUser;
96 Result.IsPragma = IsPragma;
97 Result.HasNoWarningAsError = 0;
98 Result.HasNoErrorAsFatal = 0;
99 Result.WasUpgradedFromWarning = 0;
103 diag::Severity getSeverity() const { return (diag::Severity)Severity; }
104 void setSeverity(diag::Severity Value) { Severity = (unsigned)Value; }
106 bool isUser() const { return IsUser; }
107 bool isPragma() const { return IsPragma; }
109 bool isErrorOrFatal() const {
110 return getSeverity() == diag::Severity::Error ||
111 getSeverity() == diag::Severity::Fatal;
114 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
115 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
117 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
118 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
120 /// Whether this mapping attempted to map the diagnostic to a warning, but
121 /// was overruled because the diagnostic was already mapped to an error or
123 bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
124 void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
126 /// Serialize this mapping as a raw integer.
127 unsigned serialize() const {
128 return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
129 (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
131 /// Deserialize a mapping.
132 static DiagnosticMapping deserialize(unsigned Bits) {
133 DiagnosticMapping Result;
134 Result.IsUser = (Bits >> 7) & 1;
135 Result.IsPragma = (Bits >> 6) & 1;
136 Result.HasNoWarningAsError = (Bits >> 5) & 1;
137 Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
138 Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
139 Result.Severity = Bits & 0x7;
144 /// \brief Used for handling and querying diagnostic IDs.
146 /// Can be used and shared by multiple Diagnostics for multiple translation units.
147 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
149 /// \brief The level of the diagnostic, after it has been through mapping.
151 Ignored, Note, Remark, Warning, Error, Fatal
155 /// \brief Information for uniquing and looking up custom diags.
156 diag::CustomDiagInfo *CustomDiagInfo;
162 /// \brief Return an ID for a diagnostic with the specified format string and
165 /// If this is the first request for this diagnostic, it is registered and
166 /// created, otherwise the existing ID is returned.
168 // FIXME: Replace this function with a create-only facilty like
169 // createCustomDiagIDFromFormatString() to enforce safe usage. At the time of
170 // writing, nearly all callers of this function were invalid.
171 unsigned getCustomDiagID(Level L, StringRef FormatString);
173 //===--------------------------------------------------------------------===//
174 // Diagnostic classification and reporting interfaces.
177 /// \brief Given a diagnostic ID, return a description of the issue.
178 StringRef getDescription(unsigned DiagID) const;
180 /// \brief Return true if the unmapped diagnostic levelof the specified
181 /// diagnostic ID is a Warning or Extension.
183 /// This only works on builtin diagnostics, not custom ones, and is not
184 /// legal to call on NOTEs.
185 static bool isBuiltinWarningOrExtension(unsigned DiagID);
187 /// \brief Return true if the specified diagnostic is mapped to errors by
189 static bool isDefaultMappingAsError(unsigned DiagID);
191 /// \brief Determine whether the given built-in diagnostic ID is a Note.
192 static bool isBuiltinNote(unsigned DiagID);
194 /// \brief Determine whether the given built-in diagnostic ID is for an
195 /// extension of some sort.
196 static bool isBuiltinExtensionDiag(unsigned DiagID) {
198 return isBuiltinExtensionDiag(DiagID, ignored);
201 /// \brief Determine whether the given built-in diagnostic ID is for an
202 /// extension of some sort, and whether it is enabled by default.
204 /// This also returns EnabledByDefault, which is set to indicate whether the
205 /// diagnostic is ignored by default (in which case -pedantic enables it) or
206 /// treated as a warning/error by default.
208 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
211 /// \brief Return the lowest-level warning option that enables the specified
214 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
215 static StringRef getWarningOptionForDiag(unsigned DiagID);
217 /// \brief Return the category number that a specified \p DiagID belongs to,
218 /// or 0 if no category.
219 static unsigned getCategoryNumberForDiag(unsigned DiagID);
221 /// \brief Return the number of diagnostic categories.
222 static unsigned getNumberOfCategories();
224 /// \brief Given a category ID, return the name of the category.
225 static StringRef getCategoryNameFromID(unsigned CategoryID);
227 /// \brief Return true if a given diagnostic falls into an ARC diagnostic
229 static bool isARCDiagnostic(unsigned DiagID);
231 /// \brief Enumeration describing how the emission of a diagnostic should
232 /// be treated when it occurs during C++ template argument deduction.
233 enum SFINAEResponse {
234 /// \brief The diagnostic should not be reported, but it should cause
235 /// template argument deduction to fail.
237 /// The vast majority of errors that occur during template argument
238 /// deduction fall into this category.
239 SFINAE_SubstitutionFailure,
241 /// \brief The diagnostic should be suppressed entirely.
243 /// Warnings generally fall into this category.
246 /// \brief The diagnostic should be reported.
248 /// The diagnostic should be reported. Various fatal errors (e.g.,
249 /// template instantiation depth exceeded) fall into this category.
252 /// \brief The diagnostic is an access-control diagnostic, which will be
253 /// substitution failures in some contexts and reported in others.
257 /// \brief Determines whether the given built-in diagnostic ID is
258 /// for an error that is suppressed if it occurs during C++ template
259 /// argument deduction.
261 /// When an error is suppressed due to SFINAE, the template argument
262 /// deduction fails but no diagnostic is emitted. Certain classes of
263 /// errors, such as those errors that involve C++ access control,
264 /// are not SFINAE errors.
265 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
267 /// \brief Get the string of all diagnostic flags.
269 /// \returns A list of all diagnostics flags as they would be written in a
270 /// command line invocation including their `no-` variants. For example:
271 /// `{"-Wempty-body", "-Wno-empty-body", ...}`
272 static std::vector<std::string> getDiagnosticFlags();
274 /// \brief Get the set of all diagnostic IDs in the group with the given name.
276 /// \param[out] Diags - On return, the diagnostics in the group.
277 /// \returns \c true if the given group is unknown, \c false otherwise.
278 bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
279 SmallVectorImpl<diag::kind> &Diags) const;
281 /// \brief Get the set of all diagnostic IDs.
282 void getAllDiagnostics(diag::Flavor Flavor,
283 SmallVectorImpl<diag::kind> &Diags) const;
285 /// \brief Get the diagnostic option with the closest edit distance to the
286 /// given group name.
287 static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group);
290 /// \brief Classify the specified diagnostic ID into a Level, consumable by
291 /// the DiagnosticClient.
293 /// The classification is based on the way the client configured the
294 /// DiagnosticsEngine object.
296 /// \param Loc The source location for which we are interested in finding out
297 /// the diagnostic state. Can be null in order to query the latest state.
299 getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
300 const DiagnosticsEngine &Diag) const LLVM_READONLY;
303 getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
304 const DiagnosticsEngine &Diag) const LLVM_READONLY;
306 /// \brief Used to report a diagnostic that is finally fully formed.
308 /// \returns \c true if the diagnostic was emitted, \c false if it was
310 bool ProcessDiag(DiagnosticsEngine &Diag) const;
312 /// \brief Used to emit a diagnostic that is finally fully formed,
313 /// ignoring suppression.
314 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
316 /// \brief Whether the diagnostic may leave the AST in a state where some
317 /// invariants can break.
318 bool isUnrecoverable(unsigned DiagID) const;
320 friend class DiagnosticsEngine;
323 } // end namespace clang