blob: e96d5097e620c4955e8edc1e735632f62f7a83de [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 Majnemercfaa5522013-07-20 07:15:15 +000019#include "llvm/ADT/STLExtras.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000021#include <map>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Builtin Diagnostic information
26//===----------------------------------------------------------------------===//
27
28namespace {
29
30// Diagnostic classes.
31enum {
32 CLASS_NOTE = 0x01,
33 CLASS_WARNING = 0x02,
34 CLASS_EXTENSION = 0x03,
35 CLASS_ERROR = 0x04
36};
37
38struct StaticDiagInfoRec {
39 unsigned short DiagID;
40 unsigned Mapping : 3;
41 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000042 unsigned SFINAE : 1;
43 unsigned AccessControl : 1;
Daniel Dunbar4213df32011-09-29 00:34:06 +000044 unsigned WarnNoWerror : 1;
45 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000046 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000047
Benjamin Kramerd49cb202012-02-15 20:57:03 +000048 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000049
50 uint16_t DescriptionLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000051 const char *DescriptionStr;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000052
Benjamin Kramerd49cb202012-02-15 20:57:03 +000053 unsigned getOptionGroupIndex() const {
54 return OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000055 }
56
Chris Lattner5f9e2722011-07-23 10:55:15 +000057 StringRef getDescription() const {
58 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000059 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000060
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000061 bool operator<(const StaticDiagInfoRec &RHS) const {
62 return DiagID < RHS.DiagID;
63 }
64};
65
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000066} // namespace anonymous
67
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000068static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000069#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Daniel Dunbar4213df32011-09-29 00:34:06 +000070 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
Benjamin Kramerf94d3922012-02-09 19:38:26 +000071 CATEGORY) \
Daniel Dunbar4213df32011-09-29 00:34:06 +000072 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
Benjamin Kramerd49cb202012-02-15 20:57:03 +000073 NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \
74 STR_SIZE(DESC, uint16_t), DESC },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000075#include "clang/Basic/DiagnosticCommonKinds.inc"
76#include "clang/Basic/DiagnosticDriverKinds.inc"
77#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +000078#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000079#include "clang/Basic/DiagnosticLexKinds.inc"
80#include "clang/Basic/DiagnosticParseKinds.inc"
81#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000082#include "clang/Basic/DiagnosticCommentKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000083#include "clang/Basic/DiagnosticSemaKinds.inc"
84#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000085#undef DIAG
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000086};
87
David Majnemercfaa5522013-07-20 07:15:15 +000088static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000089
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000090/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
91/// or null if the ID is invalid.
92static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000093 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
94#ifndef NDEBUG
Craig Toppera4f52522013-07-19 03:59:51 +000095 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
96 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
97 "Diag ID conflict, the enums at the start of clang::diag (in "
98 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000099
Craig Toppera4f52522013-07-19 03:59:51 +0000100 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
101 "Improperly sorted diag info");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000102 }
103#endif
104
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000105 // Out of bounds diag. Can't be in the table.
106 using namespace diag;
David Majnemercfaa5522013-07-20 07:15:15 +0000107 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000108 return 0;
109
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000110 // Compute the index of the requested diagnostic in the static table.
111 // 1. Add the number of diagnostics in each category preceeding the
112 // diagnostic and of the category the diagnostic is in. This gives us
113 // the offset of the category in the table.
114 // 2. Subtract the number of IDs in each category from our ID. This gives us
115 // the offset of the diagnostic in the category.
116 // This is cheaper than a binary search on the table as it doesn't touch
117 // memory at all.
118 unsigned Offset = 0;
David Majnemercfaa5522013-07-20 07:15:15 +0000119 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000120#define CATEGORY(NAME, PREV) \
121 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000122 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
123 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000124 }
125CATEGORY(DRIVER, COMMON)
126CATEGORY(FRONTEND, DRIVER)
127CATEGORY(SERIALIZATION, FRONTEND)
128CATEGORY(LEX, SERIALIZATION)
129CATEGORY(PARSE, LEX)
130CATEGORY(AST, PARSE)
131CATEGORY(COMMENT, AST)
132CATEGORY(SEMA, COMMENT)
133CATEGORY(ANALYSIS, SEMA)
134#undef CATEGORY
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000135
136 // Avoid out of bounds reads.
137 if (ID + Offset >= StaticDiagInfoSize)
138 return 0;
139
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000140 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
141
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000142 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
143 // If the diag id doesn't match we found a different diag, abort. This can
144 // happen when this function is called with an ID that points into a hole in
145 // the diagID space.
146 if (Found->DiagID != DiagID)
147 return 0;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000148 return Found;
149}
150
Daniel Dunbara5e41332011-09-29 01:52:06 +0000151static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000152 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000153 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000154
Daniel Dunbara5e41332011-09-29 01:52:06 +0000155 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
156 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
157
158 if (StaticInfo->WarnNoWerror) {
159 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000160 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000161 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000162 }
163
Daniel Dunbara5e41332011-09-29 01:52:06 +0000164 if (StaticInfo->WarnShowInSystemHeader) {
165 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000166 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000167 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000168 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000169 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000170
171 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000172}
173
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000174/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000175/// DiagID belongs to, or 0 if no category.
176unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
177 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
178 return Info->Category;
179 return 0;
180}
181
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000182namespace {
183 // The diagnostic category names.
184 struct StaticDiagCategoryRec {
185 const char *NameStr;
186 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000187
Chris Lattner5f9e2722011-07-23 10:55:15 +0000188 StringRef getName() const {
189 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000190 }
191 };
192}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000193
Daniel Dunbarba494c62011-09-29 01:42:25 +0000194// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
195// particularly clean, but for now we just implement this method here so we can
196// access GetDefaultDiagMapping.
197DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
198 diag::kind Diag)
199{
200 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000201 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000202
203 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000204 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000205 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000206
207 return Result.first->second;
208}
209
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000210static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000211#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000212#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000213#include "clang/Basic/DiagnosticGroups.inc"
214#undef GET_CATEGORY_TABLE
215 { 0, 0 }
216};
217
218/// getNumberOfCategories - Return the number of categories
219unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Topperb9602322013-07-15 03:38:40 +0000220 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000221}
222
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000223/// getCategoryNameFromID - Given a category ID, return the name of the
224/// category, an empty string if CategoryID is zero, or null if CategoryID is
225/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000227 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000229 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000230}
231
232
233
234DiagnosticIDs::SFINAEResponse
235DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
236 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000237 if (Info->AccessControl)
238 return SFINAE_AccessControl;
239
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000240 if (!Info->SFINAE)
241 return SFINAE_Report;
242
243 if (Info->Class == CLASS_ERROR)
244 return SFINAE_SubstitutionFailure;
245
246 // Suppress notes, warnings, and extensions;
247 return SFINAE_Suppress;
248 }
249
250 return SFINAE_Report;
251}
252
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000253/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000254///
255static unsigned getBuiltinDiagClass(unsigned DiagID) {
256 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
257 return Info->Class;
258 return ~0U;
259}
260
261//===----------------------------------------------------------------------===//
262// Custom Diagnostic information
263//===----------------------------------------------------------------------===//
264
265namespace clang {
266 namespace diag {
267 class CustomDiagInfo {
268 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
269 std::vector<DiagDesc> DiagInfo;
270 std::map<DiagDesc, unsigned> DiagIDs;
271 public:
272
273 /// getDescription - Return the description of the specified custom
274 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000275 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000276 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000277 "Invalid diagnostic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000278 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000279 }
280
281 /// getLevel - Return the level of the specified custom diagnostic.
282 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
283 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000284 "Invalid diagnostic ID");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000285 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
286 }
287
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000289 DiagnosticIDs &Diags) {
290 DiagDesc D(L, Message);
291 // Check to see if it already exists.
292 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
293 if (I != DiagIDs.end() && I->first == D)
294 return I->second;
295
296 // If not, assign a new ID.
297 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
298 DiagIDs.insert(std::make_pair(D, ID));
299 DiagInfo.push_back(D);
300 return ID;
301 }
302 };
303
304 } // end diag namespace
305} // end clang namespace
306
307
308//===----------------------------------------------------------------------===//
309// Common Diagnostic implementation
310//===----------------------------------------------------------------------===//
311
312DiagnosticIDs::DiagnosticIDs() {
313 CustomDiagInfo = 0;
314}
315
316DiagnosticIDs::~DiagnosticIDs() {
317 delete CustomDiagInfo;
318}
319
320/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000321/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000322/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000323unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000324 if (CustomDiagInfo == 0)
325 CustomDiagInfo = new diag::CustomDiagInfo();
326 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
327}
328
329
330/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
331/// level of the specified diagnostic ID is a Warning or Extension.
332/// This only works on builtin diagnostics, not custom ones, and is not legal to
333/// call on NOTEs.
334bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
335 return DiagID < diag::DIAG_UPPER_LIMIT &&
336 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
337}
338
339/// \brief Determine whether the given built-in diagnostic ID is a
340/// Note.
341bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
342 return DiagID < diag::DIAG_UPPER_LIMIT &&
343 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
344}
345
346/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
347/// ID is for an extension of some sort. This also returns EnabledByDefault,
348/// which is set to indicate whether the diagnostic is ignored by default (in
349/// which case -pedantic enables it) or treated as a warning/error by default.
350///
351bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
352 bool &EnabledByDefault) {
353 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
354 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
355 return false;
356
Daniel Dunbara5e41332011-09-29 01:52:06 +0000357 EnabledByDefault =
358 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000359 return true;
360}
361
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000362bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
363 if (DiagID >= diag::DIAG_UPPER_LIMIT)
364 return false;
365
Daniel Dunbara5e41332011-09-29 01:52:06 +0000366 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000367}
368
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000369/// getDescription - Given a diagnostic ID, return a description of the
370/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000371StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000372 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000373 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000374 return CustomDiagInfo->getDescription(DiagID);
375}
376
David Blaikied6471f72011-09-25 23:23:43 +0000377/// getDiagnosticLevel - Based on the way the client configured the
378/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
379/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000380DiagnosticIDs::Level
381DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000382 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000383 // Handle custom diagnostics, which cannot be mapped.
384 if (DiagID >= diag::DIAG_UPPER_LIMIT)
385 return CustomDiagInfo->getLevel(DiagID);
386
387 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000388 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000389 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000390}
391
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000392/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000393/// object, classify the specified diagnostic ID into a Level, consumable by
394/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000395///
396/// \param Loc The source location we are interested in finding out the
397/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000398DiagnosticIDs::Level
399DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000400 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000401 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000402 // Specific non-error diagnostics may be mapped to various levels from ignored
403 // to error. Errors can only be mapped to fatal.
404 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
405
David Blaikied6471f72011-09-25 23:23:43 +0000406 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000407 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000408 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000409
Daniel Dunbarba494c62011-09-29 01:42:25 +0000410 // Get the mapping information, or compute it lazily.
411 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
412 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000413
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000414 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000415 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000416 Result = DiagnosticIDs::Ignored;
417 break;
418 case diag::MAP_WARNING:
419 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000420 break;
421 case diag::MAP_ERROR:
422 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000423 break;
424 case diag::MAP_FATAL:
425 Result = DiagnosticIDs::Fatal;
426 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000427 }
428
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000429 // Upgrade ignored diagnostics if -Weverything is enabled.
430 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
431 !MappingInfo.isUser())
432 Result = DiagnosticIDs::Warning;
433
Bob Wilson18c407f2011-10-12 19:55:31 +0000434 // Ignore -pedantic diagnostics inside __extension__ blocks.
435 // (The diagnostics controlled by -pedantic are the extension diagnostics
436 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000437 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000438 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
439 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000440 return DiagnosticIDs::Ignored;
441
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000442 // For extension diagnostics that haven't been explicitly mapped, check if we
443 // should upgrade the diagnostic.
444 if (IsExtensionDiag && !MappingInfo.isUser()) {
445 switch (Diag.ExtBehavior) {
446 case DiagnosticsEngine::Ext_Ignore:
447 break;
448 case DiagnosticsEngine::Ext_Warn:
449 // Upgrade ignored diagnostics to warnings.
450 if (Result == DiagnosticIDs::Ignored)
451 Result = DiagnosticIDs::Warning;
452 break;
453 case DiagnosticsEngine::Ext_Error:
454 // Upgrade ignored or warning diagnostics to errors.
455 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
456 Result = DiagnosticIDs::Error;
457 break;
458 }
459 }
460
461 // At this point, ignored errors can no longer be upgraded.
462 if (Result == DiagnosticIDs::Ignored)
463 return Result;
464
465 // Honor -w, which is lower in priority than pedantic-errors, but higher than
466 // -Werror.
467 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
468 return DiagnosticIDs::Ignored;
469
470 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
471 if (Result == DiagnosticIDs::Warning) {
472 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
473 Result = DiagnosticIDs::Error;
474 }
475
476 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
477 // disabled.
478 if (Result == DiagnosticIDs::Error) {
479 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
480 Result = DiagnosticIDs::Fatal;
481 }
482
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000483 // If we are in a system header, we ignore it. We look at the diagnostic class
484 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000485 // -pedantic-errors modes, which *map* warnings/extensions to errors.
486 if (Result >= DiagnosticIDs::Warning &&
487 DiagClass != CLASS_ERROR &&
488 // Custom diagnostics always are emitted in system headers.
489 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000490 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000491 Diag.SuppressSystemWarnings &&
492 Loc.isValid() &&
493 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000494 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000495 return DiagnosticIDs::Ignored;
496
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000497 return Result;
498}
499
Daniel Dunbar3f839462011-09-29 01:47:16 +0000500struct clang::WarningOption {
501 // Be safe with the size of 'NameLen' because we don't statically check if
502 // the size will fit in the field; the struct size won't decrease with a
503 // shorter type anyway.
504 size_t NameLen;
505 const char *NameStr;
506 const short *Members;
507 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000508
Daniel Dunbar3f839462011-09-29 01:47:16 +0000509 StringRef getName() const {
510 return StringRef(NameStr, NameLen);
511 }
512};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000513
514#define GET_DIAG_ARRAYS
515#include "clang/Basic/DiagnosticGroups.inc"
516#undef GET_DIAG_ARRAYS
517
518// Second the table of options, sorted by name for fast binary lookup.
519static const WarningOption OptionTable[] = {
520#define GET_DIAG_TABLE
521#include "clang/Basic/DiagnosticGroups.inc"
522#undef GET_DIAG_TABLE
523};
Craig Topperb9602322013-07-15 03:38:40 +0000524static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000525
526static bool WarningOptionCompare(const WarningOption &LHS,
527 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000528 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000529}
530
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000531/// getWarningOptionForDiag - Return the lowest-level warning option that
532/// enables the specified diagnostic. If there is no -Wfoo flag that controls
533/// the diagnostic, this returns null.
534StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
535 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
536 return OptionTable[Info->getOptionGroupIndex()].getName();
537 return StringRef();
538}
539
Daniel Dunbar3f839462011-09-29 01:47:16 +0000540void DiagnosticIDs::getDiagnosticsInGroup(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000541 const WarningOption *Group,
542 SmallVectorImpl<diag::kind> &Diags) const {
Daniel Dunbar3f839462011-09-29 01:47:16 +0000543 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000544 if (const short *Member = Group->Members) {
545 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000546 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000547 }
548
Daniel Dunbar3f839462011-09-29 01:47:16 +0000549 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000550 if (const short *SubGroups = Group->SubGroups) {
551 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000552 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000553 }
554}
555
Daniel Dunbar3f839462011-09-29 01:47:16 +0000556bool DiagnosticIDs::getDiagnosticsInGroup(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000557 StringRef Group,
558 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000559 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000560 const WarningOption *Found =
561 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
562 WarningOptionCompare);
563 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000564 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000565 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000566
Daniel Dunbar3f839462011-09-29 01:47:16 +0000567 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000568 return false;
569}
570
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000571void DiagnosticIDs::getAllDiagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000572 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000573 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
574 Diags.push_back(StaticDiagInfo[i].DiagID);
575}
576
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000577StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
578 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000579 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000580 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
581 i != e; ++i) {
582 // Don't suggest ignored warning flags.
583 if (!i->Members && !i->SubGroups)
584 continue;
585
586 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000587 if (Distance == BestDistance) {
588 // Two matches with the same distance, don't prefer one over the other.
589 Best = "";
590 } else if (Distance < BestDistance) {
591 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000592 Best = i->getName();
593 BestDistance = Distance;
594 }
595 }
596
597 return Best;
598}
599
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000600/// ProcessDiag - This is the method used to report a diagnostic that is
601/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000602bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000603 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000604
605 if (Diag.SuppressAllDiagnostics)
606 return false;
607
608 assert(Diag.getClient() && "DiagnosticClient not set!");
609
610 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000611 unsigned DiagID = Info.getID();
Jordan Rosec6d64a22012-07-11 16:50:36 +0000612 DiagnosticIDs::Level DiagLevel
613 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000614
615 if (DiagLevel != DiagnosticIDs::Note) {
616 // Record that a fatal error occurred only when we see a second
617 // non-note diagnostic. This allows notes to be attached to the
618 // fatal error, but suppresses any diagnostics that follow those
619 // notes.
620 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
621 Diag.FatalErrorOccurred = true;
622
623 Diag.LastDiagLevel = DiagLevel;
624 }
625
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000626 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
627 if (DiagLevel >= DiagnosticIDs::Error) {
628 ++Diag.TrapNumErrorsOccurred;
629 if (isUnrecoverable(DiagID))
630 ++Diag.TrapNumUnrecoverableErrorsOccurred;
631 }
632
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000633 // If a fatal error has already been emitted, silence all subsequent
634 // diagnostics.
635 if (Diag.FatalErrorOccurred) {
636 if (DiagLevel >= DiagnosticIDs::Error &&
637 Diag.Client->IncludeInDiagnosticCounts()) {
638 ++Diag.NumErrors;
639 ++Diag.NumErrorsSuppressed;
640 }
641
642 return false;
643 }
644
645 // If the client doesn't care about this message, don't issue it. If this is
646 // a note and the last real diagnostic was ignored, ignore it too.
647 if (DiagLevel == DiagnosticIDs::Ignored ||
648 (DiagLevel == DiagnosticIDs::Note &&
649 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
650 return false;
651
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000652 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000653 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000654 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasper1c84c682012-09-28 15:45:07 +0000655
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000656 // Warnings which have been upgraded to errors do not prevent compilation.
657 if (isDefaultMappingAsError(DiagID))
658 Diag.UncompilableErrorOccurred = true;
659
Daniel Jasper1c84c682012-09-28 15:45:07 +0000660 Diag.ErrorOccurred = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000661 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000662 ++Diag.NumErrors;
663 }
664
Douglas Gregorf1d59482011-08-17 19:13:00 +0000665 // If we've emitted a lot of errors, emit a fatal error instead of it to
666 // stop a flood of bogus errors.
667 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
668 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000669 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000670 return false;
671 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000672 }
673
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000674 // Finally, report it.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000675 EmitDiag(Diag, DiagLevel);
676 return true;
677}
678
679void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
680 Diagnostic Info(&Diag);
681 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
682
David Blaikied6471f72011-09-25 23:23:43 +0000683 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000684 if (Diag.Client->IncludeInDiagnosticCounts()) {
685 if (DiagLevel == DiagnosticIDs::Warning)
686 ++Diag.NumWarnings;
687 }
688
689 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000690}
John McCall923cd572011-06-15 21:46:43 +0000691
692bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
693 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
694 // Custom diagnostics.
695 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
696 }
697
698 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000699 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000700 return false;
701
702 if (DiagID == diag::err_unavailable ||
703 DiagID == diag::err_unavailable_message)
704 return false;
705
John McCallf85e1932011-06-15 23:02:42 +0000706 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000707 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000708 return false;
709
John McCall923cd572011-06-15 21:46:43 +0000710 return true;
711}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000712
713bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
714 unsigned cat = getCategoryNumberForDiag(DiagID);
715 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
716}