blob: 697de68a5afb1560949cb359c80baf4f283722cc [file] [log] [blame]
Argyrios Kyrtzidisd0040642010-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 Kyrtzidisd0040642010-11-18 20:06:41 +000014#include "clang/Basic/DiagnosticIDs.h"
David Blaikieaf90ec12012-02-15 21:58:34 +000015#include "clang/Basic/AllDiagnostics.h"
John McCalla877d922011-06-15 21:46:43 +000016#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000017#include "clang/Basic/SourceManager.h"
David Majnemer38af2a22013-07-20 07:15:15 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000019#include "llvm/ADT/SmallVector.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidisd0040642010-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,
Tobias Grosser74160242014-02-28 09:11:08 +000033 CLASS_REMARK = 0x02,
34 CLASS_WARNING = 0x03,
35 CLASS_EXTENSION = 0x04,
36 CLASS_ERROR = 0x05
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000037};
38
39struct StaticDiagInfoRec {
Craig Topperea6caba2013-07-21 21:56:18 +000040 uint16_t DiagID;
Alp Tokerc726c362014-06-10 09:31:37 +000041 unsigned DefaultSeverity : 3;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000042 unsigned Class : 3;
Richard Smith16e1b072013-11-12 02:41:45 +000043 unsigned SFINAE : 2;
Daniel Dunbar6de48ee2011-09-29 00:34:06 +000044 unsigned WarnNoWerror : 1;
45 unsigned WarnShowInSystemHeader : 1;
Alex Lorenzf5ca27c2017-10-16 18:28:26 +000046 unsigned Category : 6;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000047
Benjamin Kramera8cafe22012-02-15 20:57:03 +000048 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000049
50 uint16_t DescriptionLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000051 const char *DescriptionStr;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000052
Benjamin Kramera8cafe22012-02-15 20:57:03 +000053 unsigned getOptionGroupIndex() const {
54 return OptionGroupIndex;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000055 }
56
Chris Lattner0e62c1c2011-07-23 10:55:15 +000057 StringRef getDescription() const {
58 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000059 }
Douglas Gregor46ce91a2011-04-15 22:04:17 +000060
Richard Smith3be1cb22014-08-07 00:24:21 +000061 diag::Flavor getFlavor() const {
62 return Class == CLASS_REMARK ? diag::Flavor::Remark
63 : diag::Flavor::WarningOrError;
64 }
65
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000066 bool operator<(const StaticDiagInfoRec &RHS) const {
67 return DiagID < RHS.DiagID;
68 }
69};
70
Erich Keane0a539b52017-08-28 18:53:17 +000071#define STRINGIFY_NAME(NAME) #NAME
72#define VALIDATE_DIAG_SIZE(NAME) \
73 static_assert( \
74 static_cast<unsigned>(diag::NUM_BUILTIN_##NAME##_DIAGNOSTICS) < \
75 static_cast<unsigned>(diag::DIAG_START_##NAME) + \
76 static_cast<unsigned>(diag::DIAG_SIZE_##NAME), \
77 STRINGIFY_NAME( \
78 DIAG_SIZE_##NAME) " is insufficient to contain all " \
79 "diagnostics, it may need to be made larger in " \
80 "DiagnosticIDs.h.");
81VALIDATE_DIAG_SIZE(COMMON)
82VALIDATE_DIAG_SIZE(DRIVER)
83VALIDATE_DIAG_SIZE(FRONTEND)
84VALIDATE_DIAG_SIZE(SERIALIZATION)
85VALIDATE_DIAG_SIZE(LEX)
86VALIDATE_DIAG_SIZE(PARSE)
87VALIDATE_DIAG_SIZE(AST)
88VALIDATE_DIAG_SIZE(COMMENT)
89VALIDATE_DIAG_SIZE(SEMA)
90VALIDATE_DIAG_SIZE(ANALYSIS)
Alex Lorenzf5ca27c2017-10-16 18:28:26 +000091VALIDATE_DIAG_SIZE(REFACTORING)
Erich Keane0a539b52017-08-28 18:53:17 +000092#undef VALIDATE_DIAG_SIZE
93#undef STRINGIFY_NAME
94
Alexander Kornienkoab9db512015-06-22 23:07:51 +000095} // namespace anonymous
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000096
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000097static const StaticDiagInfoRec StaticDiagInfo[] = {
Alp Tokerc726c362014-06-10 09:31:37 +000098#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
99 SHOWINSYSHEADER, CATEGORY) \
100 { \
101 diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, NOWERROR, \
102 SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), DESC \
103 } \
104 ,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000105#include "clang/Basic/DiagnosticCommonKinds.inc"
106#include "clang/Basic/DiagnosticDriverKinds.inc"
107#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carruth22a11b72011-12-09 00:02:23 +0000108#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000109#include "clang/Basic/DiagnosticLexKinds.inc"
110#include "clang/Basic/DiagnosticParseKinds.inc"
111#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000112#include "clang/Basic/DiagnosticCommentKinds.inc"
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000113#include "clang/Basic/DiagnosticCrossTUKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000114#include "clang/Basic/DiagnosticSemaKinds.inc"
115#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Alex Lorenzf5ca27c2017-10-16 18:28:26 +0000116#include "clang/Basic/DiagnosticRefactoringKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000117#undef DIAG
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000118};
119
David Majnemer38af2a22013-07-20 07:15:15 +0000120static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000121
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000122/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
123/// or null if the ID is invalid.
124static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000125 // Out of bounds diag. Can't be in the table.
126 using namespace diag;
David Majnemer38af2a22013-07-20 07:15:15 +0000127 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Craig Topperf1186c52014-05-08 06:41:40 +0000128 return nullptr;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000129
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000130 // Compute the index of the requested diagnostic in the static table.
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000131 // 1. Add the number of diagnostics in each category preceding the
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000132 // diagnostic and of the category the diagnostic is in. This gives us
133 // the offset of the category in the table.
134 // 2. Subtract the number of IDs in each category from our ID. This gives us
135 // the offset of the diagnostic in the category.
136 // This is cheaper than a binary search on the table as it doesn't touch
137 // memory at all.
138 unsigned Offset = 0;
David Majnemer38af2a22013-07-20 07:15:15 +0000139 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000140#define CATEGORY(NAME, PREV) \
141 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000142 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
143 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000144 }
145CATEGORY(DRIVER, COMMON)
146CATEGORY(FRONTEND, DRIVER)
147CATEGORY(SERIALIZATION, FRONTEND)
148CATEGORY(LEX, SERIALIZATION)
149CATEGORY(PARSE, LEX)
150CATEGORY(AST, PARSE)
151CATEGORY(COMMENT, AST)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000152CATEGORY(CROSSTU, COMMENT)
153CATEGORY(SEMA, CROSSTU)
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000154CATEGORY(ANALYSIS, SEMA)
Alex Lorenzf5ca27c2017-10-16 18:28:26 +0000155CATEGORY(REFACTORING, ANALYSIS)
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000156#undef CATEGORY
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000157
158 // Avoid out of bounds reads.
159 if (ID + Offset >= StaticDiagInfoSize)
Craig Topperf1186c52014-05-08 06:41:40 +0000160 return nullptr;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000161
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000162 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
163
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000164 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
165 // If the diag id doesn't match we found a different diag, abort. This can
166 // happen when this function is called with an ID that points into a hole in
167 // the diagID space.
168 if (Found->DiagID != DiagID)
Craig Topperf1186c52014-05-08 06:41:40 +0000169 return nullptr;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000170 return Found;
171}
172
Alp Tokerc726c362014-06-10 09:31:37 +0000173static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) {
174 DiagnosticMapping Info = DiagnosticMapping::Make(
Alp Toker46df1c02014-06-12 10:15:20 +0000175 diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000176
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000177 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
Alp Tokerc726c362014-06-10 09:31:37 +0000178 Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000179
180 if (StaticInfo->WarnNoWerror) {
Alp Toker46df1c02014-06-12 10:15:20 +0000181 assert(Info.getSeverity() == diag::Severity::Warning &&
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000182 "Unexpected mapping with no-Werror bit!");
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000183 Info.setNoWarningAsError(true);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000184 }
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000185 }
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000186
187 return Info;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000188}
189
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000190/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000191/// DiagID belongs to, or 0 if no category.
192unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
193 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
194 return Info->Category;
195 return 0;
196}
197
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000198namespace {
199 // The diagnostic category names.
200 struct StaticDiagCategoryRec {
201 const char *NameStr;
202 uint8_t NameLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000203
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000204 StringRef getName() const {
205 return StringRef(NameStr, NameLen);
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000206 }
207 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000208}
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000209
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000210// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
211// particularly clean, but for now we just implement this method here so we can
212// access GetDefaultDiagMapping.
Alp Tokerc726c362014-06-10 09:31:37 +0000213DiagnosticMapping &
214DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
215 std::pair<iterator, bool> Result =
216 DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000217
218 // Initialize the entry if we added it.
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000219 if (Result.second)
Alp Tokerc726c362014-06-10 09:31:37 +0000220 Result.first->second = GetDefaultDiagMapping(Diag);
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000221
222 return Result.first->second;
223}
224
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000225static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000226#define GET_CATEGORY_TABLE
John McCalla877d922011-06-15 21:46:43 +0000227#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000228#include "clang/Basic/DiagnosticGroups.inc"
229#undef GET_CATEGORY_TABLE
Craig Topperf1186c52014-05-08 06:41:40 +0000230 { nullptr, 0 }
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000231};
232
233/// getNumberOfCategories - Return the number of categories
234unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Toppere5ce8312013-07-15 03:38:40 +0000235 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000236}
237
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000238/// getCategoryNameFromID - Given a category ID, return the name of the
239/// category, an empty string if CategoryID is zero, or null if CategoryID is
240/// invalid.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000241StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000242 if (CategoryID >= getNumberOfCategories())
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000243 return StringRef();
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000244 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000245}
246
247
248
Richard Smith16e1b072013-11-12 02:41:45 +0000249DiagnosticIDs::SFINAEResponse
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000250DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
Richard Smith16e1b072013-11-12 02:41:45 +0000251 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
252 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000253 return SFINAE_Report;
254}
255
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000256/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000257///
258static unsigned getBuiltinDiagClass(unsigned DiagID) {
259 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
260 return Info->Class;
261 return ~0U;
262}
263
264//===----------------------------------------------------------------------===//
265// Custom Diagnostic information
266//===----------------------------------------------------------------------===//
267
268namespace clang {
269 namespace diag {
270 class CustomDiagInfo {
271 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
272 std::vector<DiagDesc> DiagInfo;
273 std::map<DiagDesc, unsigned> DiagIDs;
274 public:
275
276 /// getDescription - Return the description of the specified custom
277 /// diagnostic.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000278 StringRef getDescription(unsigned DiagID) const {
Richard Trieu428058f2014-08-01 01:42:01 +0000279 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000280 "Invalid diagnostic ID");
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000281 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000282 }
283
284 /// getLevel - Return the level of the specified custom diagnostic.
285 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
Richard Trieu428058f2014-08-01 01:42:01 +0000286 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000287 "Invalid diagnostic ID");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000288 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
289 }
290
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000292 DiagnosticIDs &Diags) {
293 DiagDesc D(L, Message);
294 // Check to see if it already exists.
295 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
296 if (I != DiagIDs.end() && I->first == D)
297 return I->second;
298
299 // If not, assign a new ID.
300 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
301 DiagIDs.insert(std::make_pair(D, ID));
302 DiagInfo.push_back(D);
303 return ID;
304 }
305 };
306
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000307 } // end diag namespace
308} // end clang namespace
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000309
310
311//===----------------------------------------------------------------------===//
312// Common Diagnostic implementation
313//===----------------------------------------------------------------------===//
314
Craig Topperf1186c52014-05-08 06:41:40 +0000315DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000316
317DiagnosticIDs::~DiagnosticIDs() {
318 delete CustomDiagInfo;
319}
320
321/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000322/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000323/// registered and created, otherwise the existing ID is returned.
Alp Toker61d41af2013-12-23 21:00:35 +0000324///
Alp Toker9e6d27d2014-01-26 06:41:58 +0000325/// \param FormatString A fixed diagnostic format string that will be hashed and
Alp Toker61d41af2013-12-23 21:00:35 +0000326/// mapped to a unique DiagID.
Alp Toker9e6d27d2014-01-26 06:41:58 +0000327unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
Craig Topperf1186c52014-05-08 06:41:40 +0000328 if (!CustomDiagInfo)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000329 CustomDiagInfo = new diag::CustomDiagInfo();
Alp Toker9e6d27d2014-01-26 06:41:58 +0000330 return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000331}
332
333
334/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
335/// level of the specified diagnostic ID is a Warning or Extension.
336/// This only works on builtin diagnostics, not custom ones, and is not legal to
337/// call on NOTEs.
338bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
339 return DiagID < diag::DIAG_UPPER_LIMIT &&
340 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
341}
342
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000343/// Determine whether the given built-in diagnostic ID is a
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000344/// Note.
345bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
346 return DiagID < diag::DIAG_UPPER_LIMIT &&
347 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
348}
349
350/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
351/// ID is for an extension of some sort. This also returns EnabledByDefault,
352/// which is set to indicate whether the diagnostic is ignored by default (in
353/// which case -pedantic enables it) or treated as a warning/error by default.
354///
355bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
356 bool &EnabledByDefault) {
357 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
358 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
359 return false;
Alp Tokerc726c362014-06-10 09:31:37 +0000360
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000361 EnabledByDefault =
Alp Toker46df1c02014-06-12 10:15:20 +0000362 GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000363 return true;
364}
365
Daniel Dunbaraa111382011-09-29 01:01:08 +0000366bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
367 if (DiagID >= diag::DIAG_UPPER_LIMIT)
368 return false;
369
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +0000370 return GetDefaultDiagMapping(DiagID).getSeverity() >= diag::Severity::Error;
Daniel Dunbaraa111382011-09-29 01:01:08 +0000371}
372
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000373/// getDescription - Given a diagnostic ID, return a description of the
374/// issue.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000375StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000376 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000377 return Info->getDescription();
Richard Trieu428058f2014-08-01 01:42:01 +0000378 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000379 return CustomDiagInfo->getDescription(DiagID);
380}
381
Alp Toker46df1c02014-06-12 10:15:20 +0000382static DiagnosticIDs::Level toLevel(diag::Severity SV) {
383 switch (SV) {
384 case diag::Severity::Ignored:
385 return DiagnosticIDs::Ignored;
386 case diag::Severity::Remark:
387 return DiagnosticIDs::Remark;
388 case diag::Severity::Warning:
389 return DiagnosticIDs::Warning;
390 case diag::Severity::Error:
391 return DiagnosticIDs::Error;
392 case diag::Severity::Fatal:
393 return DiagnosticIDs::Fatal;
394 }
Saleem Abdulrasoolfbfbaf62014-06-12 19:33:26 +0000395 llvm_unreachable("unexpected severity");
Alp Toker46df1c02014-06-12 10:15:20 +0000396}
397
David Blaikie9c902b52011-09-25 23:23:43 +0000398/// getDiagnosticLevel - Based on the way the client configured the
399/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
400/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000401DiagnosticIDs::Level
402DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar400e7e32011-09-29 01:20:28 +0000403 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000404 // Handle custom diagnostics, which cannot be mapped.
Richard Trieu428058f2014-08-01 01:42:01 +0000405 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
406 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000407 return CustomDiagInfo->getLevel(DiagID);
Richard Trieu428058f2014-08-01 01:42:01 +0000408 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000409
410 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rose6f524ac2012-07-11 16:50:36 +0000411 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Alp Toker04278ec2014-06-16 13:56:47 +0000412 return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000413}
414
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000415/// Based on the way the client configured the Diagnostic
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000416/// object, classify the specified diagnostic ID into a Level, consumable by
417/// the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000418///
419/// \param Loc The source location we are interested in finding out the
420/// diagnostic state. Can be null in order to query the latest state.
Alp Toker46df1c02014-06-12 10:15:20 +0000421diag::Severity
Alp Toker04278ec2014-06-16 13:56:47 +0000422DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
Alp Toker46df1c02014-06-12 10:15:20 +0000423 const DiagnosticsEngine &Diag) const {
Alp Toker04278ec2014-06-16 13:56:47 +0000424 assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE);
Alp Toker46df1c02014-06-12 10:15:20 +0000425
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000426 // Specific non-error diagnostics may be mapped to various levels from ignored
427 // to error. Errors can only be mapped to fatal.
Alp Toker46df1c02014-06-12 10:15:20 +0000428 diag::Severity Result = diag::Severity::Fatal;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000429
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000430 // Get the mapping information, or compute it lazily.
Richard Smithd230de22017-01-26 01:01:01 +0000431 DiagnosticsEngine::DiagState *State = Diag.GetDiagStateForLoc(Loc);
Alp Tokerc726c362014-06-10 09:31:37 +0000432 DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000433
Alp Toker46df1c02014-06-12 10:15:20 +0000434 // TODO: Can a null severity really get here?
435 if (Mapping.getSeverity() != diag::Severity())
436 Result = Mapping.getSeverity();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000437
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000438 // Upgrade ignored diagnostics if -Weverything is enabled.
Richard Smithe37391c2017-05-03 00:28:49 +0000439 if (State->EnableAllWarnings && Result == diag::Severity::Ignored &&
Richard Smithe423ed32014-08-21 20:44:44 +0000440 !Mapping.isUser() && getBuiltinDiagClass(DiagID) != CLASS_REMARK)
Alp Toker46df1c02014-06-12 10:15:20 +0000441 Result = diag::Severity::Warning;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000442
Bob Wilson73a4deb2011-10-12 19:55:31 +0000443 // Ignore -pedantic diagnostics inside __extension__ blocks.
444 // (The diagnostics controlled by -pedantic are the extension diagnostics
445 // that are not enabled by default.)
Daniel Dunbar787032b2011-11-28 22:19:36 +0000446 bool EnabledByDefault = false;
Bob Wilson73a4deb2011-10-12 19:55:31 +0000447 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
448 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Alp Toker46df1c02014-06-12 10:15:20 +0000449 return diag::Severity::Ignored;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000450
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000451 // For extension diagnostics that haven't been explicitly mapped, check if we
452 // should upgrade the diagnostic.
Alp Tokerac4e8e52014-06-22 21:58:33 +0000453 if (IsExtensionDiag && !Mapping.isUser())
Richard Smithe37391c2017-05-03 00:28:49 +0000454 Result = std::max(Result, State->ExtBehavior);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000455
456 // At this point, ignored errors can no longer be upgraded.
Alp Toker46df1c02014-06-12 10:15:20 +0000457 if (Result == diag::Severity::Ignored)
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000458 return Result;
459
460 // Honor -w, which is lower in priority than pedantic-errors, but higher than
461 // -Werror.
Richard Smithe37391c2017-05-03 00:28:49 +0000462 // FIXME: Under GCC, this also suppresses warnings that have been mapped to
463 // errors by -W flags and #pragma diagnostic.
464 if (Result == diag::Severity::Warning && State->IgnoreAllWarnings)
Alp Toker46df1c02014-06-12 10:15:20 +0000465 return diag::Severity::Ignored;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000466
467 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
Alp Toker46df1c02014-06-12 10:15:20 +0000468 if (Result == diag::Severity::Warning) {
Richard Smithe37391c2017-05-03 00:28:49 +0000469 if (State->WarningsAsErrors && !Mapping.hasNoWarningAsError())
Alp Toker46df1c02014-06-12 10:15:20 +0000470 Result = diag::Severity::Error;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000471 }
472
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000473 // If -Wfatal-errors is enabled, map errors to fatal unless explicitly
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000474 // disabled.
Alp Toker46df1c02014-06-12 10:15:20 +0000475 if (Result == diag::Severity::Error) {
Richard Smithe37391c2017-05-03 00:28:49 +0000476 if (State->ErrorsAsFatal && !Mapping.hasNoErrorAsFatal())
Alp Toker46df1c02014-06-12 10:15:20 +0000477 Result = diag::Severity::Fatal;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000478 }
479
Alp Tokered2c0332014-06-10 06:09:00 +0000480 // Custom diagnostics always are emitted in system headers.
481 bool ShowInSystemHeader =
482 !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
483
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000484 // If we are in a system header, we ignore it. We look at the diagnostic class
485 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000486 // -pedantic-errors modes, which *map* warnings/extensions to errors.
Richard Smithe37391c2017-05-03 00:28:49 +0000487 if (State->SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000488 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth35f53202011-07-25 16:49:02 +0000489 Diag.getSourceManager().getExpansionLoc(Loc)))
Alp Toker46df1c02014-06-12 10:15:20 +0000490 return diag::Severity::Ignored;
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000491
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000492 return Result;
493}
494
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000495#define GET_DIAG_ARRAYS
496#include "clang/Basic/DiagnosticGroups.inc"
497#undef GET_DIAG_ARRAYS
498
Craig Topperb78e9d92013-08-29 06:06:18 +0000499namespace {
500 struct WarningOption {
501 uint16_t NameOffset;
502 uint16_t Members;
503 uint16_t SubGroups;
Craig Topperda7cf8a2013-08-29 05:18:04 +0000504
Craig Topperb78e9d92013-08-29 06:06:18 +0000505 // String is stored with a pascal-style length byte.
506 StringRef getName() const {
507 return StringRef(DiagGroupNames + NameOffset + 1,
508 DiagGroupNames[NameOffset]);
509 }
510 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000511}
Craig Topperda7cf8a2013-08-29 05:18:04 +0000512
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000513// Second the table of options, sorted by name for fast binary lookup.
514static const WarningOption OptionTable[] = {
515#define GET_DIAG_TABLE
516#include "clang/Basic/DiagnosticGroups.inc"
517#undef GET_DIAG_TABLE
518};
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000519
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000520/// getWarningOptionForDiag - Return the lowest-level warning option that
521/// enables the specified diagnostic. If there is no -Wfoo flag that controls
522/// the diagnostic, this returns null.
523StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
524 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
525 return OptionTable[Info->getOptionGroupIndex()].getName();
526 return StringRef();
527}
528
Yuka Takahashi64918d02017-07-16 15:07:20 +0000529std::vector<std::string> DiagnosticIDs::getDiagnosticFlags() {
530 std::vector<std::string> Res;
531 for (size_t I = 1; DiagGroupNames[I] != '\0';) {
532 std::string Diag(DiagGroupNames + I + 1, DiagGroupNames[I]);
533 I += DiagGroupNames[I] + 1;
534 Res.push_back("-W" + Diag);
Yuka Takahashib40f4db2017-07-22 12:35:15 +0000535 Res.push_back("-Wno-" + Diag);
Yuka Takahashi64918d02017-07-16 15:07:20 +0000536 }
537
538 return Res;
539}
540
Richard Smith3be1cb22014-08-07 00:24:21 +0000541/// Return \c true if any diagnostics were found in this group, even if they
542/// were filtered out due to having the wrong flavor.
543static bool getDiagnosticsInGroup(diag::Flavor Flavor,
544 const WarningOption *Group,
Craig Topperb78e9d92013-08-29 06:06:18 +0000545 SmallVectorImpl<diag::kind> &Diags) {
Richard Smith3be1cb22014-08-07 00:24:21 +0000546 // An empty group is considered to be a warning group: we have empty groups
547 // for GCC compatibility, and GCC does not have remarks.
548 if (!Group->Members && !Group->SubGroups)
David Blaikie7a3cbb22015-03-09 02:02:07 +0000549 return Flavor == diag::Flavor::Remark;
Richard Smith3be1cb22014-08-07 00:24:21 +0000550
551 bool NotFound = true;
552
Daniel Dunbard908c122011-09-29 01:47:16 +0000553 // Add the members of the option diagnostic set.
Craig Topperd80c17e2013-08-28 04:02:50 +0000554 const int16_t *Member = DiagArrays + Group->Members;
Richard Smith3be1cb22014-08-07 00:24:21 +0000555 for (; *Member != -1; ++Member) {
556 if (GetDiagInfo(*Member)->getFlavor() == Flavor) {
557 NotFound = false;
558 Diags.push_back(*Member);
559 }
560 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000561
Daniel Dunbard908c122011-09-29 01:47:16 +0000562 // Add the members of the subgroups.
Craig Topperd80c17e2013-08-28 04:02:50 +0000563 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
564 for (; *SubGroups != (int16_t)-1; ++SubGroups)
Richard Smith3be1cb22014-08-07 00:24:21 +0000565 NotFound &= getDiagnosticsInGroup(Flavor, &OptionTable[(short)*SubGroups],
566 Diags);
567
568 return NotFound;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000569}
570
Richard Smith3be1cb22014-08-07 00:24:21 +0000571bool
572DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
573 SmallVectorImpl<diag::kind> &Diags) const {
Craig Topper924f6db2015-10-17 20:18:46 +0000574 auto Found = std::lower_bound(std::begin(OptionTable), std::end(OptionTable),
575 Group,
576 [](const WarningOption &LHS, StringRef RHS) {
577 return LHS.getName() < RHS;
578 });
Craig Topperc00e9532015-10-17 17:10:43 +0000579 if (Found == std::end(OptionTable) || Found->getName() != Group)
Daniel Dunbard908c122011-09-29 01:47:16 +0000580 return true; // Option not found.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000581
Richard Smith3be1cb22014-08-07 00:24:21 +0000582 return ::getDiagnosticsInGroup(Flavor, Found, Diags);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000583}
584
Richard Smith3be1cb22014-08-07 00:24:21 +0000585void DiagnosticIDs::getAllDiagnostics(diag::Flavor Flavor,
Gabor Horvath53b5c132017-12-20 16:55:41 +0000586 std::vector<diag::kind> &Diags) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000587 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
Richard Smith3be1cb22014-08-07 00:24:21 +0000588 if (StaticDiagInfo[i].getFlavor() == Flavor)
589 Diags.push_back(StaticDiagInfo[i].DiagID);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000590}
591
Richard Smith3be1cb22014-08-07 00:24:21 +0000592StringRef DiagnosticIDs::getNearestOption(diag::Flavor Flavor,
593 StringRef Group) {
Benjamin Kramer116d8872011-11-14 23:30:34 +0000594 StringRef Best;
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000595 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Craig Toppere353a722015-10-18 05:29:23 +0000596 for (const WarningOption &O : OptionTable) {
Benjamin Kramer116d8872011-11-14 23:30:34 +0000597 // Don't suggest ignored warning flags.
Craig Topperc00e9532015-10-17 17:10:43 +0000598 if (!O.Members && !O.SubGroups)
Benjamin Kramer116d8872011-11-14 23:30:34 +0000599 continue;
600
Craig Topperc00e9532015-10-17 17:10:43 +0000601 unsigned Distance = O.getName().edit_distance(Group, true, BestDistance);
Richard Smith3be1cb22014-08-07 00:24:21 +0000602 if (Distance > BestDistance)
603 continue;
604
605 // Don't suggest groups that are not of this kind.
606 llvm::SmallVector<diag::kind, 8> Diags;
Craig Topperc00e9532015-10-17 17:10:43 +0000607 if (::getDiagnosticsInGroup(Flavor, &O, Diags) || Diags.empty())
Richard Smith3be1cb22014-08-07 00:24:21 +0000608 continue;
609
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000610 if (Distance == BestDistance) {
611 // Two matches with the same distance, don't prefer one over the other.
612 Best = "";
613 } else if (Distance < BestDistance) {
614 // This is a better match.
Craig Topperc00e9532015-10-17 17:10:43 +0000615 Best = O.getName();
Benjamin Kramer116d8872011-11-14 23:30:34 +0000616 BestDistance = Distance;
617 }
618 }
619
620 return Best;
621}
622
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000623/// ProcessDiag - This is the method used to report a diagnostic that is
624/// finally fully formed.
David Blaikie9c902b52011-09-25 23:23:43 +0000625bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikieb5784322011-09-26 01:18:08 +0000626 Diagnostic Info(&Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000627
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000628 assert(Diag.getClient() && "DiagnosticClient not set!");
629
630 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000631 unsigned DiagID = Info.getID();
Jordan Rose6f524ac2012-07-11 16:50:36 +0000632 DiagnosticIDs::Level DiagLevel
633 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000634
Richard Smitha2686712014-12-05 21:52:58 +0000635 // Update counts for DiagnosticErrorTrap even if a fatal error occurred
636 // or diagnostics are suppressed.
637 if (DiagLevel >= DiagnosticIDs::Error) {
638 ++Diag.TrapNumErrorsOccurred;
639 if (isUnrecoverable(DiagID))
640 ++Diag.TrapNumUnrecoverableErrorsOccurred;
641 }
642
643 if (Diag.SuppressAllDiagnostics)
644 return false;
645
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000646 if (DiagLevel != DiagnosticIDs::Note) {
647 // Record that a fatal error occurred only when we see a second
648 // non-note diagnostic. This allows notes to be attached to the
649 // fatal error, but suppresses any diagnostics that follow those
650 // notes.
651 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
652 Diag.FatalErrorOccurred = true;
653
654 Diag.LastDiagLevel = DiagLevel;
655 }
656
657 // If a fatal error has already been emitted, silence all subsequent
658 // diagnostics.
Richard Smithe37391c2017-05-03 00:28:49 +0000659 if (Diag.FatalErrorOccurred && Diag.SuppressAfterFatalError) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000660 if (DiagLevel >= DiagnosticIDs::Error &&
661 Diag.Client->IncludeInDiagnosticCounts()) {
662 ++Diag.NumErrors;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000663 }
664
665 return false;
666 }
667
668 // If the client doesn't care about this message, don't issue it. If this is
669 // a note and the last real diagnostic was ignored, ignore it too.
670 if (DiagLevel == DiagnosticIDs::Ignored ||
671 (DiagLevel == DiagnosticIDs::Note &&
672 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
673 return false;
674
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000675 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000676 if (isUnrecoverable(DiagID))
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000677 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000678
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000679 // Warnings which have been upgraded to errors do not prevent compilation.
680 if (isDefaultMappingAsError(DiagID))
681 Diag.UncompilableErrorOccurred = true;
682
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000683 Diag.ErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000684 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000685 ++Diag.NumErrors;
686 }
687
Douglas Gregor14208802011-08-17 19:13:00 +0000688 // If we've emitted a lot of errors, emit a fatal error instead of it to
689 // stop a flood of bogus errors.
690 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
691 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000692 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregor14208802011-08-17 19:13:00 +0000693 return false;
694 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000695 }
696
Alex Lorenzce4518f2017-05-04 13:56:51 +0000697 // Make sure we set FatalErrorOccurred to ensure that the notes from the
698 // diagnostic that caused `fatal_too_many_errors` won't be emitted.
699 if (Diag.CurDiagID == diag::fatal_too_many_errors)
700 Diag.FatalErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000701 // Finally, report it.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000702 EmitDiag(Diag, DiagLevel);
703 return true;
704}
705
706void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
707 Diagnostic Info(&Diag);
708 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
709
David Blaikie9c902b52011-09-25 23:23:43 +0000710 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000711 if (Diag.Client->IncludeInDiagnosticCounts()) {
712 if (DiagLevel == DiagnosticIDs::Warning)
713 ++Diag.NumWarnings;
714 }
715
716 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000717}
John McCalla877d922011-06-15 21:46:43 +0000718
719bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
720 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
Richard Trieu428058f2014-08-01 01:42:01 +0000721 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
John McCalla877d922011-06-15 21:46:43 +0000722 // Custom diagnostics.
723 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
724 }
725
726 // Only errors may be unrecoverable.
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000727 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCalla877d922011-06-15 21:46:43 +0000728 return false;
729
730 if (DiagID == diag::err_unavailable ||
731 DiagID == diag::err_unavailable_message)
732 return false;
733
John McCall31168b02011-06-15 23:02:42 +0000734 // Currently we consider all ARC errors as recoverable.
Ted Kremenek337c5b82011-10-20 05:07:47 +0000735 if (isARCDiagnostic(DiagID))
John McCall31168b02011-06-15 23:02:42 +0000736 return false;
737
John McCalla877d922011-06-15 21:46:43 +0000738 return true;
739}
Ted Kremenek337c5b82011-10-20 05:07:47 +0000740
741bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
742 unsigned cat = getCategoryNumberForDiag(DiagID);
743 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
744}