blob: 1f7777873cfabefd39e2be03dc59dee44b873894 [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
14#include "clang/AST/ASTDiagnostic.h"
15#include "clang/Analysis/AnalysisDiagnostic.h"
16#include "clang/Basic/DiagnosticIDs.h"
John McCall923cd572011-06-15 21:46:43 +000017#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Driver/DriverDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Lex/LexDiagnostic.h"
22#include "clang/Parse/ParseDiagnostic.h"
23#include "clang/Sema/SemaDiagnostic.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000024#include "clang/Serialization/SerializationDiagnostic.h"
Daniel Dunbar3f839462011-09-29 01:47:16 +000025#include "llvm/ADT/SmallVector.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000026#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000027
28#include <map>
29using namespace clang;
30
31//===----------------------------------------------------------------------===//
32// Builtin Diagnostic information
33//===----------------------------------------------------------------------===//
34
35namespace {
36
37// Diagnostic classes.
38enum {
39 CLASS_NOTE = 0x01,
40 CLASS_WARNING = 0x02,
41 CLASS_EXTENSION = 0x03,
42 CLASS_ERROR = 0x04
43};
44
45struct StaticDiagInfoRec {
46 unsigned short DiagID;
47 unsigned Mapping : 3;
48 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000049 unsigned SFINAE : 1;
50 unsigned AccessControl : 1;
Daniel Dunbar4213df32011-09-29 00:34:06 +000051 unsigned WarnNoWerror : 1;
52 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000053 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000054
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000055 uint8_t NameLen;
56 uint8_t OptionGroupLen;
57
58 uint16_t DescriptionLen;
59 uint16_t BriefExplanationLen;
60 uint16_t FullExplanationLen;
61
62 const char *NameStr;
63 const char *OptionGroupStr;
64
65 const char *DescriptionStr;
66 const char *BriefExplanationStr;
67 const char *FullExplanationStr;
68
Chris Lattner5f9e2722011-07-23 10:55:15 +000069 StringRef getName() const {
70 return StringRef(NameStr, NameLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000071 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000072 StringRef getOptionGroup() const {
73 return StringRef(OptionGroupStr, OptionGroupLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000074 }
75
Chris Lattner5f9e2722011-07-23 10:55:15 +000076 StringRef getDescription() const {
77 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000078 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000079 StringRef getBriefExplanation() const {
80 return StringRef(BriefExplanationStr, BriefExplanationLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000081 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 StringRef getFullExplanation() const {
83 return StringRef(FullExplanationStr, FullExplanationLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000084 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000085
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000086 bool operator<(const StaticDiagInfoRec &RHS) const {
87 return DiagID < RHS.DiagID;
88 }
89};
90
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000091struct StaticDiagNameIndexRec {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000092 const char *NameStr;
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000093 unsigned short DiagID;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000094 uint8_t NameLen;
95
Chris Lattner5f9e2722011-07-23 10:55:15 +000096 StringRef getName() const {
97 return StringRef(NameStr, NameLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000098 }
99
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000100 bool operator<(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000101 return getName() < RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000102 }
103
104 bool operator==(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000105 return getName() == RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000106 }
107};
108
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000109template <size_t SizeOfStr, typename FieldType>
110class StringSizerHelper {
111 char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
112public:
113 enum { Size = SizeOfStr };
114};
115
116} // namespace anonymous
117
118#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000119
120static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000121#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Daniel Dunbar4213df32011-09-29 00:34:06 +0000122 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
123 CATEGORY,BRIEF,FULL) \
124 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
125 NOWERROR, SHOWINSYSHEADER, CATEGORY, \
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000126 STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t), \
127 STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t), \
128 STR_SIZE(FULL, uint16_t), \
129 #ENUM, GROUP, DESC, BRIEF, FULL },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000130#include "clang/Basic/DiagnosticCommonKinds.inc"
131#include "clang/Basic/DiagnosticDriverKinds.inc"
132#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +0000133#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000134#include "clang/Basic/DiagnosticLexKinds.inc"
135#include "clang/Basic/DiagnosticParseKinds.inc"
136#include "clang/Basic/DiagnosticASTKinds.inc"
137#include "clang/Basic/DiagnosticSemaKinds.inc"
138#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000139#undef DIAG
Daniel Dunbar4213df32011-09-29 00:34:06 +0000140 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000141};
142
143static const unsigned StaticDiagInfoSize =
144 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
145
146/// To be sorted before first use (since it's splitted among multiple files)
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000147static const StaticDiagNameIndexRec StaticDiagNameIndex[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000148#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000149#include "clang/Basic/DiagnosticIndexName.inc"
150#undef DIAG_NAME_INDEX
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000151 { 0, 0, 0 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000152};
153
154static const unsigned StaticDiagNameIndexSize =
155 sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000156
157/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
158/// or null if the ID is invalid.
159static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000160 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
161#ifndef NDEBUG
162 static bool IsFirst = true;
163 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000164 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000165 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
166 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000167 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000168
169 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
170 "Improperly sorted diag info");
171 }
172 IsFirst = false;
173 }
174#endif
175
176 // Search the diagnostic table with a binary search.
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000177 StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
Daniel Dunbar4213df32011-09-29 00:34:06 +0000178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000179
180 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000181 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
182 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000183 Found->DiagID != DiagID)
184 return 0;
185
186 return Found;
187}
188
Daniel Dunbara5e41332011-09-29 01:52:06 +0000189static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000190 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000191 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000192
Daniel Dunbara5e41332011-09-29 01:52:06 +0000193 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
194 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
195
196 if (StaticInfo->WarnNoWerror) {
197 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000198 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000199 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000200 }
201
Daniel Dunbara5e41332011-09-29 01:52:06 +0000202 if (StaticInfo->WarnShowInSystemHeader) {
203 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000204 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000205 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000206 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000207 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000208
209 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000210}
211
212/// getWarningOptionForDiag - Return the lowest-level warning option that
213/// enables the specified diagnostic. If there is no -Wfoo flag that controls
214/// the diagnostic, this returns null.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000215StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000216 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000217 return Info->getOptionGroup();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000218 return StringRef();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000219}
220
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000221/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000222/// DiagID belongs to, or 0 if no category.
223unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
224 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
225 return Info->Category;
226 return 0;
227}
228
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000229namespace {
230 // The diagnostic category names.
231 struct StaticDiagCategoryRec {
232 const char *NameStr;
233 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000234
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235 StringRef getName() const {
236 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000237 }
238 };
239}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000240
Daniel Dunbarba494c62011-09-29 01:42:25 +0000241// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
242// particularly clean, but for now we just implement this method here so we can
243// access GetDefaultDiagMapping.
244DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
245 diag::kind Diag)
246{
247 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000248 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000249
250 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000251 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000252 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000253
254 return Result.first->second;
255}
256
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000257static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000258#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000259#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000260#include "clang/Basic/DiagnosticGroups.inc"
261#undef GET_CATEGORY_TABLE
262 { 0, 0 }
263};
264
265/// getNumberOfCategories - Return the number of categories
266unsigned DiagnosticIDs::getNumberOfCategories() {
267 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
268}
269
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000270/// getCategoryNameFromID - Given a category ID, return the name of the
271/// category, an empty string if CategoryID is zero, or null if CategoryID is
272/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000274 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000275 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000276 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000277}
278
279
280
281DiagnosticIDs::SFINAEResponse
282DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
283 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000284 if (Info->AccessControl)
285 return SFINAE_AccessControl;
286
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000287 if (!Info->SFINAE)
288 return SFINAE_Report;
289
290 if (Info->Class == CLASS_ERROR)
291 return SFINAE_SubstitutionFailure;
292
293 // Suppress notes, warnings, and extensions;
294 return SFINAE_Suppress;
295 }
296
297 return SFINAE_Report;
298}
299
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000300/// getName - Given a diagnostic ID, return its name
Chris Lattner5f9e2722011-07-23 10:55:15 +0000301StringRef DiagnosticIDs::getName(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000302 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000303 return Info->getName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000304 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000305}
306
307/// getIdFromName - Given a diagnostic name, return its ID, or 0
Chris Lattner5f9e2722011-07-23 10:55:15 +0000308unsigned DiagnosticIDs::getIdFromName(StringRef Name) {
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000309 const StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000310 StaticDiagNameIndex + StaticDiagNameIndexSize;
311
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000312 if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000313
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000314 assert(Name.size() == static_cast<uint8_t>(Name.size()) &&
315 "Name is too long");
316 StaticDiagNameIndexRec Find = { Name.data(), 0,
317 static_cast<uint8_t>(Name.size()) };
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000318
319 const StaticDiagNameIndexRec *Found =
320 std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
321 if (Found == StaticDiagNameIndexEnd ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000322 Found->getName() != Name)
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000323 return diag::DIAG_UPPER_LIMIT;
324
325 return Found->DiagID;
326}
327
328/// getBriefExplanation - Given a diagnostic ID, return a brief explanation
329/// of the issue
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000331 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000332 return Info->getBriefExplanation();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000333 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000334}
335
336/// getFullExplanation - Given a diagnostic ID, return a full explanation
337/// of the issue
Chris Lattner5f9e2722011-07-23 10:55:15 +0000338StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000339 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000340 return Info->getFullExplanation();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000341 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000342}
343
344/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000345///
346static unsigned getBuiltinDiagClass(unsigned DiagID) {
347 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
348 return Info->Class;
349 return ~0U;
350}
351
352//===----------------------------------------------------------------------===//
Ted Kremenek6948bc42011-08-09 03:39:14 +0000353// diag_iterator
354//===----------------------------------------------------------------------===//
355
356llvm::StringRef DiagnosticIDs::diag_iterator::getDiagName() const {
357 return static_cast<const StaticDiagNameIndexRec*>(impl)->getName();
358}
359
360unsigned DiagnosticIDs::diag_iterator::getDiagID() const {
361 return static_cast<const StaticDiagNameIndexRec*>(impl)->DiagID;
362}
363
364DiagnosticIDs::diag_iterator &DiagnosticIDs::diag_iterator::operator++() {
365 const StaticDiagNameIndexRec* ptr =
366 static_cast<const StaticDiagNameIndexRec*>(impl);;
367 ++ptr;
368 impl = ptr;
369 return *this;
370}
371
372DiagnosticIDs::diag_iterator DiagnosticIDs::diags_begin() {
373 return DiagnosticIDs::diag_iterator(StaticDiagNameIndex);
374}
375
376DiagnosticIDs::diag_iterator DiagnosticIDs::diags_end() {
377 return DiagnosticIDs::diag_iterator(StaticDiagNameIndex +
378 StaticDiagNameIndexSize);
379}
380
381//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000382// Custom Diagnostic information
383//===----------------------------------------------------------------------===//
384
385namespace clang {
386 namespace diag {
387 class CustomDiagInfo {
388 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
389 std::vector<DiagDesc> DiagInfo;
390 std::map<DiagDesc, unsigned> DiagIDs;
391 public:
392
393 /// getDescription - Return the description of the specified custom
394 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000395 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000396 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
397 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000398 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000399 }
400
401 /// getLevel - Return the level of the specified custom diagnostic.
402 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
403 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
404 "Invalid diagnosic ID");
405 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
406 }
407
Chris Lattner5f9e2722011-07-23 10:55:15 +0000408 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000409 DiagnosticIDs &Diags) {
410 DiagDesc D(L, Message);
411 // Check to see if it already exists.
412 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
413 if (I != DiagIDs.end() && I->first == D)
414 return I->second;
415
416 // If not, assign a new ID.
417 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
418 DiagIDs.insert(std::make_pair(D, ID));
419 DiagInfo.push_back(D);
420 return ID;
421 }
422 };
423
424 } // end diag namespace
425} // end clang namespace
426
427
428//===----------------------------------------------------------------------===//
429// Common Diagnostic implementation
430//===----------------------------------------------------------------------===//
431
432DiagnosticIDs::DiagnosticIDs() {
433 CustomDiagInfo = 0;
434}
435
436DiagnosticIDs::~DiagnosticIDs() {
437 delete CustomDiagInfo;
438}
439
440/// getCustomDiagID - Return an ID for a diagnostic with the specified message
441/// and level. If this is the first request for this diagnosic, it is
442/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000443unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000444 if (CustomDiagInfo == 0)
445 CustomDiagInfo = new diag::CustomDiagInfo();
446 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
447}
448
449
450/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
451/// level of the specified diagnostic ID is a Warning or Extension.
452/// This only works on builtin diagnostics, not custom ones, and is not legal to
453/// call on NOTEs.
454bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
455 return DiagID < diag::DIAG_UPPER_LIMIT &&
456 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
457}
458
459/// \brief Determine whether the given built-in diagnostic ID is a
460/// Note.
461bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
462 return DiagID < diag::DIAG_UPPER_LIMIT &&
463 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
464}
465
466/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
467/// ID is for an extension of some sort. This also returns EnabledByDefault,
468/// which is set to indicate whether the diagnostic is ignored by default (in
469/// which case -pedantic enables it) or treated as a warning/error by default.
470///
471bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
472 bool &EnabledByDefault) {
473 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
474 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
475 return false;
476
Daniel Dunbara5e41332011-09-29 01:52:06 +0000477 EnabledByDefault =
478 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000479 return true;
480}
481
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000482bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
483 if (DiagID >= diag::DIAG_UPPER_LIMIT)
484 return false;
485
Daniel Dunbara5e41332011-09-29 01:52:06 +0000486 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000487}
488
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000489/// getDescription - Given a diagnostic ID, return a description of the
490/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000491StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000492 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000493 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000494 return CustomDiagInfo->getDescription(DiagID);
495}
496
David Blaikied6471f72011-09-25 23:23:43 +0000497/// getDiagnosticLevel - Based on the way the client configured the
498/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
499/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000500DiagnosticIDs::Level
501DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000502 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000503 // Handle custom diagnostics, which cannot be mapped.
504 if (DiagID >= diag::DIAG_UPPER_LIMIT)
505 return CustomDiagInfo->getLevel(DiagID);
506
507 unsigned DiagClass = getBuiltinDiagClass(DiagID);
508 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000509 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000510}
511
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000512/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000513/// object, classify the specified diagnostic ID into a Level, consumable by
514/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000515///
516/// \param Loc The source location we are interested in finding out the
517/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000518DiagnosticIDs::Level
519DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000520 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000521 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000522 // Specific non-error diagnostics may be mapped to various levels from ignored
523 // to error. Errors can only be mapped to fatal.
524 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
525
David Blaikied6471f72011-09-25 23:23:43 +0000526 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000527 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000528 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000529
Daniel Dunbarba494c62011-09-29 01:42:25 +0000530 // Get the mapping information, or compute it lazily.
531 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
532 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000533
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000534 switch (MappingInfo.getMapping()) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000535 default: llvm_unreachable("Unknown mapping!");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000536 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000537 Result = DiagnosticIDs::Ignored;
538 break;
539 case diag::MAP_WARNING:
540 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000541 break;
542 case diag::MAP_ERROR:
543 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000544 break;
545 case diag::MAP_FATAL:
546 Result = DiagnosticIDs::Fatal;
547 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000548 }
549
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000550 // Upgrade ignored diagnostics if -Weverything is enabled.
551 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
552 !MappingInfo.isUser())
553 Result = DiagnosticIDs::Warning;
554
Bob Wilson18c407f2011-10-12 19:55:31 +0000555 // Ignore -pedantic diagnostics inside __extension__ blocks.
556 // (The diagnostics controlled by -pedantic are the extension diagnostics
557 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000558 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000559 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
560 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000561 return DiagnosticIDs::Ignored;
562
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000563 // For extension diagnostics that haven't been explicitly mapped, check if we
564 // should upgrade the diagnostic.
565 if (IsExtensionDiag && !MappingInfo.isUser()) {
566 switch (Diag.ExtBehavior) {
567 case DiagnosticsEngine::Ext_Ignore:
568 break;
569 case DiagnosticsEngine::Ext_Warn:
570 // Upgrade ignored diagnostics to warnings.
571 if (Result == DiagnosticIDs::Ignored)
572 Result = DiagnosticIDs::Warning;
573 break;
574 case DiagnosticsEngine::Ext_Error:
575 // Upgrade ignored or warning diagnostics to errors.
576 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
577 Result = DiagnosticIDs::Error;
578 break;
579 }
580 }
581
582 // At this point, ignored errors can no longer be upgraded.
583 if (Result == DiagnosticIDs::Ignored)
584 return Result;
585
586 // Honor -w, which is lower in priority than pedantic-errors, but higher than
587 // -Werror.
588 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
589 return DiagnosticIDs::Ignored;
590
591 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
592 if (Result == DiagnosticIDs::Warning) {
593 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
594 Result = DiagnosticIDs::Error;
595 }
596
597 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
598 // disabled.
599 if (Result == DiagnosticIDs::Error) {
600 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
601 Result = DiagnosticIDs::Fatal;
602 }
603
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000604 // If we are in a system header, we ignore it. We look at the diagnostic class
605 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000606 // -pedantic-errors modes, which *map* warnings/extensions to errors.
607 if (Result >= DiagnosticIDs::Warning &&
608 DiagClass != CLASS_ERROR &&
609 // Custom diagnostics always are emitted in system headers.
610 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000611 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000612 Diag.SuppressSystemWarnings &&
613 Loc.isValid() &&
614 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000615 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000616 return DiagnosticIDs::Ignored;
617
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000618 return Result;
619}
620
Daniel Dunbar3f839462011-09-29 01:47:16 +0000621struct clang::WarningOption {
622 // Be safe with the size of 'NameLen' because we don't statically check if
623 // the size will fit in the field; the struct size won't decrease with a
624 // shorter type anyway.
625 size_t NameLen;
626 const char *NameStr;
627 const short *Members;
628 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000629
Daniel Dunbar3f839462011-09-29 01:47:16 +0000630 StringRef getName() const {
631 return StringRef(NameStr, NameLen);
632 }
633};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000634
635#define GET_DIAG_ARRAYS
636#include "clang/Basic/DiagnosticGroups.inc"
637#undef GET_DIAG_ARRAYS
638
639// Second the table of options, sorted by name for fast binary lookup.
640static const WarningOption OptionTable[] = {
641#define GET_DIAG_TABLE
642#include "clang/Basic/DiagnosticGroups.inc"
643#undef GET_DIAG_TABLE
644};
645static const size_t OptionTableSize =
646sizeof(OptionTable) / sizeof(OptionTable[0]);
647
648static bool WarningOptionCompare(const WarningOption &LHS,
649 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000650 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000651}
652
Daniel Dunbar3f839462011-09-29 01:47:16 +0000653void DiagnosticIDs::getDiagnosticsInGroup(
654 const WarningOption *Group,
655 llvm::SmallVectorImpl<diag::kind> &Diags) const
656{
657 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000658 if (const short *Member = Group->Members) {
659 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000660 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000661 }
662
Daniel Dunbar3f839462011-09-29 01:47:16 +0000663 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000664 if (const short *SubGroups = Group->SubGroups) {
665 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000666 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000667 }
668}
669
Daniel Dunbar3f839462011-09-29 01:47:16 +0000670bool DiagnosticIDs::getDiagnosticsInGroup(
671 StringRef Group,
672 llvm::SmallVectorImpl<diag::kind> &Diags) const
673{
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000674 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000675 const WarningOption *Found =
676 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
677 WarningOptionCompare);
678 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000679 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000680 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000681
Daniel Dunbar3f839462011-09-29 01:47:16 +0000682 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000683 return false;
684}
685
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000686StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
687 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000688 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000689 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
690 i != e; ++i) {
691 // Don't suggest ignored warning flags.
692 if (!i->Members && !i->SubGroups)
693 continue;
694
695 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000696 if (Distance == BestDistance) {
697 // Two matches with the same distance, don't prefer one over the other.
698 Best = "";
699 } else if (Distance < BestDistance) {
700 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000701 Best = i->getName();
702 BestDistance = Distance;
703 }
704 }
705
706 return Best;
707}
708
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000709/// ProcessDiag - This is the method used to report a diagnostic that is
710/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000711bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000712 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000713
714 if (Diag.SuppressAllDiagnostics)
715 return false;
716
717 assert(Diag.getClient() && "DiagnosticClient not set!");
718
719 // Figure out the diagnostic level of this message.
720 DiagnosticIDs::Level DiagLevel;
721 unsigned DiagID = Info.getID();
722
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000723 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
724 // Handle custom diagnostics, which cannot be mapped.
725 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000726 } else {
727 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
728 // the diagnostic level was for the previous diagnostic so that it is
729 // filtered the same as the previous diagnostic.
730 unsigned DiagClass = getBuiltinDiagClass(DiagID);
731 if (DiagClass == CLASS_NOTE) {
732 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000733 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000734 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
735 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000736 }
737 }
738
739 if (DiagLevel != DiagnosticIDs::Note) {
740 // Record that a fatal error occurred only when we see a second
741 // non-note diagnostic. This allows notes to be attached to the
742 // fatal error, but suppresses any diagnostics that follow those
743 // notes.
744 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
745 Diag.FatalErrorOccurred = true;
746
747 Diag.LastDiagLevel = DiagLevel;
748 }
749
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000750 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
751 if (DiagLevel >= DiagnosticIDs::Error) {
752 ++Diag.TrapNumErrorsOccurred;
753 if (isUnrecoverable(DiagID))
754 ++Diag.TrapNumUnrecoverableErrorsOccurred;
755 }
756
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000757 // If a fatal error has already been emitted, silence all subsequent
758 // diagnostics.
759 if (Diag.FatalErrorOccurred) {
760 if (DiagLevel >= DiagnosticIDs::Error &&
761 Diag.Client->IncludeInDiagnosticCounts()) {
762 ++Diag.NumErrors;
763 ++Diag.NumErrorsSuppressed;
764 }
765
766 return false;
767 }
768
769 // If the client doesn't care about this message, don't issue it. If this is
770 // a note and the last real diagnostic was ignored, ignore it too.
771 if (DiagLevel == DiagnosticIDs::Ignored ||
772 (DiagLevel == DiagnosticIDs::Note &&
773 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
774 return false;
775
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000776 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000777 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000778 Diag.UnrecoverableErrorOccurred = true;
Douglas Gregor85bea972011-07-06 17:40:26 +0000779
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000780 if (Diag.Client->IncludeInDiagnosticCounts()) {
781 Diag.ErrorOccurred = true;
782 ++Diag.NumErrors;
783 }
784
Douglas Gregorf1d59482011-08-17 19:13:00 +0000785 // If we've emitted a lot of errors, emit a fatal error instead of it to
786 // stop a flood of bogus errors.
787 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
788 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000789 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000790 return false;
791 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000792 }
793
Douglas Gregor4814fb52011-02-03 23:41:12 +0000794 // If we have any Fix-Its, make sure that all of the Fix-Its point into
Chandler Carruth3201f382011-07-26 05:17:23 +0000795 // source locations that aren't macro expansions. If any point into macro
796 // expansions, remove all of the Fix-Its.
Douglas Gregor4814fb52011-02-03 23:41:12 +0000797 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
798 const FixItHint &FixIt = Diag.FixItHints[I];
799 if (FixIt.RemoveRange.isInvalid() ||
800 FixIt.RemoveRange.getBegin().isMacroID() ||
801 FixIt.RemoveRange.getEnd().isMacroID()) {
802 Diag.NumFixItHints = 0;
803 break;
804 }
805 }
806
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000807 // Finally, report it.
David Blaikied6471f72011-09-25 23:23:43 +0000808 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000809 if (Diag.Client->IncludeInDiagnosticCounts()) {
810 if (DiagLevel == DiagnosticIDs::Warning)
811 ++Diag.NumWarnings;
812 }
813
814 Diag.CurDiagID = ~0U;
815
816 return true;
817}
John McCall923cd572011-06-15 21:46:43 +0000818
819bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
820 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
821 // Custom diagnostics.
822 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
823 }
824
825 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000826 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000827 return false;
828
829 if (DiagID == diag::err_unavailable ||
830 DiagID == diag::err_unavailable_message)
831 return false;
832
John McCallf85e1932011-06-15 23:02:42 +0000833 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000834 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000835 return false;
836
John McCall923cd572011-06-15 21:46:43 +0000837 return true;
838}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000839
840bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
841 unsigned cat = getCategoryNumberForDiag(DiagID);
842 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
843}
844