blob: 1a7b835ebc128d8ae6e8987ff2724e0516e3716e [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;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000046 unsigned Category : 5;
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)
91#undef VALIDATE_DIAG_SIZE
92#undef STRINGIFY_NAME
93
Alexander Kornienkoab9db512015-06-22 23:07:51 +000094} // namespace anonymous
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000095
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000096static const StaticDiagInfoRec StaticDiagInfo[] = {
Alp Tokerc726c362014-06-10 09:31:37 +000097#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
98 SHOWINSYSHEADER, CATEGORY) \
99 { \
100 diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, NOWERROR, \
101 SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), DESC \
102 } \
103 ,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000104#include "clang/Basic/DiagnosticCommonKinds.inc"
105#include "clang/Basic/DiagnosticDriverKinds.inc"
106#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carruth22a11b72011-12-09 00:02:23 +0000107#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000108#include "clang/Basic/DiagnosticLexKinds.inc"
109#include "clang/Basic/DiagnosticParseKinds.inc"
110#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000111#include "clang/Basic/DiagnosticCommentKinds.inc"
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000112#include "clang/Basic/DiagnosticCrossTUKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000113#include "clang/Basic/DiagnosticSemaKinds.inc"
114#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000115#undef DIAG
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000116};
117
David Majnemer38af2a22013-07-20 07:15:15 +0000118static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000119
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000120/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
121/// or null if the ID is invalid.
122static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000123 // Out of bounds diag. Can't be in the table.
124 using namespace diag;
David Majnemer38af2a22013-07-20 07:15:15 +0000125 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Craig Topperf1186c52014-05-08 06:41:40 +0000126 return nullptr;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000127
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000128 // Compute the index of the requested diagnostic in the static table.
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000129 // 1. Add the number of diagnostics in each category preceding the
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000130 // diagnostic and of the category the diagnostic is in. This gives us
131 // the offset of the category in the table.
132 // 2. Subtract the number of IDs in each category from our ID. This gives us
133 // the offset of the diagnostic in the category.
134 // This is cheaper than a binary search on the table as it doesn't touch
135 // memory at all.
136 unsigned Offset = 0;
David Majnemer38af2a22013-07-20 07:15:15 +0000137 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000138#define CATEGORY(NAME, PREV) \
139 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000140 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
141 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000142 }
143CATEGORY(DRIVER, COMMON)
144CATEGORY(FRONTEND, DRIVER)
145CATEGORY(SERIALIZATION, FRONTEND)
146CATEGORY(LEX, SERIALIZATION)
147CATEGORY(PARSE, LEX)
148CATEGORY(AST, PARSE)
149CATEGORY(COMMENT, AST)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000150CATEGORY(CROSSTU, COMMENT)
151CATEGORY(SEMA, CROSSTU)
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000152CATEGORY(ANALYSIS, SEMA)
153#undef CATEGORY
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000154
155 // Avoid out of bounds reads.
156 if (ID + Offset >= StaticDiagInfoSize)
Craig Topperf1186c52014-05-08 06:41:40 +0000157 return nullptr;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000158
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000159 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
160
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000161 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
162 // If the diag id doesn't match we found a different diag, abort. This can
163 // happen when this function is called with an ID that points into a hole in
164 // the diagID space.
165 if (Found->DiagID != DiagID)
Craig Topperf1186c52014-05-08 06:41:40 +0000166 return nullptr;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000167 return Found;
168}
169
Alp Tokerc726c362014-06-10 09:31:37 +0000170static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) {
171 DiagnosticMapping Info = DiagnosticMapping::Make(
Alp Toker46df1c02014-06-12 10:15:20 +0000172 diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000173
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000174 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
Alp Tokerc726c362014-06-10 09:31:37 +0000175 Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000176
177 if (StaticInfo->WarnNoWerror) {
Alp Toker46df1c02014-06-12 10:15:20 +0000178 assert(Info.getSeverity() == diag::Severity::Warning &&
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000179 "Unexpected mapping with no-Werror bit!");
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000180 Info.setNoWarningAsError(true);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000181 }
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000182 }
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000183
184 return Info;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000185}
186
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000187/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000188/// DiagID belongs to, or 0 if no category.
189unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
190 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
191 return Info->Category;
192 return 0;
193}
194
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000195namespace {
196 // The diagnostic category names.
197 struct StaticDiagCategoryRec {
198 const char *NameStr;
199 uint8_t NameLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000200
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000201 StringRef getName() const {
202 return StringRef(NameStr, NameLen);
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000203 }
204 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000205}
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000206
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000207// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
208// particularly clean, but for now we just implement this method here so we can
209// access GetDefaultDiagMapping.
Alp Tokerc726c362014-06-10 09:31:37 +0000210DiagnosticMapping &
211DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
212 std::pair<iterator, bool> Result =
213 DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000214
215 // Initialize the entry if we added it.
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000216 if (Result.second)
Alp Tokerc726c362014-06-10 09:31:37 +0000217 Result.first->second = GetDefaultDiagMapping(Diag);
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000218
219 return Result.first->second;
220}
221
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000222static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000223#define GET_CATEGORY_TABLE
John McCalla877d922011-06-15 21:46:43 +0000224#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000225#include "clang/Basic/DiagnosticGroups.inc"
226#undef GET_CATEGORY_TABLE
Craig Topperf1186c52014-05-08 06:41:40 +0000227 { nullptr, 0 }
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000228};
229
230/// getNumberOfCategories - Return the number of categories
231unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Toppere5ce8312013-07-15 03:38:40 +0000232 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000233}
234
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000235/// getCategoryNameFromID - Given a category ID, return the name of the
236/// category, an empty string if CategoryID is zero, or null if CategoryID is
237/// invalid.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000238StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000239 if (CategoryID >= getNumberOfCategories())
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000240 return StringRef();
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000241 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000242}
243
244
245
Richard Smith16e1b072013-11-12 02:41:45 +0000246DiagnosticIDs::SFINAEResponse
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000247DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
Richard Smith16e1b072013-11-12 02:41:45 +0000248 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
249 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000250 return SFINAE_Report;
251}
252
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000253/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidisd0040642010-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 Lattner0e62c1c2011-07-23 10:55:15 +0000275 StringRef getDescription(unsigned DiagID) const {
Richard Trieu428058f2014-08-01 01:42:01 +0000276 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000277 "Invalid diagnostic ID");
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000278 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000279 }
280
281 /// getLevel - Return the level of the specified custom diagnostic.
282 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
Richard Trieu428058f2014-08-01 01:42:01 +0000283 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000284 "Invalid diagnostic ID");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000285 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
286 }
287
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000288 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidisd0040642010-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
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000304 } // end diag namespace
305} // end clang namespace
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000306
307
308//===----------------------------------------------------------------------===//
309// Common Diagnostic implementation
310//===----------------------------------------------------------------------===//
311
Craig Topperf1186c52014-05-08 06:41:40 +0000312DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000313
314DiagnosticIDs::~DiagnosticIDs() {
315 delete CustomDiagInfo;
316}
317
318/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000319/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000320/// registered and created, otherwise the existing ID is returned.
Alp Toker61d41af2013-12-23 21:00:35 +0000321///
Alp Toker9e6d27d2014-01-26 06:41:58 +0000322/// \param FormatString A fixed diagnostic format string that will be hashed and
Alp Toker61d41af2013-12-23 21:00:35 +0000323/// mapped to a unique DiagID.
Alp Toker9e6d27d2014-01-26 06:41:58 +0000324unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
Craig Topperf1186c52014-05-08 06:41:40 +0000325 if (!CustomDiagInfo)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000326 CustomDiagInfo = new diag::CustomDiagInfo();
Alp Toker9e6d27d2014-01-26 06:41:58 +0000327 return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000328}
329
330
331/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
332/// level of the specified diagnostic ID is a Warning or Extension.
333/// This only works on builtin diagnostics, not custom ones, and is not legal to
334/// call on NOTEs.
335bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
336 return DiagID < diag::DIAG_UPPER_LIMIT &&
337 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
338}
339
340/// \brief Determine whether the given built-in diagnostic ID is a
341/// Note.
342bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
343 return DiagID < diag::DIAG_UPPER_LIMIT &&
344 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
345}
346
347/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
348/// ID is for an extension of some sort. This also returns EnabledByDefault,
349/// which is set to indicate whether the diagnostic is ignored by default (in
350/// which case -pedantic enables it) or treated as a warning/error by default.
351///
352bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
353 bool &EnabledByDefault) {
354 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
355 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
356 return false;
Alp Tokerc726c362014-06-10 09:31:37 +0000357
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000358 EnabledByDefault =
Alp Toker46df1c02014-06-12 10:15:20 +0000359 GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000360 return true;
361}
362
Daniel Dunbaraa111382011-09-29 01:01:08 +0000363bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
364 if (DiagID >= diag::DIAG_UPPER_LIMIT)
365 return false;
366
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +0000367 return GetDefaultDiagMapping(DiagID).getSeverity() >= diag::Severity::Error;
Daniel Dunbaraa111382011-09-29 01:01:08 +0000368}
369
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000370/// getDescription - Given a diagnostic ID, return a description of the
371/// issue.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000372StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000373 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000374 return Info->getDescription();
Richard Trieu428058f2014-08-01 01:42:01 +0000375 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000376 return CustomDiagInfo->getDescription(DiagID);
377}
378
Alp Toker46df1c02014-06-12 10:15:20 +0000379static DiagnosticIDs::Level toLevel(diag::Severity SV) {
380 switch (SV) {
381 case diag::Severity::Ignored:
382 return DiagnosticIDs::Ignored;
383 case diag::Severity::Remark:
384 return DiagnosticIDs::Remark;
385 case diag::Severity::Warning:
386 return DiagnosticIDs::Warning;
387 case diag::Severity::Error:
388 return DiagnosticIDs::Error;
389 case diag::Severity::Fatal:
390 return DiagnosticIDs::Fatal;
391 }
Saleem Abdulrasoolfbfbaf62014-06-12 19:33:26 +0000392 llvm_unreachable("unexpected severity");
Alp Toker46df1c02014-06-12 10:15:20 +0000393}
394
David Blaikie9c902b52011-09-25 23:23:43 +0000395/// getDiagnosticLevel - Based on the way the client configured the
396/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
397/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000398DiagnosticIDs::Level
399DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar400e7e32011-09-29 01:20:28 +0000400 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000401 // Handle custom diagnostics, which cannot be mapped.
Richard Trieu428058f2014-08-01 01:42:01 +0000402 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
403 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000404 return CustomDiagInfo->getLevel(DiagID);
Richard Trieu428058f2014-08-01 01:42:01 +0000405 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000406
407 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rose6f524ac2012-07-11 16:50:36 +0000408 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Alp Toker04278ec2014-06-16 13:56:47 +0000409 return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000410}
411
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000412/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000413/// object, classify the specified diagnostic ID into a Level, consumable by
414/// the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000415///
416/// \param Loc The source location we are interested in finding out the
417/// diagnostic state. Can be null in order to query the latest state.
Alp Toker46df1c02014-06-12 10:15:20 +0000418diag::Severity
Alp Toker04278ec2014-06-16 13:56:47 +0000419DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
Alp Toker46df1c02014-06-12 10:15:20 +0000420 const DiagnosticsEngine &Diag) const {
Alp Toker04278ec2014-06-16 13:56:47 +0000421 assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE);
Alp Toker46df1c02014-06-12 10:15:20 +0000422
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000423 // Specific non-error diagnostics may be mapped to various levels from ignored
424 // to error. Errors can only be mapped to fatal.
Alp Toker46df1c02014-06-12 10:15:20 +0000425 diag::Severity Result = diag::Severity::Fatal;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000426
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000427 // Get the mapping information, or compute it lazily.
Richard Smithd230de22017-01-26 01:01:01 +0000428 DiagnosticsEngine::DiagState *State = Diag.GetDiagStateForLoc(Loc);
Alp Tokerc726c362014-06-10 09:31:37 +0000429 DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000430
Alp Toker46df1c02014-06-12 10:15:20 +0000431 // TODO: Can a null severity really get here?
432 if (Mapping.getSeverity() != diag::Severity())
433 Result = Mapping.getSeverity();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000434
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000435 // Upgrade ignored diagnostics if -Weverything is enabled.
Richard Smithe37391c2017-05-03 00:28:49 +0000436 if (State->EnableAllWarnings && Result == diag::Severity::Ignored &&
Richard Smithe423ed32014-08-21 20:44:44 +0000437 !Mapping.isUser() && getBuiltinDiagClass(DiagID) != CLASS_REMARK)
Alp Toker46df1c02014-06-12 10:15:20 +0000438 Result = diag::Severity::Warning;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000439
Bob Wilson73a4deb2011-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 Dunbar787032b2011-11-28 22:19:36 +0000443 bool EnabledByDefault = false;
Bob Wilson73a4deb2011-10-12 19:55:31 +0000444 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
445 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Alp Toker46df1c02014-06-12 10:15:20 +0000446 return diag::Severity::Ignored;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000447
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000448 // For extension diagnostics that haven't been explicitly mapped, check if we
449 // should upgrade the diagnostic.
Alp Tokerac4e8e52014-06-22 21:58:33 +0000450 if (IsExtensionDiag && !Mapping.isUser())
Richard Smithe37391c2017-05-03 00:28:49 +0000451 Result = std::max(Result, State->ExtBehavior);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000452
453 // At this point, ignored errors can no longer be upgraded.
Alp Toker46df1c02014-06-12 10:15:20 +0000454 if (Result == diag::Severity::Ignored)
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000455 return Result;
456
457 // Honor -w, which is lower in priority than pedantic-errors, but higher than
458 // -Werror.
Richard Smithe37391c2017-05-03 00:28:49 +0000459 // FIXME: Under GCC, this also suppresses warnings that have been mapped to
460 // errors by -W flags and #pragma diagnostic.
461 if (Result == diag::Severity::Warning && State->IgnoreAllWarnings)
Alp Toker46df1c02014-06-12 10:15:20 +0000462 return diag::Severity::Ignored;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000463
464 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
Alp Toker46df1c02014-06-12 10:15:20 +0000465 if (Result == diag::Severity::Warning) {
Richard Smithe37391c2017-05-03 00:28:49 +0000466 if (State->WarningsAsErrors && !Mapping.hasNoWarningAsError())
Alp Toker46df1c02014-06-12 10:15:20 +0000467 Result = diag::Severity::Error;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000468 }
469
470 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
471 // disabled.
Alp Toker46df1c02014-06-12 10:15:20 +0000472 if (Result == diag::Severity::Error) {
Richard Smithe37391c2017-05-03 00:28:49 +0000473 if (State->ErrorsAsFatal && !Mapping.hasNoErrorAsFatal())
Alp Toker46df1c02014-06-12 10:15:20 +0000474 Result = diag::Severity::Fatal;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000475 }
476
Alp Tokered2c0332014-06-10 06:09:00 +0000477 // Custom diagnostics always are emitted in system headers.
478 bool ShowInSystemHeader =
479 !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
480
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000481 // If we are in a system header, we ignore it. We look at the diagnostic class
482 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000483 // -pedantic-errors modes, which *map* warnings/extensions to errors.
Richard Smithe37391c2017-05-03 00:28:49 +0000484 if (State->SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000485 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth35f53202011-07-25 16:49:02 +0000486 Diag.getSourceManager().getExpansionLoc(Loc)))
Alp Toker46df1c02014-06-12 10:15:20 +0000487 return diag::Severity::Ignored;
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000488
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000489 return Result;
490}
491
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000492#define GET_DIAG_ARRAYS
493#include "clang/Basic/DiagnosticGroups.inc"
494#undef GET_DIAG_ARRAYS
495
Craig Topperb78e9d92013-08-29 06:06:18 +0000496namespace {
497 struct WarningOption {
498 uint16_t NameOffset;
499 uint16_t Members;
500 uint16_t SubGroups;
Craig Topperda7cf8a2013-08-29 05:18:04 +0000501
Craig Topperb78e9d92013-08-29 06:06:18 +0000502 // String is stored with a pascal-style length byte.
503 StringRef getName() const {
504 return StringRef(DiagGroupNames + NameOffset + 1,
505 DiagGroupNames[NameOffset]);
506 }
507 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000508}
Craig Topperda7cf8a2013-08-29 05:18:04 +0000509
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000510// Second the table of options, sorted by name for fast binary lookup.
511static const WarningOption OptionTable[] = {
512#define GET_DIAG_TABLE
513#include "clang/Basic/DiagnosticGroups.inc"
514#undef GET_DIAG_TABLE
515};
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000516
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000517/// getWarningOptionForDiag - Return the lowest-level warning option that
518/// enables the specified diagnostic. If there is no -Wfoo flag that controls
519/// the diagnostic, this returns null.
520StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
521 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
522 return OptionTable[Info->getOptionGroupIndex()].getName();
523 return StringRef();
524}
525
Yuka Takahashi64918d02017-07-16 15:07:20 +0000526std::vector<std::string> DiagnosticIDs::getDiagnosticFlags() {
527 std::vector<std::string> Res;
528 for (size_t I = 1; DiagGroupNames[I] != '\0';) {
529 std::string Diag(DiagGroupNames + I + 1, DiagGroupNames[I]);
530 I += DiagGroupNames[I] + 1;
531 Res.push_back("-W" + Diag);
Yuka Takahashib40f4db2017-07-22 12:35:15 +0000532 Res.push_back("-Wno-" + Diag);
Yuka Takahashi64918d02017-07-16 15:07:20 +0000533 }
534
535 return Res;
536}
537
Richard Smith3be1cb22014-08-07 00:24:21 +0000538/// Return \c true if any diagnostics were found in this group, even if they
539/// were filtered out due to having the wrong flavor.
540static bool getDiagnosticsInGroup(diag::Flavor Flavor,
541 const WarningOption *Group,
Craig Topperb78e9d92013-08-29 06:06:18 +0000542 SmallVectorImpl<diag::kind> &Diags) {
Richard Smith3be1cb22014-08-07 00:24:21 +0000543 // An empty group is considered to be a warning group: we have empty groups
544 // for GCC compatibility, and GCC does not have remarks.
545 if (!Group->Members && !Group->SubGroups)
David Blaikie7a3cbb22015-03-09 02:02:07 +0000546 return Flavor == diag::Flavor::Remark;
Richard Smith3be1cb22014-08-07 00:24:21 +0000547
548 bool NotFound = true;
549
Daniel Dunbard908c122011-09-29 01:47:16 +0000550 // Add the members of the option diagnostic set.
Craig Topperd80c17e2013-08-28 04:02:50 +0000551 const int16_t *Member = DiagArrays + Group->Members;
Richard Smith3be1cb22014-08-07 00:24:21 +0000552 for (; *Member != -1; ++Member) {
553 if (GetDiagInfo(*Member)->getFlavor() == Flavor) {
554 NotFound = false;
555 Diags.push_back(*Member);
556 }
557 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000558
Daniel Dunbard908c122011-09-29 01:47:16 +0000559 // Add the members of the subgroups.
Craig Topperd80c17e2013-08-28 04:02:50 +0000560 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
561 for (; *SubGroups != (int16_t)-1; ++SubGroups)
Richard Smith3be1cb22014-08-07 00:24:21 +0000562 NotFound &= getDiagnosticsInGroup(Flavor, &OptionTable[(short)*SubGroups],
563 Diags);
564
565 return NotFound;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000566}
567
Richard Smith3be1cb22014-08-07 00:24:21 +0000568bool
569DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
570 SmallVectorImpl<diag::kind> &Diags) const {
Craig Topper924f6db2015-10-17 20:18:46 +0000571 auto Found = std::lower_bound(std::begin(OptionTable), std::end(OptionTable),
572 Group,
573 [](const WarningOption &LHS, StringRef RHS) {
574 return LHS.getName() < RHS;
575 });
Craig Topperc00e9532015-10-17 17:10:43 +0000576 if (Found == std::end(OptionTable) || Found->getName() != Group)
Daniel Dunbard908c122011-09-29 01:47:16 +0000577 return true; // Option not found.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000578
Richard Smith3be1cb22014-08-07 00:24:21 +0000579 return ::getDiagnosticsInGroup(Flavor, Found, Diags);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000580}
581
Richard Smith3be1cb22014-08-07 00:24:21 +0000582void DiagnosticIDs::getAllDiagnostics(diag::Flavor Flavor,
583 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000584 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
Richard Smith3be1cb22014-08-07 00:24:21 +0000585 if (StaticDiagInfo[i].getFlavor() == Flavor)
586 Diags.push_back(StaticDiagInfo[i].DiagID);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000587}
588
Richard Smith3be1cb22014-08-07 00:24:21 +0000589StringRef DiagnosticIDs::getNearestOption(diag::Flavor Flavor,
590 StringRef Group) {
Benjamin Kramer116d8872011-11-14 23:30:34 +0000591 StringRef Best;
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000592 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Craig Toppere353a722015-10-18 05:29:23 +0000593 for (const WarningOption &O : OptionTable) {
Benjamin Kramer116d8872011-11-14 23:30:34 +0000594 // Don't suggest ignored warning flags.
Craig Topperc00e9532015-10-17 17:10:43 +0000595 if (!O.Members && !O.SubGroups)
Benjamin Kramer116d8872011-11-14 23:30:34 +0000596 continue;
597
Craig Topperc00e9532015-10-17 17:10:43 +0000598 unsigned Distance = O.getName().edit_distance(Group, true, BestDistance);
Richard Smith3be1cb22014-08-07 00:24:21 +0000599 if (Distance > BestDistance)
600 continue;
601
602 // Don't suggest groups that are not of this kind.
603 llvm::SmallVector<diag::kind, 8> Diags;
Craig Topperc00e9532015-10-17 17:10:43 +0000604 if (::getDiagnosticsInGroup(Flavor, &O, Diags) || Diags.empty())
Richard Smith3be1cb22014-08-07 00:24:21 +0000605 continue;
606
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000607 if (Distance == BestDistance) {
608 // Two matches with the same distance, don't prefer one over the other.
609 Best = "";
610 } else if (Distance < BestDistance) {
611 // This is a better match.
Craig Topperc00e9532015-10-17 17:10:43 +0000612 Best = O.getName();
Benjamin Kramer116d8872011-11-14 23:30:34 +0000613 BestDistance = Distance;
614 }
615 }
616
617 return Best;
618}
619
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000620/// ProcessDiag - This is the method used to report a diagnostic that is
621/// finally fully formed.
David Blaikie9c902b52011-09-25 23:23:43 +0000622bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikieb5784322011-09-26 01:18:08 +0000623 Diagnostic Info(&Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000624
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000625 assert(Diag.getClient() && "DiagnosticClient not set!");
626
627 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000628 unsigned DiagID = Info.getID();
Jordan Rose6f524ac2012-07-11 16:50:36 +0000629 DiagnosticIDs::Level DiagLevel
630 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000631
Richard Smitha2686712014-12-05 21:52:58 +0000632 // Update counts for DiagnosticErrorTrap even if a fatal error occurred
633 // or diagnostics are suppressed.
634 if (DiagLevel >= DiagnosticIDs::Error) {
635 ++Diag.TrapNumErrorsOccurred;
636 if (isUnrecoverable(DiagID))
637 ++Diag.TrapNumUnrecoverableErrorsOccurred;
638 }
639
640 if (Diag.SuppressAllDiagnostics)
641 return false;
642
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000643 if (DiagLevel != DiagnosticIDs::Note) {
644 // Record that a fatal error occurred only when we see a second
645 // non-note diagnostic. This allows notes to be attached to the
646 // fatal error, but suppresses any diagnostics that follow those
647 // notes.
648 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
649 Diag.FatalErrorOccurred = true;
650
651 Diag.LastDiagLevel = DiagLevel;
652 }
653
654 // If a fatal error has already been emitted, silence all subsequent
655 // diagnostics.
Richard Smithe37391c2017-05-03 00:28:49 +0000656 if (Diag.FatalErrorOccurred && Diag.SuppressAfterFatalError) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000657 if (DiagLevel >= DiagnosticIDs::Error &&
658 Diag.Client->IncludeInDiagnosticCounts()) {
659 ++Diag.NumErrors;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000660 }
661
662 return false;
663 }
664
665 // If the client doesn't care about this message, don't issue it. If this is
666 // a note and the last real diagnostic was ignored, ignore it too.
667 if (DiagLevel == DiagnosticIDs::Ignored ||
668 (DiagLevel == DiagnosticIDs::Note &&
669 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
670 return false;
671
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000672 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000673 if (isUnrecoverable(DiagID))
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000674 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000675
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000676 // Warnings which have been upgraded to errors do not prevent compilation.
677 if (isDefaultMappingAsError(DiagID))
678 Diag.UncompilableErrorOccurred = true;
679
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000680 Diag.ErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000681 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000682 ++Diag.NumErrors;
683 }
684
Douglas Gregor14208802011-08-17 19:13:00 +0000685 // If we've emitted a lot of errors, emit a fatal error instead of it to
686 // stop a flood of bogus errors.
687 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
688 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000689 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregor14208802011-08-17 19:13:00 +0000690 return false;
691 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000692 }
693
Alex Lorenzce4518f2017-05-04 13:56:51 +0000694 // Make sure we set FatalErrorOccurred to ensure that the notes from the
695 // diagnostic that caused `fatal_too_many_errors` won't be emitted.
696 if (Diag.CurDiagID == diag::fatal_too_many_errors)
697 Diag.FatalErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000698 // Finally, report it.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000699 EmitDiag(Diag, DiagLevel);
700 return true;
701}
702
703void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
704 Diagnostic Info(&Diag);
705 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
706
David Blaikie9c902b52011-09-25 23:23:43 +0000707 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000708 if (Diag.Client->IncludeInDiagnosticCounts()) {
709 if (DiagLevel == DiagnosticIDs::Warning)
710 ++Diag.NumWarnings;
711 }
712
713 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000714}
John McCalla877d922011-06-15 21:46:43 +0000715
716bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
717 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
Richard Trieu428058f2014-08-01 01:42:01 +0000718 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
John McCalla877d922011-06-15 21:46:43 +0000719 // Custom diagnostics.
720 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
721 }
722
723 // Only errors may be unrecoverable.
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000724 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCalla877d922011-06-15 21:46:43 +0000725 return false;
726
727 if (DiagID == diag::err_unavailable ||
728 DiagID == diag::err_unavailable_message)
729 return false;
730
John McCall31168b02011-06-15 23:02:42 +0000731 // Currently we consider all ARC errors as recoverable.
Ted Kremenek337c5b82011-10-20 05:07:47 +0000732 if (isARCDiagnostic(DiagID))
John McCall31168b02011-06-15 23:02:42 +0000733 return false;
734
John McCalla877d922011-06-15 21:46:43 +0000735 return true;
736}
Ted Kremenek337c5b82011-10-20 05:07:47 +0000737
738bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
739 unsigned cat = getCategoryNumberForDiag(DiagID);
740 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
741}