1 //===--- DiagnosticIDs.h - Diagnostic IDs Handling --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines the Diagnostic IDs-related interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
15 #define LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/IntrusiveRefCntPtr.h"
19 #include "llvm/ADT/StringRef.h"
23 class DiagnosticsEngine;
26 // Import the diagnostic enums themselves.
28 // Size of each of the diagnostic categories.
30 DIAG_SIZE_COMMON = 300,
31 DIAG_SIZE_DRIVER = 250,
32 DIAG_SIZE_FRONTEND = 150,
33 DIAG_SIZE_SERIALIZATION = 120,
35 DIAG_SIZE_PARSE = 600,
37 DIAG_SIZE_COMMENT = 100,
38 DIAG_SIZE_CROSSTU = 100,
39 DIAG_SIZE_SEMA = 4000,
40 DIAG_SIZE_ANALYSIS = 100,
41 DIAG_SIZE_REFACTORING = 1000,
43 // Start position for diagnostics.
45 DIAG_START_COMMON = 0,
46 DIAG_START_DRIVER = DIAG_START_COMMON + DIAG_SIZE_COMMON,
47 DIAG_START_FRONTEND = DIAG_START_DRIVER + DIAG_SIZE_DRIVER,
48 DIAG_START_SERIALIZATION = DIAG_START_FRONTEND + DIAG_SIZE_FRONTEND,
49 DIAG_START_LEX = DIAG_START_SERIALIZATION + DIAG_SIZE_SERIALIZATION,
50 DIAG_START_PARSE = DIAG_START_LEX + DIAG_SIZE_LEX,
51 DIAG_START_AST = DIAG_START_PARSE + DIAG_SIZE_PARSE,
52 DIAG_START_COMMENT = DIAG_START_AST + DIAG_SIZE_AST,
53 DIAG_START_CROSSTU = DIAG_START_COMMENT + DIAG_SIZE_COMMENT,
54 DIAG_START_SEMA = DIAG_START_CROSSTU + DIAG_SIZE_CROSSTU,
55 DIAG_START_ANALYSIS = DIAG_START_SEMA + DIAG_SIZE_SEMA,
56 DIAG_START_REFACTORING = DIAG_START_ANALYSIS + DIAG_SIZE_ANALYSIS,
57 DIAG_UPPER_LIMIT = DIAG_START_REFACTORING + DIAG_SIZE_REFACTORING
62 /// All of the diagnostics that can be emitted by the frontend.
63 typedef unsigned kind;
65 // Get typedefs for common diagnostics.
67 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,\
68 SFINAE,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
70 #include "clang/Basic/DiagnosticCommonKinds.inc"
71 NUM_BUILTIN_COMMON_DIAGNOSTICS
75 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
76 /// to either Ignore (nothing), Remark (emit a remark), Warning
77 /// (emit a warning) or Error (emit as an error). It allows clients to
78 /// map ERRORs to Error or Fatal (stop emitting diagnostics after this one).
80 // NOTE: 0 means "uncomputed".
81 Ignored = 1, ///< Do not present this diagnostic, ignore it.
82 Remark = 2, ///< Present this diagnostic as a remark.
83 Warning = 3, ///< Present this diagnostic as a warning.
84 Error = 4, ///< Present this diagnostic as an error.
85 Fatal = 5 ///< Present this diagnostic as a fatal error.
88 /// Flavors of diagnostics we can emit. Used to filter for a particular
89 /// kind of diagnostic (for instance, for -W/-R flags).
91 WarningOrError, ///< A diagnostic that indicates a problem or potential
92 ///< problem. Can be made fatal by -Werror.
93 Remark ///< A diagnostic that indicates normal progress through
98 class DiagnosticMapping {
99 unsigned Severity : 3;
101 unsigned IsPragma : 1;
102 unsigned HasNoWarningAsError : 1;
103 unsigned HasNoErrorAsFatal : 1;
104 unsigned WasUpgradedFromWarning : 1;
107 static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
109 DiagnosticMapping Result;
110 Result.Severity = (unsigned)Severity;
111 Result.IsUser = IsUser;
112 Result.IsPragma = IsPragma;
113 Result.HasNoWarningAsError = 0;
114 Result.HasNoErrorAsFatal = 0;
115 Result.WasUpgradedFromWarning = 0;
119 diag::Severity getSeverity() const { return (diag::Severity)Severity; }
120 void setSeverity(diag::Severity Value) { Severity = (unsigned)Value; }
122 bool isUser() const { return IsUser; }
123 bool isPragma() const { return IsPragma; }
125 bool isErrorOrFatal() const {
126 return getSeverity() == diag::Severity::Error ||
127 getSeverity() == diag::Severity::Fatal;
130 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
131 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
133 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
134 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
136 /// Whether this mapping attempted to map the diagnostic to a warning, but
137 /// was overruled because the diagnostic was already mapped to an error or
139 bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
140 void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
142 /// Serialize this mapping as a raw integer.
143 unsigned serialize() const {
144 return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
145 (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
147 /// Deserialize a mapping.
148 static DiagnosticMapping deserialize(unsigned Bits) {
149 DiagnosticMapping Result;
150 Result.IsUser = (Bits >> 7) & 1;
151 Result.IsPragma = (Bits >> 6) & 1;
152 Result.HasNoWarningAsError = (Bits >> 5) & 1;
153 Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
154 Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
155 Result.Severity = Bits & 0x7;
160 /// Used for handling and querying diagnostic IDs.
162 /// Can be used and shared by multiple Diagnostics for multiple translation units.
163 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
165 /// The level of the diagnostic, after it has been through mapping.
167 Ignored, Note, Remark, Warning, Error, Fatal
171 /// Information for uniquing and looking up custom diags.
172 std::unique_ptr<diag::CustomDiagInfo> CustomDiagInfo;
178 /// Return an ID for a diagnostic with the specified format string and
181 /// If this is the first request for this diagnostic, it is registered and
182 /// created, otherwise the existing ID is returned.
184 // FIXME: Replace this function with a create-only facilty like
185 // createCustomDiagIDFromFormatString() to enforce safe usage. At the time of
186 // writing, nearly all callers of this function were invalid.
187 unsigned getCustomDiagID(Level L, StringRef FormatString);
189 //===--------------------------------------------------------------------===//
190 // Diagnostic classification and reporting interfaces.
193 /// Given a diagnostic ID, return a description of the issue.
194 StringRef getDescription(unsigned DiagID) const;
196 /// Return true if the unmapped diagnostic levelof the specified
197 /// diagnostic ID is a Warning or Extension.
199 /// This only works on builtin diagnostics, not custom ones, and is not
200 /// legal to call on NOTEs.
201 static bool isBuiltinWarningOrExtension(unsigned DiagID);
203 /// Return true if the specified diagnostic is mapped to errors by
205 static bool isDefaultMappingAsError(unsigned DiagID);
207 /// Determine whether the given built-in diagnostic ID is a Note.
208 static bool isBuiltinNote(unsigned DiagID);
210 /// Determine whether the given built-in diagnostic ID is for an
211 /// extension of some sort.
212 static bool isBuiltinExtensionDiag(unsigned DiagID) {
214 return isBuiltinExtensionDiag(DiagID, ignored);
217 /// Determine whether the given built-in diagnostic ID is for an
218 /// extension of some sort, and whether it is enabled by default.
220 /// This also returns EnabledByDefault, which is set to indicate whether the
221 /// diagnostic is ignored by default (in which case -pedantic enables it) or
222 /// treated as a warning/error by default.
224 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
227 /// Return the lowest-level warning option that enables the specified
230 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
231 static StringRef getWarningOptionForDiag(unsigned DiagID);
233 /// Return the category number that a specified \p DiagID belongs to,
234 /// or 0 if no category.
235 static unsigned getCategoryNumberForDiag(unsigned DiagID);
237 /// Return the number of diagnostic categories.
238 static unsigned getNumberOfCategories();
240 /// Given a category ID, return the name of the category.
241 static StringRef getCategoryNameFromID(unsigned CategoryID);
243 /// Return true if a given diagnostic falls into an ARC diagnostic
245 static bool isARCDiagnostic(unsigned DiagID);
247 /// Enumeration describing how the emission of a diagnostic should
248 /// be treated when it occurs during C++ template argument deduction.
249 enum SFINAEResponse {
250 /// The diagnostic should not be reported, but it should cause
251 /// template argument deduction to fail.
253 /// The vast majority of errors that occur during template argument
254 /// deduction fall into this category.
255 SFINAE_SubstitutionFailure,
257 /// The diagnostic should be suppressed entirely.
259 /// Warnings generally fall into this category.
262 /// The diagnostic should be reported.
264 /// The diagnostic should be reported. Various fatal errors (e.g.,
265 /// template instantiation depth exceeded) fall into this category.
268 /// The diagnostic is an access-control diagnostic, which will be
269 /// substitution failures in some contexts and reported in others.
273 /// Determines whether the given built-in diagnostic ID is
274 /// for an error that is suppressed if it occurs during C++ template
275 /// argument deduction.
277 /// When an error is suppressed due to SFINAE, the template argument
278 /// deduction fails but no diagnostic is emitted. Certain classes of
279 /// errors, such as those errors that involve C++ access control,
280 /// are not SFINAE errors.
281 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
283 /// Get the string of all diagnostic flags.
285 /// \returns A list of all diagnostics flags as they would be written in a
286 /// command line invocation including their `no-` variants. For example:
287 /// `{"-Wempty-body", "-Wno-empty-body", ...}`
288 static std::vector<std::string> getDiagnosticFlags();
290 /// Get the set of all diagnostic IDs in the group with the given name.
292 /// \param[out] Diags - On return, the diagnostics in the group.
293 /// \returns \c true if the given group is unknown, \c false otherwise.
294 bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
295 SmallVectorImpl<diag::kind> &Diags) const;
297 /// Get the set of all diagnostic IDs.
298 static void getAllDiagnostics(diag::Flavor Flavor,
299 std::vector<diag::kind> &Diags);
301 /// Get the diagnostic option with the closest edit distance to the
302 /// given group name.
303 static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group);
306 /// Classify the specified diagnostic ID into a Level, consumable by
307 /// the DiagnosticClient.
309 /// The classification is based on the way the client configured the
310 /// DiagnosticsEngine object.
312 /// \param Loc The source location for which we are interested in finding out
313 /// the diagnostic state. Can be null in order to query the latest state.
315 getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
316 const DiagnosticsEngine &Diag) const LLVM_READONLY;
319 getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
320 const DiagnosticsEngine &Diag) const LLVM_READONLY;
322 /// Used to report a diagnostic that is finally fully formed.
324 /// \returns \c true if the diagnostic was emitted, \c false if it was
326 bool ProcessDiag(DiagnosticsEngine &Diag) const;
328 /// Used to emit a diagnostic that is finally fully formed,
329 /// ignoring suppression.
330 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
332 /// Whether the diagnostic may leave the AST in a state where some
333 /// invariants can break.
334 bool isUnrecoverable(unsigned DiagID) const;
336 friend class DiagnosticsEngine;
339 } // end namespace clang