blob: 47798596ccd2ae0fdbf1efd562401dd2f6c6b5fc [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"
David Majnemercfaa5522013-07-20 07:15:15 +000018#include "llvm/ADT/STLExtras.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070019#include "llvm/ADT/SmallVector.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000021#include <map>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Builtin Diagnostic information
26//===----------------------------------------------------------------------===//
27
28namespace {
29
30// Diagnostic classes.
31enum {
32 CLASS_NOTE = 0x01,
Stephen Hines651f13c2014-04-23 16:59:28 -070033 CLASS_REMARK = 0x02,
34 CLASS_WARNING = 0x03,
35 CLASS_EXTENSION = 0x04,
36 CLASS_ERROR = 0x05
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000037};
38
39struct StaticDiagInfoRec {
Craig Topper8bfffa52013-07-21 21:56:18 +000040 uint16_t DiagID;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000041 unsigned Mapping : 3;
42 unsigned Class : 3;
Richard Smith3347b492013-11-12 02:41:45 +000043 unsigned SFINAE : 2;
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, \
Richard Smith3347b492013-11-12 02:41:45 +000070 SFINAE,NOWERROR,SHOWINSYSHEADER,CATEGORY) \
71 { diag::ENUM, DEFAULT_MAPPING, CLASS, \
Richard Smith2a7ff772013-11-12 02:58:11 +000072 DiagnosticIDs::SFINAE, \
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 Toppercbfb8d72013-07-21 18:58:40 +000095 static bool IsFirst = true; // So the check is only performed on first call.
96 if (IsFirst) {
97 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
98 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
99 "Diag ID conflict, the enums at the start of clang::diag (in "
100 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000101
Craig Toppercbfb8d72013-07-21 18:58:40 +0000102 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
103 "Improperly sorted diag info");
104 }
105 IsFirst = false;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000106 }
107#endif
108
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000109 // Out of bounds diag. Can't be in the table.
110 using namespace diag;
David Majnemercfaa5522013-07-20 07:15:15 +0000111 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700112 return nullptr;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000113
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000114 // Compute the index of the requested diagnostic in the static table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700115 // 1. Add the number of diagnostics in each category preceding the
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000116 // diagnostic and of the category the diagnostic is in. This gives us
117 // the offset of the category in the table.
118 // 2. Subtract the number of IDs in each category from our ID. This gives us
119 // the offset of the diagnostic in the category.
120 // This is cheaper than a binary search on the table as it doesn't touch
121 // memory at all.
122 unsigned Offset = 0;
David Majnemercfaa5522013-07-20 07:15:15 +0000123 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000124#define CATEGORY(NAME, PREV) \
125 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000126 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
127 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000128 }
129CATEGORY(DRIVER, COMMON)
130CATEGORY(FRONTEND, DRIVER)
131CATEGORY(SERIALIZATION, FRONTEND)
132CATEGORY(LEX, SERIALIZATION)
133CATEGORY(PARSE, LEX)
134CATEGORY(AST, PARSE)
135CATEGORY(COMMENT, AST)
136CATEGORY(SEMA, COMMENT)
137CATEGORY(ANALYSIS, SEMA)
138#undef CATEGORY
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000139
140 // Avoid out of bounds reads.
141 if (ID + Offset >= StaticDiagInfoSize)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700142 return nullptr;
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000143
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000144 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
145
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000146 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
147 // If the diag id doesn't match we found a different diag, abort. This can
148 // happen when this function is called with an ID that points into a hole in
149 // the diagID space.
150 if (Found->DiagID != DiagID)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700151 return nullptr;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000152 return Found;
153}
154
Daniel Dunbara5e41332011-09-29 01:52:06 +0000155static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000156 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000157 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000158
Daniel Dunbara5e41332011-09-29 01:52:06 +0000159 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
160 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
161
162 if (StaticInfo->WarnNoWerror) {
163 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000164 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000165 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000166 }
167
Daniel Dunbara5e41332011-09-29 01:52:06 +0000168 if (StaticInfo->WarnShowInSystemHeader) {
169 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000170 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000171 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000172 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000173 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000174
175 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000176}
177
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000178/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000179/// DiagID belongs to, or 0 if no category.
180unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
181 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
182 return Info->Category;
183 return 0;
184}
185
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000186namespace {
187 // The diagnostic category names.
188 struct StaticDiagCategoryRec {
189 const char *NameStr;
190 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000191
Chris Lattner5f9e2722011-07-23 10:55:15 +0000192 StringRef getName() const {
193 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000194 }
195 };
196}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000197
Daniel Dunbarba494c62011-09-29 01:42:25 +0000198// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
199// particularly clean, but for now we just implement this method here so we can
200// access GetDefaultDiagMapping.
201DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
202 diag::kind Diag)
203{
204 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000205 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000206
207 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000208 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000209 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000210
211 return Result.first->second;
212}
213
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000214static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000215#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000216#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000217#include "clang/Basic/DiagnosticGroups.inc"
218#undef GET_CATEGORY_TABLE
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700219 { nullptr, 0 }
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000220};
221
222/// getNumberOfCategories - Return the number of categories
223unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Topperb9602322013-07-15 03:38:40 +0000224 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000225}
226
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000227/// getCategoryNameFromID - Given a category ID, return the name of the
228/// category, an empty string if CategoryID is zero, or null if CategoryID is
229/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000230StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000231 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000232 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000233 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000234}
235
236
237
Richard Smith3347b492013-11-12 02:41:45 +0000238DiagnosticIDs::SFINAEResponse
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000239DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
Richard Smith3347b492013-11-12 02:41:45 +0000240 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
241 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000242 return SFINAE_Report;
243}
244
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000245/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000246///
247static unsigned getBuiltinDiagClass(unsigned DiagID) {
248 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
249 return Info->Class;
250 return ~0U;
251}
252
253//===----------------------------------------------------------------------===//
254// Custom Diagnostic information
255//===----------------------------------------------------------------------===//
256
257namespace clang {
258 namespace diag {
259 class CustomDiagInfo {
260 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
261 std::vector<DiagDesc> DiagInfo;
262 std::map<DiagDesc, unsigned> DiagIDs;
263 public:
264
265 /// getDescription - Return the description of the specified custom
266 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000268 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000269 "Invalid diagnostic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000270 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000271 }
272
273 /// getLevel - Return the level of the specified custom diagnostic.
274 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
275 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000276 "Invalid diagnostic ID");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000277 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
278 }
279
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000281 DiagnosticIDs &Diags) {
282 DiagDesc D(L, Message);
283 // Check to see if it already exists.
284 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
285 if (I != DiagIDs.end() && I->first == D)
286 return I->second;
287
288 // If not, assign a new ID.
289 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
290 DiagIDs.insert(std::make_pair(D, ID));
291 DiagInfo.push_back(D);
292 return ID;
293 }
294 };
295
296 } // end diag namespace
297} // end clang namespace
298
299
300//===----------------------------------------------------------------------===//
301// Common Diagnostic implementation
302//===----------------------------------------------------------------------===//
303
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700304DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000305
306DiagnosticIDs::~DiagnosticIDs() {
307 delete CustomDiagInfo;
308}
309
310/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000311/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000312/// registered and created, otherwise the existing ID is returned.
Stephen Hines651f13c2014-04-23 16:59:28 -0700313///
314/// \param FormatString A fixed diagnostic format string that will be hashed and
315/// mapped to a unique DiagID.
316unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700317 if (!CustomDiagInfo)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000318 CustomDiagInfo = new diag::CustomDiagInfo();
Stephen Hines651f13c2014-04-23 16:59:28 -0700319 return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000320}
321
322
323/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
324/// level of the specified diagnostic ID is a Warning or Extension.
325/// This only works on builtin diagnostics, not custom ones, and is not legal to
326/// call on NOTEs.
327bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
328 return DiagID < diag::DIAG_UPPER_LIMIT &&
329 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
330}
331
332/// \brief Determine whether the given built-in diagnostic ID is a
333/// Note.
334bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
335 return DiagID < diag::DIAG_UPPER_LIMIT &&
336 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
337}
338
339/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
340/// ID is for an extension of some sort. This also returns EnabledByDefault,
341/// which is set to indicate whether the diagnostic is ignored by default (in
342/// which case -pedantic enables it) or treated as a warning/error by default.
343///
344bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
345 bool &EnabledByDefault) {
346 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
347 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
348 return false;
349
Daniel Dunbara5e41332011-09-29 01:52:06 +0000350 EnabledByDefault =
351 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000352 return true;
353}
354
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000355bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
356 if (DiagID >= diag::DIAG_UPPER_LIMIT)
357 return false;
358
Daniel Dunbara5e41332011-09-29 01:52:06 +0000359 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000360}
361
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700362bool DiagnosticIDs::isRemark(unsigned DiagID) {
363 return DiagID < diag::DIAG_UPPER_LIMIT &&
364 getBuiltinDiagClass(DiagID) == CLASS_REMARK;
365}
366
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000367/// getDescription - Given a diagnostic ID, return a description of the
368/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000369StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000370 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000371 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000372 return CustomDiagInfo->getDescription(DiagID);
373}
374
David Blaikied6471f72011-09-25 23:23:43 +0000375/// getDiagnosticLevel - Based on the way the client configured the
376/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
377/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000378DiagnosticIDs::Level
379DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000380 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000381 // Handle custom diagnostics, which cannot be mapped.
382 if (DiagID >= diag::DIAG_UPPER_LIMIT)
383 return CustomDiagInfo->getLevel(DiagID);
384
385 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000386 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000387 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000388}
389
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000390/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000391/// object, classify the specified diagnostic ID into a Level, consumable by
392/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000393///
394/// \param Loc The source location we are interested in finding out the
395/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000396DiagnosticIDs::Level
397DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000398 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000399 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000400 // Specific non-error diagnostics may be mapped to various levels from ignored
401 // to error. Errors can only be mapped to fatal.
402 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
403
David Blaikied6471f72011-09-25 23:23:43 +0000404 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000405 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000406 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000407
Daniel Dunbarba494c62011-09-29 01:42:25 +0000408 // Get the mapping information, or compute it lazily.
409 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
410 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000411
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000412 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000413 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000414 Result = DiagnosticIDs::Ignored;
415 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700416 case diag::MAP_REMARK:
417 Result = DiagnosticIDs::Remark;
418 break;
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000419 case diag::MAP_WARNING:
420 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000421 break;
422 case diag::MAP_ERROR:
423 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000424 break;
425 case diag::MAP_FATAL:
426 Result = DiagnosticIDs::Fatal;
427 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000428 }
429
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000430 // Upgrade ignored diagnostics if -Weverything is enabled.
431 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
432 !MappingInfo.isUser())
433 Result = DiagnosticIDs::Warning;
434
Stephen Hines651f13c2014-04-23 16:59:28 -0700435 // Diagnostics of class REMARK are either printed as remarks or in case they
436 // have been added to -Werror they are printed as errors.
437 if (DiagClass == CLASS_REMARK && Result == DiagnosticIDs::Warning)
438 Result = DiagnosticIDs::Remark;
439
Bob Wilson18c407f2011-10-12 19:55:31 +0000440 // Ignore -pedantic diagnostics inside __extension__ blocks.
441 // (The diagnostics controlled by -pedantic are the extension diagnostics
442 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000443 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000444 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
445 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000446 return DiagnosticIDs::Ignored;
447
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000448 // For extension diagnostics that haven't been explicitly mapped, check if we
449 // should upgrade the diagnostic.
450 if (IsExtensionDiag && !MappingInfo.isUser()) {
451 switch (Diag.ExtBehavior) {
452 case DiagnosticsEngine::Ext_Ignore:
453 break;
454 case DiagnosticsEngine::Ext_Warn:
455 // Upgrade ignored diagnostics to warnings.
456 if (Result == DiagnosticIDs::Ignored)
457 Result = DiagnosticIDs::Warning;
458 break;
459 case DiagnosticsEngine::Ext_Error:
460 // Upgrade ignored or warning diagnostics to errors.
461 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
462 Result = DiagnosticIDs::Error;
463 break;
464 }
465 }
466
467 // At this point, ignored errors can no longer be upgraded.
468 if (Result == DiagnosticIDs::Ignored)
469 return Result;
470
471 // Honor -w, which is lower in priority than pedantic-errors, but higher than
472 // -Werror.
473 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
474 return DiagnosticIDs::Ignored;
475
476 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
477 if (Result == DiagnosticIDs::Warning) {
478 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
479 Result = DiagnosticIDs::Error;
480 }
481
482 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
483 // disabled.
484 if (Result == DiagnosticIDs::Error) {
485 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
486 Result = DiagnosticIDs::Fatal;
487 }
488
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000489 // If we are in a system header, we ignore it. We look at the diagnostic class
490 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000491 // -pedantic-errors modes, which *map* warnings/extensions to errors.
492 if (Result >= DiagnosticIDs::Warning &&
493 DiagClass != CLASS_ERROR &&
494 // Custom diagnostics always are emitted in system headers.
495 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000496 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000497 Diag.SuppressSystemWarnings &&
498 Loc.isValid() &&
499 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000500 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000501 return DiagnosticIDs::Ignored;
502
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000503 return Result;
504}
505
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000506#define GET_DIAG_ARRAYS
507#include "clang/Basic/DiagnosticGroups.inc"
508#undef GET_DIAG_ARRAYS
509
Craig Topper46c8fca2013-08-29 06:06:18 +0000510namespace {
511 struct WarningOption {
512 uint16_t NameOffset;
513 uint16_t Members;
514 uint16_t SubGroups;
Craig Topper354f20a2013-08-29 05:18:04 +0000515
Craig Topper46c8fca2013-08-29 06:06:18 +0000516 // String is stored with a pascal-style length byte.
517 StringRef getName() const {
518 return StringRef(DiagGroupNames + NameOffset + 1,
519 DiagGroupNames[NameOffset]);
520 }
521 };
522}
Craig Topper354f20a2013-08-29 05:18:04 +0000523
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000524// Second the table of options, sorted by name for fast binary lookup.
525static const WarningOption OptionTable[] = {
526#define GET_DIAG_TABLE
527#include "clang/Basic/DiagnosticGroups.inc"
528#undef GET_DIAG_TABLE
529};
Craig Topperb9602322013-07-15 03:38:40 +0000530static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000531
Craig Topper354f20a2013-08-29 05:18:04 +0000532static bool WarningOptionCompare(const WarningOption &LHS, StringRef RHS) {
533 return LHS.getName() < RHS;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000534}
535
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000536/// getWarningOptionForDiag - Return the lowest-level warning option that
537/// enables the specified diagnostic. If there is no -Wfoo flag that controls
538/// the diagnostic, this returns null.
539StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
540 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
541 return OptionTable[Info->getOptionGroupIndex()].getName();
542 return StringRef();
543}
544
Craig Topper46c8fca2013-08-29 06:06:18 +0000545static void getDiagnosticsInGroup(const WarningOption *Group,
546 SmallVectorImpl<diag::kind> &Diags) {
Daniel Dunbar3f839462011-09-29 01:47:16 +0000547 // Add the members of the option diagnostic set.
Craig Topperb1aa16a2013-08-28 04:02:50 +0000548 const int16_t *Member = DiagArrays + Group->Members;
549 for (; *Member != -1; ++Member)
550 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000551
Daniel Dunbar3f839462011-09-29 01:47:16 +0000552 // Add the members of the subgroups.
Craig Topperb1aa16a2013-08-28 04:02:50 +0000553 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
554 for (; *SubGroups != (int16_t)-1; ++SubGroups)
555 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000556}
557
Daniel Dunbar3f839462011-09-29 01:47:16 +0000558bool DiagnosticIDs::getDiagnosticsInGroup(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000559 StringRef Group,
560 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000561 const WarningOption *Found =
Craig Topper354f20a2013-08-29 05:18:04 +0000562 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Group,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000563 WarningOptionCompare);
564 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000565 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000566 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000567
Craig Topper46c8fca2013-08-29 06:06:18 +0000568 ::getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000569 return false;
570}
571
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000572void DiagnosticIDs::getAllDiagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000573 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000574 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
575 Diags.push_back(StaticDiagInfo[i].DiagID);
576}
577
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000578StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
579 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000580 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000581 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
582 i != e; ++i) {
583 // Don't suggest ignored warning flags.
584 if (!i->Members && !i->SubGroups)
585 continue;
586
587 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000588 if (Distance == BestDistance) {
589 // Two matches with the same distance, don't prefer one over the other.
590 Best = "";
591 } else if (Distance < BestDistance) {
592 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000593 Best = i->getName();
594 BestDistance = Distance;
595 }
596 }
597
598 return Best;
599}
600
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000601/// ProcessDiag - This is the method used to report a diagnostic that is
602/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000603bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000604 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000605
606 if (Diag.SuppressAllDiagnostics)
607 return false;
608
609 assert(Diag.getClient() && "DiagnosticClient not set!");
610
611 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000612 unsigned DiagID = Info.getID();
Jordan Rosec6d64a22012-07-11 16:50:36 +0000613 DiagnosticIDs::Level DiagLevel
614 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000615
616 if (DiagLevel != DiagnosticIDs::Note) {
617 // Record that a fatal error occurred only when we see a second
618 // non-note diagnostic. This allows notes to be attached to the
619 // fatal error, but suppresses any diagnostics that follow those
620 // notes.
621 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
622 Diag.FatalErrorOccurred = true;
623
624 Diag.LastDiagLevel = DiagLevel;
625 }
626
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000627 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
628 if (DiagLevel >= DiagnosticIDs::Error) {
629 ++Diag.TrapNumErrorsOccurred;
630 if (isUnrecoverable(DiagID))
631 ++Diag.TrapNumUnrecoverableErrorsOccurred;
632 }
633
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000634 // If a fatal error has already been emitted, silence all subsequent
635 // diagnostics.
636 if (Diag.FatalErrorOccurred) {
637 if (DiagLevel >= DiagnosticIDs::Error &&
638 Diag.Client->IncludeInDiagnosticCounts()) {
639 ++Diag.NumErrors;
640 ++Diag.NumErrorsSuppressed;
641 }
642
643 return false;
644 }
645
646 // If the client doesn't care about this message, don't issue it. If this is
647 // a note and the last real diagnostic was ignored, ignore it too.
648 if (DiagLevel == DiagnosticIDs::Ignored ||
649 (DiagLevel == DiagnosticIDs::Note &&
650 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
651 return false;
652
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000653 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000654 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000655 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasper1c84c682012-09-28 15:45:07 +0000656
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000657 // Warnings which have been upgraded to errors do not prevent compilation.
658 if (isDefaultMappingAsError(DiagID))
659 Diag.UncompilableErrorOccurred = true;
660
Daniel Jasper1c84c682012-09-28 15:45:07 +0000661 Diag.ErrorOccurred = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000662 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000663 ++Diag.NumErrors;
664 }
665
Douglas Gregorf1d59482011-08-17 19:13:00 +0000666 // If we've emitted a lot of errors, emit a fatal error instead of it to
667 // stop a flood of bogus errors.
668 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
669 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000670 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000671 return false;
672 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000673 }
674
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000675 // Finally, report it.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000676 EmitDiag(Diag, DiagLevel);
677 return true;
678}
679
680void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
681 Diagnostic Info(&Diag);
682 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
683
David Blaikied6471f72011-09-25 23:23:43 +0000684 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000685 if (Diag.Client->IncludeInDiagnosticCounts()) {
686 if (DiagLevel == DiagnosticIDs::Warning)
687 ++Diag.NumWarnings;
688 }
689
690 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000691}
John McCall923cd572011-06-15 21:46:43 +0000692
693bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
694 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
695 // Custom diagnostics.
696 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
697 }
698
699 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000700 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000701 return false;
702
703 if (DiagID == diag::err_unavailable ||
704 DiagID == diag::err_unavailable_message)
705 return false;
706
John McCallf85e1932011-06-15 23:02:42 +0000707 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000708 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000709 return false;
710
John McCall923cd572011-06-15 21:46:43 +0000711 return true;
712}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000713
714bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
715 unsigned cat = getCategoryNumberForDiag(DiagID);
716 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
717}