blob: e8bb10f54e95fb3a53a3770256d89ed49488db82 [file] [log] [blame]
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001//===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Diagnostic IDs-related interfaces.
10//
11//===----------------------------------------------------------------------===//
12
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000013#include "clang/Basic/DiagnosticIDs.h"
David Blaikieaf90ec12012-02-15 21:58:34 +000014#include "clang/Basic/AllDiagnostics.h"
John McCalla877d922011-06-15 21:46:43 +000015#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000016#include "clang/Basic/SourceManager.h"
David Majnemer38af2a22013-07-20 07:15:15 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000018#include "llvm/ADT/SmallVector.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000019#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000020#include <map>
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Builtin Diagnostic information
25//===----------------------------------------------------------------------===//
26
27namespace {
28
29// Diagnostic classes.
30enum {
31 CLASS_NOTE = 0x01,
Tobias Grosser74160242014-02-28 09:11:08 +000032 CLASS_REMARK = 0x02,
33 CLASS_WARNING = 0x03,
34 CLASS_EXTENSION = 0x04,
35 CLASS_ERROR = 0x05
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000036};
37
38struct StaticDiagInfoRec {
Craig Topperea6caba2013-07-21 21:56:18 +000039 uint16_t DiagID;
Alp Tokerc726c362014-06-10 09:31:37 +000040 unsigned DefaultSeverity : 3;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000041 unsigned Class : 3;
Richard Smith16e1b072013-11-12 02:41:45 +000042 unsigned SFINAE : 2;
Daniel Dunbar6de48ee2011-09-29 00:34:06 +000043 unsigned WarnNoWerror : 1;
44 unsigned WarnShowInSystemHeader : 1;
Alex Lorenzf5ca27c2017-10-16 18:28:26 +000045 unsigned Category : 6;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000046
Benjamin Kramera8cafe22012-02-15 20:57:03 +000047 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000048
49 uint16_t DescriptionLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000050 const char *DescriptionStr;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000051
Benjamin Kramera8cafe22012-02-15 20:57:03 +000052 unsigned getOptionGroupIndex() const {
53 return OptionGroupIndex;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000054 }
55
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056 StringRef getDescription() const {
57 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +000058 }
Douglas Gregor46ce91a2011-04-15 22:04:17 +000059
Richard Smith3be1cb22014-08-07 00:24:21 +000060 diag::Flavor getFlavor() const {
61 return Class == CLASS_REMARK ? diag::Flavor::Remark
62 : diag::Flavor::WarningOrError;
63 }
64
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000065 bool operator<(const StaticDiagInfoRec &RHS) const {
66 return DiagID < RHS.DiagID;
67 }
68};
69
Erich Keane0a539b52017-08-28 18:53:17 +000070#define STRINGIFY_NAME(NAME) #NAME
71#define VALIDATE_DIAG_SIZE(NAME) \
72 static_assert( \
73 static_cast<unsigned>(diag::NUM_BUILTIN_##NAME##_DIAGNOSTICS) < \
74 static_cast<unsigned>(diag::DIAG_START_##NAME) + \
75 static_cast<unsigned>(diag::DIAG_SIZE_##NAME), \
76 STRINGIFY_NAME( \
77 DIAG_SIZE_##NAME) " is insufficient to contain all " \
78 "diagnostics, it may need to be made larger in " \
79 "DiagnosticIDs.h.");
80VALIDATE_DIAG_SIZE(COMMON)
81VALIDATE_DIAG_SIZE(DRIVER)
82VALIDATE_DIAG_SIZE(FRONTEND)
83VALIDATE_DIAG_SIZE(SERIALIZATION)
84VALIDATE_DIAG_SIZE(LEX)
85VALIDATE_DIAG_SIZE(PARSE)
86VALIDATE_DIAG_SIZE(AST)
87VALIDATE_DIAG_SIZE(COMMENT)
88VALIDATE_DIAG_SIZE(SEMA)
89VALIDATE_DIAG_SIZE(ANALYSIS)
Alex Lorenzf5ca27c2017-10-16 18:28:26 +000090VALIDATE_DIAG_SIZE(REFACTORING)
Erich Keane0a539b52017-08-28 18:53:17 +000091#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"
Alex Lorenzf5ca27c2017-10-16 18:28:26 +0000115#include "clang/Basic/DiagnosticRefactoringKinds.inc"
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000116#undef DIAG
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000117};
118
David Majnemer38af2a22013-07-20 07:15:15 +0000119static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000120
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000121/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
122/// or null if the ID is invalid.
123static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000124 // Out of bounds diag. Can't be in the table.
125 using namespace diag;
David Majnemer38af2a22013-07-20 07:15:15 +0000126 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Craig Topperf1186c52014-05-08 06:41:40 +0000127 return nullptr;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000128
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000129 // Compute the index of the requested diagnostic in the static table.
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000130 // 1. Add the number of diagnostics in each category preceding the
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000131 // diagnostic and of the category the diagnostic is in. This gives us
132 // the offset of the category in the table.
133 // 2. Subtract the number of IDs in each category from our ID. This gives us
134 // the offset of the diagnostic in the category.
135 // This is cheaper than a binary search on the table as it doesn't touch
136 // memory at all.
137 unsigned Offset = 0;
David Majnemer38af2a22013-07-20 07:15:15 +0000138 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000139#define CATEGORY(NAME, PREV) \
140 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000141 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
142 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000143 }
144CATEGORY(DRIVER, COMMON)
145CATEGORY(FRONTEND, DRIVER)
146CATEGORY(SERIALIZATION, FRONTEND)
147CATEGORY(LEX, SERIALIZATION)
148CATEGORY(PARSE, LEX)
149CATEGORY(AST, PARSE)
150CATEGORY(COMMENT, AST)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000151CATEGORY(CROSSTU, COMMENT)
152CATEGORY(SEMA, CROSSTU)
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000153CATEGORY(ANALYSIS, SEMA)
Alex Lorenzf5ca27c2017-10-16 18:28:26 +0000154CATEGORY(REFACTORING, ANALYSIS)
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000155#undef CATEGORY
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000156
157 // Avoid out of bounds reads.
158 if (ID + Offset >= StaticDiagInfoSize)
Craig Topperf1186c52014-05-08 06:41:40 +0000159 return nullptr;
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000160
Argyrios Kyrtzidisf86e8cc2013-01-02 22:26:07 +0000161 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
162
Benjamin Kramer256f1dd2012-12-11 18:00:22 +0000163 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
164 // If the diag id doesn't match we found a different diag, abort. This can
165 // happen when this function is called with an ID that points into a hole in
166 // the diagID space.
167 if (Found->DiagID != DiagID)
Craig Topperf1186c52014-05-08 06:41:40 +0000168 return nullptr;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000169 return Found;
170}
171
Alp Tokerc726c362014-06-10 09:31:37 +0000172static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) {
173 DiagnosticMapping Info = DiagnosticMapping::Make(
Alp Toker46df1c02014-06-12 10:15:20 +0000174 diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000175
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000176 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
Alp Tokerc726c362014-06-10 09:31:37 +0000177 Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000178
179 if (StaticInfo->WarnNoWerror) {
Alp Toker46df1c02014-06-12 10:15:20 +0000180 assert(Info.getSeverity() == diag::Severity::Warning &&
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000181 "Unexpected mapping with no-Werror bit!");
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000182 Info.setNoWarningAsError(true);
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000183 }
Daniel Dunbar6de48ee2011-09-29 00:34:06 +0000184 }
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000185
186 return Info;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000187}
188
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000189/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000190/// DiagID belongs to, or 0 if no category.
191unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
192 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
193 return Info->Category;
194 return 0;
195}
196
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000197namespace {
198 // The diagnostic category names.
199 struct StaticDiagCategoryRec {
200 const char *NameStr;
201 uint8_t NameLen;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000202
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000203 StringRef getName() const {
204 return StringRef(NameStr, NameLen);
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000205 }
206 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000207}
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000208
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000209// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
210// particularly clean, but for now we just implement this method here so we can
211// access GetDefaultDiagMapping.
Alp Tokerc726c362014-06-10 09:31:37 +0000212DiagnosticMapping &
213DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
214 std::pair<iterator, bool> Result =
215 DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000216
217 // Initialize the entry if we added it.
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000218 if (Result.second)
Alp Tokerc726c362014-06-10 09:31:37 +0000219 Result.first->second = GetDefaultDiagMapping(Diag);
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000220
221 return Result.first->second;
222}
223
Benjamin Kramerdc0a46d2011-06-13 18:38:45 +0000224static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000225#define GET_CATEGORY_TABLE
John McCalla877d922011-06-15 21:46:43 +0000226#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000227#include "clang/Basic/DiagnosticGroups.inc"
228#undef GET_CATEGORY_TABLE
Craig Topperf1186c52014-05-08 06:41:40 +0000229 { nullptr, 0 }
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000230};
231
232/// getNumberOfCategories - Return the number of categories
233unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Toppere5ce8312013-07-15 03:38:40 +0000234 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000235}
236
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000237/// getCategoryNameFromID - Given a category ID, return the name of the
238/// category, an empty string if CategoryID is zero, or null if CategoryID is
239/// invalid.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000240StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000241 if (CategoryID >= getNumberOfCategories())
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242 return StringRef();
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000243 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000244}
245
246
247
Richard Smith16e1b072013-11-12 02:41:45 +0000248DiagnosticIDs::SFINAEResponse
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000249DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
Richard Smith16e1b072013-11-12 02:41:45 +0000250 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
251 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000252 return SFINAE_Report;
253}
254
Douglas Gregor46ce91a2011-04-15 22:04:17 +0000255/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000256///
257static unsigned getBuiltinDiagClass(unsigned DiagID) {
258 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
259 return Info->Class;
260 return ~0U;
261}
262
263//===----------------------------------------------------------------------===//
264// Custom Diagnostic information
265//===----------------------------------------------------------------------===//
266
267namespace clang {
268 namespace diag {
269 class CustomDiagInfo {
270 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
271 std::vector<DiagDesc> DiagInfo;
272 std::map<DiagDesc, unsigned> DiagIDs;
273 public:
274
275 /// getDescription - Return the description of the specified custom
276 /// diagnostic.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000277 StringRef getDescription(unsigned DiagID) const {
Richard Trieu428058f2014-08-01 01:42:01 +0000278 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000279 "Invalid diagnostic ID");
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000280 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000281 }
282
283 /// getLevel - Return the level of the specified custom diagnostic.
284 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
Richard Trieu428058f2014-08-01 01:42:01 +0000285 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000286 "Invalid diagnostic ID");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000287 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
288 }
289
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000290 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000291 DiagnosticIDs &Diags) {
292 DiagDesc D(L, Message);
293 // Check to see if it already exists.
294 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
295 if (I != DiagIDs.end() && I->first == D)
296 return I->second;
297
298 // If not, assign a new ID.
299 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
300 DiagIDs.insert(std::make_pair(D, ID));
301 DiagInfo.push_back(D);
302 return ID;
303 }
304 };
305
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000306 } // end diag namespace
307} // end clang namespace
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000308
309
310//===----------------------------------------------------------------------===//
311// Common Diagnostic implementation
312//===----------------------------------------------------------------------===//
313
Craig Topperf1186c52014-05-08 06:41:40 +0000314DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000315
316DiagnosticIDs::~DiagnosticIDs() {
317 delete CustomDiagInfo;
318}
319
320/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitb3318502013-03-01 21:41:22 +0000321/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000322/// registered and created, otherwise the existing ID is returned.
Alp Toker61d41af2013-12-23 21:00:35 +0000323///
Alp Toker9e6d27d2014-01-26 06:41:58 +0000324/// \param FormatString A fixed diagnostic format string that will be hashed and
Alp Toker61d41af2013-12-23 21:00:35 +0000325/// mapped to a unique DiagID.
Alp Toker9e6d27d2014-01-26 06:41:58 +0000326unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
Craig Topperf1186c52014-05-08 06:41:40 +0000327 if (!CustomDiagInfo)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000328 CustomDiagInfo = new diag::CustomDiagInfo();
Alp Toker9e6d27d2014-01-26 06:41:58 +0000329 return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000330}
331
332
333/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
334/// level of the specified diagnostic ID is a Warning or Extension.
335/// This only works on builtin diagnostics, not custom ones, and is not legal to
336/// call on NOTEs.
337bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
338 return DiagID < diag::DIAG_UPPER_LIMIT &&
339 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
340}
341
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000342/// Determine whether the given built-in diagnostic ID is a
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000343/// Note.
344bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
345 return DiagID < diag::DIAG_UPPER_LIMIT &&
346 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
347}
348
349/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
350/// ID is for an extension of some sort. This also returns EnabledByDefault,
351/// which is set to indicate whether the diagnostic is ignored by default (in
352/// which case -pedantic enables it) or treated as a warning/error by default.
353///
354bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
355 bool &EnabledByDefault) {
356 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
357 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
358 return false;
Alp Tokerc726c362014-06-10 09:31:37 +0000359
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000360 EnabledByDefault =
Alp Toker46df1c02014-06-12 10:15:20 +0000361 GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000362 return true;
363}
364
Daniel Dunbaraa111382011-09-29 01:01:08 +0000365bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
366 if (DiagID >= diag::DIAG_UPPER_LIMIT)
367 return false;
368
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +0000369 return GetDefaultDiagMapping(DiagID).getSeverity() >= diag::Severity::Error;
Daniel Dunbaraa111382011-09-29 01:01:08 +0000370}
371
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000372/// getDescription - Given a diagnostic ID, return a description of the
373/// issue.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000374StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000375 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000376 return Info->getDescription();
Richard Trieu428058f2014-08-01 01:42:01 +0000377 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000378 return CustomDiagInfo->getDescription(DiagID);
379}
380
Alp Toker46df1c02014-06-12 10:15:20 +0000381static DiagnosticIDs::Level toLevel(diag::Severity SV) {
382 switch (SV) {
383 case diag::Severity::Ignored:
384 return DiagnosticIDs::Ignored;
385 case diag::Severity::Remark:
386 return DiagnosticIDs::Remark;
387 case diag::Severity::Warning:
388 return DiagnosticIDs::Warning;
389 case diag::Severity::Error:
390 return DiagnosticIDs::Error;
391 case diag::Severity::Fatal:
392 return DiagnosticIDs::Fatal;
393 }
Saleem Abdulrasoolfbfbaf62014-06-12 19:33:26 +0000394 llvm_unreachable("unexpected severity");
Alp Toker46df1c02014-06-12 10:15:20 +0000395}
396
David Blaikie9c902b52011-09-25 23:23:43 +0000397/// getDiagnosticLevel - Based on the way the client configured the
398/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
399/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000400DiagnosticIDs::Level
401DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar400e7e32011-09-29 01:20:28 +0000402 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000403 // Handle custom diagnostics, which cannot be mapped.
Richard Trieu428058f2014-08-01 01:42:01 +0000404 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
405 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000406 return CustomDiagInfo->getLevel(DiagID);
Richard Trieu428058f2014-08-01 01:42:01 +0000407 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000408
409 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rose6f524ac2012-07-11 16:50:36 +0000410 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Alp Toker04278ec2014-06-16 13:56:47 +0000411 return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000412}
413
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000414/// Based on the way the client configured the Diagnostic
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000415/// object, classify the specified diagnostic ID into a Level, consumable by
416/// the DiagnosticClient.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000417///
418/// \param Loc The source location we are interested in finding out the
419/// diagnostic state. Can be null in order to query the latest state.
Alp Toker46df1c02014-06-12 10:15:20 +0000420diag::Severity
Alp Toker04278ec2014-06-16 13:56:47 +0000421DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
Alp Toker46df1c02014-06-12 10:15:20 +0000422 const DiagnosticsEngine &Diag) const {
Alp Toker04278ec2014-06-16 13:56:47 +0000423 assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE);
Alp Toker46df1c02014-06-12 10:15:20 +0000424
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000425 // Specific non-error diagnostics may be mapped to various levels from ignored
426 // to error. Errors can only be mapped to fatal.
Alp Toker46df1c02014-06-12 10:15:20 +0000427 diag::Severity Result = diag::Severity::Fatal;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000428
Daniel Dunbare8c12a22011-09-29 01:42:25 +0000429 // Get the mapping information, or compute it lazily.
Richard Smithd230de22017-01-26 01:01:01 +0000430 DiagnosticsEngine::DiagState *State = Diag.GetDiagStateForLoc(Loc);
Alp Tokerc726c362014-06-10 09:31:37 +0000431 DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000432
Alp Toker46df1c02014-06-12 10:15:20 +0000433 // TODO: Can a null severity really get here?
434 if (Mapping.getSeverity() != diag::Severity())
435 Result = Mapping.getSeverity();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000436
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000437 // Upgrade ignored diagnostics if -Weverything is enabled.
Richard Smithe37391c2017-05-03 00:28:49 +0000438 if (State->EnableAllWarnings && Result == diag::Severity::Ignored &&
Richard Smithe423ed32014-08-21 20:44:44 +0000439 !Mapping.isUser() && getBuiltinDiagClass(DiagID) != CLASS_REMARK)
Alp Toker46df1c02014-06-12 10:15:20 +0000440 Result = diag::Severity::Warning;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000441
Bob Wilson73a4deb2011-10-12 19:55:31 +0000442 // Ignore -pedantic diagnostics inside __extension__ blocks.
443 // (The diagnostics controlled by -pedantic are the extension diagnostics
444 // that are not enabled by default.)
Daniel Dunbar787032b2011-11-28 22:19:36 +0000445 bool EnabledByDefault = false;
Bob Wilson73a4deb2011-10-12 19:55:31 +0000446 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
447 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Alp Toker46df1c02014-06-12 10:15:20 +0000448 return diag::Severity::Ignored;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000449
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000450 // For extension diagnostics that haven't been explicitly mapped, check if we
451 // should upgrade the diagnostic.
Alp Tokerac4e8e52014-06-22 21:58:33 +0000452 if (IsExtensionDiag && !Mapping.isUser())
Richard Smithe37391c2017-05-03 00:28:49 +0000453 Result = std::max(Result, State->ExtBehavior);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000454
455 // At this point, ignored errors can no longer be upgraded.
Alp Toker46df1c02014-06-12 10:15:20 +0000456 if (Result == diag::Severity::Ignored)
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000457 return Result;
458
459 // Honor -w, which is lower in priority than pedantic-errors, but higher than
460 // -Werror.
Richard Smithe37391c2017-05-03 00:28:49 +0000461 // FIXME: Under GCC, this also suppresses warnings that have been mapped to
462 // errors by -W flags and #pragma diagnostic.
463 if (Result == diag::Severity::Warning && State->IgnoreAllWarnings)
Alp Toker46df1c02014-06-12 10:15:20 +0000464 return diag::Severity::Ignored;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000465
466 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
Alp Toker46df1c02014-06-12 10:15:20 +0000467 if (Result == diag::Severity::Warning) {
Richard Smithe37391c2017-05-03 00:28:49 +0000468 if (State->WarningsAsErrors && !Mapping.hasNoWarningAsError())
Alp Toker46df1c02014-06-12 10:15:20 +0000469 Result = diag::Severity::Error;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000470 }
471
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000472 // If -Wfatal-errors is enabled, map errors to fatal unless explicitly
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000473 // disabled.
Alp Toker46df1c02014-06-12 10:15:20 +0000474 if (Result == diag::Severity::Error) {
Richard Smithe37391c2017-05-03 00:28:49 +0000475 if (State->ErrorsAsFatal && !Mapping.hasNoErrorAsFatal())
Alp Toker46df1c02014-06-12 10:15:20 +0000476 Result = diag::Severity::Fatal;
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000477 }
478
Alp Tokered2c0332014-06-10 06:09:00 +0000479 // Custom diagnostics always are emitted in system headers.
480 bool ShowInSystemHeader =
481 !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
482
Daniel Dunbar866fcd32011-09-29 02:03:01 +0000483 // If we are in a system header, we ignore it. We look at the diagnostic class
484 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000485 // -pedantic-errors modes, which *map* warnings/extensions to errors.
Richard Smithe37391c2017-05-03 00:28:49 +0000486 if (State->SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000487 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth35f53202011-07-25 16:49:02 +0000488 Diag.getSourceManager().getExpansionLoc(Loc)))
Alp Toker46df1c02014-06-12 10:15:20 +0000489 return diag::Severity::Ignored;
Argyrios Kyrtzidis8a8b8772011-04-21 23:08:18 +0000490
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000491 return Result;
492}
493
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000494#define GET_DIAG_ARRAYS
495#include "clang/Basic/DiagnosticGroups.inc"
496#undef GET_DIAG_ARRAYS
497
Craig Topperb78e9d92013-08-29 06:06:18 +0000498namespace {
499 struct WarningOption {
500 uint16_t NameOffset;
501 uint16_t Members;
502 uint16_t SubGroups;
Craig Topperda7cf8a2013-08-29 05:18:04 +0000503
Craig Topperb78e9d92013-08-29 06:06:18 +0000504 // String is stored with a pascal-style length byte.
505 StringRef getName() const {
506 return StringRef(DiagGroupNames + NameOffset + 1,
507 DiagGroupNames[NameOffset]);
508 }
509 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000510}
Craig Topperda7cf8a2013-08-29 05:18:04 +0000511
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000512// Second the table of options, sorted by name for fast binary lookup.
513static const WarningOption OptionTable[] = {
514#define GET_DIAG_TABLE
515#include "clang/Basic/DiagnosticGroups.inc"
516#undef GET_DIAG_TABLE
517};
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000518
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000519/// getWarningOptionForDiag - Return the lowest-level warning option that
520/// enables the specified diagnostic. If there is no -Wfoo flag that controls
521/// the diagnostic, this returns null.
522StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
523 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
524 return OptionTable[Info->getOptionGroupIndex()].getName();
525 return StringRef();
526}
527
Yuka Takahashi64918d02017-07-16 15:07:20 +0000528std::vector<std::string> DiagnosticIDs::getDiagnosticFlags() {
529 std::vector<std::string> Res;
530 for (size_t I = 1; DiagGroupNames[I] != '\0';) {
531 std::string Diag(DiagGroupNames + I + 1, DiagGroupNames[I]);
532 I += DiagGroupNames[I] + 1;
533 Res.push_back("-W" + Diag);
Yuka Takahashib40f4db2017-07-22 12:35:15 +0000534 Res.push_back("-Wno-" + Diag);
Yuka Takahashi64918d02017-07-16 15:07:20 +0000535 }
536
537 return Res;
538}
539
Richard Smith3be1cb22014-08-07 00:24:21 +0000540/// Return \c true if any diagnostics were found in this group, even if they
541/// were filtered out due to having the wrong flavor.
542static bool getDiagnosticsInGroup(diag::Flavor Flavor,
543 const WarningOption *Group,
Craig Topperb78e9d92013-08-29 06:06:18 +0000544 SmallVectorImpl<diag::kind> &Diags) {
Richard Smith3be1cb22014-08-07 00:24:21 +0000545 // An empty group is considered to be a warning group: we have empty groups
546 // for GCC compatibility, and GCC does not have remarks.
547 if (!Group->Members && !Group->SubGroups)
David Blaikie7a3cbb22015-03-09 02:02:07 +0000548 return Flavor == diag::Flavor::Remark;
Richard Smith3be1cb22014-08-07 00:24:21 +0000549
550 bool NotFound = true;
551
Daniel Dunbard908c122011-09-29 01:47:16 +0000552 // Add the members of the option diagnostic set.
Craig Topperd80c17e2013-08-28 04:02:50 +0000553 const int16_t *Member = DiagArrays + Group->Members;
Richard Smith3be1cb22014-08-07 00:24:21 +0000554 for (; *Member != -1; ++Member) {
555 if (GetDiagInfo(*Member)->getFlavor() == Flavor) {
556 NotFound = false;
557 Diags.push_back(*Member);
558 }
559 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000560
Daniel Dunbard908c122011-09-29 01:47:16 +0000561 // Add the members of the subgroups.
Craig Topperd80c17e2013-08-28 04:02:50 +0000562 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
563 for (; *SubGroups != (int16_t)-1; ++SubGroups)
Richard Smith3be1cb22014-08-07 00:24:21 +0000564 NotFound &= getDiagnosticsInGroup(Flavor, &OptionTable[(short)*SubGroups],
565 Diags);
566
567 return NotFound;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000568}
569
Richard Smith3be1cb22014-08-07 00:24:21 +0000570bool
571DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
572 SmallVectorImpl<diag::kind> &Diags) const {
Craig Topper924f6db2015-10-17 20:18:46 +0000573 auto Found = std::lower_bound(std::begin(OptionTable), std::end(OptionTable),
574 Group,
575 [](const WarningOption &LHS, StringRef RHS) {
576 return LHS.getName() < RHS;
577 });
Craig Topperc00e9532015-10-17 17:10:43 +0000578 if (Found == std::end(OptionTable) || Found->getName() != Group)
Daniel Dunbard908c122011-09-29 01:47:16 +0000579 return true; // Option not found.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000580
Richard Smith3be1cb22014-08-07 00:24:21 +0000581 return ::getDiagnosticsInGroup(Flavor, Found, Diags);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000582}
583
Richard Smith3be1cb22014-08-07 00:24:21 +0000584void DiagnosticIDs::getAllDiagnostics(diag::Flavor Flavor,
Gabor Horvath53b5c132017-12-20 16:55:41 +0000585 std::vector<diag::kind> &Diags) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000586 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
Richard Smith3be1cb22014-08-07 00:24:21 +0000587 if (StaticDiagInfo[i].getFlavor() == Flavor)
588 Diags.push_back(StaticDiagInfo[i].DiagID);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000589}
590
Richard Smith3be1cb22014-08-07 00:24:21 +0000591StringRef DiagnosticIDs::getNearestOption(diag::Flavor Flavor,
592 StringRef Group) {
Benjamin Kramer116d8872011-11-14 23:30:34 +0000593 StringRef Best;
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000594 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Craig Toppere353a722015-10-18 05:29:23 +0000595 for (const WarningOption &O : OptionTable) {
Benjamin Kramer116d8872011-11-14 23:30:34 +0000596 // Don't suggest ignored warning flags.
Craig Topperc00e9532015-10-17 17:10:43 +0000597 if (!O.Members && !O.SubGroups)
Benjamin Kramer116d8872011-11-14 23:30:34 +0000598 continue;
599
Craig Topperc00e9532015-10-17 17:10:43 +0000600 unsigned Distance = O.getName().edit_distance(Group, true, BestDistance);
Richard Smith3be1cb22014-08-07 00:24:21 +0000601 if (Distance > BestDistance)
602 continue;
603
604 // Don't suggest groups that are not of this kind.
605 llvm::SmallVector<diag::kind, 8> Diags;
Craig Topperc00e9532015-10-17 17:10:43 +0000606 if (::getDiagnosticsInGroup(Flavor, &O, Diags) || Diags.empty())
Richard Smith3be1cb22014-08-07 00:24:21 +0000607 continue;
608
Benjamin Kramer176a5cb2011-11-15 12:26:39 +0000609 if (Distance == BestDistance) {
610 // Two matches with the same distance, don't prefer one over the other.
611 Best = "";
612 } else if (Distance < BestDistance) {
613 // This is a better match.
Craig Topperc00e9532015-10-17 17:10:43 +0000614 Best = O.getName();
Benjamin Kramer116d8872011-11-14 23:30:34 +0000615 BestDistance = Distance;
616 }
617 }
618
619 return Best;
620}
621
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000622/// ProcessDiag - This is the method used to report a diagnostic that is
623/// finally fully formed.
David Blaikie9c902b52011-09-25 23:23:43 +0000624bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikieb5784322011-09-26 01:18:08 +0000625 Diagnostic Info(&Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000626
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000627 assert(Diag.getClient() && "DiagnosticClient not set!");
628
629 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000630 unsigned DiagID = Info.getID();
Jordan Rose6f524ac2012-07-11 16:50:36 +0000631 DiagnosticIDs::Level DiagLevel
632 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000633
Richard Smitha2686712014-12-05 21:52:58 +0000634 // Update counts for DiagnosticErrorTrap even if a fatal error occurred
635 // or diagnostics are suppressed.
636 if (DiagLevel >= DiagnosticIDs::Error) {
637 ++Diag.TrapNumErrorsOccurred;
638 if (isUnrecoverable(DiagID))
639 ++Diag.TrapNumUnrecoverableErrorsOccurred;
640 }
641
642 if (Diag.SuppressAllDiagnostics)
643 return false;
644
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000645 if (DiagLevel != DiagnosticIDs::Note) {
646 // Record that a fatal error occurred only when we see a second
647 // non-note diagnostic. This allows notes to be attached to the
648 // fatal error, but suppresses any diagnostics that follow those
649 // notes.
650 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
651 Diag.FatalErrorOccurred = true;
652
653 Diag.LastDiagLevel = DiagLevel;
654 }
655
656 // If a fatal error has already been emitted, silence all subsequent
657 // diagnostics.
Richard Smithe37391c2017-05-03 00:28:49 +0000658 if (Diag.FatalErrorOccurred && Diag.SuppressAfterFatalError) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000659 if (DiagLevel >= DiagnosticIDs::Error &&
660 Diag.Client->IncludeInDiagnosticCounts()) {
661 ++Diag.NumErrors;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000662 }
663
664 return false;
665 }
666
667 // If the client doesn't care about this message, don't issue it. If this is
668 // a note and the last real diagnostic was ignored, ignore it too.
669 if (DiagLevel == DiagnosticIDs::Ignored ||
670 (DiagLevel == DiagnosticIDs::Note &&
671 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
672 return false;
673
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000674 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000675 if (isUnrecoverable(DiagID))
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000676 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000677
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000678 // Warnings which have been upgraded to errors do not prevent compilation.
679 if (isDefaultMappingAsError(DiagID))
680 Diag.UncompilableErrorOccurred = true;
681
Daniel Jasperd2e6f652012-09-28 15:45:07 +0000682 Diag.ErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000683 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000684 ++Diag.NumErrors;
685 }
686
Fangrui Song6907ce22018-07-30 19:24:48 +0000687 // If we've emitted a lot of errors, emit a fatal error instead of it to
Douglas Gregor14208802011-08-17 19:13:00 +0000688 // stop a flood of bogus errors.
689 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
690 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000691 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregor14208802011-08-17 19:13:00 +0000692 return false;
693 }
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000694 }
695
Alex Lorenzce4518f2017-05-04 13:56:51 +0000696 // Make sure we set FatalErrorOccurred to ensure that the notes from the
697 // diagnostic that caused `fatal_too_many_errors` won't be emitted.
698 if (Diag.CurDiagID == diag::fatal_too_many_errors)
699 Diag.FatalErrorOccurred = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000700 // Finally, report it.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000701 EmitDiag(Diag, DiagLevel);
702 return true;
703}
704
705void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
706 Diagnostic Info(&Diag);
707 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
708
David Blaikie9c902b52011-09-25 23:23:43 +0000709 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000710 if (Diag.Client->IncludeInDiagnosticCounts()) {
711 if (DiagLevel == DiagnosticIDs::Warning)
712 ++Diag.NumWarnings;
713 }
714
715 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000716}
John McCalla877d922011-06-15 21:46:43 +0000717
718bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
719 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
Richard Trieu428058f2014-08-01 01:42:01 +0000720 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
John McCalla877d922011-06-15 21:46:43 +0000721 // Custom diagnostics.
722 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
723 }
724
725 // Only errors may be unrecoverable.
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000726 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCalla877d922011-06-15 21:46:43 +0000727 return false;
728
729 if (DiagID == diag::err_unavailable ||
730 DiagID == diag::err_unavailable_message)
731 return false;
732
John McCall31168b02011-06-15 23:02:42 +0000733 // Currently we consider all ARC errors as recoverable.
Ted Kremenek337c5b82011-10-20 05:07:47 +0000734 if (isARCDiagnostic(DiagID))
John McCall31168b02011-06-15 23:02:42 +0000735 return false;
736
John McCalla877d922011-06-15 21:46:43 +0000737 return true;
738}
Ted Kremenek337c5b82011-10-20 05:07:47 +0000739
740bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
741 unsigned cat = getCategoryNumberForDiag(DiagID);
742 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
743}