clang 20.0.0git
DiagnosticIDs.cpp
Go to the documentation of this file.
1//===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===//
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// This file implements the Diagnostic IDs-related interfaces.
10//
11//===----------------------------------------------------------------------===//
12
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringTable.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/Path.h"
22#include <map>
23#include <optional>
24using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Builtin Diagnostic information
28//===----------------------------------------------------------------------===//
29
30namespace {
31
32struct StaticDiagInfoRec;
33
34// Store the descriptions in a separate table to avoid pointers that need to
35// be relocated, and also decrease the amount of data needed on 64-bit
36// platforms. See "How To Write Shared Libraries" by Ulrich Drepper.
37struct StaticDiagInfoDescriptionStringTable {
38#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
39 SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \
40 char ENUM##_desc[sizeof(DESC)];
42#undef DIAG
43};
44
45const StaticDiagInfoDescriptionStringTable StaticDiagInfoDescriptions = {
46#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
47 SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \
48 DESC,
50#undef DIAG
51};
52
53extern const StaticDiagInfoRec StaticDiagInfo[];
54
55// Stored separately from StaticDiagInfoRec to pack better. Otherwise,
56// StaticDiagInfoRec would have extra padding on 64-bit platforms.
57const uint32_t StaticDiagInfoDescriptionOffsets[] = {
58#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
59 SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \
60 offsetof(StaticDiagInfoDescriptionStringTable, ENUM##_desc),
62#undef DIAG
63};
64
65// Diagnostic classes.
66enum DiagnosticClass {
67 CLASS_NOTE = 0x01,
68 CLASS_REMARK = 0x02,
69 CLASS_WARNING = 0x03,
70 CLASS_EXTENSION = 0x04,
71 CLASS_ERROR = 0x05
72};
73
74struct StaticDiagInfoRec {
75 uint16_t DiagID;
76 LLVM_PREFERRED_TYPE(diag::Severity)
77 uint8_t DefaultSeverity : 3;
78 LLVM_PREFERRED_TYPE(DiagnosticClass)
79 uint8_t Class : 3;
80 LLVM_PREFERRED_TYPE(DiagnosticIDs::SFINAEResponse)
81 uint8_t SFINAE : 2;
82 uint8_t Category : 6;
83 LLVM_PREFERRED_TYPE(bool)
84 uint8_t WarnNoWerror : 1;
85 LLVM_PREFERRED_TYPE(bool)
86 uint8_t WarnShowInSystemHeader : 1;
87 LLVM_PREFERRED_TYPE(bool)
88 uint8_t WarnShowInSystemMacro : 1;
89
90 uint16_t OptionGroupIndex : 15;
91 LLVM_PREFERRED_TYPE(bool)
92 uint16_t Deferrable : 1;
93
94 uint16_t DescriptionLen;
95
96 unsigned getOptionGroupIndex() const {
97 return OptionGroupIndex;
98 }
99
100 StringRef getDescription() const {
101 size_t MyIndex = this - &StaticDiagInfo[0];
102 uint32_t StringOffset = StaticDiagInfoDescriptionOffsets[MyIndex];
103 const char* Table = reinterpret_cast<const char*>(&StaticDiagInfoDescriptions);
104 return StringRef(&Table[StringOffset], DescriptionLen);
105 }
106
107 diag::Flavor getFlavor() const {
108 return Class == CLASS_REMARK ? diag::Flavor::Remark
109 : diag::Flavor::WarningOrError;
110 }
111
112 bool operator<(const StaticDiagInfoRec &RHS) const {
113 return DiagID < RHS.DiagID;
114 }
115};
116
117#define STRINGIFY_NAME(NAME) #NAME
118#define VALIDATE_DIAG_SIZE(NAME) \
119 static_assert( \
120 static_cast<unsigned>(diag::NUM_BUILTIN_##NAME##_DIAGNOSTICS) < \
121 static_cast<unsigned>(diag::DIAG_START_##NAME) + \
122 static_cast<unsigned>(diag::DIAG_SIZE_##NAME), \
123 STRINGIFY_NAME( \
124 DIAG_SIZE_##NAME) " is insufficient to contain all " \
125 "diagnostics, it may need to be made larger in " \
126 "DiagnosticIDs.h.");
127VALIDATE_DIAG_SIZE(COMMON)
128VALIDATE_DIAG_SIZE(DRIVER)
129VALIDATE_DIAG_SIZE(FRONTEND)
130VALIDATE_DIAG_SIZE(SERIALIZATION)
135VALIDATE_DIAG_SIZE(CROSSTU)
137VALIDATE_DIAG_SIZE(ANALYSIS)
138VALIDATE_DIAG_SIZE(REFACTORING)
139VALIDATE_DIAG_SIZE(INSTALLAPI)
140#undef VALIDATE_DIAG_SIZE
141#undef STRINGIFY_NAME
142
143const StaticDiagInfoRec StaticDiagInfo[] = {
144// clang-format off
145#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
146 SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \
147 { \
148 diag::ENUM, \
149 DEFAULT_SEVERITY, \
150 CLASS, \
151 DiagnosticIDs::SFINAE, \
152 CATEGORY, \
153 NOWERROR, \
154 SHOWINSYSHEADER, \
155 SHOWINSYSMACRO, \
156 GROUP, \
157 DEFERRABLE, \
158 STR_SIZE(DESC, uint16_t)},
159#include "clang/Basic/DiagnosticCommonKinds.inc"
160#include "clang/Basic/DiagnosticDriverKinds.inc"
161#include "clang/Basic/DiagnosticFrontendKinds.inc"
162#include "clang/Basic/DiagnosticSerializationKinds.inc"
163#include "clang/Basic/DiagnosticLexKinds.inc"
164#include "clang/Basic/DiagnosticParseKinds.inc"
165#include "clang/Basic/DiagnosticASTKinds.inc"
166#include "clang/Basic/DiagnosticCommentKinds.inc"
167#include "clang/Basic/DiagnosticCrossTUKinds.inc"
168#include "clang/Basic/DiagnosticSemaKinds.inc"
169#include "clang/Basic/DiagnosticAnalysisKinds.inc"
170#include "clang/Basic/DiagnosticRefactoringKinds.inc"
171#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
172// clang-format on
173#undef DIAG
174};
175
176} // namespace
177
178static const unsigned StaticDiagInfoSize = std::size(StaticDiagInfo);
179
180/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
181/// or null if the ID is invalid.
182static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
183 // Out of bounds diag. Can't be in the table.
184 using namespace diag;
185 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
186 return nullptr;
187
188 // Compute the index of the requested diagnostic in the static table.
189 // 1. Add the number of diagnostics in each category preceding the
190 // diagnostic and of the category the diagnostic is in. This gives us
191 // the offset of the category in the table.
192 // 2. Subtract the number of IDs in each category from our ID. This gives us
193 // the offset of the diagnostic in the category.
194 // This is cheaper than a binary search on the table as it doesn't touch
195 // memory at all.
196 unsigned Offset = 0;
197 unsigned ID = DiagID - DIAG_START_COMMON - 1;
198#define CATEGORY(NAME, PREV) \
199 if (DiagID > DIAG_START_##NAME) { \
200 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
201 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
202 }
203CATEGORY(DRIVER, COMMON)
204CATEGORY(FRONTEND, DRIVER)
205CATEGORY(SERIALIZATION, FRONTEND)
206CATEGORY(LEX, SERIALIZATION)
207CATEGORY(PARSE, LEX)
208CATEGORY(AST, PARSE)
209CATEGORY(COMMENT, AST)
210CATEGORY(CROSSTU, COMMENT)
211CATEGORY(SEMA, CROSSTU)
212CATEGORY(ANALYSIS, SEMA)
213CATEGORY(REFACTORING, ANALYSIS)
214CATEGORY(INSTALLAPI, REFACTORING)
215#undef CATEGORY
216
217 // Avoid out of bounds reads.
218 if (ID + Offset >= StaticDiagInfoSize)
219 return nullptr;
220
221 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
222
223 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
224 // If the diag id doesn't match we found a different diag, abort. This can
225 // happen when this function is called with an ID that points into a hole in
226 // the diagID space.
227 if (Found->DiagID != DiagID)
228 return nullptr;
229 return Found;
230}
231
234 diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
235
236 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
237 Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
238
239 if (StaticInfo->WarnNoWerror) {
240 assert(Info.getSeverity() == diag::Severity::Warning &&
241 "Unexpected mapping with no-Werror bit!");
242 Info.setNoWarningAsError(true);
243 }
244 }
245
246 return Info;
247}
248
249/// getCategoryNumberForDiag - Return the category number that a specified
250/// DiagID belongs to, or 0 if no category.
252 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
253 return Info->Category;
254 return 0;
255}
256
257namespace {
258 // The diagnostic category names.
259 struct StaticDiagCategoryRec {
260 const char *NameStr;
261 uint8_t NameLen;
262
263 StringRef getName() const {
264 return StringRef(NameStr, NameLen);
265 }
266 };
267}
268
269static const StaticDiagCategoryRec CategoryNameTable[] = {
270#define GET_CATEGORY_TABLE
271#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
272#include "clang/Basic/DiagnosticGroups.inc"
273#undef GET_CATEGORY_TABLE
274 { nullptr, 0 }
275};
276
277/// getNumberOfCategories - Return the number of categories
279 return std::size(CategoryNameTable) - 1;
280}
281
282/// getCategoryNameFromID - Given a category ID, return the name of the
283/// category, an empty string if CategoryID is zero, or null if CategoryID is
284/// invalid.
285StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
286 if (CategoryID >= getNumberOfCategories())
287 return StringRef();
288 return CategoryNameTable[CategoryID].getName();
289}
290
291
292
295 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
296 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
297 return SFINAE_Report;
298}
299
300bool DiagnosticIDs::isDeferrable(unsigned DiagID) {
301 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
302 return Info->Deferrable;
303 return false;
304}
305
306/// getBuiltinDiagClass - Return the class field of the diagnostic.
307///
308static unsigned getBuiltinDiagClass(unsigned DiagID) {
309 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
310 return Info->Class;
311 return ~0U;
312}
313
314//===----------------------------------------------------------------------===//
315// Custom Diagnostic information
316//===----------------------------------------------------------------------===//
317
318namespace clang {
319 namespace diag {
321 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
322 std::vector<DiagDesc> DiagInfo;
323 std::map<DiagDesc, unsigned> DiagIDs;
324 public:
325
326 /// getDescription - Return the description of the specified custom
327 /// diagnostic.
328 StringRef getDescription(unsigned DiagID) const {
329 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
330 "Invalid diagnostic ID");
331 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
332 }
333
334 /// getLevel - Return the level of the specified custom diagnostic.
335 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
336 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
337 "Invalid diagnostic ID");
338 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
339 }
340
341 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
342 DiagnosticIDs &Diags) {
343 DiagDesc D(L, std::string(Message));
344 // Check to see if it already exists.
345 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
346 if (I != DiagIDs.end() && I->first == D)
347 return I->second;
348
349 // If not, assign a new ID.
350 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
351 DiagIDs.insert(std::make_pair(D, ID));
352 DiagInfo.push_back(D);
353 return ID;
354 }
355 };
356
357 } // end diag namespace
358} // end clang namespace
359
360
361//===----------------------------------------------------------------------===//
362// Common Diagnostic implementation
363//===----------------------------------------------------------------------===//
364
366
368
369/// getCustomDiagID - Return an ID for a diagnostic with the specified message
370/// and level. If this is the first request for this diagnostic, it is
371/// registered and created, otherwise the existing ID is returned.
372///
373/// \param FormatString A fixed diagnostic format string that will be hashed and
374/// mapped to a unique DiagID.
375unsigned