blob: 4716a449a7cdf894bb4f0afac171176cb5b54448 [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) { \
128 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV; \
129 ID -= DIAG_START_##NAME - DIAG_START_##PREV + 1; \
130 }
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
147 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
148 // If the diag id doesn't match we found a different diag, abort. This can
149 // happen when this function is called with an ID that points into a hole in
150 // the diagID space.
151 if (Found->DiagID != DiagID)
152 return 0;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000153 return Found;
154}
155
Daniel Dunbara5e41332011-09-29 01:52:06 +0000156static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000157 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000158 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000159
Daniel Dunbara5e41332011-09-29 01:52:06 +0000160 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
161 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
162
163 if (StaticInfo->WarnNoWerror) {
164 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000165 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000166 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000167 }
168
Daniel Dunbara5e41332011-09-29 01:52:06 +0000169 if (StaticInfo->WarnShowInSystemHeader) {
170 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000171 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000172 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000173 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000174 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000175
176 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000177}
178
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000179/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000180/// DiagID belongs to, or 0 if no category.
181unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
182 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
183 return Info->Category;
184 return 0;
185}
186
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000187namespace {
188 // The diagnostic category names.
189 struct StaticDiagCategoryRec {
190 const char *NameStr;
191 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000192
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193 StringRef getName() const {
194 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000195 }
196 };
197}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000198
Daniel Dunbarba494c62011-09-29 01:42:25 +0000199// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
200// particularly clean, but for now we just implement this method here so we can
201// access GetDefaultDiagMapping.
202DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
203 diag::kind Diag)
204{
205 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000206 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000207
208 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000209 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000210 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000211
212 return Result.first->second;
213}
214
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000215static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000216#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000217#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000218#include "clang/Basic/DiagnosticGroups.inc"
219#undef GET_CATEGORY_TABLE
220 { 0, 0 }
221};
222
223/// getNumberOfCategories - Return the number of categories
224unsigned DiagnosticIDs::getNumberOfCategories() {
225 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
226}
227
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000228/// getCategoryNameFromID - Given a category ID, return the name of the
229/// category, an empty string if CategoryID is zero, or null if CategoryID is
230/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000232 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000234 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000235}
236
237
238
239DiagnosticIDs::SFINAEResponse
240DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
241 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000242 if (Info->AccessControl)
243 return SFINAE_AccessControl;
244
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000245 if (!Info->SFINAE)
246 return SFINAE_Report;
247
248 if (Info->Class == CLASS_ERROR)
249 return SFINAE_SubstitutionFailure;
250
251 // Suppress notes, warnings, and extensions;
252 return SFINAE_Suppress;
253 }
254
255 return SFINAE_Report;
256}
257
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000258/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000259///
260static unsigned getBuiltinDiagClass(unsigned DiagID) {
261 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
262 return Info->Class;
263 return ~0U;
264}
265
266//===----------------------------------------------------------------------===//
267// Custom Diagnostic information
268//===----------------------------------------------------------------------===//
269
270namespace clang {
271 namespace diag {
272 class CustomDiagInfo {
273 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
274 std::vector<DiagDesc> DiagInfo;
275 std::map<DiagDesc, unsigned> DiagIDs;
276 public:
277
278 /// getDescription - Return the description of the specified custom
279 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000281 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
282 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000283 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000284 }
285
286 /// getLevel - Return the level of the specified custom diagnostic.
287 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
288 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
289 "Invalid diagnosic ID");
290 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
291 }
292
Chris Lattner5f9e2722011-07-23 10:55:15 +0000293 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000294 DiagnosticIDs &Diags) {
295 DiagDesc D(L, Message);
296 // Check to see if it already exists.
297 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
298 if (I != DiagIDs.end() && I->first == D)
299 return I->second;
300
301 // If not, assign a new ID.
302 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
303 DiagIDs.insert(std::make_pair(D, ID));
304 DiagInfo.push_back(D);
305 return ID;
306 }
307 };
308
309 } // end diag namespace
310} // end clang namespace
311
312
313//===----------------------------------------------------------------------===//
314// Common Diagnostic implementation
315//===----------------------------------------------------------------------===//
316
317DiagnosticIDs::DiagnosticIDs() {
318 CustomDiagInfo = 0;
319}
320
321DiagnosticIDs::~DiagnosticIDs() {
322 delete CustomDiagInfo;
323}
324
325/// getCustomDiagID - Return an ID for a diagnostic with the specified message
326/// and level. If this is the first request for this diagnosic, it is
327/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000328unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000329 if (CustomDiagInfo == 0)
330 CustomDiagInfo = new diag::CustomDiagInfo();
331 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
332}
333
334
335/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
336/// level of the specified diagnostic ID is a Warning or Extension.
337/// This only works on builtin diagnostics, not custom ones, and is not legal to
338/// call on NOTEs.
339bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
340 return DiagID < diag::DIAG_UPPER_LIMIT &&
341 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
342}
343
344/// \brief Determine whether the given built-in diagnostic ID is a
345/// Note.
346bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
347 return DiagID < diag::DIAG_UPPER_LIMIT &&
348 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
349}
350
351/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
352/// ID is for an extension of some sort. This also returns EnabledByDefault,
353/// which is set to indicate whether the diagnostic is ignored by default (in
354/// which case -pedantic enables it) or treated as a warning/error by default.
355///
356bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
357 bool &EnabledByDefault) {
358 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
359 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
360 return false;
361
Daniel Dunbara5e41332011-09-29 01:52:06 +0000362 EnabledByDefault =
363 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000364 return true;
365}
366
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000367bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
368 if (DiagID >= diag::DIAG_UPPER_LIMIT)
369 return false;
370
Daniel Dunbara5e41332011-09-29 01:52:06 +0000371 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000372}
373
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000374/// getDescription - Given a diagnostic ID, return a description of the
375/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000376StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000377 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000378 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000379 return CustomDiagInfo->getDescription(DiagID);
380}
381
David Blaikied6471f72011-09-25 23:23:43 +0000382/// getDiagnosticLevel - Based on the way the client configured the
383/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
384/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000385DiagnosticIDs::Level
386DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000387 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000388 // Handle custom diagnostics, which cannot be mapped.
389 if (DiagID >= diag::DIAG_UPPER_LIMIT)
390 return CustomDiagInfo->getLevel(DiagID);
391
392 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000393 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000394 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000395}
396
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000397/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000398/// object, classify the specified diagnostic ID into a Level, consumable by
399/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000400///
401/// \param Loc The source location we are interested in finding out the
402/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000403DiagnosticIDs::Level
404DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000405 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000406 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000407 // Specific non-error diagnostics may be mapped to various levels from ignored
408 // to error. Errors can only be mapped to fatal.
409 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
410
David Blaikied6471f72011-09-25 23:23:43 +0000411 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000412 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000413 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000414
Daniel Dunbarba494c62011-09-29 01:42:25 +0000415 // Get the mapping information, or compute it lazily.
416 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
417 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000418
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000419 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000420 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000421 Result = DiagnosticIDs::Ignored;
422 break;
423 case diag::MAP_WARNING:
424 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000425 break;
426 case diag::MAP_ERROR:
427 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000428 break;
429 case diag::MAP_FATAL:
430 Result = DiagnosticIDs::Fatal;
431 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000432 }
433
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000434 // Upgrade ignored diagnostics if -Weverything is enabled.
435 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
436 !MappingInfo.isUser())
437 Result = DiagnosticIDs::Warning;
438
Bob Wilson18c407f2011-10-12 19:55:31 +0000439 // Ignore -pedantic diagnostics inside __extension__ blocks.
440 // (The diagnostics controlled by -pedantic are the extension diagnostics
441 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000442 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000443 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
444 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000445 return DiagnosticIDs::Ignored;
446
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000447 // For extension diagnostics that haven't been explicitly mapped, check if we
448 // should upgrade the diagnostic.
449 if (IsExtensionDiag && !MappingInfo.isUser()) {
450 switch (Diag.ExtBehavior) {
451 case DiagnosticsEngine::Ext_Ignore:
452 break;
453 case DiagnosticsEngine::Ext_Warn:
454 // Upgrade ignored diagnostics to warnings.
455 if (Result == DiagnosticIDs::Ignored)
456 Result = DiagnosticIDs::Warning;
457 break;
458 case DiagnosticsEngine::Ext_Error:
459 // Upgrade ignored or warning diagnostics to errors.
460 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
461 Result = DiagnosticIDs::Error;
462 break;
463 }
464 }
465
466 // At this point, ignored errors can no longer be upgraded.
467 if (Result == DiagnosticIDs::Ignored)
468 return Result;
469
470 // Honor -w, which is lower in priority than pedantic-errors, but higher than
471 // -Werror.
472 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
473 return DiagnosticIDs::Ignored;
474
475 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
476 if (Result == DiagnosticIDs::Warning) {
477 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
478 Result = DiagnosticIDs::Error;
479 }
480
481 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
482 // disabled.
483 if (Result == DiagnosticIDs::Error) {
484 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
485 Result = DiagnosticIDs::Fatal;
486 }
487
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000488 // If we are in a system header, we ignore it. We look at the diagnostic class
489 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000490 // -pedantic-errors modes, which *map* warnings/extensions to errors.
491 if (Result >= DiagnosticIDs::Warning &&
492 DiagClass != CLASS_ERROR &&
493 // Custom diagnostics always are emitted in system headers.
494 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000495 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000496 Diag.SuppressSystemWarnings &&
497 Loc.isValid() &&
498 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000499 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000500 return DiagnosticIDs::Ignored;
501
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000502 return Result;
503}
504
Daniel Dunbar3f839462011-09-29 01:47:16 +0000505struct clang::WarningOption {
506 // Be safe with the size of 'NameLen' because we don't statically check if
507 // the size will fit in the field; the struct size won't decrease with a
508 // shorter type anyway.
509 size_t NameLen;
510 const char *NameStr;
511 const short *Members;
512 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000513
Daniel Dunbar3f839462011-09-29 01:47:16 +0000514 StringRef getName() const {
515 return StringRef(NameStr, NameLen);
516 }
517};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000518
519#define GET_DIAG_ARRAYS
520#include "clang/Basic/DiagnosticGroups.inc"
521#undef GET_DIAG_ARRAYS
522
523// Second the table of options, sorted by name for fast binary lookup.
524static const WarningOption OptionTable[] = {
525#define GET_DIAG_TABLE
526#include "clang/Basic/DiagnosticGroups.inc"
527#undef GET_DIAG_TABLE
528};
529static const size_t OptionTableSize =
530sizeof(OptionTable) / sizeof(OptionTable[0]);
531
532static bool WarningOptionCompare(const WarningOption &LHS,
533 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000534 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000535}
536
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000537/// getWarningOptionForDiag - Return the lowest-level warning option that
538/// enables the specified diagnostic. If there is no -Wfoo flag that controls
539/// the diagnostic, this returns null.
540StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
541 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
542 return OptionTable[Info->getOptionGroupIndex()].getName();
543 return StringRef();
544}
545
Daniel Dunbar3f839462011-09-29 01:47:16 +0000546void DiagnosticIDs::getDiagnosticsInGroup(
547 const WarningOption *Group,
548 llvm::SmallVectorImpl<diag::kind> &Diags) const
549{
550 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000551 if (const short *Member = Group->Members) {
552 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000553 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000554 }
555
Daniel Dunbar3f839462011-09-29 01:47:16 +0000556 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000557 if (const short *SubGroups = Group->SubGroups) {
558 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000559 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000560 }
561}
562
Daniel Dunbar3f839462011-09-29 01:47:16 +0000563bool DiagnosticIDs::getDiagnosticsInGroup(
564 StringRef Group,
565 llvm::SmallVectorImpl<diag::kind> &Diags) const
566{
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000567 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000568 const WarningOption *Found =
569 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
570 WarningOptionCompare);
571 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000572 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000573 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000574
Daniel Dunbar3f839462011-09-29 01:47:16 +0000575 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000576 return false;
577}
578
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000579void DiagnosticIDs::getAllDiagnostics(
580 llvm::SmallVectorImpl<diag::kind> &Diags) const {
581 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
582 Diags.push_back(StaticDiagInfo[i].DiagID);
583}
584
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000585StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
586 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000587 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000588 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
589 i != e; ++i) {
590 // Don't suggest ignored warning flags.
591 if (!i->Members && !i->SubGroups)
592 continue;
593
594 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000595 if (Distance == BestDistance) {
596 // Two matches with the same distance, don't prefer one over the other.
597 Best = "";
598 } else if (Distance < BestDistance) {
599 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000600 Best = i->getName();
601 BestDistance = Distance;
602 }
603 }
604
605 return Best;
606}
607
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000608/// ProcessDiag - This is the method used to report a diagnostic that is
609/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000610bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000611 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000612
613 if (Diag.SuppressAllDiagnostics)
614 return false;
615
616 assert(Diag.getClient() && "DiagnosticClient not set!");
617
618 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000619 unsigned DiagID = Info.getID();
Jordan Rosec6d64a22012-07-11 16:50:36 +0000620 DiagnosticIDs::Level DiagLevel
621 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000622
623 if (DiagLevel != DiagnosticIDs::Note) {
624 // Record that a fatal error occurred only when we see a second
625 // non-note diagnostic. This allows notes to be attached to the
626 // fatal error, but suppresses any diagnostics that follow those
627 // notes.
628 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
629 Diag.FatalErrorOccurred = true;
630
631 Diag.LastDiagLevel = DiagLevel;
632 }
633
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000634 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
635 if (DiagLevel >= DiagnosticIDs::Error) {
636 ++Diag.TrapNumErrorsOccurred;
637 if (isUnrecoverable(DiagID))
638 ++Diag.TrapNumUnrecoverableErrorsOccurred;
639 }
640
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000641 // If a fatal error has already been emitted, silence all subsequent
642 // diagnostics.
643 if (Diag.FatalErrorOccurred) {
644 if (DiagLevel >= DiagnosticIDs::Error &&
645 Diag.Client->IncludeInDiagnosticCounts()) {
646 ++Diag.NumErrors;
647 ++Diag.NumErrorsSuppressed;
648 }
649
650 return false;
651 }
652
653 // If the client doesn't care about this message, don't issue it. If this is
654 // a note and the last real diagnostic was ignored, ignore it too.
655 if (DiagLevel == DiagnosticIDs::Ignored ||
656 (DiagLevel == DiagnosticIDs::Note &&
657 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
658 return false;
659
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000660 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000661 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000662 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasper1c84c682012-09-28 15:45:07 +0000663
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000664 // Warnings which have been upgraded to errors do not prevent compilation.
665 if (isDefaultMappingAsError(DiagID))
666 Diag.UncompilableErrorOccurred = true;
667
Daniel Jasper1c84c682012-09-28 15:45:07 +0000668 Diag.ErrorOccurred = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000669 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000670 ++Diag.NumErrors;
671 }
672
Douglas Gregorf1d59482011-08-17 19:13:00 +0000673 // If we've emitted a lot of errors, emit a fatal error instead of it to
674 // stop a flood of bogus errors.
675 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
676 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000677 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000678 return false;
679 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000680 }
681
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000682 // Finally, report it.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000683 EmitDiag(Diag, DiagLevel);
684 return true;
685}
686
687void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
688 Diagnostic Info(&Diag);
689 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
690
David Blaikied6471f72011-09-25 23:23:43 +0000691 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000692 if (Diag.Client->IncludeInDiagnosticCounts()) {
693 if (DiagLevel == DiagnosticIDs::Warning)
694 ++Diag.NumWarnings;
695 }
696
697 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000698}
John McCall923cd572011-06-15 21:46:43 +0000699
700bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
701 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
702 // Custom diagnostics.
703 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
704 }
705
706 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000707 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000708 return false;
709
710 if (DiagID == diag::err_unavailable ||
711 DiagID == diag::err_unavailable_message)
712 return false;
713
John McCallf85e1932011-06-15 23:02:42 +0000714 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000715 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000716 return false;
717
John McCall923cd572011-06-15 21:46:43 +0000718 return true;
719}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000720
721bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
722 unsigned cat = getCategoryNumberForDiag(DiagID);
723 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
724}