blob: 0bb0b9f928e5b1e9b941cceb75cfff4978da4611 [file] [log] [blame]
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001//===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic IDs-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000014#include "clang/Basic/DiagnosticIDs.h"
David Blaikie6c448862012-02-15 21:58:34 +000015#include "clang/Basic/AllDiagnostics.h"
John McCall923cd572011-06-15 21:46:43 +000016#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000017#include "clang/Basic/SourceManager.h"
David Majnemercfaa5522013-07-20 07:15:15 +000018#include "llvm/ADT/STLExtras.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070019#include "llvm/ADT/SmallVector.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000021#include <map>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Builtin Diagnostic information
26//===----------------------------------------------------------------------===//
27
28namespace {
29
30// Diagnostic classes.
31enum {
32 CLASS_NOTE = 0x01,
Stephen Hines651f13c2014-04-23 16:59:28 -070033 CLASS_REMARK = 0x02,
34 CLASS_WARNING = 0x03,
35 CLASS_EXTENSION = 0x04,
36 CLASS_ERROR = 0x05
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000037};
38
39struct StaticDiagInfoRec {
Craig Topper8bfffa52013-07-21 21:56:18 +000040 uint16_t DiagID;
Stephen Hinesc568f1e2014-07-21 00:47:37 -070041 unsigned DefaultSeverity : 3;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000042 unsigned Class : 3;
Richard Smith3347b492013-11-12 02:41:45 +000043 unsigned SFINAE : 2;
Daniel Dunbar4213df32011-09-29 00:34:06 +000044 unsigned WarnNoWerror : 1;
45 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000046 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000047
Benjamin Kramerd49cb202012-02-15 20:57:03 +000048 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000049
50 uint16_t DescriptionLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000051 const char *DescriptionStr;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000052
Benjamin Kramerd49cb202012-02-15 20:57:03 +000053 unsigned getOptionGroupIndex() const {
54 return OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000055 }
56
Chris Lattner5f9e2722011-07-23 10:55:15 +000057 StringRef getDescription() const {
58 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000059 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000060
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000061 bool operator<(const StaticDiagInfoRec &RHS) const {
62 return DiagID < RHS.DiagID;
63 }
64};
65
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000066} // namespace anonymous
67
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000068static const StaticDiagInfoRec StaticDiagInfo[] = {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070069#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
70 SHOWINSYSHEADER, CATEGORY) \
71 { \
72 diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, NOWERROR, \
73 SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), DESC \
74 } \
75 ,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000076#include "clang/Basic/DiagnosticCommonKinds.inc"
77#include "clang/Basic/DiagnosticDriverKinds.inc"
78#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +000079#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000080#include "clang/Basic/DiagnosticLexKinds.inc"
81#include "clang/Basic/DiagnosticParseKinds.inc"
82#include "clang/Basic/DiagnosticASTKinds.inc"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000083#include "clang/Basic/DiagnosticCommentKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000084#include "clang/Basic/DiagnosticSemaKinds.inc"
85#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000086#undef DIAG
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000087};
88
David Majnemercfaa5522013-07-20 07:15:15 +000089static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000090
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000091/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
92/// or null if the ID is invalid.
93static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000094 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
95#ifndef NDEBUG
Craig Toppercbfb8d72013-07-21 18:58:40 +000096 static bool IsFirst = true; // So the check is only performed on first call.
97 if (IsFirst) {
98 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
99 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
100 "Diag ID conflict, the enums at the start of clang::diag (in "
101 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000102
Craig Toppercbfb8d72013-07-21 18:58:40 +0000103 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
104 "Improperly sorted diag info");
105 }
106 IsFirst = false;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000107 }
108#endif
109
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000110 // Out of bounds diag. Can't be in the table.
111 using namespace diag;
David Majnemercfaa5522013-07-20 07:15:15 +0000112 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700113 return nullptr;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000114
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000115 // Compute the index of the requested diagnostic in the static table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700116 // 1. Add the number of diagnostics in each category preceding the
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000117 // diagnostic and of the category the diagnostic is in. This gives us
118 // the offset of the category in the table.
119 // 2. Subtract the number of IDs in each category from our ID. This gives us
120 // the offset of the diagnostic in the category.
121 // This is cheaper than a binary search on the table as it doesn't touch
122 // memory at all.
123 unsigned Offset = 0;
David Majnemercfaa5522013-07-20 07:15:15 +0000124 unsigned ID = DiagID - DIAG_START_COMMON - 1;
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000125#define CATEGORY(NAME, PREV) \
126 if (DiagID > DIAG_START_##NAME) { \
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000127 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
128 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000129 }
130CATEGORY(DRIVER, COMMON)
131CATEGORY(FRONTEND, DRIVER)
132CATEGORY(SERIALIZATION, FRONTEND)
133CATEGORY(LEX, SERIALIZATION)
134CATEGORY(PARSE, LEX)
135CATEGORY(AST, PARSE)
136CATEGORY(COMMENT, AST)
137CATEGORY(SEMA, COMMENT)
138CATEGORY(ANALYSIS, SEMA)
139#undef CATEGORY
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000140
141 // Avoid out of bounds reads.
142 if (ID + Offset >= StaticDiagInfoSize)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700143 return nullptr;
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000144
Argyrios Kyrtzidis35abfcd2013-01-02 22:26:07 +0000145 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
146
Benjamin Kramera07b59e2012-12-11 18:00:22 +0000147 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
148 // If the diag id doesn't match we found a different diag, abort. This can
149 // happen when this function is called with an ID that points into a hole in
150 // the diagID space.
151 if (Found->DiagID != DiagID)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700152 return nullptr;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000153 return Found;
154}
155
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700156static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) {
157 DiagnosticMapping Info = DiagnosticMapping::Make(
158 diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000159
Daniel Dunbara5e41332011-09-29 01:52:06 +0000160 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700161 Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
Daniel Dunbara5e41332011-09-29 01:52:06 +0000162
163 if (StaticInfo->WarnNoWerror) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700164 assert(Info.getSeverity() == diag::Severity::Warning &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000165 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000166 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000167 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000168 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000169
170 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000171}
172
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000173/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000174/// DiagID belongs to, or 0 if no category.
175unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
176 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
177 return Info->Category;
178 return 0;
179}
180
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000181namespace {
182 // The diagnostic category names.
183 struct StaticDiagCategoryRec {
184 const char *NameStr;
185 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000186
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 StringRef getName() const {
188 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000189 }
190 };
191}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000192
Daniel Dunbarba494c62011-09-29 01:42:25 +0000193// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
194// particularly clean, but for now we just implement this method here so we can
195// access GetDefaultDiagMapping.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700196DiagnosticMapping &
197DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
198 std::pair<iterator, bool> Result =
199 DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000200
201 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000202 if (Result.second)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700203 Result.first->second = GetDefaultDiagMapping(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000204
205 return Result.first->second;
206}
207
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000208static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000209#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000210#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000211#include "clang/Basic/DiagnosticGroups.inc"
212#undef GET_CATEGORY_TABLE
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700213 { nullptr, 0 }
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000214};
215
216/// getNumberOfCategories - Return the number of categories
217unsigned DiagnosticIDs::getNumberOfCategories() {
Craig Topperb9602322013-07-15 03:38:40 +0000218 return llvm::array_lengthof(CategoryNameTable) - 1;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000219}
220
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000221/// getCategoryNameFromID - Given a category ID, return the name of the
222/// category, an empty string if CategoryID is zero, or null if CategoryID is
223/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000225 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000227 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000228}
229
230
231
Richard Smith3347b492013-11-12 02:41:45 +0000232DiagnosticIDs::SFINAEResponse
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000233DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
Richard Smith3347b492013-11-12 02:41:45 +0000234 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
235 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000236 return SFINAE_Report;
237}
238
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000239/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000240///
241static unsigned getBuiltinDiagClass(unsigned DiagID) {
242 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
243 return Info->Class;
244 return ~0U;
245}
246
247//===----------------------------------------------------------------------===//
248// Custom Diagnostic information
249//===----------------------------------------------------------------------===//
250
251namespace clang {
252 namespace diag {
253 class CustomDiagInfo {
254 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
255 std::vector<DiagDesc> DiagInfo;
256 std::map<DiagDesc, unsigned> DiagIDs;
257 public:
258
259 /// getDescription - Return the description of the specified custom
260 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000262 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000263 "Invalid diagnostic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000264 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000265 }
266
267 /// getLevel - Return the level of the specified custom diagnostic.
268 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
269 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000270 "Invalid diagnostic ID");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000271 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
272 }
273
Chris Lattner5f9e2722011-07-23 10:55:15 +0000274 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000275 DiagnosticIDs &Diags) {
276 DiagDesc D(L, Message);
277 // Check to see if it already exists.
278 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
279 if (I != DiagIDs.end() && I->first == D)
280 return I->second;
281
282 // If not, assign a new ID.
283 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
284 DiagIDs.insert(std::make_pair(D, ID));
285 DiagInfo.push_back(D);
286 return ID;
287 }
288 };
289
290 } // end diag namespace
291} // end clang namespace
292
293
294//===----------------------------------------------------------------------===//
295// Common Diagnostic implementation
296//===----------------------------------------------------------------------===//
297
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700298DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000299
300DiagnosticIDs::~DiagnosticIDs() {
301 delete CustomDiagInfo;
302}
303
304/// getCustomDiagID - Return an ID for a diagnostic with the specified message
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000305/// and level. If this is the first request for this diagnostic, it is
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000306/// registered and created, otherwise the existing ID is returned.
Stephen Hines651f13c2014-04-23 16:59:28 -0700307///
308/// \param FormatString A fixed diagnostic format string that will be hashed and
309/// mapped to a unique DiagID.
310unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700311 if (!CustomDiagInfo)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000312 CustomDiagInfo = new diag::CustomDiagInfo();
Stephen Hines651f13c2014-04-23 16:59:28 -0700313 return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000314}
315
316
317/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
318/// level of the specified diagnostic ID is a Warning or Extension.
319/// This only works on builtin diagnostics, not custom ones, and is not legal to
320/// call on NOTEs.
321bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
322 return DiagID < diag::DIAG_UPPER_LIMIT &&
323 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
324}
325
326/// \brief Determine whether the given built-in diagnostic ID is a
327/// Note.
328bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
329 return DiagID < diag::DIAG_UPPER_LIMIT &&
330 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
331}
332
333/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
334/// ID is for an extension of some sort. This also returns EnabledByDefault,
335/// which is set to indicate whether the diagnostic is ignored by default (in
336/// which case -pedantic enables it) or treated as a warning/error by default.
337///
338bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
339 bool &EnabledByDefault) {
340 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
341 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
342 return false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700343
Daniel Dunbara5e41332011-09-29 01:52:06 +0000344 EnabledByDefault =
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700345 GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000346 return true;
347}
348
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000349bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
350 if (DiagID >= diag::DIAG_UPPER_LIMIT)
351 return false;
352
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700353 return GetDefaultDiagMapping(DiagID).getSeverity() == diag::Severity::Error;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700354}
355
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000356/// getDescription - Given a diagnostic ID, return a description of the
357/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000358StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000359 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000360 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000361 return CustomDiagInfo->getDescription(DiagID);
362}
363
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700364static DiagnosticIDs::Level toLevel(diag::Severity SV) {
365 switch (SV) {
366 case diag::Severity::Ignored:
367 return DiagnosticIDs::Ignored;
368 case diag::Severity::Remark:
369 return DiagnosticIDs::Remark;
370 case diag::Severity::Warning:
371 return DiagnosticIDs::Warning;
372 case diag::Severity::Error:
373 return DiagnosticIDs::Error;
374 case diag::Severity::Fatal:
375 return DiagnosticIDs::Fatal;
376 }
377 llvm_unreachable("unexpected severity");
378}
379
David Blaikied6471f72011-09-25 23:23:43 +0000380/// getDiagnosticLevel - Based on the way the client configured the
381/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
382/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000383DiagnosticIDs::Level
384DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000385 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000386 // Handle custom diagnostics, which cannot be mapped.
387 if (DiagID >= diag::DIAG_UPPER_LIMIT)
388 return CustomDiagInfo->getLevel(DiagID);
389
390 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Jordan Rosec6d64a22012-07-11 16:50:36 +0000391 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700392 return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000393}
394
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000395/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000396/// object, classify the specified diagnostic ID into a Level, consumable by
397/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000398///
399/// \param Loc The source location we are interested in finding out the
400/// diagnostic state. Can be null in order to query the latest state.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700401diag::Severity
402DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
403 const DiagnosticsEngine &Diag) const {
404 assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE);
405
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000406 // Specific non-error diagnostics may be mapped to various levels from ignored
407 // to error. Errors can only be mapped to fatal.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700408 diag::Severity Result = diag::Severity::Fatal;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000409
David Blaikied6471f72011-09-25 23:23:43 +0000410 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000411 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000412 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000413
Daniel Dunbarba494c62011-09-29 01:42:25 +0000414 // Get the mapping information, or compute it lazily.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700415 DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000416
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700417 // TODO: Can a null severity really get here?
418 if (Mapping.getSeverity() != diag::Severity())
419 Result = Mapping.getSeverity();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000420
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000421 // Upgrade ignored diagnostics if -Weverything is enabled.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700422 if (Diag.EnableAllWarnings && Result == diag::Severity::Ignored &&
423 !Mapping.isUser())
424 Result = diag::Severity::Warning;
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000425
Stephen Hines651f13c2014-04-23 16:59:28 -0700426 // Diagnostics of class REMARK are either printed as remarks or in case they
427 // have been added to -Werror they are printed as errors.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700428 // FIXME: Disregarding user-requested remark mappings like this is bogus.
429 if (Result == diag::Severity::Warning &&
430 getBuiltinDiagClass(DiagID) == CLASS_REMARK)
431 Result = diag::Severity::Remark;
Stephen Hines651f13c2014-04-23 16:59:28 -0700432
Bob Wilson18c407f2011-10-12 19:55:31 +0000433 // Ignore -pedantic diagnostics inside __extension__ blocks.
434 // (The diagnostics controlled by -pedantic are the extension diagnostics
435 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000436 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000437 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
438 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700439 return diag::Severity::Ignored;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000440
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000441 // For extension diagnostics that haven't been explicitly mapped, check if we
442 // should upgrade the diagnostic.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700443 if (IsExtensionDiag && !Mapping.isUser())
444 Result = std::max(Result, Diag.ExtBehavior);
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000445
446 // At this point, ignored errors can no longer be upgraded.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700447 if (Result == diag::Severity::Ignored)
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000448 return Result;
449
450 // Honor -w, which is lower in priority than pedantic-errors, but higher than
451 // -Werror.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700452 if (Result == diag::Severity::Warning && Diag.IgnoreAllWarnings)
453 return diag::Severity::Ignored;
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000454
455 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700456 if (Result == diag::Severity::Warning) {
457 if (Diag.WarningsAsErrors && !Mapping.hasNoWarningAsError())
458 Result = diag::Severity::Error;
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000459 }
460
461 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
462 // disabled.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700463 if (Result == diag::Severity::Error) {
464 if (Diag.ErrorsAsFatal && !Mapping.hasNoErrorAsFatal())
465 Result = diag::Severity::Fatal;
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000466 }
467
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700468 // Custom diagnostics always are emitted in system headers.
469 bool ShowInSystemHeader =
470 !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
471
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000472 // If we are in a system header, we ignore it. We look at the diagnostic class
473 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000474 // -pedantic-errors modes, which *map* warnings/extensions to errors.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700475 if (Diag.SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000476 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000477 Diag.getSourceManager().getExpansionLoc(Loc)))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700478 return diag::Severity::Ignored;
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000479
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000480 return Result;
481}
482
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000483#define GET_DIAG_ARRAYS
484#include "clang/Basic/DiagnosticGroups.inc"
485#undef GET_DIAG_ARRAYS
486
Craig Topper46c8fca2013-08-29 06:06:18 +0000487namespace {
488 struct WarningOption {
489 uint16_t NameOffset;
490 uint16_t Members;
491 uint16_t SubGroups;
Craig Topper354f20a2013-08-29 05:18:04 +0000492
Craig Topper46c8fca2013-08-29 06:06:18 +0000493 // String is stored with a pascal-style length byte.
494 StringRef getName() const {
495 return StringRef(DiagGroupNames + NameOffset + 1,
496 DiagGroupNames[NameOffset]);
497 }
498 };
499}
Craig Topper354f20a2013-08-29 05:18:04 +0000500
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000501// Second the table of options, sorted by name for fast binary lookup.
502static const WarningOption OptionTable[] = {
503#define GET_DIAG_TABLE
504#include "clang/Basic/DiagnosticGroups.inc"
505#undef GET_DIAG_TABLE
506};
Craig Topperb9602322013-07-15 03:38:40 +0000507static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000508
Craig Topper354f20a2013-08-29 05:18:04 +0000509static bool WarningOptionCompare(const WarningOption &LHS, StringRef RHS) {
510 return LHS.getName() < RHS;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000511}
512
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000513/// getWarningOptionForDiag - Return the lowest-level warning option that
514/// enables the specified diagnostic. If there is no -Wfoo flag that controls
515/// the diagnostic, this returns null.
516StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
517 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
518 return OptionTable[Info->getOptionGroupIndex()].getName();
519 return StringRef();
520}
521
Craig Topper46c8fca2013-08-29 06:06:18 +0000522static void getDiagnosticsInGroup(const WarningOption *Group,
523 SmallVectorImpl<diag::kind> &Diags) {
Daniel Dunbar3f839462011-09-29 01:47:16 +0000524 // Add the members of the option diagnostic set.
Craig Topperb1aa16a2013-08-28 04:02:50 +0000525 const int16_t *Member = DiagArrays + Group->Members;
526 for (; *Member != -1; ++Member)
527 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000528
Daniel Dunbar3f839462011-09-29 01:47:16 +0000529 // Add the members of the subgroups.
Craig Topperb1aa16a2013-08-28 04:02:50 +0000530 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
531 for (; *SubGroups != (int16_t)-1; ++SubGroups)
532 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000533}
534
Daniel Dunbar3f839462011-09-29 01:47:16 +0000535bool DiagnosticIDs::getDiagnosticsInGroup(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000536 StringRef Group,
537 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000538 const WarningOption *Found =
Craig Topper354f20a2013-08-29 05:18:04 +0000539 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Group,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000540 WarningOptionCompare);
541 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000542 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000543 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000544
Craig Topper46c8fca2013-08-29 06:06:18 +0000545 ::getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000546 return false;
547}
548
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000549void DiagnosticIDs::getAllDiagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000550 SmallVectorImpl<diag::kind> &Diags) const {
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000551 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
552 Diags.push_back(StaticDiagInfo[i].DiagID);
553}
554
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000555StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
556 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000557 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000558 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
559 i != e; ++i) {
560 // Don't suggest ignored warning flags.
561 if (!i->Members && !i->SubGroups)
562 continue;
563
564 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000565 if (Distance == BestDistance) {
566 // Two matches with the same distance, don't prefer one over the other.
567 Best = "";
568 } else if (Distance < BestDistance) {
569 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000570 Best = i->getName();
571 BestDistance = Distance;
572 }
573 }
574
575 return Best;
576}
577
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000578/// ProcessDiag - This is the method used to report a diagnostic that is
579/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000580bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000581 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000582
583 if (Diag.SuppressAllDiagnostics)
584 return false;
585
586 assert(Diag.getClient() && "DiagnosticClient not set!");
587
588 // Figure out the diagnostic level of this message.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000589 unsigned DiagID = Info.getID();
Jordan Rosec6d64a22012-07-11 16:50:36 +0000590 DiagnosticIDs::Level DiagLevel
591 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000592
593 if (DiagLevel != DiagnosticIDs::Note) {
594 // Record that a fatal error occurred only when we see a second
595 // non-note diagnostic. This allows notes to be attached to the
596 // fatal error, but suppresses any diagnostics that follow those
597 // notes.
598 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
599 Diag.FatalErrorOccurred = true;
600
601 Diag.LastDiagLevel = DiagLevel;
602 }
603
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000604 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
605 if (DiagLevel >= DiagnosticIDs::Error) {
606 ++Diag.TrapNumErrorsOccurred;
607 if (isUnrecoverable(DiagID))
608 ++Diag.TrapNumUnrecoverableErrorsOccurred;
609 }
610
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000611 // If a fatal error has already been emitted, silence all subsequent
612 // diagnostics.
613 if (Diag.FatalErrorOccurred) {
614 if (DiagLevel >= DiagnosticIDs::Error &&
615 Diag.Client->IncludeInDiagnosticCounts()) {
616 ++Diag.NumErrors;
617 ++Diag.NumErrorsSuppressed;
618 }
619
620 return false;
621 }
622
623 // If the client doesn't care about this message, don't issue it. If this is
624 // a note and the last real diagnostic was ignored, ignore it too.
625 if (DiagLevel == DiagnosticIDs::Ignored ||
626 (DiagLevel == DiagnosticIDs::Note &&
627 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
628 return false;
629
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000630 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000631 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000632 Diag.UnrecoverableErrorOccurred = true;
Daniel Jasper1c84c682012-09-28 15:45:07 +0000633
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000634 // Warnings which have been upgraded to errors do not prevent compilation.
635 if (isDefaultMappingAsError(DiagID))
636 Diag.UncompilableErrorOccurred = true;
637
Daniel Jasper1c84c682012-09-28 15:45:07 +0000638 Diag.ErrorOccurred = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000639 if (Diag.Client->IncludeInDiagnosticCounts()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000640 ++Diag.NumErrors;
641 }
642
Douglas Gregorf1d59482011-08-17 19:13:00 +0000643 // If we've emitted a lot of errors, emit a fatal error instead of it to
644 // stop a flood of bogus errors.
645 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
646 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000647 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000648 return false;
649 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000650 }
651
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000652 // Finally, report it.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000653 EmitDiag(Diag, DiagLevel);
654 return true;
655}
656
657void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
658 Diagnostic Info(&Diag);
659 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
660
David Blaikied6471f72011-09-25 23:23:43 +0000661 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000662 if (Diag.Client->IncludeInDiagnosticCounts()) {
663 if (DiagLevel == DiagnosticIDs::Warning)
664 ++Diag.NumWarnings;
665 }
666
667 Diag.CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000668}
John McCall923cd572011-06-15 21:46:43 +0000669
670bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
671 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
672 // Custom diagnostics.
673 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
674 }
675
676 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000677 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000678 return false;
679
680 if (DiagID == diag::err_unavailable ||
681 DiagID == diag::err_unavailable_message)
682 return false;
683
John McCallf85e1932011-06-15 23:02:42 +0000684 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000685 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000686 return false;
687
John McCall923cd572011-06-15 21:46:43 +0000688 return true;
689}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000690
691bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
692 unsigned cat = getCategoryNumberForDiag(DiagID);
693 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
694}