blob: 754d7c0322bf72a066d1f070881c7444f674b76b [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
14#include "clang/AST/ASTDiagnostic.h"
15#include "clang/Analysis/AnalysisDiagnostic.h"
16#include "clang/Basic/DiagnosticIDs.h"
John McCall923cd572011-06-15 21:46:43 +000017#include "clang/Basic/DiagnosticCategories.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Driver/DriverDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Lex/LexDiagnostic.h"
22#include "clang/Parse/ParseDiagnostic.h"
23#include "clang/Sema/SemaDiagnostic.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000024#include "clang/Serialization/SerializationDiagnostic.h"
Daniel Dunbar3f839462011-09-29 01:47:16 +000025#include "llvm/ADT/SmallVector.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000026#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000027
28#include <map>
29using namespace clang;
30
31//===----------------------------------------------------------------------===//
32// Builtin Diagnostic information
33//===----------------------------------------------------------------------===//
34
35namespace {
36
37// Diagnostic classes.
38enum {
39 CLASS_NOTE = 0x01,
40 CLASS_WARNING = 0x02,
41 CLASS_EXTENSION = 0x03,
42 CLASS_ERROR = 0x04
43};
44
45struct StaticDiagInfoRec {
46 unsigned short DiagID;
47 unsigned Mapping : 3;
48 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000049 unsigned SFINAE : 1;
50 unsigned AccessControl : 1;
Daniel Dunbar4213df32011-09-29 00:34:06 +000051 unsigned WarnNoWerror : 1;
52 unsigned WarnShowInSystemHeader : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000053 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000054
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000055 uint8_t OptionGroupLen;
56
57 uint16_t DescriptionLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000058
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000059 const char *OptionGroupStr;
60
61 const char *DescriptionStr;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000062
Chris Lattner5f9e2722011-07-23 10:55:15 +000063 StringRef getOptionGroup() const {
64 return StringRef(OptionGroupStr, OptionGroupLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000065 }
66
Chris Lattner5f9e2722011-07-23 10:55:15 +000067 StringRef getDescription() const {
68 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000069 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000070
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000071 bool operator<(const StaticDiagInfoRec &RHS) const {
72 return DiagID < RHS.DiagID;
73 }
74};
75
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000076template <size_t SizeOfStr, typename FieldType>
77class StringSizerHelper {
78 char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
79public:
80 enum { Size = SizeOfStr };
81};
82
83} // namespace anonymous
84
85#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000086
87static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000088#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Daniel Dunbar4213df32011-09-29 00:34:06 +000089 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
Benjamin Kramerf94d3922012-02-09 19:38:26 +000090 CATEGORY) \
Daniel Dunbar4213df32011-09-29 00:34:06 +000091 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
92 NOWERROR, SHOWINSYSHEADER, CATEGORY, \
David Blaikieceb15652012-02-15 19:45:34 +000093 STR_SIZE(GROUP, uint8_t), \
Benjamin Kramerf94d3922012-02-09 19:38:26 +000094 STR_SIZE(DESC, uint16_t), \
David Blaikieceb15652012-02-15 19:45:34 +000095 GROUP, DESC },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000096#include "clang/Basic/DiagnosticCommonKinds.inc"
97#include "clang/Basic/DiagnosticDriverKinds.inc"
98#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +000099#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000100#include "clang/Basic/DiagnosticLexKinds.inc"
101#include "clang/Basic/DiagnosticParseKinds.inc"
102#include "clang/Basic/DiagnosticASTKinds.inc"
103#include "clang/Basic/DiagnosticSemaKinds.inc"
104#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000105#undef DIAG
David Blaikieceb15652012-02-15 19:45:34 +0000106 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000107};
108
109static const unsigned StaticDiagInfoSize =
110 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
111
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000112/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
113/// or null if the ID is invalid.
114static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000115 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
116#ifndef NDEBUG
117 static bool IsFirst = true;
118 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000119 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000120 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
121 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000122 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000123
124 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
125 "Improperly sorted diag info");
126 }
127 IsFirst = false;
128 }
129#endif
130
131 // Search the diagnostic table with a binary search.
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000132 StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
David Blaikieceb15652012-02-15 19:45:34 +0000133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000134
135 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000136 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
137 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000138 Found->DiagID != DiagID)
139 return 0;
140
141 return Found;
142}
143
Daniel Dunbara5e41332011-09-29 01:52:06 +0000144static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000145 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000146 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000147
Daniel Dunbara5e41332011-09-29 01:52:06 +0000148 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
149 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
150
151 if (StaticInfo->WarnNoWerror) {
152 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000153 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000154 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000155 }
156
Daniel Dunbara5e41332011-09-29 01:52:06 +0000157 if (StaticInfo->WarnShowInSystemHeader) {
158 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000159 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000160 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000161 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000162 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000163
164 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000165}
166
167/// getWarningOptionForDiag - Return the lowest-level warning option that
168/// enables the specified diagnostic. If there is no -Wfoo flag that controls
169/// the diagnostic, this returns null.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000171 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000172 return Info->getOptionGroup();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173 return StringRef();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000174}
175
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000176/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000177/// DiagID belongs to, or 0 if no category.
178unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
179 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
180 return Info->Category;
181 return 0;
182}
183
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000184namespace {
185 // The diagnostic category names.
186 struct StaticDiagCategoryRec {
187 const char *NameStr;
188 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000189
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 StringRef getName() const {
191 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000192 }
193 };
194}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000195
Daniel Dunbarba494c62011-09-29 01:42:25 +0000196// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
197// particularly clean, but for now we just implement this method here so we can
198// access GetDefaultDiagMapping.
199DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
200 diag::kind Diag)
201{
202 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000203 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000204
205 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000206 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000207 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000208
209 return Result.first->second;
210}
211
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000212static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000213#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000214#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000215#include "clang/Basic/DiagnosticGroups.inc"
216#undef GET_CATEGORY_TABLE
217 { 0, 0 }
218};
219
220/// getNumberOfCategories - Return the number of categories
221unsigned DiagnosticIDs::getNumberOfCategories() {
222 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
223}
224
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000225/// getCategoryNameFromID - Given a category ID, return the name of the
226/// category, an empty string if CategoryID is zero, or null if CategoryID is
227/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000229 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000230 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000231 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000232}
233
234
235
236DiagnosticIDs::SFINAEResponse
237DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
238 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000239 if (Info->AccessControl)
240 return SFINAE_AccessControl;
241
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000242 if (!Info->SFINAE)
243 return SFINAE_Report;
244
245 if (Info->Class == CLASS_ERROR)
246 return SFINAE_SubstitutionFailure;
247
248 // Suppress notes, warnings, and extensions;
249 return SFINAE_Suppress;
250 }
251
252 return SFINAE_Report;
253}
254
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000255/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-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 Lattner5f9e2722011-07-23 10:55:15 +0000277 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000278 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
279 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000280 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000281 }
282
283 /// getLevel - Return the level of the specified custom diagnostic.
284 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
285 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
286 "Invalid diagnosic ID");
287 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
288 }
289
Chris Lattner5f9e2722011-07-23 10:55:15 +0000290 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-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
306 } // end diag namespace
307} // end clang namespace
308
309
310//===----------------------------------------------------------------------===//
311// Common Diagnostic implementation
312//===----------------------------------------------------------------------===//
313
314DiagnosticIDs::DiagnosticIDs() {
315 CustomDiagInfo = 0;
316}
317
318DiagnosticIDs::~DiagnosticIDs() {
319 delete CustomDiagInfo;
320}
321
322/// getCustomDiagID - Return an ID for a diagnostic with the specified message
323/// and level. If this is the first request for this diagnosic, it is
324/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000325unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000326 if (CustomDiagInfo == 0)
327 CustomDiagInfo = new diag::CustomDiagInfo();
328 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
329}
330
331
332/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
333/// level of the specified diagnostic ID is a Warning or Extension.
334/// This only works on builtin diagnostics, not custom ones, and is not legal to
335/// call on NOTEs.
336bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
337 return DiagID < diag::DIAG_UPPER_LIMIT &&
338 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
339}
340
341/// \brief Determine whether the given built-in diagnostic ID is a
342/// Note.
343bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
344 return DiagID < diag::DIAG_UPPER_LIMIT &&
345 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
346}
347
348/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
349/// ID is for an extension of some sort. This also returns EnabledByDefault,
350/// which is set to indicate whether the diagnostic is ignored by default (in
351/// which case -pedantic enables it) or treated as a warning/error by default.
352///
353bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
354 bool &EnabledByDefault) {
355 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
356 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
357 return false;
358
Daniel Dunbara5e41332011-09-29 01:52:06 +0000359 EnabledByDefault =
360 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000361 return true;
362}
363
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000364bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
365 if (DiagID >= diag::DIAG_UPPER_LIMIT)
366 return false;
367
Daniel Dunbara5e41332011-09-29 01:52:06 +0000368 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000369}
370
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000371/// getDescription - Given a diagnostic ID, return a description of the
372/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000373StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000374 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000375 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000376 return CustomDiagInfo->getDescription(DiagID);
377}
378
David Blaikied6471f72011-09-25 23:23:43 +0000379/// getDiagnosticLevel - Based on the way the client configured the
380/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
381/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000382DiagnosticIDs::Level
383DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000384 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000385 // Handle custom diagnostics, which cannot be mapped.
386 if (DiagID >= diag::DIAG_UPPER_LIMIT)
387 return CustomDiagInfo->getLevel(DiagID);
388
389 unsigned DiagClass = getBuiltinDiagClass(DiagID);
390 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000391 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000392}
393
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000394/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000395/// object, classify the specified diagnostic ID into a Level, consumable by
396/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000397///
398/// \param Loc The source location we are interested in finding out the
399/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000400DiagnosticIDs::Level
401DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000402 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000403 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000404 // Specific non-error diagnostics may be mapped to various levels from ignored
405 // to error. Errors can only be mapped to fatal.
406 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
407
David Blaikied6471f72011-09-25 23:23:43 +0000408 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000409 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000410 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000411
Daniel Dunbarba494c62011-09-29 01:42:25 +0000412 // Get the mapping information, or compute it lazily.
413 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
414 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000415
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000416 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000417 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000418 Result = DiagnosticIDs::Ignored;
419 break;
420 case diag::MAP_WARNING:
421 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000422 break;
423 case diag::MAP_ERROR:
424 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000425 break;
426 case diag::MAP_FATAL:
427 Result = DiagnosticIDs::Fatal;
428 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000429 }
430
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000431 // Upgrade ignored diagnostics if -Weverything is enabled.
432 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
433 !MappingInfo.isUser())
434 Result = DiagnosticIDs::Warning;
435
Bob Wilson18c407f2011-10-12 19:55:31 +0000436 // Ignore -pedantic diagnostics inside __extension__ blocks.
437 // (The diagnostics controlled by -pedantic are the extension diagnostics
438 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000439 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000440 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
441 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000442 return DiagnosticIDs::Ignored;
443
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000444 // For extension diagnostics that haven't been explicitly mapped, check if we
445 // should upgrade the diagnostic.
446 if (IsExtensionDiag && !MappingInfo.isUser()) {
447 switch (Diag.ExtBehavior) {
448 case DiagnosticsEngine::Ext_Ignore:
449 break;
450 case DiagnosticsEngine::Ext_Warn:
451 // Upgrade ignored diagnostics to warnings.
452 if (Result == DiagnosticIDs::Ignored)
453 Result = DiagnosticIDs::Warning;
454 break;
455 case DiagnosticsEngine::Ext_Error:
456 // Upgrade ignored or warning diagnostics to errors.
457 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
458 Result = DiagnosticIDs::Error;
459 break;
460 }
461 }
462
463 // At this point, ignored errors can no longer be upgraded.
464 if (Result == DiagnosticIDs::Ignored)
465 return Result;
466
467 // Honor -w, which is lower in priority than pedantic-errors, but higher than
468 // -Werror.
469 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
470 return DiagnosticIDs::Ignored;
471
472 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
473 if (Result == DiagnosticIDs::Warning) {
474 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
475 Result = DiagnosticIDs::Error;
476 }
477
478 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
479 // disabled.
480 if (Result == DiagnosticIDs::Error) {
481 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
482 Result = DiagnosticIDs::Fatal;
483 }
484
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000485 // If we are in a system header, we ignore it. We look at the diagnostic class
486 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000487 // -pedantic-errors modes, which *map* warnings/extensions to errors.
488 if (Result >= DiagnosticIDs::Warning &&
489 DiagClass != CLASS_ERROR &&
490 // Custom diagnostics always are emitted in system headers.
491 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000492 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000493 Diag.SuppressSystemWarnings &&
494 Loc.isValid() &&
495 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000496 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000497 return DiagnosticIDs::Ignored;
498
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000499 return Result;
500}
501
Daniel Dunbar3f839462011-09-29 01:47:16 +0000502struct clang::WarningOption {
503 // Be safe with the size of 'NameLen' because we don't statically check if
504 // the size will fit in the field; the struct size won't decrease with a
505 // shorter type anyway.
506 size_t NameLen;
507 const char *NameStr;
508 const short *Members;
509 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000510
Daniel Dunbar3f839462011-09-29 01:47:16 +0000511 StringRef getName() const {
512 return StringRef(NameStr, NameLen);
513 }
514};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000515
516#define GET_DIAG_ARRAYS
517#include "clang/Basic/DiagnosticGroups.inc"
518#undef GET_DIAG_ARRAYS
519
520// Second the table of options, sorted by name for fast binary lookup.
521static const WarningOption OptionTable[] = {
522#define GET_DIAG_TABLE
523#include "clang/Basic/DiagnosticGroups.inc"
524#undef GET_DIAG_TABLE
525};
526static const size_t OptionTableSize =
527sizeof(OptionTable) / sizeof(OptionTable[0]);
528
529static bool WarningOptionCompare(const WarningOption &LHS,
530 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000531 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000532}
533
Daniel Dunbar3f839462011-09-29 01:47:16 +0000534void DiagnosticIDs::getDiagnosticsInGroup(
535 const WarningOption *Group,
536 llvm::SmallVectorImpl<diag::kind> &Diags) const
537{
538 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000539 if (const short *Member = Group->Members) {
540 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000541 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000542 }
543
Daniel Dunbar3f839462011-09-29 01:47:16 +0000544 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000545 if (const short *SubGroups = Group->SubGroups) {
546 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000547 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000548 }
549}
550
Daniel Dunbar3f839462011-09-29 01:47:16 +0000551bool DiagnosticIDs::getDiagnosticsInGroup(
552 StringRef Group,
553 llvm::SmallVectorImpl<diag::kind> &Diags) const
554{
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000555 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000556 const WarningOption *Found =
557 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
558 WarningOptionCompare);
559 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000560 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000561 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000562
Daniel Dunbar3f839462011-09-29 01:47:16 +0000563 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000564 return false;
565}
566
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000567void DiagnosticIDs::getAllDiagnostics(
568 llvm::SmallVectorImpl<diag::kind> &Diags) const {
569 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
570 Diags.push_back(StaticDiagInfo[i].DiagID);
571}
572
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000573StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
574 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000575 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000576 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
577 i != e; ++i) {
578 // Don't suggest ignored warning flags.
579 if (!i->Members && !i->SubGroups)
580 continue;
581
582 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000583 if (Distance == BestDistance) {
584 // Two matches with the same distance, don't prefer one over the other.
585 Best = "";
586 } else if (Distance < BestDistance) {
587 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000588 Best = i->getName();
589 BestDistance = Distance;
590 }
591 }
592
593 return Best;
594}
595
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000596/// ProcessDiag - This is the method used to report a diagnostic that is
597/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000598bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000599 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000600
601 if (Diag.SuppressAllDiagnostics)
602 return false;
603
604 assert(Diag.getClient() && "DiagnosticClient not set!");
605
606 // Figure out the diagnostic level of this message.
607 DiagnosticIDs::Level DiagLevel;
608 unsigned DiagID = Info.getID();
609
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000610 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
611 // Handle custom diagnostics, which cannot be mapped.
612 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000613 } else {
614 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
615 // the diagnostic level was for the previous diagnostic so that it is
616 // filtered the same as the previous diagnostic.
617 unsigned DiagClass = getBuiltinDiagClass(DiagID);
618 if (DiagClass == CLASS_NOTE) {
619 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000620 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000621 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
622 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000623 }
624 }
625
626 if (DiagLevel != DiagnosticIDs::Note) {
627 // Record that a fatal error occurred only when we see a second
628 // non-note diagnostic. This allows notes to be attached to the
629 // fatal error, but suppresses any diagnostics that follow those
630 // notes.
631 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
632 Diag.FatalErrorOccurred = true;
633
634 Diag.LastDiagLevel = DiagLevel;
635 }
636
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000637 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
638 if (DiagLevel >= DiagnosticIDs::Error) {
639 ++Diag.TrapNumErrorsOccurred;
640 if (isUnrecoverable(DiagID))
641 ++Diag.TrapNumUnrecoverableErrorsOccurred;
642 }
643
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000644 // If a fatal error has already been emitted, silence all subsequent
645 // diagnostics.
646 if (Diag.FatalErrorOccurred) {
647 if (DiagLevel >= DiagnosticIDs::Error &&
648 Diag.Client->IncludeInDiagnosticCounts()) {
649 ++Diag.NumErrors;
650 ++Diag.NumErrorsSuppressed;
651 }
652
653 return false;
654 }
655
656 // If the client doesn't care about this message, don't issue it. If this is
657 // a note and the last real diagnostic was ignored, ignore it too.
658 if (DiagLevel == DiagnosticIDs::Ignored ||
659 (DiagLevel == DiagnosticIDs::Note &&
660 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
661 return false;
662
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000663 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000664 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000665 Diag.UnrecoverableErrorOccurred = true;
Douglas Gregor85bea972011-07-06 17:40:26 +0000666
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000667 if (Diag.Client->IncludeInDiagnosticCounts()) {
668 Diag.ErrorOccurred = true;
669 ++Diag.NumErrors;
670 }
671
Douglas Gregorf1d59482011-08-17 19:13:00 +0000672 // If we've emitted a lot of errors, emit a fatal error instead of it to
673 // stop a flood of bogus errors.
674 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
675 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000676 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000677 return false;
678 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000679 }
680
Douglas Gregor4814fb52011-02-03 23:41:12 +0000681 // If we have any Fix-Its, make sure that all of the Fix-Its point into
Chandler Carruth3201f382011-07-26 05:17:23 +0000682 // source locations that aren't macro expansions. If any point into macro
683 // expansions, remove all of the Fix-Its.
Argyrios Kyrtzidiscbf46a02012-02-03 05:58:22 +0000684 for (unsigned I = 0, N = Diag.FixItHints.size(); I != N; ++I) {
Douglas Gregor4814fb52011-02-03 23:41:12 +0000685 const FixItHint &FixIt = Diag.FixItHints[I];
686 if (FixIt.RemoveRange.isInvalid() ||
687 FixIt.RemoveRange.getBegin().isMacroID() ||
688 FixIt.RemoveRange.getEnd().isMacroID()) {
Argyrios Kyrtzidiscbf46a02012-02-03 05:58:22 +0000689 Diag.FixItHints.clear();
Douglas Gregor4814fb52011-02-03 23:41:12 +0000690 break;
691 }
692 }
693
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000694 // Finally, report it.
David Blaikied6471f72011-09-25 23:23:43 +0000695 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000696 if (Diag.Client->IncludeInDiagnosticCounts()) {
697 if (DiagLevel == DiagnosticIDs::Warning)
698 ++Diag.NumWarnings;
699 }
700
701 Diag.CurDiagID = ~0U;
702
703 return true;
704}
John McCall923cd572011-06-15 21:46:43 +0000705
706bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
707 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
708 // Custom diagnostics.
709 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
710 }
711
712 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000713 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000714 return false;
715
716 if (DiagID == diag::err_unavailable ||
717 DiagID == diag::err_unavailable_message)
718 return false;
719
John McCallf85e1932011-06-15 23:02:42 +0000720 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000721 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000722 return false;
723
John McCall923cd572011-06-15 21:46:43 +0000724 return true;
725}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000726
727bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
728 unsigned cat = getCategoryNumberForDiag(DiagID);
729 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
730}
731