blob: e1f400adaa56792ece0150bcf7ec406fd2628d8a [file] [log] [blame]
Argyrios Kyrtzidis33e4e702010-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 Kyrtzidis33e4e702010-11-18 20:06:41 +000014#include "clang/Basic/DiagnosticIDs.h"
David Blaikie6c448862012-02-15 21:58:34 +000015#include "clang/Basic/AllDiagnostics.h"
John McCall923cd572011-06-15 21:46:43 +000016#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000017#include "clang/Basic/SourceManager.h"
Daniel Dunbar3f839462011-09-29 01:47:16 +000018#include "llvm/ADT/SmallVector.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000019#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000020#include <map>
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Builtin Diagnostic information
25//===----------------------------------------------------------------------===//
26
27namespace {
28
29// Diagnostic classes.
30enum {
31 CLASS_NOTE = 0x01,
32 CLASS_WARNING = 0x02,
33 CLASS_EXTENSION = 0x03,
34 CLASS_ERROR = 0x04
35};
36
37struct StaticDiagInfoRec {
38 unsigned short DiagID;
39 unsigned Mapping : 3;
40 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000041 unsigned SFINAE : 1;
42 unsigned AccessControl : 1;
Daniel Dunbar4213df32011-09-29 00:34:06 +000043 unsigned WarnNoWerror : 1;
44 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000045 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000046
Benjamin Kramerd49cb202012-02-15 20:57:03 +000047 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000048
49 uint16_t DescriptionLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000050 const char *DescriptionStr;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000051
Benjamin Kramerd49cb202012-02-15 20:57:03 +000052 unsigned getOptionGroupIndex() const {
53 return OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000054 }
55
Chris Lattner5f9e2722011-07-23 10:55:15 +000056 StringRef getDescription() const {
57 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000058 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000059
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000060 bool operator<(const StaticDiagInfoRec &RHS) const {
61 return DiagID < RHS.DiagID;
62 }
63};
64
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000065} // namespace anonymous
66
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000067static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000068#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Daniel Dunbar4213df32011-09-29 00:34:06 +000069 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
Benjamin Kramerf94d3922012-02-09 19:38:26 +000070 CATEGORY) \
Daniel Dunbar4213df32011-09-29 00:34:06 +000071 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
Benjamin Kramerd49cb202012-02-15 20:57:03 +000072 NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \
73 STR_SIZE(DESC, uint16_t), DESC },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000074#include "clang/Basic/DiagnosticCommonKinds.inc"
75#include "clang/Basic/DiagnosticDriverKinds.inc"
76#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +000077#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000078#include "clang/Basic/DiagnosticLexKinds.inc"
79#include "clang/Basic/DiagnosticParseKinds.inc"
80#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000081#include "clang/Basic/DiagnosticCommentKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000082#include "clang/Basic/DiagnosticSemaKinds.inc"
83#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000084#undef DIAG
Benjamin Kramerd49cb202012-02-15 20:57:03 +000085 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000086};
87
88static const unsigned StaticDiagInfoSize =
89 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
90
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000091/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
92/// or null if the ID is invalid.
93static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000094 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
95#ifndef NDEBUG
96 static bool IsFirst = true;
97 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000098 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000099 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
100 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000101 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000102
103 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
104 "Improperly sorted diag info");
105 }
106 IsFirst = false;
107 }
108#endif
109
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000110 // Out of bounds diag. Can't be in the table.
111 using namespace diag;
112 if (DiagID >= DIAG_UPPER_LIMIT)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000113 return 0;
114
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000115 // Compute the index of the requested diagnostic in the static table.
116 // 1. Add the number of diagnostics in each category preceeding the
117 // diagnostic and of the category the diagnostic is in. This gives us
118 // the offset of the category in the table.
119 // 2. Subtract the number of IDs in each category from our ID. This gives us
120 // the offset of the diagnostic in the category.
121 // This is cheaper than a binary search on the table as it doesn't touch
122 // memory at all.
123 unsigned Offset = 0;
124 unsigned ID = DiagID;
125#define DIAG_START_COMMON 0 // Sentinel value.
126#define CATEGORY(NAME, PREV) \
127 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000128 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
129 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000130 }
131CATEGORY(DRIVER, COMMON)
132CATEGORY(FRONTEND, DRIVER)
133CATEGORY(SERIALIZATION, FRONTEND)
134CATEGORY(LEX, SERIALIZATION)
135CATEGORY(PARSE, LEX)
136CATEGORY(AST, PARSE)
137CATEGORY(COMMENT, AST)
138CATEGORY(SEMA, COMMENT)
139CATEGORY(ANALYSIS, SEMA)
140#undef CATEGORY
141#undef DIAG_START_COMMON
142
143 // Avoid out of bounds reads.
144 if (ID + Offset >= StaticDiagInfoSize)
145 return 0;
146
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000147 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
148
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000149 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
150 // If the diag id doesn't match we found a different diag, abort. This can
151 // happen when this function is called with an ID that points into a hole in
152 // the diagID space.
153 if (Found->DiagID != DiagID)
154 return 0;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000155 return Found;
156}
157
Daniel Dunbara5e41332011-09-29 01:52:06 +0000158static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000159 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000160 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000161
Daniel Dunbara5e41332011-09-29 01:52:06 +0000162 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
163 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
164
165 if (StaticInfo->WarnNoWerror) {
166 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000167 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000168 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000169 }
170
Daniel Dunbara5e41332011-09-29 01:52:06 +0000171 if (StaticInfo->WarnShowInSystemHeader) {
172 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000173 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000174 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000175 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000176 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000177
178 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000179}
180
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000181/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000182/// DiagID belongs to, or 0 if no category.
183unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
184 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
185 return Info->Category;
186 return 0;
187}
188
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000189namespace {
190 // The diagnostic category names.
191 struct StaticDiagCategoryRec {
192 const char *NameStr;
193 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000194
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 StringRef getName() const {
196 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000197 }
198 };
199}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000200
Daniel Dunbarba494c62011-09-29 01:42:25 +0000201// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
202// particularly clean, but for now we just implement this method here so we can
203// access GetDefaultDiagMapping.
204DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
205 diag::kind Diag)
206{
207 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000208 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000209
210 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000211 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000212 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000213
214 return Result.first->second;
215}
216
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000217static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000218#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000219#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000220#include "clang/Basic/DiagnosticGroups.inc"
221#undef GET_CATEGORY_TABLE
222 { 0, 0 }
223};
224
225/// getNumberOfCategories - Return the number of categories
226unsigned DiagnosticIDs::getNumberOfCategories() {
227 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
228}
229
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000230/// getCategoryNameFromID - Given a category ID, return the name of the
231/// category, an empty string if CategoryID is zero, or null if CategoryID is
232/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000234 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000236 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000237}
238
239
240
241DiagnosticIDs::SFINAEResponse
242DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
243 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000244 if (Info->AccessControl)
245 return SFINAE_AccessControl;
246
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000247 if (!Info->SFINAE)
248 return SFINAE_Report;
249
250 if (Info->Class == CLASS_ERROR)
251 return SFINAE_SubstitutionFailure;
252
253 // Suppress notes, warnings, and extensions;
254 return SFINAE_Suppress;
255 }
256
257 return SFINAE_Report;
258}
259
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000260/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000261///
262static unsigned getBuiltinDiagClass(unsigned DiagID) {
263 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
264 return Info->Class;
265 return ~0U;
266}
267
268//===----------------------------------------------------------------------===//
269// Custom Diagnostic information
270//===----------------------------------------------------------------------===//
271
272namespace clang {
273 namespace diag {
274 class CustomDiagInfo {
275 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
276 std::vector<DiagDesc> DiagInfo;
277 std::map<DiagDesc, unsigned> DiagIDs;
278 public:
279
280 /// getDescription - Return the description of the specified custom
281 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000282 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000283 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
284 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000285 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000286 }
287
288 /// getLevel - Return the level of the specified custom diagnostic.
289 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
290 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
291 "Invalid diagnosic ID");
292 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
293 }
294
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000296 DiagnosticIDs &Diags) {
297 DiagDesc D(L, Message);
298 // Check to see if it already exists.
299 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
300 if (I != DiagIDs.end() && I->first == D)
301 return I->second;
302
303 // If not, assign a new ID.
304 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
305 DiagIDs.insert(std::make_pair(D, ID));
306 DiagInfo.push_back(D);
307 return ID;
308 }
309 };
310
311 } // end diag namespace
312} // end clang namespace
313
314
315//===----------------------------------------------------------------------===//
316// Common Diagnostic implementation
317//===----------------------------------------------------------------------===//
318
319DiagnosticIDs::DiagnosticIDs() {
320 CustomDiagInfo = 0;
321}
322
323DiagnosticIDs::~DiagnosticIDs() {
324 delete CustomDiagInfo;
325}
326
327/// getCustomDiagID - Return an ID for a diagnostic with the specified message
328/// and level. If this is the first request for this diagnosic, it is
329/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000331 if (CustomDiagInfo == 0)
332 CustomDiagInfo = new diag::CustomDiagInfo();
333 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
334}
335
336
337/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
338/// level of the specified diagnostic ID is a Warning or Extension.
339/// This only works on builtin diagnostics, not custom ones, and is not legal to
340/// call on NOTEs.
341bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
342 return DiagID < diag::DIAG_UPPER_LIMIT &&
343 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
344}
345
346/// \brief Determine whether the given built-in diagnostic ID is a
347/// Note.
348bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
349 return DiagID < diag::DIAG_UPPER_LIMIT &&
350 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
351}
352
353/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
354/// ID is for an extension of some sort. This also returns EnabledByDefault,
355/// which is set to indicate whether the diagnostic is ignored by default (in
356/// which case -pedantic enables it) or treated as a warning/error by default.
357///
358bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
359 bool &EnabledByDefault) {
360 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
361 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
362 return false;
363
Daniel Dunbara5e41332011-09-29 01:52:06 +0000364 EnabledByDefault =
365 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000366 return true;
367}
368
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000369bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
370 if (DiagID >= diag::DIAG_UPPER_LIMIT)
371 return false;
372
Daniel Dunbara5e41332011-09-29 01:52:06 +0000373 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000374}
375
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000376/// getDescription - Given a diagnostic ID, return a description of the
377/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000378StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000379 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000380 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000381 return CustomDiagInfo->getDescription(DiagID);
382}
383
David Blaikied6471f72011-09-25 23:23:43 +0000384/// getDiagnosticLevel - Based on the way the client configured the
385/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
386/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000387DiagnosticIDs::Level
388DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000389 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000390 // Handle custom diagnostics, which cannot be mapped.
391 if (DiagID >= diag::DIAG_UPPER_LIMIT)
392 return CustomDiagInfo->getLevel(DiagID);
393
394 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000395 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000396 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000397}
398
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000399/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000400/// object, classify the specified diagnostic ID into a Level, consumable by
401/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000402///
403/// \param Loc The source location we are interested in finding out the
404/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000405DiagnosticIDs::Level
406DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000407 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000408 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000409 // Specific non-error diagnostics may be mapped to various levels from ignored
410 // to error. Errors can only be mapped to fatal.
411 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
412
David Blaikied6471f72011-09-25 23:23:43 +0000413 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000414 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000415 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000416
Daniel Dunbarba494c62011-09-29 01:42:25 +0000417 // Get the mapping information, or compute it lazily.
418 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
419 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000420
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000421 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000422 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000423 Result = DiagnosticIDs::Ignored;
424 break;
425 case diag::MAP_WARNING:
426 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000427 break;
428 case diag::MAP_ERROR:
429 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000430 break;
431 case diag::MAP_FATAL:
432 Result = DiagnosticIDs::Fatal;
433 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000434 }
435
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000436 // Upgrade ignored diagnostics if -Weverything is enabled.
437 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
438 !MappingInfo.isUser())
439 Result = DiagnosticIDs::Warning;
440
Bob Wilson18c407f2011-10-12 19:55:31 +0000441 // Ignore -pedantic diagnostics inside __extension__ blocks.
442 // (The diagnostics controlled by -pedantic are the extension diagnostics
443 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000444 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000445 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
446 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000447 return DiagnosticIDs::Ignored;
448
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000449 // For extension diagnostics that haven't been explicitly mapped, check if we
450 // should upgrade the diagnostic.
451 if (IsExtensionDiag && !MappingInfo.isUser()) {
452 switch (Diag.ExtBehavior) {
453 case DiagnosticsEngine::Ext_Ignore:
454 break;
455 case DiagnosticsEngine::Ext_Warn:
456 // Upgrade ignored diagnostics to warnings.
457 if (Result == DiagnosticIDs::Ignored)
458 Result = DiagnosticIDs::Warning;
459 break;
460 case DiagnosticsEngine::Ext_Error:
461 // Upgrade ignored or warning diagnostics to errors.
462 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
463 Result = DiagnosticIDs::Error;
464 break;
465 }
466 }
467
468 // At this point, ignored errors can no longer be upgraded.
469 if (Result == DiagnosticIDs::Ignored)
470 return Result;
471
472 // Honor -w, which is lower in priority than pedantic-errors, but higher than
473 // -Werror.
474 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
475 return DiagnosticIDs::Ignored;
476
477 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
478 if (Result == DiagnosticIDs::Warning) {
479 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
480 Result = DiagnosticIDs::Error;
481 }
482
483 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
484 // disabled.
485 if (Result == DiagnosticIDs::Error) {
486 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
487 Result = DiagnosticIDs::Fatal;
488 }
489
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000490 // If we are in a system header, we ignore it. We look at the diagnostic class
491 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000492 // -pedantic-errors modes, which *map* warnings/extensions to errors.
493 if (Result >= DiagnosticIDs::Warning &&
494 DiagClass != CLASS_ERROR &&
495 // Custom diagnostics always are emitted in system headers.
496 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000497 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000498 Diag.SuppressSystemWarnings &&
499 Loc.isValid() &&
500 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000501 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000502 return DiagnosticIDs::Ignored;
503
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000504 return Result;
505}
506
Daniel Dunbar3f839462011-09-29 01:47:16 +0000507struct clang::WarningOption {
508 // Be safe with the size of 'NameLen' because we don't statically check if
509 // the size will fit in the field; the struct size won't decrease with a
510 // shorter type anyway.
511 size_t NameLen;
512 const char *NameStr;
513 const short *Members;
514 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000515
Daniel Dunbar3f839462011-09-29 01:47:16 +0000516 StringRef getName() const {
517 return StringRef(NameStr, NameLen);
518 }
519};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000520
521#define GET_DIAG_ARRAYS
522#include "clang/Basic/DiagnosticGroups.inc"
523#undef GET_DIAG_ARRAYS
524
525// Second the table of options, sorted by name for fast binary lookup.
526static const WarningOption OptionTable[] = {
527#define GET_DIAG_TABLE
528#include "clang/Basic/DiagnosticGroups.inc"
529#undef GET_DIAG_TABLE
530};
531static const size_t OptionTableSize =
532sizeof(OptionTable) / sizeof(OptionTable[0]);
533
534static bool WarningOptionCompare(const WarningOption &LHS,
535 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000536 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000537}
538
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000539/// getWarningOptionForDiag - Return the lowest-level warning option that
540/// enables the specified diagnostic. If there is no -Wfoo flag that controls
541/// the diagnostic, this returns null.
542StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
543 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
544 return OptionTable[Info->getOptionGroupIndex()].getName();
545 return StringRef();
546}
547
Daniel Dunbar3f839462011-09-29 01:47:16 +0000548void DiagnosticIDs::getDiagnosticsInGroup(
549 const WarningOption *Group,
550 llvm::SmallVectorImpl<diag::kind> &Diags) const
551{
552 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000553 if (const short *Member = Group->Members) {
554 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000555 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000556 }
557
Daniel Dunbar3f839462011-09-29 01:47:16 +0000558 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000559 if (const short *SubGroups = Group->SubGroups) {
560 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000561 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000562 }
563}
564
Daniel Dunbar3f839462011-09-29 01:47:16 +0000565bool DiagnosticIDs::getDiagnosticsInGroup(
566 StringRef Group,
567 llvm::SmallVectorImpl<diag::kind> &Diags) const
568{
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000569 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000570 const WarningOption *Found =
571 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
572 WarningOptionCompare);
573 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000574 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000575 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000576
Daniel Dunbar3f839462011-09-29 01:47:16 +0000577 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000578 return false;
579}
580
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000581void DiagnosticIDs::getAllDiagnostics(
582 llvm::SmallVectorImpl<diag::kind> &Diags) const {
583 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
584 Diags.push_back(StaticDiagInfo[i].DiagID);
585}
586
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000587StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
588 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000589 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000590 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
591 i != e; ++i) {
592 // Don't suggest ignored warning flags.
593 if (!i->Members && !i->SubGroups)
594 continue;
595
596 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000597 if (Distance == BestDistance) {
598 // Two matches with the same distance, don't prefer one over the other.
599 Best = "";
600 } else if (Distance < BestDistance) {
601 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000602 Best = i->getName();
603 BestDistance = Distance;
604 }
605 }
606
607 return Best;
608}
609
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000610/// ProcessDiag - This is the method used to report a diagnostic that is
611/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000612bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000613 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000614
615 if (Diag.SuppressAllDiagnostics)
616 return false;
617
618 assert(Diag.getClient() && "DiagnosticClient not set!");
619
620 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000621 unsigned DiagID = Info.getID();
Jordan Rosec6d64a22012-07-11 16:50:36 +0000622 DiagnosticIDs::Level DiagLevel
623 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000624
625 if (DiagLevel != DiagnosticIDs::Note) {
626 // Record that a fatal error occurred only when we see a second
627 // non-note diagnostic. This allows notes to be attached to the
628 // fatal error, but suppresses any diagnostics that follow those
629 // notes.
630 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
631 Diag.FatalErrorOccurred = true;
632
633 Diag.LastDiagLevel = DiagLevel;
634 }
635
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000636 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
637 if (DiagLevel >= DiagnosticIDs::Error) {
638 ++Diag.TrapNumErrorsOccurred;
639 if (isUnrecoverable(DiagID))
640 ++Diag.TrapNumUnrecoverableErrorsOccurred;
641 }
642
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000643 // If a fatal error has already been emitted, silence all subsequent
644 // diagnostics.
645 if (Diag.FatalErrorOccurred) {
646 if (DiagLevel >= DiagnosticIDs::Error &&
647 Diag.Client->IncludeInDiagnosticCounts()) {
648 ++Diag.NumErrors;
649 ++Diag.NumErrorsSuppressed;
650 }
651
652 return false;
653 }
654
655 // If the client doesn't care about this message, don't issue it. If this is
656 // a note and the last real diagnostic was ignored, ignore it too.
657 if (DiagLevel == DiagnosticIDs::Ignored ||
658 (DiagLevel == DiagnosticIDs::Note &&
659 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
660 return false;
661
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000662 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000663 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000664 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasper1c84c682012-09-28 15:45:07 +0000665
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000666 // Warnings which have been upgraded to errors do not prevent compilation.
667 if (isDefaultMappingAsError(DiagID))
668 Diag.UncompilableErrorOccurred = true;
669
Daniel Jasper1c84c682012-09-28 15:45:07 +0000670 Diag.ErrorOccurred = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000671 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000672 ++Diag.NumErrors;
673 }
674
Douglas Gregorf1d59482011-08-17 19:13:00 +0000675 // If we've emitted a lot of errors, emit a fatal error instead of it to
676 // stop a flood of bogus errors.
677 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
678 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000679 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000680 return false;
681 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000682 }
683
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000684 // Finally, report it.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000685 EmitDiag(Diag, DiagLevel);
686 return true;
687}
688
689void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
690 Diagnostic Info(&Diag);
691 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
692
David Blaikied6471f72011-09-25 23:23:43 +0000693 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000694 if (Diag.Client->IncludeInDiagnosticCounts()) {
695 if (DiagLevel == DiagnosticIDs::Warning)
696 ++Diag.NumWarnings;
697 }
698
699 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000700}
John McCall923cd572011-06-15 21:46:43 +0000701
702bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
703 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
704 // Custom diagnostics.
705 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
706 }
707
708 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000709 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000710 return false;
711
712 if (DiagID == diag::err_unavailable ||
713 DiagID == diag::err_unavailable_message)
714 return false;
715
John McCallf85e1932011-06-15 23:02:42 +0000716 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000717 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000718 return false;
719
John McCall923cd572011-06-15 21:46:43 +0000720 return true;
721}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000722
723bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
724 unsigned cat = getCategoryNumberForDiag(DiagID);
725 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
726}