blob: 90d5ed00214c704785f6fb257f701cd1f80cca5c [file] [log] [blame]
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001//===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===//
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// This file implements the Diagnostic IDs-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000014#include "clang/Basic/DiagnosticIDs.h"
David Blaikieaf90ec12012-02-15 21:58:34 +000015#include "clang/Basic/AllDiagnostics.h"
John McCalla877d922011-06-15 21:46:43 +000016#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000017#include "clang/Basic/SourceManager.h"
Daniel Dunbard908c122011-09-29 01:47:16 +000018#include "llvm/ADT/SmallVector.h"
David Majnemer38af2a22013-07-20 07:15:15 +000019#include "llvm/ADT/STLExtras.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000021#include <map>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Builtin Diagnostic information
26//===----------------------------------------------------------------------===//
27
28namespace {
29
30// Diagnostic classes.
31enum {
32 CLASS_NOTE = 0x01,
33 CLASS_WARNING = 0x02,
34 CLASS_EXTENSION = 0x03,
35 CLASS_ERROR = 0x04
36};
37
38struct StaticDiagInfoRec {
Craig Topperea6caba2013-07-21 21:56:18 +000039 uint16_t DiagID;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000040 unsigned Mapping : 3;
41 unsigned Class : 3;
Richard Smith16e1b072013-11-12 02:41:45 +000042 unsigned SFINAE : 2;
Daniel Dunbar6de48ee2011-09-29 00:34:06 +000043 unsigned WarnNoWerror : 1;
44 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000045 unsigned Category : 5;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000046
Benjamin Kramera8cafe22012-02-15 20:57:03 +000047 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000048
49 uint16_t DescriptionLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000050 const char *DescriptionStr;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000051
Benjamin Kramera8cafe22012-02-15 20:57:03 +000052 unsigned getOptionGroupIndex() const {
53 return OptionGroupIndex;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000054 }
55
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056 StringRef getDescription() const {
57 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000058 }
Douglas Gregor46ce91a2011-04-15 22:04:17 +000059
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000060 bool operator<(const StaticDiagInfoRec &RHS) const {
61 return DiagID < RHS.DiagID;
62 }
63};
64
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000065} // namespace anonymous
66
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000067static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000068#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Richard Smith16e1b072013-11-12 02:41:45 +000069 SFINAE,NOWERROR,SHOWINSYSHEADER,CATEGORY) \
70 { diag::ENUM, DEFAULT_MAPPING, CLASS, \
Richard Smithadf30182013-11-12 02:58:11 +000071 DiagnosticIDs::SFINAE, \
Benjamin Kramera8cafe22012-02-15 20:57:03 +000072 NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \
73 STR_SIZE(DESC, uint16_t), DESC },
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000074#include "clang/Basic/DiagnosticCommonKinds.inc"
75#include "clang/Basic/DiagnosticDriverKinds.inc"
76#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carruth22a11b72011-12-09 00:02:23 +000077#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000078#include "clang/Basic/DiagnosticLexKinds.inc"
79#include "clang/Basic/DiagnosticParseKinds.inc"
80#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000081#include "clang/Basic/DiagnosticCommentKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000082#include "clang/Basic/DiagnosticSemaKinds.inc"
83#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000084#undef DIAG
Douglas Gregor46ce91a2011-04-15 22:04:17 +000085};
86
David Majnemer38af2a22013-07-20 07:15:15 +000087static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
Douglas Gregor46ce91a2011-04-15 22:04:17 +000088
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000089/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
90/// or null if the ID is invalid.
91static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000092 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
93#ifndef NDEBUG
Craig Topper31e71a32013-07-21 18:58:40 +000094 static bool IsFirst = true; // So the check is only performed on first call.
95 if (IsFirst) {
96 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
97 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
98 "Diag ID conflict, the enums at the start of clang::diag (in "
99 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000100
Craig Topper31e71a32013-07-21 18:58:40 +0000101 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
102 "Improperly sorted diag info");
103 }
104 IsFirst = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000105 }
106#endif
107
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000108 // Out of bounds diag. Can't be in the table.
109 using namespace diag;
David Majnemer38af2a22013-07-20 07:15:15 +0000110 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000111 return 0;
112
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000113 // Compute the index of the requested diagnostic in the static table.
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000114 // 1. Add the number of diagnostics in each category preceding the
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000115 // diagnostic and of the category the diagnostic is in. This gives us
116 // the offset of the category in the table.
117 // 2. Subtract the number of IDs in each category from our ID. This gives us
118 // the offset of the diagnostic in the category.
119 // This is cheaper than a binary search on the table as it doesn't touch
120 // memory at all.
121 unsigned Offset = 0;
David Majnemer38af2a22013-07-20 07:15:15 +0000122 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000123#define CATEGORY(NAME, PREV) \
124 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000125 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
126 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000127 }
128CATEGORY(DRIVER, COMMON)
129CATEGORY(FRONTEND, DRIVER)
130CATEGORY(SERIALIZATION, FRONTEND)
131CATEGORY(LEX, SERIALIZATION)
132CATEGORY(PARSE, LEX)
133CATEGORY(AST, PARSE)
134CATEGORY(COMMENT, AST)
135CATEGORY(SEMA, COMMENT)
136CATEGORY(ANALYSIS, SEMA)
137#undef CATEGORY
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000138
139 // Avoid out of bounds reads.
140 if (ID + Offset >= StaticDiagInfoSize)
141 return 0;
142
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000143 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
144
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000145 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
146 // If the diag id doesn't match we found a different diag, abort. This can
147 // happen when this function is called with an ID that points into a hole in
148 // the diagID space.
149 if (Found->DiagID != DiagID)
150 return 0;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000151 return Found;
152}
153
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000154static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000155 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000156 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000157
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000158 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
159 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
160
161 if (StaticInfo->WarnNoWerror) {
162 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000163 "Unexpected mapping with no-Werror bit!");
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000164 Info.setNoWarningAsError(true);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000165 }
166
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000167 if (StaticInfo->WarnShowInSystemHeader) {
168 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000169 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000170 Info.setShowInSystemHeader(true);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000171 }
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000172 }
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000173
174 return Info;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000175}
176
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000177/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000178/// DiagID belongs to, or 0 if no category.
179unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
180 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
181 return Info->Category;
182 return 0;
183}
184
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000185namespace {
186 // The diagnostic category names.
187 struct StaticDiagCategoryRec {
188 const char *NameStr;
189 uint8_t NameLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000190
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000191 StringRef getName() const {
192 return StringRef(NameStr, NameLen);
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000193 }
194 };
195}
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000196
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000197// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
198// particularly clean, but for now we just implement this method here so we can
199// access GetDefaultDiagMapping.
200DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
201 diag::kind Diag)
202{
203 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000204 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000205
206 // Initialize the entry if we added it.
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000207 if (Result.second)
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000208 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000209
210 return Result.first->second;
211}
212
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000213static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000214#define GET_CATEGORY_TABLE
John McCalla877d922011-06-15 21:46:43 +0000215#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000216#include "clang/Basic/DiagnosticGroups.inc"
217#undef GET_CATEGORY_TABLE
218 { 0, 0 }
219};
220
221/// getNumberOfCategories - Return the number of categories
222unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Toppere5ce8312013-07-15 03:38:40 +0000223 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000224}
225
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000226/// getCategoryNameFromID - Given a category ID, return the name of the
227/// category, an empty string if CategoryID is zero, or null if CategoryID is
228/// invalid.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000229StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000230 if (CategoryID >= getNumberOfCategories())
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000231 return StringRef();
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000232 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000233}
234
235
236
Richard Smith16e1b072013-11-12 02:41:45 +0000237DiagnosticIDs::SFINAEResponse
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000238DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
Richard Smith16e1b072013-11-12 02:41:45 +0000239 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
240 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000241 return SFINAE_Report;
242}
243
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000244/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000245///
246static unsigned getBuiltinDiagClass(unsigned DiagID) {
247 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
248 return Info->Class;
249 return ~0U;
250}
251
252//===----------------------------------------------------------------------===//
253// Custom Diagnostic information
254//===----------------------------------------------------------------------===//
255
256namespace clang {
257 namespace diag {
258 class CustomDiagInfo {
259 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
260 std::vector<DiagDesc> DiagInfo;
261 std::map<DiagDesc, unsigned> DiagIDs;
262 public:
263
264 /// getDescription - Return the description of the specified custom
265 /// diagnostic.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000266 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000267 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000268 "Invalid diagnostic ID");
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000269 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000270 }
271
272 /// getLevel - Return the level of the specified custom diagnostic.
273 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
274 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000275 "Invalid diagnostic ID");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000276 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
277 }
278
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000280 DiagnosticIDs &Diags) {
281 DiagDesc D(L, Message);
282 // Check to see if it already exists.
283 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
284 if (I != DiagIDs.end() && I->first == D)
285 return I->second;
286
287 // If not, assign a new ID.
288 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
289 DiagIDs.insert(std::make_pair(D, ID));
290 DiagInfo.push_back(D);
291 return ID;
292 }
293 };
294
295 } // end diag namespace
296} // end clang namespace
297
298
299//===----------------------------------------------------------------------===//
300// Common Diagnostic implementation
301//===----------------------------------------------------------------------===//
302
303DiagnosticIDs::DiagnosticIDs() {
304 CustomDiagInfo = 0;
305}
306
307DiagnosticIDs::~DiagnosticIDs() {
308 delete CustomDiagInfo;
309}
310
311/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000312/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000313/// registered and created, otherwise the existing ID is returned.
Alp Toker61d41af2013-12-23 21:00:35 +0000314///
315/// \param Message A fixed diagnostic format string that will be hashed and
316/// mapped to a unique DiagID.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000317unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000318 if (CustomDiagInfo == 0)
319 CustomDiagInfo = new diag::CustomDiagInfo();
320 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
321}
322
323
324/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
325/// level of the specified diagnostic ID is a Warning or Extension.
326/// This only works on builtin diagnostics, not custom ones, and is not legal to
327/// call on NOTEs.
328bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
329 return DiagID < diag::DIAG_UPPER_LIMIT &&
330 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
331}
332
333/// \brief Determine whether the given built-in diagnostic ID is a
334/// Note.
335bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
336 return DiagID < diag::DIAG_UPPER_LIMIT &&
337 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
338}
339
340/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
341/// ID is for an extension of some sort. This also returns EnabledByDefault,
342/// which is set to indicate whether the diagnostic is ignored by default (in
343/// which case -pedantic enables it) or treated as a warning/error by default.
344///
345bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
346 bool &EnabledByDefault) {
347 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
348 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
349 return false;
350
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000351 EnabledByDefault =
352 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000353 return true;
354}
355
Daniel Dunbaraa111382011-09-29 01:01:08 +0000356bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
357 if (DiagID >= diag::DIAG_UPPER_LIMIT)
358 return false;
359
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000360 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbaraa111382011-09-29 01:01:08 +0000361}
362
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000363/// getDescription - Given a diagnostic ID, return a description of the
364/// issue.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000365StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000366 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000367 return Info->getDescription();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000368 return CustomDiagInfo->getDescription(DiagID);
369}
370
David Blaikie9c902b52011-09-25 23:23:43 +0000371/// getDiagnosticLevel - Based on the way the client configured the
372/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
373/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000374DiagnosticIDs::Level
375DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar400e7e32011-09-29 01:20:28 +0000376 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000377 // Handle custom diagnostics, which cannot be mapped.
378 if (DiagID >= diag::DIAG_UPPER_LIMIT)
379 return CustomDiagInfo->getLevel(DiagID);
380
381 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rose6f524ac2012-07-11 16:50:36 +0000382 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar400e7e32011-09-29 01:20:28 +0000383 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000384}
385
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000386/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000387/// object, classify the specified diagnostic ID into a Level, consumable by
388/// the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000389///
390/// \param Loc The source location we are interested in finding out the
391/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000392DiagnosticIDs::Level
393DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000394 SourceLocation Loc,
Daniel Dunbar400e7e32011-09-29 01:20:28 +0000395 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000396 // Specific non-error diagnostics may be mapped to various levels from ignored
397 // to error. Errors can only be mapped to fatal.
398 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
399
David Blaikie9c902b52011-09-25 23:23:43 +0000400 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000401 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikie9c902b52011-09-25 23:23:43 +0000402 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000403
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000404 // Get the mapping information, or compute it lazily.
405 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
406 (diag::kind)DiagID);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000407
Daniel Dunbara3637e62011-09-29 01:30:00 +0000408 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000409 case diag::MAP_IGNORE:
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000410 Result = DiagnosticIDs::Ignored;
411 break;
412 case diag::MAP_WARNING:
413 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000414 break;
415 case diag::MAP_ERROR:
416 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000417 break;
418 case diag::MAP_FATAL:
419 Result = DiagnosticIDs::Fatal;
420 break;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000421 }
422
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000423 // Upgrade ignored diagnostics if -Weverything is enabled.
424 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
425 !MappingInfo.isUser())
426 Result = DiagnosticIDs::Warning;
427
Bob Wilson73a4deb2011-10-12 19:55:31 +0000428 // Ignore -pedantic diagnostics inside __extension__ blocks.
429 // (The diagnostics controlled by -pedantic are the extension diagnostics
430 // that are not enabled by default.)
Daniel Dunbar787032b2011-11-28 22:19:36 +0000431 bool EnabledByDefault = false;
Bob Wilson73a4deb2011-10-12 19:55:31 +0000432 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
433 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000434 return DiagnosticIDs::Ignored;
435
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000436 // For extension diagnostics that haven't been explicitly mapped, check if we
437 // should upgrade the diagnostic.
438 if (IsExtensionDiag && !MappingInfo.isUser()) {
439 switch (Diag.ExtBehavior) {
440 case DiagnosticsEngine::Ext_Ignore:
441 break;
442 case DiagnosticsEngine::Ext_Warn:
443 // Upgrade ignored diagnostics to warnings.
444 if (Result == DiagnosticIDs::Ignored)
445 Result = DiagnosticIDs::Warning;
446 break;
447 case DiagnosticsEngine::Ext_Error:
448 // Upgrade ignored or warning diagnostics to errors.
449 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
450 Result = DiagnosticIDs::Error;
451 break;
452 }
453 }
454
455 // At this point, ignored errors can no longer be upgraded.
456 if (Result == DiagnosticIDs::Ignored)
457 return Result;
458
459 // Honor -w, which is lower in priority than pedantic-errors, but higher than
460 // -Werror.
461 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
462 return DiagnosticIDs::Ignored;
463
464 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
465 if (Result == DiagnosticIDs::Warning) {
466 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
467 Result = DiagnosticIDs::Error;
468 }
469
470 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
471 // disabled.
472 if (Result == DiagnosticIDs::Error) {
473 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
474 Result = DiagnosticIDs::Fatal;
475 }
476
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000477 // If we are in a system header, we ignore it. We look at the diagnostic class
478 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000479 // -pedantic-errors modes, which *map* warnings/extensions to errors.
480 if (Result >= DiagnosticIDs::Warning &&
481 DiagClass != CLASS_ERROR &&
482 // Custom diagnostics always are emitted in system headers.
483 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000484 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000485 Diag.SuppressSystemWarnings &&
486 Loc.isValid() &&
487 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth35f53202011-07-25 16:49:02 +0000488 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000489 return DiagnosticIDs::Ignored;
490
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000491 return Result;
492}
493
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000494#define GET_DIAG_ARRAYS
495#include "clang/Basic/DiagnosticGroups.inc"
496#undef GET_DIAG_ARRAYS
497
Craig Topperb78e9d92013-08-29 06:06:18 +0000498namespace {
499 struct WarningOption {
500 uint16_t NameOffset;
501 uint16_t Members;
502 uint16_t SubGroups;
Craig Topperda7cf8a2013-08-29 05:18:04 +0000503
Craig Topperb78e9d92013-08-29 06:06:18 +0000504 // String is stored with a pascal-style length byte.
505 StringRef getName() const {
506 return StringRef(DiagGroupNames + NameOffset + 1,
507 DiagGroupNames[NameOffset]);
508 }
509 };
510}
Craig Topperda7cf8a2013-08-29 05:18:04 +0000511
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000512// Second the table of options, sorted by name for fast binary lookup.
513static const WarningOption OptionTable[] = {
514#define GET_DIAG_TABLE
515#include "clang/Basic/DiagnosticGroups.inc"
516#undef GET_DIAG_TABLE
517};
Craig Toppere5ce8312013-07-15 03:38:40 +0000518static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000519
Craig Topperda7cf8a2013-08-29 05:18:04 +0000520static bool WarningOptionCompare(const WarningOption &LHS, StringRef RHS) {
521 return LHS.getName() < RHS;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000522}
523
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000524/// getWarningOptionForDiag - Return the lowest-level warning option that
525/// enables the specified diagnostic. If there is no -Wfoo flag that controls
526/// the diagnostic, this returns null.
527StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
528 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
529 return OptionTable[Info->getOptionGroupIndex()].getName();
530 return StringRef();
531}
532
Craig Topperb78e9d92013-08-29 06:06:18 +0000533static void getDiagnosticsInGroup(const WarningOption *Group,
534 SmallVectorImpl<diag::kind> &Diags) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000535 // Add the members of the option diagnostic set.
Craig Topperd80c17e2013-08-28 04:02:50 +0000536 const int16_t *Member = DiagArrays + Group->Members;
537 for (; *Member != -1; ++Member)
538 Diags.push_back(*Member);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000539
Daniel Dunbard908c122011-09-29 01:47:16 +0000540 // Add the members of the subgroups.
Craig Topperd80c17e2013-08-28 04:02:50 +0000541 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
542 for (; *SubGroups != (int16_t)-1; ++SubGroups)
543 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000544}
545
Daniel Dunbard908c122011-09-29 01:47:16 +0000546bool DiagnosticIDs::getDiagnosticsInGroup(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000547 StringRef Group,
548 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000549 const WarningOption *Found =
Craig Topperda7cf8a2013-08-29 05:18:04 +0000550 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Group,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000551 WarningOptionCompare);
552 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000553 Found->getName() != Group)
Daniel Dunbard908c122011-09-29 01:47:16 +0000554 return true; // Option not found.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000555
Craig Topperb78e9d92013-08-29 06:06:18 +0000556 ::getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000557 return false;
558}
559
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000560void DiagnosticIDs::getAllDiagnostics(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000561 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000562 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
563 Diags.push_back(StaticDiagInfo[i].DiagID);
564}
565
Benjamin Kramer116d8872011-11-14 23:30:34 +0000566StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
567 StringRef Best;
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000568 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramer116d8872011-11-14 23:30:34 +0000569 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
570 i != e; ++i) {
571 // Don't suggest ignored warning flags.
572 if (!i->Members && !i->SubGroups)
573 continue;
574
575 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000576 if (Distance == BestDistance) {
577 // Two matches with the same distance, don't prefer one over the other.
578 Best = "";
579 } else if (Distance < BestDistance) {
580 // This is a better match.
Benjamin Kramer116d8872011-11-14 23:30:34 +0000581 Best = i->getName();
582 BestDistance = Distance;
583 }
584 }
585
586 return Best;
587}
588
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000589/// ProcessDiag - This is the method used to report a diagnostic that is
590/// finally fully formed.
David Blaikie9c902b52011-09-25 23:23:43 +0000591bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikieb5784322011-09-26 01:18:08 +0000592 Diagnostic Info(&Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000593
594 if (Diag.SuppressAllDiagnostics)
595 return false;
596
597 assert(Diag.getClient() && "DiagnosticClient not set!");
598
599 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000600 unsigned DiagID = Info.getID();
Jordan Rose6f524ac2012-07-11 16:50:36 +0000601 DiagnosticIDs::Level DiagLevel
602 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000603
604 if (DiagLevel != DiagnosticIDs::Note) {
605 // Record that a fatal error occurred only when we see a second
606 // non-note diagnostic. This allows notes to be attached to the
607 // fatal error, but suppresses any diagnostics that follow those
608 // notes.
609 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
610 Diag.FatalErrorOccurred = true;
611
612 Diag.LastDiagLevel = DiagLevel;
613 }
614
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000615 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
616 if (DiagLevel >= DiagnosticIDs::Error) {
617 ++Diag.TrapNumErrorsOccurred;
618 if (isUnrecoverable(DiagID))
619 ++Diag.TrapNumUnrecoverableErrorsOccurred;
620 }
621
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000622 // If a fatal error has already been emitted, silence all subsequent
623 // diagnostics.
624 if (Diag.FatalErrorOccurred) {
625 if (DiagLevel >= DiagnosticIDs::Error &&
626 Diag.Client->IncludeInDiagnosticCounts()) {
627 ++Diag.NumErrors;
628 ++Diag.NumErrorsSuppressed;
629 }
630
631 return false;
632 }
633
634 // If the client doesn't care about this message, don't issue it. If this is
635 // a note and the last real diagnostic was ignored, ignore it too.
636 if (DiagLevel == DiagnosticIDs::Ignored ||
637 (DiagLevel == DiagnosticIDs::Note &&
638 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
639 return false;
640
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000641 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000642 if (isUnrecoverable(DiagID))
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000643 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000644
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000645 // Warnings which have been upgraded to errors do not prevent compilation.
646 if (isDefaultMappingAsError(DiagID))
647 Diag.UncompilableErrorOccurred = true;
648
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000649 Diag.ErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000650 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000651 ++Diag.NumErrors;
652 }
653
Douglas Gregor14208802011-08-17 19:13:00 +0000654 // If we've emitted a lot of errors, emit a fatal error instead of it to
655 // stop a flood of bogus errors.
656 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
657 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000658 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregor14208802011-08-17 19:13:00 +0000659 return false;
660 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000661 }
662
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000663 // Finally, report it.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000664 EmitDiag(Diag, DiagLevel);
665 return true;
666}
667
668void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
669 Diagnostic Info(&Diag);
670 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
671
David Blaikie9c902b52011-09-25 23:23:43 +0000672 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000673 if (Diag.Client->IncludeInDiagnosticCounts()) {
674 if (DiagLevel == DiagnosticIDs::Warning)
675 ++Diag.NumWarnings;
676 }
677
678 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000679}
John McCalla877d922011-06-15 21:46:43 +0000680
681bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
682 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
683 // Custom diagnostics.
684 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
685 }
686
687 // Only errors may be unrecoverable.
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000688 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCalla877d922011-06-15 21:46:43 +0000689 return false;
690
691 if (DiagID == diag::err_unavailable ||
692 DiagID == diag::err_unavailable_message)
693 return false;
694
John McCall31168b02011-06-15 23:02:42 +0000695 // Currently we consider all ARC errors as recoverable.
Ted Kremenek337c5b82011-10-20 05:07:47 +0000696 if (isARCDiagnostic(DiagID))
John McCall31168b02011-06-15 23:02:42 +0000697 return false;
698
John McCalla877d922011-06-15 21:46:43 +0000699 return true;
700}
Ted Kremenek337c5b82011-10-20 05:07:47 +0000701
702bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
703 unsigned cat = getCategoryNumberForDiag(DiagID);
704 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
705}