clang 17.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 unsigned Severity : 3;
104 unsigned IsUser : 1;
105 unsigned IsPragma : 1;
106 unsigned HasNoWarningAsError : 1;
107 unsigned HasNoErrorAsFatal : 1;
108 unsigned WasUpgradedFromWarning : 1;
109
110public:
111 static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
112 bool IsPragma) {
114 Result.Severity = (unsigned)Severity;
115 Result.IsUser = IsUser;
116 Result.IsPragma = IsPragma;
117 Result.HasNoWarningAsError = 0;
118 Result.HasNoErrorAsFatal = 0;
119 Result.WasUpgradedFromWarning = 0;
120 return Result;
121 }
122
123 diag::Severity getSeverity() const { return (diag::Severity)Severity; }
125
126 bool isUser() const { return IsUser; }
127 bool isPragma() const { return IsPragma; }
128
129 bool isErrorOrFatal() const {
132 }
133
134 bool hasNoWarningAsError() const { return HasNoWarningAsError; }
135 void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
136
137 bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
138 void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
139
140 /// Whether this mapping attempted to map the diagnostic to a warning, but
141 /// was overruled because the diagnostic was already mapped to an error or
142 /// fatal error.
143 bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
144 void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
145
146 /// Serialize this mapping as a raw integer.
147 unsigned serialize() const {
148 return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
149 (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
150 }
151 /// Deserialize a mapping.
152 static DiagnosticMapping deserialize(unsigned Bits) {
154 Result.IsUser = (Bits >> 7) & 1;
155 Result.IsPragma = (Bits >> 6) & 1;
156 Result.HasNoWarningAsError = (Bits >> 5) & 1;
157 Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
158 Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
159 Result.Severity = Bits & 0x7;
160 return Result;
161 }
162};
163
164/// Used for handling and querying diagnostic IDs.
165///
166/// Can be used and shared by multiple Diagnostics for multiple translation units.
167class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
168public:
169 /// The level of the diagnostic, after it has been through mapping.
170 enum Level {
172 };
173
174private:
175 /// Information for uniquing and looking up custom diags.
176 std::unique_ptr<diag::CustomDiagInfo> CustomDiagInfo;
177
178public:
181
182 /// Return an ID for a diagnostic with the specified format string and
183 /// level.
184 ///
185 /// If this is the first request for this diagnostic, it is registered and
186 /// created, otherwise the existing ID is returned.
187
188 // FIXME: Replace this function with a create-only facilty like
189 // createCustomDiagIDFromFormatString() to enforce safe usage. At the time of
190 // writing, nearly all callers of this function were invalid.
191 unsigned getCustomDiagID(Level L, StringRef FormatString);
192
193 //===--------------------------------------------------------------------===//
194 // Diagnostic classification and reporting interfaces.
195 //
196
197 /// Given a diagnostic ID, return a description of the issue.
198 StringRef getDescription(unsigned DiagID) const;
199
200 /// Return true if the unmapped diagnostic levelof the specified
201 /// diagnostic ID is a Warning or Extension.
202 ///
203 /// This only works on builtin diagnostics, not custom ones, and is not
204 /// legal to call on NOTEs.
205 static bool isBuiltinWarningOrExtension(unsigned DiagID);
206
207 /// Return true if the specified diagnostic is mapped to errors by
208 /// default.
209 static bool isDefaultMappingAsError(unsigned DiagID);
210
211 /// Determine whether the given built-in diagnostic ID is a Note.
212 static bool isBuiltinNote(unsigned DiagID);
213
214 /// Determine whether the given built-in diagnostic ID is for an
215 /// extension of some sort.
216 static bool isBuiltinExtensionDiag(unsigned DiagID) {
217 bool ignored;
218 return isBuiltinExtensionDiag(DiagID, ignored);
219 }
220
221 /// Determine whether the given built-in diagnostic ID is for an
222 /// extension of some sort, and whether it is enabled by default.
223 ///
224 /// This also returns EnabledByDefault, which is set to indicate whether the
225 /// diagnostic is ignored by default (in which case -pedantic enables it) or
226 /// treated as a warning/error by default.
227 ///
228 static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
229
230 /// Given a group ID, returns the flag that toggles the group.
231 /// For example, for Group::DeprecatedDeclarations, returns
232 /// "deprecated-declarations".
233 static StringRef getWarningOptionForGroup(diag::Group);
234
235 /// Given a diagnostic group ID, return its documentation.
236 static StringRef getWarningOptionDocumentation(diag::Group GroupID);
237
238 /// Given a group ID, returns the flag that toggles the group.
239 /// For example, for "deprecated-declarations", returns
240 /// Group::DeprecatedDeclarations.
241 static std::optional<diag::Group> getGroupForWarningOption(StringRef);
242
243 /// Return the lowest-level group that contains the specified diagnostic.
244 static std::optional<diag::Group> getGroupForDiag(unsigned DiagID);
245
246 /// Return the lowest-level warning option that enables the specified
247 /// diagnostic.
248 ///
249 /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
250 static StringRef getWarningOptionForDiag(unsigned DiagID);
251
252 /// Return the category number that a specified \p DiagID belongs to,
253 /// or 0 if no category.
254 static unsigned getCategoryNumberForDiag(unsigned DiagID);
255
256 /// Return the number of diagnostic categories.
257 static unsigned getNumberOfCategories();
258
259 /// Given a category ID, return the name of the category.
260 static StringRef getCategoryNameFromID(unsigned CategoryID);
261
262 /// Return true if a given diagnostic falls into an ARC diagnostic
263 /// category.
264 static bool isARCDiagnostic(unsigned DiagID);
265
266 /// Enumeration describing how the emission of a diagnostic should
267 /// be treated when it occurs during C++ template argument deduction.
269 /// The diagnostic should not be reported, but it should cause
270 /// template argument deduction to fail.
271 ///
272 /// The vast majority of errors that occur during template argument
273 /// deduction fall into this category.
275
276 /// The diagnostic should be suppressed entirely.
277 ///
278 /// Warnings generally fall into this category.
280
281 /// The diagnostic should be reported.
282 ///
283 /// The diagnostic should be reported. Various fatal errors (e.g.,
284 /// template instantiation depth exceeded) fall into this category.
286
287 /// The diagnostic is an access-control diagnostic, which will be
288 /// substitution failures in some contexts and reported in others.
290 };
291
292 /// Determines whether the given built-in diagnostic ID is
293 /// for an error that is suppressed if it occurs during C++ template
294 /// argument deduction.
295 ///
296 /// When an error is suppressed due to SFINAE, the template argument
297 /// deduction fails but no diagnostic is emitted. Certain classes of
298 /// errors, such as those errors that involve C++ access control,
299 /// are not SFINAE errors.
300 static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
301
302 /// Whether the diagnostic message can be deferred.
303 ///
304 /// For single source offloading languages, a diagnostic message occurred
305 /// in a device host function may be deferred until the function is sure
306 /// to be emitted.
307 static bool isDeferrable(unsigned DiagID);
308
309 /// Get the string of all diagnostic flags.
310 ///
311 /// \returns A list of all diagnostics flags as they would be written in a
312 /// command line invocation including their `no-` variants. For example:
313 /// `{"-Wempty-body", "-Wno-empty-body", ...}`
314 static std::vector<std::string> getDiagnosticFlags();
315
316 /// Get the set of all diagnostic IDs in the group with the given name.
317 ///
318 /// \param[out] Diags - On return, the diagnostics in the group.
319 /// \returns \c true if the given group is unknown, \c false otherwise.
320 bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
321 SmallVectorImpl<diag::kind> &Diags) const;
322
323 /// Get the set of all diagnostic IDs.
324 static void getAllDiagnostics(diag::Flavor Flavor,
325 std::vector<diag::kind> &Diags);
326
327 /// Get the diagnostic option with the closest edit distance to the
328 /// given group name.
329 static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group);
330
331private:
332 /// Classify the specified diagnostic ID into a Level, consumable by
333 /// the DiagnosticClient.
334 ///
335 /// The classification is based on the way the client configured the
336 /// DiagnosticsEngine object.
337 ///
338 /// \param Loc The source location for which we are interested in finding out
339 /// the diagnostic state. Can be null in order to query the latest state.
341 getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
342 const DiagnosticsEngine &Diag) const LLVM_READONLY;
343
345 getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
346 const DiagnosticsEngine &Diag) const LLVM_READONLY;
347
348 /// Used to report a diagnostic that is finally fully formed.
349 ///
350 /// \returns \c true if the diagnostic was emitted, \c false if it was
351 /// suppressed.
352 bool ProcessDiag(DiagnosticsEngine &Diag) const;
353
354 /// Used to emit a diagnostic that is finally fully formed,
355 /// ignoring suppression.
356 void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
357
358 /// Whether the diagnostic may leave the AST in a state where some
359 /// invariants can break.
360 bool isUnrecoverable(unsigned DiagID) const;
361
362 friend class DiagnosticsEngine;
363};
364
365} // end namespace clang
366
367#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.
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.
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
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.
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:66
@ DIAG_START_SERIALIZATION
Definition: DiagnosticIDs.h:51
@ DIAG_START_REFACTORING
Definition: DiagnosticIDs.h:59
@ DIAG_SIZE_SERIALIZATION
Definition: DiagnosticIDs.h:36
@ DIAG_SIZE_REFACTORING
Definition: DiagnosticIDs.h:44
@ Result
The result type of a method or function.