blob: 4b88c60ead946d40506985d6abcd2ddb1de5d29b [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"
David Blaikie9fe8c742011-09-23 05:35:21 +000024#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000025
26#include <map>
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// Builtin Diagnostic information
31//===----------------------------------------------------------------------===//
32
33namespace {
34
35// Diagnostic classes.
36enum {
37 CLASS_NOTE = 0x01,
38 CLASS_WARNING = 0x02,
39 CLASS_EXTENSION = 0x03,
40 CLASS_ERROR = 0x04
41};
42
43struct StaticDiagInfoRec {
44 unsigned short DiagID;
45 unsigned Mapping : 3;
46 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000047 unsigned SFINAE : 1;
48 unsigned AccessControl : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000049 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000050
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000051 uint8_t NameLen;
52 uint8_t OptionGroupLen;
53
54 uint16_t DescriptionLen;
55 uint16_t BriefExplanationLen;
56 uint16_t FullExplanationLen;
57
58 const char *NameStr;
59 const char *OptionGroupStr;
60
61 const char *DescriptionStr;
62 const char *BriefExplanationStr;
63 const char *FullExplanationStr;
64
Chris Lattner5f9e2722011-07-23 10:55:15 +000065 StringRef getName() const {
66 return StringRef(NameStr, NameLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000067 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000068 StringRef getOptionGroup() const {
69 return StringRef(OptionGroupStr, OptionGroupLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000070 }
71
Chris Lattner5f9e2722011-07-23 10:55:15 +000072 StringRef getDescription() const {
73 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000074 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000075 StringRef getBriefExplanation() const {
76 return StringRef(BriefExplanationStr, BriefExplanationLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000077 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000078 StringRef getFullExplanation() const {
79 return StringRef(FullExplanationStr, FullExplanationLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000080 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000081
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000082 bool operator<(const StaticDiagInfoRec &RHS) const {
83 return DiagID < RHS.DiagID;
84 }
85};
86
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000087struct StaticDiagNameIndexRec {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000088 const char *NameStr;
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000089 unsigned short DiagID;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000090 uint8_t NameLen;
91
Chris Lattner5f9e2722011-07-23 10:55:15 +000092 StringRef getName() const {
93 return StringRef(NameStr, NameLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000094 }
95
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000096 bool operator<(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000097 return getName() < RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000098 }
99
100 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
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000105template <size_t SizeOfStr, typename FieldType>
106class StringSizerHelper {
107 char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
108public:
109 enum { Size = SizeOfStr };
110};
111
112} // namespace anonymous
113
114#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000115
116static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000117#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
118 SFINAE,ACCESS,CATEGORY,BRIEF,FULL) \
119 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, CATEGORY, \
120 STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t), \
121 STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t), \
122 STR_SIZE(FULL, uint16_t), \
123 #ENUM, GROUP, DESC, BRIEF, FULL },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000124#include "clang/Basic/DiagnosticCommonKinds.inc"
125#include "clang/Basic/DiagnosticDriverKinds.inc"
126#include "clang/Basic/DiagnosticFrontendKinds.inc"
127#include "clang/Basic/DiagnosticLexKinds.inc"
128#include "clang/Basic/DiagnosticParseKinds.inc"
129#include "clang/Basic/DiagnosticASTKinds.inc"
130#include "clang/Basic/DiagnosticSemaKinds.inc"
131#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000132#undef DIAG
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000134};
135
136static const unsigned StaticDiagInfoSize =
137 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
138
139/// To be sorted before first use (since it's splitted among multiple files)
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000140static const StaticDiagNameIndexRec StaticDiagNameIndex[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000141#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000142#include "clang/Basic/DiagnosticIndexName.inc"
143#undef DIAG_NAME_INDEX
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000144 { 0, 0, 0 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000145};
146
147static const unsigned StaticDiagNameIndexSize =
148 sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000149
150/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
151/// or null if the ID is invalid.
152static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000153 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
154#ifndef NDEBUG
155 static bool IsFirst = true;
156 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000157 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000158 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
159 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000160 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000161
162 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
163 "Improperly sorted diag info");
164 }
165 IsFirst = false;
166 }
167#endif
168
169 // Search the diagnostic table with a binary search.
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000170 StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000172
173 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000174 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
175 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000176 Found->DiagID != DiagID)
177 return 0;
178
179 return Found;
180}
181
182static unsigned GetDefaultDiagMapping(unsigned DiagID) {
183 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
184 return Info->Mapping;
185 return diag::MAP_FATAL;
186}
187
188/// getWarningOptionForDiag - Return the lowest-level warning option that
189/// enables the specified diagnostic. If there is no -Wfoo flag that controls
190/// the diagnostic, this returns null.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000191StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000192 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000193 return Info->getOptionGroup();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000194 return StringRef();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000195}
196
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000197/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000198/// DiagID belongs to, or 0 if no category.
199unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
200 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
201 return Info->Category;
202 return 0;
203}
204
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000205namespace {
206 // The diagnostic category names.
207 struct StaticDiagCategoryRec {
208 const char *NameStr;
209 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000210
Chris Lattner5f9e2722011-07-23 10:55:15 +0000211 StringRef getName() const {
212 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000213 }
214 };
215}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000216
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000217static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000218#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000219#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000220#include "clang/Basic/DiagnosticGroups.inc"
221#undef GET_CATEGORY_TABLE
222 { 0, 0 }
223};
224
225/// getNumberOfCategories - Return the number of categories
226unsigned DiagnosticIDs::getNumberOfCategories() {
227 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
228}
229
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000230/// getCategoryNameFromID - Given a category ID, return the name of the
231/// category, an empty string if CategoryID is zero, or null if CategoryID is
232/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000234 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000236 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000237}
238
239
240
241DiagnosticIDs::SFINAEResponse
242DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
243 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000244 if (Info->AccessControl)
245 return SFINAE_AccessControl;
246
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000247 if (!Info->SFINAE)
248 return SFINAE_Report;
249
250 if (Info->Class == CLASS_ERROR)
251 return SFINAE_SubstitutionFailure;
252
253 // Suppress notes, warnings, and extensions;
254 return SFINAE_Suppress;
255 }
256
257 return SFINAE_Report;
258}
259
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000260/// getName - Given a diagnostic ID, return its name
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261StringRef DiagnosticIDs::getName(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000262 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000263 return Info->getName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000265}
266
267/// getIdFromName - Given a diagnostic name, return its ID, or 0
Chris Lattner5f9e2722011-07-23 10:55:15 +0000268unsigned DiagnosticIDs::getIdFromName(StringRef Name) {
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000269 const StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000270 StaticDiagNameIndex + StaticDiagNameIndexSize;
271
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000272 if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000273
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000274 assert(Name.size() == static_cast<uint8_t>(Name.size()) &&
275 "Name is too long");
276 StaticDiagNameIndexRec Find = { Name.data(), 0,
277 static_cast<uint8_t>(Name.size()) };
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000278
279 const StaticDiagNameIndexRec *Found =
280 std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
281 if (Found == StaticDiagNameIndexEnd ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000282 Found->getName() != Name)
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000283 return diag::DIAG_UPPER_LIMIT;
284
285 return Found->DiagID;
286}
287
288/// getBriefExplanation - Given a diagnostic ID, return a brief explanation
289/// of the issue
Chris Lattner5f9e2722011-07-23 10:55:15 +0000290StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000291 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000292 return Info->getBriefExplanation();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000293 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000294}
295
296/// getFullExplanation - Given a diagnostic ID, return a full explanation
297/// of the issue
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000299 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000300 return Info->getFullExplanation();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000301 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000302}
303
304/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000305///
306static unsigned getBuiltinDiagClass(unsigned DiagID) {
307 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
308 return Info->Class;
309 return ~0U;
310}
311
312//===----------------------------------------------------------------------===//
Ted Kremenek6948bc42011-08-09 03:39:14 +0000313// diag_iterator
314//===----------------------------------------------------------------------===//
315
316llvm::StringRef DiagnosticIDs::diag_iterator::getDiagName() const {
317 return static_cast<const StaticDiagNameIndexRec*>(impl)->getName();
318}
319
320unsigned DiagnosticIDs::diag_iterator::getDiagID() const {
321 return static_cast<const StaticDiagNameIndexRec*>(impl)->DiagID;
322}
323
324DiagnosticIDs::diag_iterator &DiagnosticIDs::diag_iterator::operator++() {
325 const StaticDiagNameIndexRec* ptr =
326 static_cast<const StaticDiagNameIndexRec*>(impl);;
327 ++ptr;
328 impl = ptr;
329 return *this;
330}
331
332DiagnosticIDs::diag_iterator DiagnosticIDs::diags_begin() {
333 return DiagnosticIDs::diag_iterator(StaticDiagNameIndex);
334}
335
336DiagnosticIDs::diag_iterator DiagnosticIDs::diags_end() {
337 return DiagnosticIDs::diag_iterator(StaticDiagNameIndex +
338 StaticDiagNameIndexSize);
339}
340
341//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000342// Custom Diagnostic information
343//===----------------------------------------------------------------------===//
344
345namespace clang {
346 namespace diag {
347 class CustomDiagInfo {
348 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
349 std::vector<DiagDesc> DiagInfo;
350 std::map<DiagDesc, unsigned> DiagIDs;
351 public:
352
353 /// getDescription - Return the description of the specified custom
354 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000355 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000356 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
357 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000358 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000359 }
360
361 /// getLevel - Return the level of the specified custom diagnostic.
362 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
363 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
364 "Invalid diagnosic ID");
365 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
366 }
367
Chris Lattner5f9e2722011-07-23 10:55:15 +0000368 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000369 DiagnosticIDs &Diags) {
370 DiagDesc D(L, Message);
371 // Check to see if it already exists.
372 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
373 if (I != DiagIDs.end() && I->first == D)
374 return I->second;
375
376 // If not, assign a new ID.
377 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
378 DiagIDs.insert(std::make_pair(D, ID));
379 DiagInfo.push_back(D);
380 return ID;
381 }
382 };
383
384 } // end diag namespace
385} // end clang namespace
386
387
388//===----------------------------------------------------------------------===//
389// Common Diagnostic implementation
390//===----------------------------------------------------------------------===//
391
392DiagnosticIDs::DiagnosticIDs() {
393 CustomDiagInfo = 0;
394}
395
396DiagnosticIDs::~DiagnosticIDs() {
397 delete CustomDiagInfo;
398}
399
400/// getCustomDiagID - Return an ID for a diagnostic with the specified message
401/// and level. If this is the first request for this diagnosic, it is
402/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000403unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000404 if (CustomDiagInfo == 0)
405 CustomDiagInfo = new diag::CustomDiagInfo();
406 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
407}
408
409
410/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
411/// level of the specified diagnostic ID is a Warning or Extension.
412/// This only works on builtin diagnostics, not custom ones, and is not legal to
413/// call on NOTEs.
414bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
415 return DiagID < diag::DIAG_UPPER_LIMIT &&
416 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
417}
418
419/// \brief Determine whether the given built-in diagnostic ID is a
420/// Note.
421bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
422 return DiagID < diag::DIAG_UPPER_LIMIT &&
423 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
424}
425
426/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
427/// ID is for an extension of some sort. This also returns EnabledByDefault,
428/// which is set to indicate whether the diagnostic is ignored by default (in
429/// which case -pedantic enables it) or treated as a warning/error by default.
430///
431bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
432 bool &EnabledByDefault) {
433 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
434 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
435 return false;
436
437 EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE;
438 return true;
439}
440
441/// getDescription - Given a diagnostic ID, return a description of the
442/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000443StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000444 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000445 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000446 return CustomDiagInfo->getDescription(DiagID);
447}
448
David Blaikied6471f72011-09-25 23:23:43 +0000449/// getDiagnosticLevel - Based on the way the client configured the
450/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
451/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000452DiagnosticIDs::Level
453DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
David Blaikied6471f72011-09-25 23:23:43 +0000454 const DiagnosticsEngine &Diag,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000455 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000456 // Handle custom diagnostics, which cannot be mapped.
457 if (DiagID >= diag::DIAG_UPPER_LIMIT)
458 return CustomDiagInfo->getLevel(DiagID);
459
460 unsigned DiagClass = getBuiltinDiagClass(DiagID);
461 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Ted Kremenek7decebf2011-02-25 01:28:26 +0000462 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag, mapping);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000463}
464
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000465/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000466/// object, classify the specified diagnostic ID into a Level, consumable by
467/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000468///
469/// \param Loc The source location we are interested in finding out the
470/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000471DiagnosticIDs::Level
472DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000473 SourceLocation Loc,
David Blaikied6471f72011-09-25 23:23:43 +0000474 const DiagnosticsEngine &Diag,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000475 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000476 // Specific non-error diagnostics may be mapped to various levels from ignored
477 // to error. Errors can only be mapped to fatal.
478 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
479
David Blaikied6471f72011-09-25 23:23:43 +0000480 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000481 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000482 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000483
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000484 // Get the mapping information, if unset, compute it lazily.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000485 unsigned MappingInfo = Diag.getDiagnosticMappingInfo((diag::kind)DiagID,
486 State);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000487 if (MappingInfo == 0) {
488 MappingInfo = GetDefaultDiagMapping(DiagID);
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000489 Diag.setDiagnosticMappingInternal(DiagID, MappingInfo, State, false, false);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000490 }
Ted Kremenek7decebf2011-02-25 01:28:26 +0000491
492 if (mapping)
493 *mapping = (diag::Mapping) (MappingInfo & 7);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000494
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000495 bool ShouldEmitInSystemHeader = false;
496
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000497 switch (MappingInfo & 7) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000498 default: llvm_unreachable("Unknown mapping!");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000499 case diag::MAP_IGNORE:
Ted Kremenek1e473cc2011-08-18 01:12:56 +0000500 if (Diag.EnableAllWarnings) {
501 // Leave the warning disabled if it was explicitly ignored.
502 if ((MappingInfo & 8) != 0)
503 return DiagnosticIDs::Ignored;
504
505 Result = Diag.WarningsAsErrors ? DiagnosticIDs::Error
506 : DiagnosticIDs::Warning;
507 }
508 // Otherwise, ignore this diagnostic unless this is an extension diagnostic
509 // and we're mapping them onto warnings or errors.
510 else if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
David Blaikied6471f72011-09-25 23:23:43 +0000511 Diag.ExtBehavior == DiagnosticsEngine::Ext_Ignore || // Ext ignored
Ted Kremenek1e473cc2011-08-18 01:12:56 +0000512 (MappingInfo & 8) != 0) { // User explicitly mapped it.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000513 return DiagnosticIDs::Ignored;
Ted Kremenek1e473cc2011-08-18 01:12:56 +0000514 }
515 else {
516 Result = DiagnosticIDs::Warning;
517 }
518
David Blaikied6471f72011-09-25 23:23:43 +0000519 if (Diag.ExtBehavior == DiagnosticsEngine::Ext_Error)
Ted Kremenek1e473cc2011-08-18 01:12:56 +0000520 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000521 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
522 Result = DiagnosticIDs::Fatal;
523 break;
524 case diag::MAP_ERROR:
525 Result = DiagnosticIDs::Error;
526 if (Diag.ErrorsAsFatal)
527 Result = DiagnosticIDs::Fatal;
528 break;
529 case diag::MAP_FATAL:
530 Result = DiagnosticIDs::Fatal;
531 break;
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000532 case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER:
533 ShouldEmitInSystemHeader = true;
534 // continue as MAP_WARNING.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000535 case diag::MAP_WARNING:
536 // If warnings are globally mapped to ignore or error, do it.
537 if (Diag.IgnoreAllWarnings)
538 return DiagnosticIDs::Ignored;
539
540 Result = DiagnosticIDs::Warning;
541
542 // If this is an extension diagnostic and we're in -pedantic-error mode, and
543 // if the user didn't explicitly map it, upgrade to an error.
David Blaikied6471f72011-09-25 23:23:43 +0000544 if (Diag.ExtBehavior == DiagnosticsEngine::Ext_Error &&
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000545 (MappingInfo & 8) == 0 &&
546 isBuiltinExtensionDiag(DiagID))
547 Result = DiagnosticIDs::Error;
548
549 if (Diag.WarningsAsErrors)
550 Result = DiagnosticIDs::Error;
551 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
552 Result = DiagnosticIDs::Fatal;
553 break;
554
555 case diag::MAP_WARNING_NO_WERROR:
556 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
557 // not be adjusted by -Werror or -pedantic-errors.
558 Result = DiagnosticIDs::Warning;
559
560 // If warnings are globally mapped to ignore or error, do it.
561 if (Diag.IgnoreAllWarnings)
562 return DiagnosticIDs::Ignored;
563
564 break;
565
566 case diag::MAP_ERROR_NO_WFATAL:
567 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
568 // unaffected by -Wfatal-errors.
569 Result = DiagnosticIDs::Error;
570 break;
571 }
572
573 // Okay, we're about to return this as a "diagnostic to emit" one last check:
574 // if this is any sort of extension warning, and if we're in an __extension__
575 // block, silence it.
576 if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
577 return DiagnosticIDs::Ignored;
578
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000579 // If we are in a system header, we ignore it.
580 // We also want to ignore extensions and warnings in -Werror and
581 // -pedantic-errors modes, which *map* warnings/extensions to errors.
582 if (Result >= DiagnosticIDs::Warning &&
583 DiagClass != CLASS_ERROR &&
584 // Custom diagnostics always are emitted in system headers.
585 DiagID < diag::DIAG_UPPER_LIMIT &&
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000586 !ShouldEmitInSystemHeader &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000587 Diag.SuppressSystemWarnings &&
588 Loc.isValid() &&
589 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000590 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000591 return DiagnosticIDs::Ignored;
592
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000593 return Result;
594}
595
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000596namespace {
597 struct WarningOption {
598 // Be safe with the size of 'NameLen' because we don't statically check if
599 // the size will fit in the field; the struct size won't decrease with a
600 // shorter type anyway.
601 size_t NameLen;
602 const char *NameStr;
603 const short *Members;
604 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000605
Chris Lattner5f9e2722011-07-23 10:55:15 +0000606 StringRef getName() const {
607 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000608 }
609 };
610}
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000611
612#define GET_DIAG_ARRAYS
613#include "clang/Basic/DiagnosticGroups.inc"
614#undef GET_DIAG_ARRAYS
615
616// Second the table of options, sorted by name for fast binary lookup.
617static const WarningOption OptionTable[] = {
618#define GET_DIAG_TABLE
619#include "clang/Basic/DiagnosticGroups.inc"
620#undef GET_DIAG_TABLE
621};
622static const size_t OptionTableSize =
623sizeof(OptionTable) / sizeof(OptionTable[0]);
624
625static bool WarningOptionCompare(const WarningOption &LHS,
626 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000627 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000628}
629
630static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
David Blaikied6471f72011-09-25 23:23:43 +0000631 SourceLocation Loc, DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000632 // Option exists, poke all the members of its diagnostic set.
633 if (const short *Member = Group->Members) {
634 for (; *Member != -1; ++Member)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000635 Diag.setDiagnosticMapping(*Member, Mapping, Loc);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000636 }
637
638 // Enable/disable all subgroups along with this one.
639 if (const short *SubGroups = Group->SubGroups) {
640 for (; *SubGroups != (short)-1; ++SubGroups)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000641 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000642 }
643}
644
645/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
646/// "unknown-pragmas" to have the specified mapping. This returns true and
647/// ignores the request if "Group" was unknown, false otherwise.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000648bool DiagnosticIDs::setDiagnosticGroupMapping(StringRef Group,
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000649 diag::Mapping Map,
650 SourceLocation Loc,
David Blaikied6471f72011-09-25 23:23:43 +0000651 DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000652 assert((Loc.isValid() ||
653 Diag.DiagStatePoints.empty() ||
654 Diag.DiagStatePoints.back().Loc.isInvalid()) &&
655 "Loc should be invalid only when the mapping comes from command-line");
656 assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
657 Diag.DiagStatePoints.back().Loc.isInvalid() ||
658 !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
659 Diag.DiagStatePoints.back().Loc)) &&
660 "Source location of new mapping is before the previous one!");
661
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000662 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000663 const WarningOption *Found =
664 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
665 WarningOptionCompare);
666 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000667 Found->getName() != Group)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000668 return true; // Option not found.
669
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000670 MapGroupMembers(Found, Map, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000671 return false;
672}
673
674/// ProcessDiag - This is the method used to report a diagnostic that is
675/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000676bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000677 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000678
679 if (Diag.SuppressAllDiagnostics)
680 return false;
681
682 assert(Diag.getClient() && "DiagnosticClient not set!");
683
684 // Figure out the diagnostic level of this message.
685 DiagnosticIDs::Level DiagLevel;
686 unsigned DiagID = Info.getID();
687
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000688 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
689 // Handle custom diagnostics, which cannot be mapped.
690 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000691 } else {
692 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
693 // the diagnostic level was for the previous diagnostic so that it is
694 // filtered the same as the previous diagnostic.
695 unsigned DiagClass = getBuiltinDiagClass(DiagID);
696 if (DiagClass == CLASS_NOTE) {
697 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000698 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000699 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
700 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000701 }
702 }
703
704 if (DiagLevel != DiagnosticIDs::Note) {
705 // Record that a fatal error occurred only when we see a second
706 // non-note diagnostic. This allows notes to be attached to the
707 // fatal error, but suppresses any diagnostics that follow those
708 // notes.
709 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
710 Diag.FatalErrorOccurred = true;
711
712 Diag.LastDiagLevel = DiagLevel;
713 }
714
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000715 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
716 if (DiagLevel >= DiagnosticIDs::Error) {
717 ++Diag.TrapNumErrorsOccurred;
718 if (isUnrecoverable(DiagID))
719 ++Diag.TrapNumUnrecoverableErrorsOccurred;
720 }
721
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000722 // If a fatal error has already been emitted, silence all subsequent
723 // diagnostics.
724 if (Diag.FatalErrorOccurred) {
725 if (DiagLevel >= DiagnosticIDs::Error &&
726 Diag.Client->IncludeInDiagnosticCounts()) {
727 ++Diag.NumErrors;
728 ++Diag.NumErrorsSuppressed;
729 }
730
731 return false;
732 }
733
734 // If the client doesn't care about this message, don't issue it. If this is
735 // a note and the last real diagnostic was ignored, ignore it too.
736 if (DiagLevel == DiagnosticIDs::Ignored ||
737 (DiagLevel == DiagnosticIDs::Note &&
738 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
739 return false;
740
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000741 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000742 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000743 Diag.UnrecoverableErrorOccurred = true;
Douglas Gregor85bea972011-07-06 17:40:26 +0000744
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000745 if (Diag.Client->IncludeInDiagnosticCounts()) {
746 Diag.ErrorOccurred = true;
747 ++Diag.NumErrors;
748 }
749
Douglas Gregorf1d59482011-08-17 19:13:00 +0000750 // If we've emitted a lot of errors, emit a fatal error instead of it to
751 // stop a flood of bogus errors.
752 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
753 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000754 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000755 return false;
756 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000757 }
758
Douglas Gregor4814fb52011-02-03 23:41:12 +0000759 // If we have any Fix-Its, make sure that all of the Fix-Its point into
Chandler Carruth3201f382011-07-26 05:17:23 +0000760 // source locations that aren't macro expansions. If any point into macro
761 // expansions, remove all of the Fix-Its.
Douglas Gregor4814fb52011-02-03 23:41:12 +0000762 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
763 const FixItHint &FixIt = Diag.FixItHints[I];
764 if (FixIt.RemoveRange.isInvalid() ||
765 FixIt.RemoveRange.getBegin().isMacroID() ||
766 FixIt.RemoveRange.getEnd().isMacroID()) {
767 Diag.NumFixItHints = 0;
768 break;
769 }
770 }
771
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000772 // Finally, report it.
David Blaikied6471f72011-09-25 23:23:43 +0000773 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000774 if (Diag.Client->IncludeInDiagnosticCounts()) {
775 if (DiagLevel == DiagnosticIDs::Warning)
776 ++Diag.NumWarnings;
777 }
778
779 Diag.CurDiagID = ~0U;
780
781 return true;
782}
John McCall923cd572011-06-15 21:46:43 +0000783
784bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
785 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
786 // Custom diagnostics.
787 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
788 }
789
790 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000791 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000792 return false;
793
794 if (DiagID == diag::err_unavailable ||
795 DiagID == diag::err_unavailable_message)
796 return false;
797
John McCallf85e1932011-06-15 23:02:42 +0000798 // Currently we consider all ARC errors as recoverable.
799 if (getCategoryNumberForDiag(DiagID) ==
800 diag::DiagCat_Automatic_Reference_Counting_Issue)
801 return false;
802
John McCall923cd572011-06-15 21:46:43 +0000803 return true;
804}