]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Basic/DiagnosticIDs.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Basic / DiagnosticIDs.h
1 //===--- DiagnosticIDs.h - Diagnostic IDs Handling --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Defines the Diagnostic IDs-related interfaces.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
16 #define LLVM_CLANG_BASIC_DIAGNOSTICIDS_H
17
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/StringRef.h"
21
22 namespace clang {
23   class DiagnosticsEngine;
24   class SourceLocation;
25
26   // Import the diagnostic enums themselves.
27   namespace diag {
28     // Start position for diagnostics.
29     enum {
30       DIAG_START_COMMON        =                                 0,
31       DIAG_START_DRIVER        = DIAG_START_COMMON          +  300,
32       DIAG_START_FRONTEND      = DIAG_START_DRIVER          +  200,
33       DIAG_START_SERIALIZATION = DIAG_START_FRONTEND        +  100,
34       DIAG_START_LEX           = DIAG_START_SERIALIZATION   +  120,
35       DIAG_START_PARSE         = DIAG_START_LEX             +  300,
36       DIAG_START_AST           = DIAG_START_PARSE           +  500,
37       DIAG_START_COMMENT       = DIAG_START_AST             +  110,
38       DIAG_START_SEMA          = DIAG_START_COMMENT         +  100,
39       DIAG_START_ANALYSIS      = DIAG_START_SEMA            + 3500,
40       DIAG_UPPER_LIMIT         = DIAG_START_ANALYSIS        +  100
41     };
42
43     class CustomDiagInfo;
44
45     /// \brief All of the diagnostics that can be emitted by the frontend.
46     typedef unsigned kind;
47
48     // Get typedefs for common diagnostics.
49     enum {
50 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,\
51              SFINAE,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
52 #define COMMONSTART
53 #include "clang/Basic/DiagnosticCommonKinds.inc"
54       NUM_BUILTIN_COMMON_DIAGNOSTICS
55 #undef DIAG
56     };
57
58     /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
59     /// to either Ignore (nothing), Remark (emit a remark), Warning
60     /// (emit a warning) or Error (emit as an error).  It allows clients to
61     /// map ERRORs to Error or Fatal (stop emitting diagnostics after this one).
62     enum class Severity {
63       // NOTE: 0 means "uncomputed".
64       Ignored = 1, ///< Do not present this diagnostic, ignore it.
65       Remark = 2,  ///< Present this diagnostic as a remark.
66       Warning = 3, ///< Present this diagnostic as a warning.
67       Error = 4,   ///< Present this diagnostic as an error.
68       Fatal = 5    ///< Present this diagnostic as a fatal error.
69     };
70
71     /// Flavors of diagnostics we can emit. Used to filter for a particular
72     /// kind of diagnostic (for instance, for -W/-R flags).
73     enum class Flavor {
74       WarningOrError, ///< A diagnostic that indicates a problem or potential
75                       ///< problem. Can be made fatal by -Werror.
76       Remark          ///< A diagnostic that indicates normal progress through
77                       ///< compilation.
78     };
79   }
80
81 class DiagnosticMapping {
82   unsigned Severity : 3;
83   unsigned IsUser : 1;
84   unsigned IsPragma : 1;
85   unsigned HasNoWarningAsError : 1;
86   unsigned HasNoErrorAsFatal : 1;
87   unsigned WasUpgradedFromWarning : 1;
88
89 public:
90   static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
91                                 bool IsPragma) {
92     DiagnosticMapping Result;
93     Result.Severity = (unsigned)Severity;
94     Result.IsUser = IsUser;
95     Result.IsPragma = IsPragma;
96     Result.HasNoWarningAsError = 0;
97     Result.HasNoErrorAsFatal = 0;
98     Result.WasUpgradedFromWarning = 0;
99     return Result;
100   }
101
102   diag::Severity getSeverity() const { return (diag::Severity)Severity; }
103   void setSeverity(diag::Severity Value) { Severity = (unsigned)Value; }
104
105   bool isUser() const { return IsUser; }
106   bool isPragma() const { return IsPragma; }
107
108   bool isErrorOrFatal() const {
109     return getSeverity() == diag::Severity::Error ||
110            getSeverity() == diag::Severity::Fatal;
111   }
112
113   bool hasNoWarningAsError() const { return HasNoWarningAsError; }
114   void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
115
116   bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
117   void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
118
119   /// Whether this mapping attempted to map the diagnostic to a warning, but
120   /// was overruled because the diagnostic was already mapped to an error or
121   /// fatal error.
122   bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
123   void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
124
125   /// Serialize this mapping as a raw integer.
126   unsigned serialize() const {
127     return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
128            (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
129   }
130   /// Deserialize a mapping.
131   static DiagnosticMapping deserialize(unsigned Bits) {
132     DiagnosticMapping Result;
133     Result.IsUser = (Bits >> 7) & 1;
134     Result.IsPragma = (Bits >> 6) & 1;
135     Result.HasNoWarningAsError = (Bits >> 5) & 1;
136     Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
137     Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
138     Result.Severity = Bits & 0x7;
139     return Result;
140   }
141 };
142
143 /// \brief Used for handling and querying diagnostic IDs.
144 ///
145 /// Can be used and shared by multiple Diagnostics for multiple translation units.
146 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
147 public:
148   /// \brief The level of the diagnostic, after it has been through mapping.
149   enum Level {
150     Ignored, Note, Remark, Warning, Error, Fatal
151   };
152
153 private:
154   /// \brief Information for uniquing and looking up custom diags.
155   diag::CustomDiagInfo *CustomDiagInfo;
156
157 public:
158   DiagnosticIDs();
159   ~DiagnosticIDs();
160
161   /// \brief Return an ID for a diagnostic with the specified format string and
162   /// level.
163   ///
164   /// If this is the first request for this diagnostic, it is registered and
165   /// created, otherwise the existing ID is returned.
166
167   // FIXME: Replace this function with a create-only facilty like
168   // createCustomDiagIDFromFormatString() to enforce safe usage. At the time of
169   // writing, nearly all callers of this function were invalid.
170   unsigned getCustomDiagID(Level L, StringRef FormatString);
171
172   //===--------------------------------------------------------------------===//
173   // Diagnostic classification and reporting interfaces.
174   //
175
176   /// \brief Given a diagnostic ID, return a description of the issue.
177   StringRef getDescription(unsigned DiagID) const;
178
179   /// \brief Return true if the unmapped diagnostic levelof the specified
180   /// diagnostic ID is a Warning or Extension.
181   ///
182   /// This only works on builtin diagnostics, not custom ones, and is not
183   /// legal to call on NOTEs.
184   static bool isBuiltinWarningOrExtension(unsigned DiagID);
185
186   /// \brief Return true if the specified diagnostic is mapped to errors by
187   /// default.
188   static bool isDefaultMappingAsError(unsigned DiagID);
189
190   /// \brief Determine whether the given built-in diagnostic ID is a Note.
191   static bool isBuiltinNote(unsigned DiagID);
192
193   /// \brief Determine whether the given built-in diagnostic ID is for an
194   /// extension of some sort.
195   static bool isBuiltinExtensionDiag(unsigned DiagID) {
196     bool ignored;
197     return isBuiltinExtensionDiag(DiagID, ignored);
198   }
199   
200   /// \brief Determine whether the given built-in diagnostic ID is for an
201   /// extension of some sort, and whether it is enabled by default.
202   ///
203   /// This also returns EnabledByDefault, which is set to indicate whether the
204   /// diagnostic is ignored by default (in which case -pedantic enables it) or
205   /// treated as a warning/error by default.
206   ///
207   static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
208   
209
210   /// \brief Return the lowest-level warning option that enables the specified
211   /// diagnostic.
212   ///
213   /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
214   static StringRef getWarningOptionForDiag(unsigned DiagID);
215   
216   /// \brief Return the category number that a specified \p DiagID belongs to,
217   /// or 0 if no category.
218   static unsigned getCategoryNumberForDiag(unsigned DiagID);
219
220   /// \brief Return the number of diagnostic categories.
221   static unsigned getNumberOfCategories();
222
223   /// \brief Given a category ID, return the name of the category.
224   static StringRef getCategoryNameFromID(unsigned CategoryID);
225   
226   /// \brief Return true if a given diagnostic falls into an ARC diagnostic
227   /// category.
228   static bool isARCDiagnostic(unsigned DiagID);
229
230   /// \brief Enumeration describing how the emission of a diagnostic should
231   /// be treated when it occurs during C++ template argument deduction.
232   enum SFINAEResponse {
233     /// \brief The diagnostic should not be reported, but it should cause
234     /// template argument deduction to fail.
235     ///
236     /// The vast majority of errors that occur during template argument 
237     /// deduction fall into this category.
238     SFINAE_SubstitutionFailure,
239     
240     /// \brief The diagnostic should be suppressed entirely.
241     ///
242     /// Warnings generally fall into this category.
243     SFINAE_Suppress,
244     
245     /// \brief The diagnostic should be reported.
246     ///
247     /// The diagnostic should be reported. Various fatal errors (e.g., 
248     /// template instantiation depth exceeded) fall into this category.
249     SFINAE_Report,
250     
251     /// \brief The diagnostic is an access-control diagnostic, which will be
252     /// substitution failures in some contexts and reported in others.
253     SFINAE_AccessControl
254   };
255   
256   /// \brief Determines whether the given built-in diagnostic ID is
257   /// for an error that is suppressed if it occurs during C++ template
258   /// argument deduction.
259   ///
260   /// When an error is suppressed due to SFINAE, the template argument
261   /// deduction fails but no diagnostic is emitted. Certain classes of
262   /// errors, such as those errors that involve C++ access control,
263   /// are not SFINAE errors.
264   static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
265
266   /// \brief Get the set of all diagnostic IDs in the group with the given name.
267   ///
268   /// \param[out] Diags - On return, the diagnostics in the group.
269   /// \returns \c true if the given group is unknown, \c false otherwise.
270   bool getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
271                              SmallVectorImpl<diag::kind> &Diags) const;
272
273   /// \brief Get the set of all diagnostic IDs.
274   void getAllDiagnostics(diag::Flavor Flavor,
275                          SmallVectorImpl<diag::kind> &Diags) const;
276
277   /// \brief Get the diagnostic option with the closest edit distance to the
278   /// given group name.
279   static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group);
280
281 private:
282   /// \brief Classify the specified diagnostic ID into a Level, consumable by
283   /// the DiagnosticClient.
284   /// 
285   /// The classification is based on the way the client configured the
286   /// DiagnosticsEngine object.
287   ///
288   /// \param Loc The source location for which we are interested in finding out
289   /// the diagnostic state. Can be null in order to query the latest state.
290   DiagnosticIDs::Level
291   getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
292                      const DiagnosticsEngine &Diag) const LLVM_READONLY;
293
294   diag::Severity
295   getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
296                         const DiagnosticsEngine &Diag) const LLVM_READONLY;
297
298   /// \brief Used to report a diagnostic that is finally fully formed.
299   ///
300   /// \returns \c true if the diagnostic was emitted, \c false if it was
301   /// suppressed.
302   bool ProcessDiag(DiagnosticsEngine &Diag) const;
303
304   /// \brief Used to emit a diagnostic that is finally fully formed,
305   /// ignoring suppression.
306   void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
307
308   /// \brief Whether the diagnostic may leave the AST in a state where some
309   /// invariants can break.
310   bool isUnrecoverable(unsigned DiagID) const;
311
312   friend class DiagnosticsEngine;
313 };
314
315 }  // end namespace clang
316
317 #endif