clang 19.0.0git
DiagnosticIDs.h
Go to the documentation of this file.
1//===--- DiagnosticIDs.h - Diagnostic IDs Handling --------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Defines the Diagnostic IDs-related interfaces.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
15#define LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
16
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/IntrusiveRefCntPtr.h"
19#include "llvm/ADT/StringRef.h"
20#include <optional>
21#include <vector>
22
23namespace clang {
24 class DiagnosticsEngine;
25 class SourceLocation;
26
27 // Import the diagnostic enums themselves.
28 namespace diag {
29 enum class Group;
30
31 // Size of each of the diagnostic categories.
32 enum {
45 };
46 // Start position for diagnostics.
47 enum {
61 };
62
63 class CustomDiagInfo;
64
65 /// All of the diagnostics that can be emitted by the frontend.
66 typedef unsigned kind;
67
68 // Get typedefs for common diagnostics.
69 enum {
70#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, CATEGORY, \
71 NOWERROR, SHOWINSYSHEADER, SHOWINSYSMACRO, DEFFERABLE) \
72 ENUM,
73#define COMMONSTART
74#include "clang/Basic/DiagnosticCommonKinds.inc"
76#undef DIAG
77 };
78
79 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
80 /// to either Ignore (nothing), Remark (emit a remark), Warning
81 /// (emit a warning) or Error (emit as an error). It allows clients to
82 /// map ERRORs to Error or Fatal (stop emitting diagnostics after this one).
83 enum class Severity {
84 // NOTE: 0 means "uncomputed".
85 Ignored = 1, ///< Do not present this diagnostic, ignore it.
86 Remark = 2, ///< Present this diagnostic as a remark.
87 Warning = 3, ///< Present this diagnostic as a warning.
88 Error = 4, ///< Present this diagnostic as an error.
89 Fatal = 5 ///< Present this diagnostic as a fatal error.
90 };
91
92 /// Flavors of diagnostics we can emit. Used to filter for a particular
93 /// kind of diagnostic (for instance, for -W/-R flags).
94 enum class Flavor {
95 WarningOrError, ///< A diagnostic that indicates a problem or potential
96 ///< problem. Can be made fatal by -Werror.
97 Remark ///< A diagnostic that indicates normal progress through
98 ///< compilation.
99 };
100 }
101
103 LLVM_PREFERRED_TYPE(diag::Severity)
104 unsigned Severity : 3;
105 LLVM_PREFERRED_TYPE(bool)
106 unsigned IsUser : 1;
107 LLVM_PREFERRED_TYPE(bool)
108 unsigned IsPragma : 1;
109 LLVM_PREFERRED_TYPE(bool)
110 unsigned HasNoWarningAsError : 1;
111 LLVM_PREFERRED_TYPE(bool)
112 unsigned HasNoErrorAsFatal : 1;
113 LLVM_PREFERRED_TYPE(bool)
114 unsigned WasUpgradedFromWarning : 1;
115
116public:
117 static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
118 bool IsPragma) {
120 Result.Severity = (unsigned)Severity;
121 Result.IsUser = IsUser;
122 Result.IsPragma = IsPragma;
123 Result.HasNoWarningAsError = 0;
124 Result.HasNoErrorAsFatal = 0;
125 Result.WasUpgradedFromWarning = 0;
126 return Result;
127 }
128
129 diag::Severity getSeverity() const { return (diag::Severity)Severity; }
131
132 bool isUser() const { return IsUser; }
133 bool isPragma() const { return IsPragma; }
134
135 bool isErrorOrFatal() const {
138 }
139
140 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
141 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
142
143 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
144 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
145
146 /// Whether this mapping attempted to map the diagnostic to a warning, but
147 /// was overruled because the diagnostic was already mapped to an error or
148 /// fatal error.
149 bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
150 void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
151
152 /// Serialize this mapping as a raw integer.
153 unsigned serialize() const {
154 return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
155 (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
156 }
157 /// Deserialize a mapping.
158 static DiagnosticMapping deserialize(unsigned Bits) {
160 Result.IsUser = (Bits >> 7) & 1;
161 Result.IsPragma = (Bits >> 6) & 1;
162 Result.HasNoWarningAsError = (Bits >> 5) & 1;
163 Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
164 Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
165 Result.Severity = Bits & 0x7;
166 return Result;
167 }
168
170 return serialize() == Other.serialize();
171 }
172};
173
174/// Used for handling and querying diagnostic IDs.
175///
176/// Can be used and shared by multiple Diagnostics for multiple translation units.
177class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
178public:
179 /// The level of the diagnostic, after it has been through mapping.
180 enum Level {
182 };
183
184private:
185 /// Information for uniquing and looking up custom diags.
186 std::unique_ptr<diag::CustomDiagInfo> CustomDiagInfo;
187
188public:
191
192 /// Return an ID for a diagnostic with the specified format string and
193 /// level.
194 ///
195 /// If this is the first request for this diagnostic, it is registered and
196 /// created, otherwise the existing ID is returned.
197
198 // FIXME: Replace this function with a create-only facilty like
199 // createCustomDiagIDFromFormatString() to enforce safe usage. At the time of
200 // writing, nearly all callers of this function were invalid.
201 unsigned getCustomDiagID(Level L, StringRef FormatString);
202
203 //===--------------------------------------------------------------------===//
204 // Diagnostic classification and reporting interfaces.
205 //
206
207 /// Given a diagnostic ID, return a description of the issue.
208 StringRef getDescription(unsigned DiagID) const;
209
210 /// Return true if the unmapped diagnostic levelof the specified
211 /// diagnostic ID is a Warning or Extension.
212 ///
213 /// This only works on builtin diagnostics, not custom ones, and is not
214 /// legal to call on NOTEs.
215 static bool isBuiltinWarningOrExtension(unsigned DiagID);
216
217 /// Return true if the specified diagnostic is mapped to errors by
218 /// default.
219 static bool isDefaultMappingAsError(unsigned DiagID);
220
221 /// Get the default mapping for this diagnostic.
222 static DiagnosticMapping getDefaultMapping(unsigned DiagID);
223
224 /// Determine whether the given built-in diagnostic ID is a Note.
225 static bool isBuiltinNote(unsigned DiagID);
226
227 /// Determine whether the given built-in diagnostic ID is for an
228 /// extension of some sort.
229 static bool isBuiltinExtensionDiag(unsigned DiagID) {
230 bool ignored;
231 return isBuiltinExtensionDiag(DiagID, ignored);
232 }
233
234 /// Determine whether the given built-in diagnostic ID is for an
235 /// extension of some sort, and whether it is enabled by default.
236 ///
237 /// This also returns EnabledByDefault, which is set to indicate whether the
238 /// diagnostic is ignored by default (in which case -pedantic enables it) or
239 /// treated as a warning/error by default.
240 ///
241 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
242
243 /// Given a group ID, returns the flag that toggles the group.
244 /// For example, for Group::DeprecatedDeclarations, returns
245 /// "deprecated-declarations".
246 static StringRef getWarningOptionForGroup(diag::Group);
247
248 /// Given a diagnostic group ID, return its documentation.
249 static StringRef getWarningOptionDocumentation(diag::Group GroupID);
250
251 /// Given a group ID, returns the flag that toggles the group.
252 /// For example, for "deprecated-declarations", returns
253 /// Group::DeprecatedDeclarations.
254 static std::optional<diag::Group> getGroupForWarningOption(StringRef);
255
256 /// Return the lowest-level group that contains the specified diagnostic.
257 static std::optional<diag::Group> getGroupForDiag(unsigned DiagID);
258
259 /// Return the lowest-level warning option that enables the specified
260 /// diagnostic.
261 ///
262 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
263 static StringRef getWarningOptionForDiag(unsigned DiagID);
264
265 /// Return the category number that a specified \p DiagID belongs to,
266 /// or 0 if no category.
267 static unsigned getCategoryNumberForDiag(unsigned DiagID);
268
269 /// Return the number of diagnostic categories.
270 static unsigned getNumberOfCategories();
271
272 /// Given a category ID, return the name of the category.
273 static StringRef getCategoryNameFromID(unsigned CategoryID);
274
275 /// Return true if a given diagnostic falls into an ARC diagnostic
276 /// category.
277 static bool isARCDiagnostic(unsigned DiagID);
278
279 /// Enumeration describing how the emission of a diagnostic should
280 /// be treated when it occurs during C++ template argument deduction.
282 /// The diagnostic should not be reported, but it should cause
283 /// template argument deduction to fail.
284 ///
285 /// The vast majority of errors that occur during template argument
286 /// deduction fall into this category.
288
289 /// The diagnostic should be suppressed entirely.
290 ///
291 /// Warnings generally fall into this category.
293
294 /// The diagnostic should be reported.
295 ///
296 /// The diagnostic should be reported. Various fatal errors (e.g.,
297 /// template instantiation depth exceeded) fall into this category.
299
300 /// The diagnostic is an access-control diagnostic, which will be
301 /// substitution failures in some contexts and reported in others.
303 };
304
305 /// Determines whether the given built-in diagnostic ID is
306 /// for an error that is suppressed if it occurs during C++ template
307 /// argument deduction.
308 ///
309 /// When an error is suppressed due to SFINAE, the template argument
310 /// deduction fails but no diagnostic is emitted. Certain classes of
311 /// errors, such as those errors that involve C++ access control,
312 /// are not SFINAE errors.
313 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
314
315 /// Whether the diagnostic message can be deferred.
316 ///
317 /// For single source offloading languages, a diagnostic message occurred
318 /// in a device host function may be deferred until the function is sure
319 /// to be emitted.
320 static bool isDeferrable(unsigned DiagID);
321
322 /// Get the string of all diagnostic flags.
323 ///
324 /// \returns A list of all diagnostics flags as they would be written in a
325 /// command line invocation including their `no-` variants. For example:
326 /// `{"-Wempty-body", "-Wno-empty-body", ...}`
327 static std::vector<std::string> getDiagnosticFlags();
328
329 /// Get the set of all diagnostic IDs in the group with the given name.
330 ///
331 /// \param[out] Diags - On return, the diagnostics in the group.
332 /// \returns \c true if the given group is unknown, \c false otherwise.
333 bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
334 SmallVectorImpl<diag::kind> &Diags) const;
335
336 /// Get the set of all diagnostic IDs.
337 static void getAllDiagnostics(diag::Flavor Flavor,
338 std::vector<diag::kind> &Diags);
339
340 /// Get the diagnostic option with the closest edit distance to the
341 /// given group name.
342 static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group);
343
344private:
345 /// Classify the specified diagnostic ID into a Level, consumable by
346 /// the DiagnosticClient.
347 ///
348 /// The classification is based on the way the client configured the
349 /// DiagnosticsEngine object.
350 ///
351 /// \param Loc The source location for which we are interested in finding out
352 /// the diagnostic state. Can be null in order to query the latest state.
354 getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
355 const DiagnosticsEngine &Diag) const LLVM_READONLY;
356
358 getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
359 const DiagnosticsEngine &Diag) const LLVM_READONLY;
360
361 /// Used to report a diagnostic that is finally fully formed.
362 ///
363 /// \returns \c true if the diagnostic was emitted, \c false if it was
364 /// suppressed.
365 bool ProcessDiag(DiagnosticsEngine &Diag) const;
366
367 /// Used to emit a diagnostic that is finally fully formed,
368 /// ignoring suppression.
369 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
370
371 /// Whether the diagnostic may leave the AST in a state where some
372 /// invariants can break.
373 bool isUnrecoverable(unsigned DiagID) const;
374
375 friend class DiagnosticsEngine;
376};
377
378} // end namespace clang
379
380#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Used for handling and querying diagnostic IDs.
static bool isBuiltinExtensionDiag(unsigned DiagID)
Determine whether the given built-in diagnostic ID is for an extension of some sort.
static StringRef getCategoryNameFromID(unsigned CategoryID)
Given a category ID, return the name of the category.
static unsigned getNumberOfCategories()
Return the number of diagnostic categories.
static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group)
Get the diagnostic option with the closest edit distance to the given group name.
bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group, SmallVectorImpl< diag::kind > &Diags) const
Get the set of all diagnostic IDs in the group with the given name.
static std::vector< std::string > getDiagnosticFlags()
Get the string of all diagnostic flags.
static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID)
Determines whether the given built-in diagnostic ID is for an error that is suppressed if it occurs d...
static bool isDefaultMappingAsError(unsigned DiagID)
Return true if the specified diagnostic is mapped to errors by default.
static bool isBuiltinNote(unsigned DiagID)
Determine whether the given built-in diagnostic ID is a Note.
static bool isBuiltinWarningOrExtension(unsigned DiagID)
Return true if the unmapped diagnostic levelof the specified diagnostic ID is a Warning or Extension.
StringRef getDescription(unsigned DiagID) const
Given a diagnostic ID, return a description of the issue.
SFINAEResponse
Enumeration describing how the emission of a diagnostic should be treated when it occurs during C++ t...
@ SFINAE_SubstitutionFailure
The diagnostic should not be reported, but it should cause template argument deduction to fail.
@ SFINAE_Suppress
The diagnostic should be suppressed entirely.
@ SFINAE_AccessControl
The diagnostic is an access-control diagnostic, which will be substitution failures in some contexts ...
@ SFINAE_Report
The diagnostic should be reported.
static StringRef getWarningOptionForDiag(unsigned DiagID)
Return the lowest-level warning option that enables the specified diagnostic.
static StringRef getWarningOptionDocumentation(diag::Group GroupID)
Given a diagnostic group ID, return its documentation.
static std::optional< diag::Group > getGroupForWarningOption(StringRef)
Given a group ID, returns the flag that toggles the group.
static unsigned getCategoryNumberForDiag(unsigned DiagID)
Return the category number that a specified DiagID belongs to, or 0 if no category.
static std::optional< diag::Group > getGroupForDiag(unsigned DiagID)
Return the lowest-level group that contains the specified diagnostic.
static StringRef getWarningOptionForGroup(diag::Group)
Given a group ID, returns the flag that toggles the group.
static bool isARCDiagnostic(unsigned DiagID)
Return true if a given diagnostic falls into an ARC diagnostic category.
Level
The level of the diagnostic, after it has been through mapping.
static void getAllDiagnostics(diag::Flavor Flavor, std::vector< diag::kind > &Diags)
Get the set of all diagnostic IDs.
static DiagnosticMapping getDefaultMapping(unsigned DiagID)
Get the default mapping for this diagnostic.
unsigned getCustomDiagID(Level L, StringRef FormatString)
Return an ID for a diagnostic with the specified format string and level.
static bool isDeferrable(unsigned DiagID)
Whether the diagnostic message can be deferred.
bool hasNoErrorAsFatal() const
bool wasUpgradedFromWarning() const
Whether this mapping attempted to map the diagnostic to a warning, but was overruled because the diag...
unsigned serialize() const
Serialize this mapping as a raw integer.
bool operator==(DiagnosticMapping Other) const
void setNoWarningAsError(bool Value)
void setSeverity(diag::Severity Value)
static DiagnosticMapping deserialize(unsigned Bits)
Deserialize a mapping.
diag::Severity getSeverity() const
void setUpgradedFromWarning(bool Value)
static DiagnosticMapping Make(diag::Severity Severity, bool IsUser, bool IsPragma)
void setNoErrorAsFatal(bool Value)
bool hasNoWarningAsError() const
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Encodes a location in the source.
@ NUM_BUILTIN_COMMON_DIAGNOSTICS
Definition: DiagnosticIDs.h:75
@ DIAG_SIZE_SERIALIZATION
Definition: DiagnosticIDs.h:36
@ DIAG_SIZE_REFACTORING
Definition: DiagnosticIDs.h:44
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing),...
Definition: DiagnosticIDs.h:83
@ Warning
Present this diagnostic as a warning.
@ Fatal
Present this diagnostic as a fatal error.
@ Error
Present this diagnostic as an error.
@ Remark
Present this diagnostic as a remark.
@ Ignored
Do not present this diagnostic, ignore it.
Flavor
Flavors of diagnostics we can emit.
Definition: DiagnosticIDs.h:94
@ WarningOrError
A diagnostic that indicates a problem or potential problem.
@ DIAG_START_SERIALIZATION
Definition: DiagnosticIDs.h:51
@ DIAG_START_REFACTORING
Definition: DiagnosticIDs.h:59
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:66
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
@ Other
Other implicit parameter.