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 /// 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 // Size of each of the diagnostic categories.
31 DIAG_SIZE_COMMON = 300,
32 DIAG_SIZE_DRIVER = 200,
33 DIAG_SIZE_FRONTEND = 100,
34 DIAG_SIZE_SERIALIZATION = 120,
36 DIAG_SIZE_PARSE = 500,
38 DIAG_SIZE_COMMENT = 100,
39 DIAG_SIZE_CROSSTU = 100,
40 DIAG_SIZE_SEMA = 3500,
41 DIAG_SIZE_ANALYSIS = 100,
42 DIAG_SIZE_REFACTORING = 1000,
44 // Start position for diagnostics.
46 DIAG_START_COMMON = 0,
47 DIAG_START_DRIVER = DIAG_START_COMMON + DIAG_SIZE_COMMON,
48 DIAG_START_FRONTEND = DIAG_START_DRIVER + DIAG_SIZE_DRIVER,
49 DIAG_START_SERIALIZATION = DIAG_START_FRONTEND + DIAG_SIZE_FRONTEND,
50 DIAG_START_LEX = DIAG_START_SERIALIZATION + DIAG_SIZE_SERIALIZATION,
51 DIAG_START_PARSE = DIAG_START_LEX + DIAG_SIZE_LEX,
52 DIAG_START_AST = DIAG_START_PARSE + DIAG_SIZE_PARSE,
53 DIAG_START_COMMENT = DIAG_START_AST + DIAG_SIZE_AST,
54 DIAG_START_CROSSTU = DIAG_START_COMMENT + DIAG_SIZE_CROSSTU,
55 DIAG_START_SEMA = DIAG_START_CROSSTU + DIAG_SIZE_COMMENT,
56 DIAG_START_ANALYSIS = DIAG_START_SEMA + DIAG_SIZE_SEMA,
57 DIAG_START_REFACTORING = DIAG_START_ANALYSIS + DIAG_SIZE_ANALYSIS,
58 DIAG_UPPER_LIMIT = DIAG_START_REFACTORING + DIAG_SIZE_REFACTORING
63 /// All of the diagnostics that can be emitted by the frontend.
64 typedef unsigned kind;
66 // Get typedefs for common diagnostics.
68 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,\
69 SFINAE,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
71 #include "clang/Basic/DiagnosticCommonKinds.inc"
72 NUM_BUILTIN_COMMON_DIAGNOSTICS
76 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
77 /// to either Ignore (nothing), Remark (emit a remark), Warning
78 /// (emit a warning) or Error (emit as an error). It allows clients to
79 /// map ERRORs to Error or Fatal (stop emitting diagnostics after this one).
81 // NOTE: 0 means "uncomputed".
82 Ignored = 1, ///< Do not present this diagnostic, ignore it.
83 Remark = 2, ///< Present this diagnostic as a remark.
84 Warning = 3, ///< Present this diagnostic as a warning.
85 Error = 4, ///< Present this diagnostic as an error.
86 Fatal = 5 ///< Present this diagnostic as a fatal error.
89 /// Flavors of diagnostics we can emit. Used to filter for a particular
90 /// kind of diagnostic (for instance, for -W/-R flags).
92 WarningOrError, ///< A diagnostic that indicates a problem or potential
93 ///< problem. Can be made fatal by -Werror.
94 Remark ///< A diagnostic that indicates normal progress through
99 class DiagnosticMapping {
100 unsigned Severity : 3;
102 unsigned IsPragma : 1;
103 unsigned HasNoWarningAsError : 1;
104 unsigned HasNoErrorAsFatal : 1;
105 unsigned WasUpgradedFromWarning : 1;
108 static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
110 DiagnosticMapping Result;
111 Result.Severity = (unsigned)Severity;
112 Result.IsUser = IsUser;
113 Result.IsPragma = IsPragma;
114 Result.HasNoWarningAsError = 0;
115 Result.HasNoErrorAsFatal = 0;
116 Result.WasUpgradedFromWarning = 0;
120 diag::Severity getSeverity() const { return (diag::Severity)Severity; }
121 void setSeverity(diag::Severity Value) { Severity = (unsigned)Value; }
123 bool isUser() const { return IsUser; }
124 bool isPragma() const { return IsPragma; }
126 bool isErrorOrFatal() const {
127 return getSeverity() == diag::Severity::Error ||
128 getSeverity() == diag::Severity::Fatal;
131 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
132 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
134 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
135 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
137 /// Whether this mapping attempted to map the diagnostic to a warning, but
138 /// was overruled because the diagnostic was already mapped to an error or
140 bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
141 void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
143 /// Serialize this mapping as a raw integer.
144 unsigned serialize() const {
145 return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
146 (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
148 /// Deserialize a mapping.
149 static DiagnosticMapping deserialize(unsigned Bits) {
150 DiagnosticMapping Result;
151 Result.IsUser = (Bits >> 7) & 1;
152 Result.IsPragma = (Bits >> 6) & 1;
153 Result.HasNoWarningAsError = (Bits >> 5) & 1;
154 Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
155 Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
156 Result.Severity = Bits & 0x7;
161 /// Used for handling and querying diagnostic IDs.
163 /// Can be used and shared by multiple Diagnostics for multiple translation units.
164 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
166 /// The level of the diagnostic, after it has been through mapping.
168 Ignored, Note, Remark, Warning, Error, Fatal
172 /// Information for uniquing and looking up custom diags.
173 diag::CustomDiagInfo *CustomDiagInfo;
179 /// Return an ID for a diagnostic with the specified format string and
182 /// If this is the first request for this diagnostic, it is registered and
183 /// created, otherwise the existing ID is returned.
185 // FIXME: Replace this function with a create-only facilty like
186 // createCustomDiagIDFromFormatString() to enforce safe usage. At the time of
187 // writing, nearly all callers of this function were invalid.
188 unsigned getCustomDiagID(Level L, StringRef FormatString);
190 //===--------------------------------------------------------------------===//
191 // Diagnostic classification and reporting interfaces.
194 /// Given a diagnostic ID, return a description of the issue.
195 StringRef getDescription(unsigned DiagID) const;
197 /// Return true if the unmapped diagnostic levelof the specified
198 /// diagnostic ID is a Warning or Extension.
200 /// This only works on builtin diagnostics, not custom ones, and is not
201 /// legal to call on NOTEs.
202 static bool isBuiltinWarningOrExtension(unsigned DiagID);
204 /// Return true if the specified diagnostic is mapped to errors by
206 static bool isDefaultMappingAsError(unsigned DiagID);
208 /// Determine whether the given built-in diagnostic ID is a Note.
209 static bool isBuiltinNote(unsigned DiagID);
211 /// Determine whether the given built-in diagnostic ID is for an
212 /// extension of some sort.
213 static bool isBuiltinExtensionDiag(unsigned DiagID) {
215 return isBuiltinExtensionDiag(DiagID, ignored);
218 /// Determine whether the given built-in diagnostic ID is for an
219 /// extension of some sort, and whether it is enabled by default.
221 /// This also returns EnabledByDefault, which is set to indicate whether the
222 /// diagnostic is ignored by default (in which case -pedantic enables it) or
223 /// treated as a warning/error by default.
225 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
228 /// Return the lowest-level warning option that enables the specified
231 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
232 static StringRef getWarningOptionForDiag(unsigned DiagID);
234 /// Return the category number that a specified \p DiagID belongs to,
235 /// or 0 if no category.
236 static unsigned getCategoryNumberForDiag(unsigned DiagID);
238 /// Return the number of diagnostic categories.
239 static unsigned getNumberOfCategories();
241 /// Given a category ID, return the name of the category.
242 static StringRef getCategoryNameFromID(unsigned CategoryID);
244 /// Return true if a given diagnostic falls into an ARC diagnostic
246 static bool isARCDiagnostic(unsigned DiagID);
248 /// Enumeration describing how the emission of a diagnostic should
249 /// be treated when it occurs during C++ template argument deduction.
250 enum SFINAEResponse {
251 /// The diagnostic should not be reported, but it should cause
252 /// template argument deduction to fail.
254 /// The vast majority of errors that occur during template argument
255 /// deduction fall into this category.
256 SFINAE_SubstitutionFailure,
258 /// The diagnostic should be suppressed entirely.
260 /// Warnings generally fall into this category.
263 /// The diagnostic should be reported.
265 /// The diagnostic should be reported. Various fatal errors (e.g.,
266 /// template instantiation depth exceeded) fall into this category.
269 /// The diagnostic is an access-control diagnostic, which will be
270 /// substitution failures in some contexts and reported in others.
274 /// Determines whether the given built-in diagnostic ID is
275 /// for an error that is suppressed if it occurs during C++ template
276 /// argument deduction.
278 /// When an error is suppressed due to SFINAE, the template argument
279 /// deduction fails but no diagnostic is emitted. Certain classes of
280 /// errors, such as those errors that involve C++ access control,
281 /// are not SFINAE errors.
282 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
284 /// Get the string of all diagnostic flags.
286 /// \returns A list of all diagnostics flags as they would be written in a
287 /// command line invocation including their `no-` variants. For example:
288 /// `{"-Wempty-body", "-Wno-empty-body", ...}`
289 static std::vector<std::string> getDiagnosticFlags();
291 /// Get the set of all diagnostic IDs in the group with the given name.
293 /// \param[out] Diags - On return, the diagnostics in the group.
294 /// \returns \c true if the given group is unknown, \c false otherwise.
295 bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
296 SmallVectorImpl<diag::kind> &Diags) const;
298 /// Get the set of all diagnostic IDs.
299 static void getAllDiagnostics(diag::Flavor Flavor,
300 std::vector<diag::kind> &Diags);
302 /// Get the diagnostic option with the closest edit distance to the
303 /// given group name.
304 static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group);
307 /// Classify the specified diagnostic ID into a Level, consumable by
308 /// the DiagnosticClient.
310 /// The classification is based on the way the client configured the
311 /// DiagnosticsEngine object.
313 /// \param Loc The source location for which we are interested in finding out
314 /// the diagnostic state. Can be null in order to query the latest state.
316 getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
317 const DiagnosticsEngine &Diag) const LLVM_READONLY;
320 getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
321 const DiagnosticsEngine &Diag) const LLVM_READONLY;
323 /// Used to report a diagnostic that is finally fully formed.
325 /// \returns \c true if the diagnostic was emitted, \c false if it was
327 bool ProcessDiag(DiagnosticsEngine &Diag) const;
329 /// Used to emit a diagnostic that is finally fully formed,
330 /// ignoring suppression.
331 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
333 /// Whether the diagnostic may leave the AST in a state where some
334 /// invariants can break.
335 bool isUnrecoverable(unsigned DiagID) const;
337 friend class DiagnosticsEngine;
340 } // end namespace clang