blob: ed976436e284e11d21d145362f96cca44bf16207 [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
21#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 =
90 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
91
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
111 // Search the diagnostic table with a binary search.
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000112 StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000114
115 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000116 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
117 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000118 Found->DiagID != DiagID)
119 return 0;
120
121 return Found;
122}
123
Daniel Dunbara5e41332011-09-29 01:52:06 +0000124static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000125 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000126 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000127
Daniel Dunbara5e41332011-09-29 01:52:06 +0000128 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
129 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
130
131 if (StaticInfo->WarnNoWerror) {
132 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000133 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000134 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000135 }
136
Daniel Dunbara5e41332011-09-29 01:52:06 +0000137 if (StaticInfo->WarnShowInSystemHeader) {
138 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000139 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000140 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000141 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000142 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000143
144 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000145}
146
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000147/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000148/// DiagID belongs to, or 0 if no category.
149unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
150 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
151 return Info->Category;
152 return 0;
153}
154
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000155namespace {
156 // The diagnostic category names.
157 struct StaticDiagCategoryRec {
158 const char *NameStr;
159 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000160
Chris Lattner5f9e2722011-07-23 10:55:15 +0000161 StringRef getName() const {
162 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000163 }
164 };
165}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000166
Daniel Dunbarba494c62011-09-29 01:42:25 +0000167// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
168// particularly clean, but for now we just implement this method here so we can
169// access GetDefaultDiagMapping.
170DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
171 diag::kind Diag)
172{
173 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000174 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000175
176 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000177 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000178 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000179
180 return Result.first->second;
181}
182
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000183static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000184#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000185#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000186#include "clang/Basic/DiagnosticGroups.inc"
187#undef GET_CATEGORY_TABLE
188 { 0, 0 }
189};
190
191/// getNumberOfCategories - Return the number of categories
192unsigned DiagnosticIDs::getNumberOfCategories() {
193 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
194}
195
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000196/// getCategoryNameFromID - Given a category ID, return the name of the
197/// category, an empty string if CategoryID is zero, or null if CategoryID is
198/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000200 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000202 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000203}
204
205
206
207DiagnosticIDs::SFINAEResponse
208DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
209 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000210 if (Info->AccessControl)
211 return SFINAE_AccessControl;
212
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000213 if (!Info->SFINAE)
214 return SFINAE_Report;
215
216 if (Info->Class == CLASS_ERROR)
217 return SFINAE_SubstitutionFailure;
218
219 // Suppress notes, warnings, and extensions;
220 return SFINAE_Suppress;
221 }
222
223 return SFINAE_Report;
224}
225
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000226/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000227///
228static unsigned getBuiltinDiagClass(unsigned DiagID) {
229 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
230 return Info->Class;
231 return ~0U;
232}
233
234//===----------------------------------------------------------------------===//
235// Custom Diagnostic information
236//===----------------------------------------------------------------------===//
237
238namespace clang {
239 namespace diag {
240 class CustomDiagInfo {
241 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
242 std::vector<DiagDesc> DiagInfo;
243 std::map<DiagDesc, unsigned> DiagIDs;
244 public:
245
246 /// getDescription - Return the description of the specified custom
247 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000248 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000249 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
250 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000251 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000252 }
253
254 /// getLevel - Return the level of the specified custom diagnostic.
255 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
256 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
257 "Invalid diagnosic ID");
258 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
259 }
260
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000262 DiagnosticIDs &Diags) {
263 DiagDesc D(L, Message);
264 // Check to see if it already exists.
265 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
266 if (I != DiagIDs.end() && I->first == D)
267 return I->second;
268
269 // If not, assign a new ID.
270 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
271 DiagIDs.insert(std::make_pair(D, ID));
272 DiagInfo.push_back(D);
273 return ID;
274 }
275 };
276
277 } // end diag namespace
278} // end clang namespace
279
280
281//===----------------------------------------------------------------------===//
282// Common Diagnostic implementation
283//===----------------------------------------------------------------------===//
284
285DiagnosticIDs::DiagnosticIDs() {
286 CustomDiagInfo = 0;
287}
288
289DiagnosticIDs::~DiagnosticIDs() {
290 delete CustomDiagInfo;
291}
292
293/// getCustomDiagID - Return an ID for a diagnostic with the specified message
294/// and level. If this is the first request for this diagnosic, it is
295/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000297 if (CustomDiagInfo == 0)
298 CustomDiagInfo = new diag::CustomDiagInfo();
299 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
300}
301
302
303/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
304/// level of the specified diagnostic ID is a Warning or Extension.
305/// This only works on builtin diagnostics, not custom ones, and is not legal to
306/// call on NOTEs.
307bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
308 return DiagID < diag::DIAG_UPPER_LIMIT &&
309 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
310}
311
312/// \brief Determine whether the given built-in diagnostic ID is a
313/// Note.
314bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
315 return DiagID < diag::DIAG_UPPER_LIMIT &&
316 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
317}
318
319/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
320/// ID is for an extension of some sort. This also returns EnabledByDefault,
321/// which is set to indicate whether the diagnostic is ignored by default (in
322/// which case -pedantic enables it) or treated as a warning/error by default.
323///
324bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
325 bool &EnabledByDefault) {
326 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
327 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
328 return false;
329
Daniel Dunbara5e41332011-09-29 01:52:06 +0000330 EnabledByDefault =
331 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000332 return true;
333}
334
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000335bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
336 if (DiagID >= diag::DIAG_UPPER_LIMIT)
337 return false;
338
Daniel Dunbara5e41332011-09-29 01:52:06 +0000339 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000340}
341
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000342/// getDescription - Given a diagnostic ID, return a description of the
343/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000344StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000345 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000346 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000347 return CustomDiagInfo->getDescription(DiagID);
348}
349
David Blaikied6471f72011-09-25 23:23:43 +0000350/// getDiagnosticLevel - Based on the way the client configured the
351/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
352/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000353DiagnosticIDs::Level
354DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000355 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000356 // Handle custom diagnostics, which cannot be mapped.
357 if (DiagID >= diag::DIAG_UPPER_LIMIT)
358 return CustomDiagInfo->getLevel(DiagID);
359
360 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000361 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000362 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000363}
364
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000365/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000366/// object, classify the specified diagnostic ID into a Level, consumable by
367/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000368///
369/// \param Loc The source location we are interested in finding out the
370/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000371DiagnosticIDs::Level
372DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000373 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000374 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000375 // Specific non-error diagnostics may be mapped to various levels from ignored
376 // to error. Errors can only be mapped to fatal.
377 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
378
David Blaikied6471f72011-09-25 23:23:43 +0000379 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000380 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000381 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000382
Daniel Dunbarba494c62011-09-29 01:42:25 +0000383 // Get the mapping information, or compute it lazily.
384 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
385 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000386
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000387 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000388 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000389 Result = DiagnosticIDs::Ignored;
390 break;
391 case diag::MAP_WARNING:
392 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000393 break;
394 case diag::MAP_ERROR:
395 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000396 break;
397 case diag::MAP_FATAL:
398 Result = DiagnosticIDs::Fatal;
399 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000400 }
401
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000402 // Upgrade ignored diagnostics if -Weverything is enabled.
403 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
404 !MappingInfo.isUser())
405 Result = DiagnosticIDs::Warning;
406
Bob Wilson18c407f2011-10-12 19:55:31 +0000407 // Ignore -pedantic diagnostics inside __extension__ blocks.
408 // (The diagnostics controlled by -pedantic are the extension diagnostics
409 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000410 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000411 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
412 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000413 return DiagnosticIDs::Ignored;
414
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000415 // For extension diagnostics that haven't been explicitly mapped, check if we
416 // should upgrade the diagnostic.
417 if (IsExtensionDiag && !MappingInfo.isUser()) {
418 switch (Diag.ExtBehavior) {
419 case DiagnosticsEngine::Ext_Ignore:
420 break;
421 case DiagnosticsEngine::Ext_Warn:
422 // Upgrade ignored diagnostics to warnings.
423 if (Result == DiagnosticIDs::Ignored)
424 Result = DiagnosticIDs::Warning;
425 break;
426 case DiagnosticsEngine::Ext_Error:
427 // Upgrade ignored or warning diagnostics to errors.
428 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
429 Result = DiagnosticIDs::Error;
430 break;
431 }
432 }
433
434 // At this point, ignored errors can no longer be upgraded.
435 if (Result == DiagnosticIDs::Ignored)
436 return Result;
437
438 // Honor -w, which is lower in priority than pedantic-errors, but higher than
439 // -Werror.
440 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
441 return DiagnosticIDs::Ignored;
442
443 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
444 if (Result == DiagnosticIDs::Warning) {
445 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
446 Result = DiagnosticIDs::Error;
447 }
448
449 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
450 // disabled.
451 if (Result == DiagnosticIDs::Error) {
452 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
453 Result = DiagnosticIDs::Fatal;
454 }
455
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000456 // If we are in a system header, we ignore it. We look at the diagnostic class
457 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000458 // -pedantic-errors modes, which *map* warnings/extensions to errors.
459 if (Result >= DiagnosticIDs::Warning &&
460 DiagClass != CLASS_ERROR &&
461 // Custom diagnostics always are emitted in system headers.
462 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000463 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000464 Diag.SuppressSystemWarnings &&
465 Loc.isValid() &&
466 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000467 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000468 return DiagnosticIDs::Ignored;
469
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000470 return Result;
471}
472
Daniel Dunbar3f839462011-09-29 01:47:16 +0000473struct clang::WarningOption {
474 // Be safe with the size of 'NameLen' because we don't statically check if
475 // the size will fit in the field; the struct size won't decrease with a
476 // shorter type anyway.
477 size_t NameLen;
478 const char *NameStr;
479 const short *Members;
480 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000481
Daniel Dunbar3f839462011-09-29 01:47:16 +0000482 StringRef getName() const {
483 return StringRef(NameStr, NameLen);
484 }
485};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000486
487#define GET_DIAG_ARRAYS
488#include "clang/Basic/DiagnosticGroups.inc"
489#undef GET_DIAG_ARRAYS
490
491// Second the table of options, sorted by name for fast binary lookup.
492static const WarningOption OptionTable[] = {
493#define GET_DIAG_TABLE
494#include "clang/Basic/DiagnosticGroups.inc"
495#undef GET_DIAG_TABLE
496};
497static const size_t OptionTableSize =
498sizeof(OptionTable) / sizeof(OptionTable[0]);
499
500static bool WarningOptionCompare(const WarningOption &LHS,
501 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000502 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000503}
504
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000505/// getWarningOptionForDiag - Return the lowest-level warning option that
506/// enables the specified diagnostic. If there is no -Wfoo flag that controls
507/// the diagnostic, this returns null.
508StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
509 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
510 return OptionTable[Info->getOptionGroupIndex()].getName();
511 return StringRef();
512}
513
Daniel Dunbar3f839462011-09-29 01:47:16 +0000514void DiagnosticIDs::getDiagnosticsInGroup(
515 const WarningOption *Group,
516 llvm::SmallVectorImpl<diag::kind> &Diags) const
517{
518 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000519 if (const short *Member = Group->Members) {
520 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000521 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000522 }
523
Daniel Dunbar3f839462011-09-29 01:47:16 +0000524 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000525 if (const short *SubGroups = Group->SubGroups) {
526 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000527 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000528 }
529}
530
Daniel Dunbar3f839462011-09-29 01:47:16 +0000531bool DiagnosticIDs::getDiagnosticsInGroup(
532 StringRef Group,
533 llvm::SmallVectorImpl<diag::kind> &Diags) const
534{
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000535 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000536 const WarningOption *Found =
537 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
538 WarningOptionCompare);
539 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000540 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000541 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000542
Daniel Dunbar3f839462011-09-29 01:47:16 +0000543 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000544 return false;
545}
546
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000547void DiagnosticIDs::getAllDiagnostics(
548 llvm::SmallVectorImpl<diag::kind> &Diags) const {
549 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
550 Diags.push_back(StaticDiagInfo[i].DiagID);
551}
552
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000553StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
554 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000555 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000556 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
557 i != e; ++i) {
558 // Don't suggest ignored warning flags.
559 if (!i->Members && !i->SubGroups)
560 continue;
561
562 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000563 if (Distance == BestDistance) {
564 // Two matches with the same distance, don't prefer one over the other.
565 Best = "";
566 } else if (Distance < BestDistance) {
567 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000568 Best = i->getName();
569 BestDistance = Distance;
570 }
571 }
572
573 return Best;
574}
575
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000576/// ProcessDiag - This is the method used to report a diagnostic that is
577/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000578bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000579 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000580
581 if (Diag.SuppressAllDiagnostics)
582 return false;
583
584 assert(Diag.getClient() && "DiagnosticClient not set!");
585
586 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000587 unsigned DiagID = Info.getID();
Jordan Rosec6d64a22012-07-11 16:50:36 +0000588 DiagnosticIDs::Level DiagLevel
589 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000590
591 if (DiagLevel != DiagnosticIDs::Note) {
592 // Record that a fatal error occurred only when we see a second
593 // non-note diagnostic. This allows notes to be attached to the
594 // fatal error, but suppresses any diagnostics that follow those
595 // notes.
596 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
597 Diag.FatalErrorOccurred = true;
598
599 Diag.LastDiagLevel = DiagLevel;
600 }
601
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000602 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
603 if (DiagLevel >= DiagnosticIDs::Error) {
604 ++Diag.TrapNumErrorsOccurred;
605 if (isUnrecoverable(DiagID))
606 ++Diag.TrapNumUnrecoverableErrorsOccurred;
607 }
608
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000609 // If a fatal error has already been emitted, silence all subsequent
610 // diagnostics.
611 if (Diag.FatalErrorOccurred) {
612 if (DiagLevel >= DiagnosticIDs::Error &&
613 Diag.Client->IncludeInDiagnosticCounts()) {
614 ++Diag.NumErrors;
615 ++Diag.NumErrorsSuppressed;
616 }
617
618 return false;
619 }
620
621 // If the client doesn't care about this message, don't issue it. If this is
622 // a note and the last real diagnostic was ignored, ignore it too.
623 if (DiagLevel == DiagnosticIDs::Ignored ||
624 (DiagLevel == DiagnosticIDs::Note &&
625 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
626 return false;
627
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000628 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000629 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000630 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasper1c84c682012-09-28 15:45:07 +0000631
632 Diag.ErrorOccurred = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000633 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000634 ++Diag.NumErrors;
635 }
636
Douglas Gregorf1d59482011-08-17 19:13:00 +0000637 // If we've emitted a lot of errors, emit a fatal error instead of it to
638 // stop a flood of bogus errors.
639 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
640 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000641 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000642 return false;
643 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000644 }
645
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000646 // Finally, report it.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000647 EmitDiag(Diag, DiagLevel);
648 return true;
649}
650
651void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
652 Diagnostic Info(&Diag);
653 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
654
David Blaikied6471f72011-09-25 23:23:43 +0000655 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000656 if (Diag.Client->IncludeInDiagnosticCounts()) {
657 if (DiagLevel == DiagnosticIDs::Warning)
658 ++Diag.NumWarnings;
659 }
660
661 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000662}
John McCall923cd572011-06-15 21:46:43 +0000663
664bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
665 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
666 // Custom diagnostics.
667 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
668 }
669
670 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000671 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000672 return false;
673
674 if (DiagID == diag::err_unavailable ||
675 DiagID == diag::err_unavailable_message)
676 return false;
677
John McCallf85e1932011-06-15 23:02:42 +0000678 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000679 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000680 return false;
681
John McCall923cd572011-06-15 21:46:43 +0000682 return true;
683}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000684
685bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
686 unsigned cat = getCategoryNumberForDiag(DiagID);
687 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
688}