blob: c2cb366e5e03e2bcfa1848d93b895ead3d5892d5 [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"
Craig Topperb9602322013-07-15 03:38:40 +000018#include "llvm/ADT/STLExtras.h"
Daniel Dunbar3f839462011-09-29 01:47:16 +000019#include "llvm/ADT/SmallVector.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-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 {
39 unsigned short DiagID;
40 unsigned Mapping : 3;
41 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000042 unsigned SFINAE : 1;
43 unsigned AccessControl : 1;
Daniel Dunbar4213df32011-09-29 00:34:06 +000044 unsigned WarnNoWerror : 1;
45 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000046 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000047
Benjamin Kramerd49cb202012-02-15 20:57:03 +000048 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000049
50 uint16_t DescriptionLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000051 const char *DescriptionStr;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000052
Benjamin Kramerd49cb202012-02-15 20:57:03 +000053 unsigned getOptionGroupIndex() const {
54 return OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000055 }
56
Chris Lattner5f9e2722011-07-23 10:55:15 +000057 StringRef getDescription() const {
58 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000059 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000060
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000061 bool operator<(const StaticDiagInfoRec &RHS) const {
62 return DiagID < RHS.DiagID;
63 }
64};
65
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000066} // namespace anonymous
67
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000068static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000069#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Daniel Dunbar4213df32011-09-29 00:34:06 +000070 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
Benjamin Kramerf94d3922012-02-09 19:38:26 +000071 CATEGORY) \
Daniel Dunbar4213df32011-09-29 00:34:06 +000072 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
Benjamin Kramerd49cb202012-02-15 20:57:03 +000073 NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \
74 STR_SIZE(DESC, uint16_t), DESC },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000075#include "clang/Basic/DiagnosticCommonKinds.inc"
76#include "clang/Basic/DiagnosticDriverKinds.inc"
77#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +000078#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000079#include "clang/Basic/DiagnosticLexKinds.inc"
80#include "clang/Basic/DiagnosticParseKinds.inc"
81#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000082#include "clang/Basic/DiagnosticCommentKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000083#include "clang/Basic/DiagnosticSemaKinds.inc"
84#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000085#undef DIAG
Benjamin Kramerd49cb202012-02-15 20:57:03 +000086 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000087};
88
89static const unsigned StaticDiagInfoSize =
Craig Topperb9602322013-07-15 03:38:40 +000090 llvm::array_lengthof(StaticDiagInfo)-1;
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000091
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000092/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
93/// or null if the ID is invalid.
94static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000095 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
96#ifndef NDEBUG
97 static bool IsFirst = true;
98 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000099 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000100 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
101 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000102 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000103
104 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
105 "Improperly sorted diag info");
106 }
107 IsFirst = false;
108 }
109#endif
110
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000111 // Out of bounds diag. Can't be in the table.
112 using namespace diag;
113 if (DiagID >= DIAG_UPPER_LIMIT)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000114 return 0;
115
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000116 // Compute the index of the requested diagnostic in the static table.
117 // 1. Add the number of diagnostics in each category preceeding the
118 // diagnostic and of the category the diagnostic is in. This gives us
119 // the offset of the category in the table.
120 // 2. Subtract the number of IDs in each category from our ID. This gives us
121 // the offset of the diagnostic in the category.
122 // This is cheaper than a binary search on the table as it doesn't touch
123 // memory at all.
124 unsigned Offset = 0;
125 unsigned ID = DiagID;
126#define DIAG_START_COMMON 0 // Sentinel value.
127#define CATEGORY(NAME, PREV) \
128 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000129 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
130 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000131 }
132CATEGORY(DRIVER, COMMON)
133CATEGORY(FRONTEND, DRIVER)
134CATEGORY(SERIALIZATION, FRONTEND)
135CATEGORY(LEX, SERIALIZATION)
136CATEGORY(PARSE, LEX)
137CATEGORY(AST, PARSE)
138CATEGORY(COMMENT, AST)
139CATEGORY(SEMA, COMMENT)
140CATEGORY(ANALYSIS, SEMA)
141#undef CATEGORY
142#undef DIAG_START_COMMON
143
144 // Avoid out of bounds reads.
145 if (ID + Offset >= StaticDiagInfoSize)
146 return 0;
147
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000148 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
149
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000150 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
151 // If the diag id doesn't match we found a different diag, abort. This can
152 // happen when this function is called with an ID that points into a hole in
153 // the diagID space.
154 if (Found->DiagID != DiagID)
155 return 0;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000156 return Found;
157}
158
Daniel Dunbara5e41332011-09-29 01:52:06 +0000159static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000160 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000161 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000162
Daniel Dunbara5e41332011-09-29 01:52:06 +0000163 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
164 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
165
166 if (StaticInfo->WarnNoWerror) {
167 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000168 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000169 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000170 }
171
Daniel Dunbara5e41332011-09-29 01:52:06 +0000172 if (StaticInfo->WarnShowInSystemHeader) {
173 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000174 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000175 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000176 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000177 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000178
179 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000180}
181
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000182/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000183/// DiagID belongs to, or 0 if no category.
184unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
185 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
186 return Info->Category;
187 return 0;
188}
189
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000190namespace {
191 // The diagnostic category names.
192 struct StaticDiagCategoryRec {
193 const char *NameStr;
194 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000195
Chris Lattner5f9e2722011-07-23 10:55:15 +0000196 StringRef getName() const {
197 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000198 }
199 };
200}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000201
Daniel Dunbarba494c62011-09-29 01:42:25 +0000202// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
203// particularly clean, but for now we just implement this method here so we can
204// access GetDefaultDiagMapping.
205DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
206 diag::kind Diag)
207{
208 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000209 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000210
211 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000212 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000213 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000214
215 return Result.first->second;
216}
217
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000218static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000219#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000220#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000221#include "clang/Basic/DiagnosticGroups.inc"
222#undef GET_CATEGORY_TABLE
223 { 0, 0 }
224};
225
226/// getNumberOfCategories - Return the number of categories
227unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Topperb9602322013-07-15 03:38:40 +0000228 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000229}
230
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000231/// getCategoryNameFromID - Given a category ID, return the name of the
232/// category, an empty string if CategoryID is zero, or null if CategoryID is
233/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000234StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000235 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000236 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000237 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000238}
239
240
241
242DiagnosticIDs::SFINAEResponse
243DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
244 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000245 if (Info->AccessControl)
246 return SFINAE_AccessControl;
247
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000248 if (!Info->SFINAE)
249 return SFINAE_Report;
250
251 if (Info->Class == CLASS_ERROR)
252 return SFINAE_SubstitutionFailure;
253
254 // Suppress notes, warnings, and extensions;
255 return SFINAE_Suppress;
256 }
257
258 return SFINAE_Report;
259}
260
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000261/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000262///
263static unsigned getBuiltinDiagClass(unsigned DiagID) {
264 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
265 return Info->Class;
266 return ~0U;
267}
268
269//===----------------------------------------------------------------------===//
270// Custom Diagnostic information
271//===----------------------------------------------------------------------===//
272
273namespace clang {
274 namespace diag {
275 class CustomDiagInfo {
276 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
277 std::vector<DiagDesc> DiagInfo;
278 std::map<DiagDesc, unsigned> DiagIDs;
279 public:
280
281 /// getDescription - Return the description of the specified custom
282 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000283 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000284 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000285 "Invalid diagnostic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000286 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000287 }
288
289 /// getLevel - Return the level of the specified custom diagnostic.
290 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
291 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000292 "Invalid diagnostic ID");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000293 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
294 }
295
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000297 DiagnosticIDs &Diags) {
298 DiagDesc D(L, Message);
299 // Check to see if it already exists.
300 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
301 if (I != DiagIDs.end() && I->first == D)
302 return I->second;
303
304 // If not, assign a new ID.
305 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
306 DiagIDs.insert(std::make_pair(D, ID));
307 DiagInfo.push_back(D);
308 return ID;
309 }
310 };
311
312 } // end diag namespace
313} // end clang namespace
314
315
316//===----------------------------------------------------------------------===//
317// Common Diagnostic implementation
318//===----------------------------------------------------------------------===//
319
320DiagnosticIDs::DiagnosticIDs() {
321 CustomDiagInfo = 0;
322}
323
324DiagnosticIDs::~DiagnosticIDs() {
325 delete CustomDiagInfo;
326}
327
328/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000329/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000330/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000331unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000332 if (CustomDiagInfo == 0)
333 CustomDiagInfo = new diag::CustomDiagInfo();
334 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
335}
336
337
338/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
339/// level of the specified diagnostic ID is a Warning or Extension.
340/// This only works on builtin diagnostics, not custom ones, and is not legal to
341/// call on NOTEs.
342bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
343 return DiagID < diag::DIAG_UPPER_LIMIT &&
344 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
345}
346
347/// \brief Determine whether the given built-in diagnostic ID is a
348/// Note.
349bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
350 return DiagID < diag::DIAG_UPPER_LIMIT &&
351 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
352}
353
354/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
355/// ID is for an extension of some sort. This also returns EnabledByDefault,
356/// which is set to indicate whether the diagnostic is ignored by default (in
357/// which case -pedantic enables it) or treated as a warning/error by default.
358///
359bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
360 bool &EnabledByDefault) {
361 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
362 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
363 return false;
364
Daniel Dunbara5e41332011-09-29 01:52:06 +0000365 EnabledByDefault =
366 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000367 return true;
368}
369
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000370bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
371 if (DiagID >= diag::DIAG_UPPER_LIMIT)
372 return false;
373
Daniel Dunbara5e41332011-09-29 01:52:06 +0000374 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000375}
376
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000377/// getDescription - Given a diagnostic ID, return a description of the
378/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000379StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000380 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000381 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000382 return CustomDiagInfo->getDescription(DiagID);
383}
384
David Blaikied6471f72011-09-25 23:23:43 +0000385/// getDiagnosticLevel - Based on the way the client configured the
386/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
387/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000388DiagnosticIDs::Level
389DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000390 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000391 // Handle custom diagnostics, which cannot be mapped.
392 if (DiagID >= diag::DIAG_UPPER_LIMIT)
393 return CustomDiagInfo->getLevel(DiagID);
394
395 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000396 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000397 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000398}
399
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000400/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000401/// object, classify the specified diagnostic ID into a Level, consumable by
402/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000403///
404/// \param Loc The source location we are interested in finding out the
405/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000406DiagnosticIDs::Level
407DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000408 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000409 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000410 // Specific non-error diagnostics may be mapped to various levels from ignored
411 // to error. Errors can only be mapped to fatal.
412 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
413
David Blaikied6471f72011-09-25 23:23:43 +0000414 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000415 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000416 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000417
Daniel Dunbarba494c62011-09-29 01:42:25 +0000418 // Get the mapping information, or compute it lazily.
419 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
420 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000421
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000422 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000423 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000424 Result = DiagnosticIDs::Ignored;
425 break;
426 case diag::MAP_WARNING:
427 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000428 break;
429 case diag::MAP_ERROR:
430 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000431 break;
432 case diag::MAP_FATAL:
433 Result = DiagnosticIDs::Fatal;
434 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000435 }
436
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000437 // Upgrade ignored diagnostics if -Weverything is enabled.
438 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
439 !MappingInfo.isUser())
440 Result = DiagnosticIDs::Warning;
441
Bob Wilson18c407f2011-10-12 19:55:31 +0000442 // Ignore -pedantic diagnostics inside __extension__ blocks.
443 // (The diagnostics controlled by -pedantic are the extension diagnostics
444 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000445 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000446 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
447 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000448 return DiagnosticIDs::Ignored;
449
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000450 // For extension diagnostics that haven't been explicitly mapped, check if we
451 // should upgrade the diagnostic.
452 if (IsExtensionDiag && !MappingInfo.isUser()) {
453 switch (Diag.ExtBehavior) {
454 case DiagnosticsEngine::Ext_Ignore:
455 break;
456 case DiagnosticsEngine::Ext_Warn:
457 // Upgrade ignored diagnostics to warnings.
458 if (Result == DiagnosticIDs::Ignored)
459 Result = DiagnosticIDs::Warning;
460 break;
461 case DiagnosticsEngine::Ext_Error:
462 // Upgrade ignored or warning diagnostics to errors.
463 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
464 Result = DiagnosticIDs::Error;
465 break;
466 }
467 }
468
469 // At this point, ignored errors can no longer be upgraded.
470 if (Result == DiagnosticIDs::Ignored)
471 return Result;
472
473 // Honor -w, which is lower in priority than pedantic-errors, but higher than
474 // -Werror.
475 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
476 return DiagnosticIDs::Ignored;
477
478 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
479 if (Result == DiagnosticIDs::Warning) {
480 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
481 Result = DiagnosticIDs::Error;
482 }
483
484 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
485 // disabled.
486 if (Result == DiagnosticIDs::Error) {
487 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
488 Result = DiagnosticIDs::Fatal;
489 }
490
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000491 // If we are in a system header, we ignore it. We look at the diagnostic class
492 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000493 // -pedantic-errors modes, which *map* warnings/extensions to errors.
494 if (Result >= DiagnosticIDs::Warning &&
495 DiagClass != CLASS_ERROR &&
496 // Custom diagnostics always are emitted in system headers.
497 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000498 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000499 Diag.SuppressSystemWarnings &&
500 Loc.isValid() &&
501 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000502 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000503 return DiagnosticIDs::Ignored;
504
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000505 return Result;
506}
507
Daniel Dunbar3f839462011-09-29 01:47:16 +0000508struct clang::WarningOption {
509 // Be safe with the size of 'NameLen' because we don't statically check if
510 // the size will fit in the field; the struct size won't decrease with a
511 // shorter type anyway.
512 size_t NameLen;
513 const char *NameStr;
514 const short *Members;
515 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000516
Daniel Dunbar3f839462011-09-29 01:47:16 +0000517 StringRef getName() const {
518 return StringRef(NameStr, NameLen);
519 }
520};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000521
522#define GET_DIAG_ARRAYS
523#include "clang/Basic/DiagnosticGroups.inc"
524#undef GET_DIAG_ARRAYS
525
526// Second the table of options, sorted by name for fast binary lookup.
527static const WarningOption OptionTable[] = {
528#define GET_DIAG_TABLE
529#include "clang/Basic/DiagnosticGroups.inc"
530#undef GET_DIAG_TABLE
531};
Craig Topperb9602322013-07-15 03:38:40 +0000532static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000533
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(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000549 const WarningOption *Group,
550 SmallVectorImpl<diag::kind> &Diags) const {
Daniel Dunbar3f839462011-09-29 01:47:16 +0000551 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000552 if (const short *Member = Group->Members) {
553 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000554 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000555 }
556
Daniel Dunbar3f839462011-09-29 01:47:16 +0000557 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000558 if (const short *SubGroups = Group->SubGroups) {
559 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000560 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000561 }
562}
563
Daniel Dunbar3f839462011-09-29 01:47:16 +0000564bool DiagnosticIDs::getDiagnosticsInGroup(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000565 StringRef Group,
566 SmallVectorImpl<diag::kind> &Diags) const {
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(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000580 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000581 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}