blob: 2f28c5dbf79a56788fbaafbaf4e45d41df934b64 [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
Benjamin Kramerd49cb202012-02-15 20:57:03 +000055 uint16_t OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000056
57 uint16_t DescriptionLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000058 const char *DescriptionStr;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000059
Benjamin Kramerd49cb202012-02-15 20:57:03 +000060 unsigned getOptionGroupIndex() const {
61 return OptionGroupIndex;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000062 }
63
Chris Lattner5f9e2722011-07-23 10:55:15 +000064 StringRef getDescription() const {
65 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000066 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000067
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000068 bool operator<(const StaticDiagInfoRec &RHS) const {
69 return DiagID < RHS.DiagID;
70 }
71};
72
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000073template <size_t SizeOfStr, typename FieldType>
74class StringSizerHelper {
75 char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
76public:
77 enum { Size = SizeOfStr };
78};
79
80} // namespace anonymous
81
82#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000083
84static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000085#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
Daniel Dunbar4213df32011-09-29 00:34:06 +000086 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
Benjamin Kramerf94d3922012-02-09 19:38:26 +000087 CATEGORY) \
Daniel Dunbar4213df32011-09-29 00:34:06 +000088 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
Benjamin Kramerd49cb202012-02-15 20:57:03 +000089 NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \
90 STR_SIZE(DESC, uint16_t), DESC },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000091#include "clang/Basic/DiagnosticCommonKinds.inc"
92#include "clang/Basic/DiagnosticDriverKinds.inc"
93#include "clang/Basic/DiagnosticFrontendKinds.inc"
Chandler Carrutha2398d72011-12-09 00:02:23 +000094#include "clang/Basic/DiagnosticSerializationKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000095#include "clang/Basic/DiagnosticLexKinds.inc"
96#include "clang/Basic/DiagnosticParseKinds.inc"
97#include "clang/Basic/DiagnosticASTKinds.inc"
98#include "clang/Basic/DiagnosticSemaKinds.inc"
99#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000100#undef DIAG
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000101 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000102};
103
104static const unsigned StaticDiagInfoSize =
105 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
106
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000107/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
108/// or null if the ID is invalid.
109static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000110 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
111#ifndef NDEBUG
112 static bool IsFirst = true;
113 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000114 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000115 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
116 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000117 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000118
119 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
120 "Improperly sorted diag info");
121 }
122 IsFirst = false;
123 }
124#endif
125
126 // Search the diagnostic table with a binary search.
Jeffrey Yasskin7c5109b2011-08-13 05:47:04 +0000127 StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000129
130 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000131 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
132 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000133 Found->DiagID != DiagID)
134 return 0;
135
136 return Found;
137}
138
Daniel Dunbara5e41332011-09-29 01:52:06 +0000139static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000140 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
Daniel Dunbara5e41332011-09-29 01:52:06 +0000141 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000142
Daniel Dunbara5e41332011-09-29 01:52:06 +0000143 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
144 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
145
146 if (StaticInfo->WarnNoWerror) {
147 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000148 "Unexpected mapping with no-Werror bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000149 Info.setNoWarningAsError(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000150 }
151
Daniel Dunbara5e41332011-09-29 01:52:06 +0000152 if (StaticInfo->WarnShowInSystemHeader) {
153 assert(Info.getMapping() == diag::MAP_WARNING &&
Daniel Dunbar4213df32011-09-29 00:34:06 +0000154 "Unexpected mapping with show-in-system-header bit!");
Daniel Dunbara5e41332011-09-29 01:52:06 +0000155 Info.setShowInSystemHeader(true);
Daniel Dunbar4213df32011-09-29 00:34:06 +0000156 }
Daniel Dunbar4213df32011-09-29 00:34:06 +0000157 }
Daniel Dunbara5e41332011-09-29 01:52:06 +0000158
159 return Info;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000160}
161
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000162/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000163/// DiagID belongs to, or 0 if no category.
164unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
165 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
166 return Info->Category;
167 return 0;
168}
169
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000170namespace {
171 // The diagnostic category names.
172 struct StaticDiagCategoryRec {
173 const char *NameStr;
174 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000175
Chris Lattner5f9e2722011-07-23 10:55:15 +0000176 StringRef getName() const {
177 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000178 }
179 };
180}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000181
Daniel Dunbarba494c62011-09-29 01:42:25 +0000182// Unfortunately, the split between DiagnosticIDs and Diagnostic is not
183// particularly clean, but for now we just implement this method here so we can
184// access GetDefaultDiagMapping.
185DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
186 diag::kind Diag)
187{
188 std::pair<iterator, bool> Result = DiagMap.insert(
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000189 std::make_pair(Diag, DiagnosticMappingInfo()));
Daniel Dunbarba494c62011-09-29 01:42:25 +0000190
191 // Initialize the entry if we added it.
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000192 if (Result.second)
Daniel Dunbara5e41332011-09-29 01:52:06 +0000193 Result.first->second = GetDefaultDiagMappingInfo(Diag);
Daniel Dunbarba494c62011-09-29 01:42:25 +0000194
195 return Result.first->second;
196}
197
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000198static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000199#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000200#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000201#include "clang/Basic/DiagnosticGroups.inc"
202#undef GET_CATEGORY_TABLE
203 { 0, 0 }
204};
205
206/// getNumberOfCategories - Return the number of categories
207unsigned DiagnosticIDs::getNumberOfCategories() {
208 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
209}
210
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000211/// getCategoryNameFromID - Given a category ID, return the name of the
212/// category, an empty string if CategoryID is zero, or null if CategoryID is
213/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000214StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000215 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000216 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000217 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000218}
219
220
221
222DiagnosticIDs::SFINAEResponse
223DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
224 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000225 if (Info->AccessControl)
226 return SFINAE_AccessControl;
227
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000228 if (!Info->SFINAE)
229 return SFINAE_Report;
230
231 if (Info->Class == CLASS_ERROR)
232 return SFINAE_SubstitutionFailure;
233
234 // Suppress notes, warnings, and extensions;
235 return SFINAE_Suppress;
236 }
237
238 return SFINAE_Report;
239}
240
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000241/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000242///
243static unsigned getBuiltinDiagClass(unsigned DiagID) {
244 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
245 return Info->Class;
246 return ~0U;
247}
248
249//===----------------------------------------------------------------------===//
250// Custom Diagnostic information
251//===----------------------------------------------------------------------===//
252
253namespace clang {
254 namespace diag {
255 class CustomDiagInfo {
256 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
257 std::vector<DiagDesc> DiagInfo;
258 std::map<DiagDesc, unsigned> DiagIDs;
259 public:
260
261 /// getDescription - Return the description of the specified custom
262 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000263 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000264 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
265 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000266 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000267 }
268
269 /// getLevel - Return the level of the specified custom diagnostic.
270 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
271 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
272 "Invalid diagnosic ID");
273 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
274 }
275
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000277 DiagnosticIDs &Diags) {
278 DiagDesc D(L, Message);
279 // Check to see if it already exists.
280 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
281 if (I != DiagIDs.end() && I->first == D)
282 return I->second;
283
284 // If not, assign a new ID.
285 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
286 DiagIDs.insert(std::make_pair(D, ID));
287 DiagInfo.push_back(D);
288 return ID;
289 }
290 };
291
292 } // end diag namespace
293} // end clang namespace
294
295
296//===----------------------------------------------------------------------===//
297// Common Diagnostic implementation
298//===----------------------------------------------------------------------===//
299
300DiagnosticIDs::DiagnosticIDs() {
301 CustomDiagInfo = 0;
302}
303
304DiagnosticIDs::~DiagnosticIDs() {
305 delete CustomDiagInfo;
306}
307
308/// getCustomDiagID - Return an ID for a diagnostic with the specified message
309/// and level. If this is the first request for this diagnosic, it is
310/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000312 if (CustomDiagInfo == 0)
313 CustomDiagInfo = new diag::CustomDiagInfo();
314 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
315}
316
317
318/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
319/// level of the specified diagnostic ID is a Warning or Extension.
320/// This only works on builtin diagnostics, not custom ones, and is not legal to
321/// call on NOTEs.
322bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
323 return DiagID < diag::DIAG_UPPER_LIMIT &&
324 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
325}
326
327/// \brief Determine whether the given built-in diagnostic ID is a
328/// Note.
329bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
330 return DiagID < diag::DIAG_UPPER_LIMIT &&
331 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
332}
333
334/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
335/// ID is for an extension of some sort. This also returns EnabledByDefault,
336/// which is set to indicate whether the diagnostic is ignored by default (in
337/// which case -pedantic enables it) or treated as a warning/error by default.
338///
339bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
340 bool &EnabledByDefault) {
341 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
342 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
343 return false;
344
Daniel Dunbara5e41332011-09-29 01:52:06 +0000345 EnabledByDefault =
346 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000347 return true;
348}
349
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000350bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
351 if (DiagID >= diag::DIAG_UPPER_LIMIT)
352 return false;
353
Daniel Dunbara5e41332011-09-29 01:52:06 +0000354 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
Daniel Dunbar76101cf2011-09-29 01:01:08 +0000355}
356
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000357/// getDescription - Given a diagnostic ID, return a description of the
358/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000359StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000360 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000361 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000362 return CustomDiagInfo->getDescription(DiagID);
363}
364
David Blaikied6471f72011-09-25 23:23:43 +0000365/// getDiagnosticLevel - Based on the way the client configured the
366/// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
367/// by consumable the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000368DiagnosticIDs::Level
369DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000370 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000371 // Handle custom diagnostics, which cannot be mapped.
372 if (DiagID >= diag::DIAG_UPPER_LIMIT)
373 return CustomDiagInfo->getLevel(DiagID);
374
375 unsigned DiagClass = getBuiltinDiagClass(DiagID);
376 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000377 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000378}
379
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000380/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000381/// object, classify the specified diagnostic ID into a Level, consumable by
382/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000383///
384/// \param Loc The source location we are interested in finding out the
385/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000386DiagnosticIDs::Level
387DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000388 SourceLocation Loc,
Daniel Dunbar1656aae2011-09-29 01:20:28 +0000389 const DiagnosticsEngine &Diag) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000390 // Specific non-error diagnostics may be mapped to various levels from ignored
391 // to error. Errors can only be mapped to fatal.
392 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
393
David Blaikied6471f72011-09-25 23:23:43 +0000394 DiagnosticsEngine::DiagStatePointsTy::iterator
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000395 Pos = Diag.GetDiagStatePointForLoc(Loc);
David Blaikied6471f72011-09-25 23:23:43 +0000396 DiagnosticsEngine::DiagState *State = Pos->State;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000397
Daniel Dunbarba494c62011-09-29 01:42:25 +0000398 // Get the mapping information, or compute it lazily.
399 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
400 (diag::kind)DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000401
Daniel Dunbarb1c99c62011-09-29 01:30:00 +0000402 switch (MappingInfo.getMapping()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000403 case diag::MAP_IGNORE:
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000404 Result = DiagnosticIDs::Ignored;
405 break;
406 case diag::MAP_WARNING:
407 Result = DiagnosticIDs::Warning;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000408 break;
409 case diag::MAP_ERROR:
410 Result = DiagnosticIDs::Error;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000411 break;
412 case diag::MAP_FATAL:
413 Result = DiagnosticIDs::Fatal;
414 break;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000415 }
416
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000417 // Upgrade ignored diagnostics if -Weverything is enabled.
418 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
419 !MappingInfo.isUser())
420 Result = DiagnosticIDs::Warning;
421
Bob Wilson18c407f2011-10-12 19:55:31 +0000422 // Ignore -pedantic diagnostics inside __extension__ blocks.
423 // (The diagnostics controlled by -pedantic are the extension diagnostics
424 // that are not enabled by default.)
Daniel Dunbarf3dee202011-11-28 22:19:36 +0000425 bool EnabledByDefault = false;
Bob Wilson18c407f2011-10-12 19:55:31 +0000426 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
427 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000428 return DiagnosticIDs::Ignored;
429
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000430 // For extension diagnostics that haven't been explicitly mapped, check if we
431 // should upgrade the diagnostic.
432 if (IsExtensionDiag && !MappingInfo.isUser()) {
433 switch (Diag.ExtBehavior) {
434 case DiagnosticsEngine::Ext_Ignore:
435 break;
436 case DiagnosticsEngine::Ext_Warn:
437 // Upgrade ignored diagnostics to warnings.
438 if (Result == DiagnosticIDs::Ignored)
439 Result = DiagnosticIDs::Warning;
440 break;
441 case DiagnosticsEngine::Ext_Error:
442 // Upgrade ignored or warning diagnostics to errors.
443 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
444 Result = DiagnosticIDs::Error;
445 break;
446 }
447 }
448
449 // At this point, ignored errors can no longer be upgraded.
450 if (Result == DiagnosticIDs::Ignored)
451 return Result;
452
453 // Honor -w, which is lower in priority than pedantic-errors, but higher than
454 // -Werror.
455 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
456 return DiagnosticIDs::Ignored;
457
458 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
459 if (Result == DiagnosticIDs::Warning) {
460 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
461 Result = DiagnosticIDs::Error;
462 }
463
464 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
465 // disabled.
466 if (Result == DiagnosticIDs::Error) {
467 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
468 Result = DiagnosticIDs::Fatal;
469 }
470
Daniel Dunbaraeacae52011-09-29 02:03:01 +0000471 // If we are in a system header, we ignore it. We look at the diagnostic class
472 // because we also want to ignore extensions and warnings in -Werror and
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000473 // -pedantic-errors modes, which *map* warnings/extensions to errors.
474 if (Result >= DiagnosticIDs::Warning &&
475 DiagClass != CLASS_ERROR &&
476 // Custom diagnostics always are emitted in system headers.
477 DiagID < diag::DIAG_UPPER_LIMIT &&
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000478 !MappingInfo.hasShowInSystemHeader() &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000479 Diag.SuppressSystemWarnings &&
480 Loc.isValid() &&
481 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000482 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000483 return DiagnosticIDs::Ignored;
484
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000485 return Result;
486}
487
Daniel Dunbar3f839462011-09-29 01:47:16 +0000488struct clang::WarningOption {
489 // Be safe with the size of 'NameLen' because we don't statically check if
490 // the size will fit in the field; the struct size won't decrease with a
491 // shorter type anyway.
492 size_t NameLen;
493 const char *NameStr;
494 const short *Members;
495 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000496
Daniel Dunbar3f839462011-09-29 01:47:16 +0000497 StringRef getName() const {
498 return StringRef(NameStr, NameLen);
499 }
500};
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000501
502#define GET_DIAG_ARRAYS
503#include "clang/Basic/DiagnosticGroups.inc"
504#undef GET_DIAG_ARRAYS
505
506// Second the table of options, sorted by name for fast binary lookup.
507static const WarningOption OptionTable[] = {
508#define GET_DIAG_TABLE
509#include "clang/Basic/DiagnosticGroups.inc"
510#undef GET_DIAG_TABLE
511};
512static const size_t OptionTableSize =
513sizeof(OptionTable) / sizeof(OptionTable[0]);
514
515static bool WarningOptionCompare(const WarningOption &LHS,
516 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000517 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000518}
519
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000520/// getWarningOptionForDiag - Return the lowest-level warning option that
521/// enables the specified diagnostic. If there is no -Wfoo flag that controls
522/// the diagnostic, this returns null.
523StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
524 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
525 return OptionTable[Info->getOptionGroupIndex()].getName();
526 return StringRef();
527}
528
Daniel Dunbar3f839462011-09-29 01:47:16 +0000529void DiagnosticIDs::getDiagnosticsInGroup(
530 const WarningOption *Group,
531 llvm::SmallVectorImpl<diag::kind> &Diags) const
532{
533 // Add the members of the option diagnostic set.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000534 if (const short *Member = Group->Members) {
535 for (; *Member != -1; ++Member)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000536 Diags.push_back(*Member);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000537 }
538
Daniel Dunbar3f839462011-09-29 01:47:16 +0000539 // Add the members of the subgroups.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000540 if (const short *SubGroups = Group->SubGroups) {
541 for (; *SubGroups != (short)-1; ++SubGroups)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000542 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000543 }
544}
545
Daniel Dunbar3f839462011-09-29 01:47:16 +0000546bool DiagnosticIDs::getDiagnosticsInGroup(
547 StringRef Group,
548 llvm::SmallVectorImpl<diag::kind> &Diags) const
549{
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000550 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000551 const WarningOption *Found =
552 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
553 WarningOptionCompare);
554 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000555 Found->getName() != Group)
Daniel Dunbar3f839462011-09-29 01:47:16 +0000556 return true; // Option not found.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000557
Daniel Dunbar3f839462011-09-29 01:47:16 +0000558 getDiagnosticsInGroup(Found, Diags);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000559 return false;
560}
561
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000562void DiagnosticIDs::getAllDiagnostics(
563 llvm::SmallVectorImpl<diag::kind> &Diags) const {
564 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
565 Diags.push_back(StaticDiagInfo[i].DiagID);
566}
567
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000568StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
569 StringRef Best;
Benjamin Kramerdce63272011-11-15 12:26:39 +0000570 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000571 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
572 i != e; ++i) {
573 // Don't suggest ignored warning flags.
574 if (!i->Members && !i->SubGroups)
575 continue;
576
577 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
Benjamin Kramerdce63272011-11-15 12:26:39 +0000578 if (Distance == BestDistance) {
579 // Two matches with the same distance, don't prefer one over the other.
580 Best = "";
581 } else if (Distance < BestDistance) {
582 // This is a better match.
Benjamin Kramera70cb9d2011-11-14 23:30:34 +0000583 Best = i->getName();
584 BestDistance = Distance;
585 }
586 }
587
588 return Best;
589}
590
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000591/// ProcessDiag - This is the method used to report a diagnostic that is
592/// finally fully formed.
David Blaikied6471f72011-09-25 23:23:43 +0000593bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
David Blaikie40847cf2011-09-26 01:18:08 +0000594 Diagnostic Info(&Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000595
596 if (Diag.SuppressAllDiagnostics)
597 return false;
598
599 assert(Diag.getClient() && "DiagnosticClient not set!");
600
601 // Figure out the diagnostic level of this message.
602 DiagnosticIDs::Level DiagLevel;
603 unsigned DiagID = Info.getID();
604
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000605 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
606 // Handle custom diagnostics, which cannot be mapped.
607 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000608 } else {
609 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
610 // the diagnostic level was for the previous diagnostic so that it is
611 // filtered the same as the previous diagnostic.
612 unsigned DiagClass = getBuiltinDiagClass(DiagID);
613 if (DiagClass == CLASS_NOTE) {
614 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000615 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000616 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
617 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000618 }
619 }
620
621 if (DiagLevel != DiagnosticIDs::Note) {
622 // Record that a fatal error occurred only when we see a second
623 // non-note diagnostic. This allows notes to be attached to the
624 // fatal error, but suppresses any diagnostics that follow those
625 // notes.
626 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
627 Diag.FatalErrorOccurred = true;
628
629 Diag.LastDiagLevel = DiagLevel;
630 }
631
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000632 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
633 if (DiagLevel >= DiagnosticIDs::Error) {
634 ++Diag.TrapNumErrorsOccurred;
635 if (isUnrecoverable(DiagID))
636 ++Diag.TrapNumUnrecoverableErrorsOccurred;
637 }
638
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000639 // If a fatal error has already been emitted, silence all subsequent
640 // diagnostics.
641 if (Diag.FatalErrorOccurred) {
642 if (DiagLevel >= DiagnosticIDs::Error &&
643 Diag.Client->IncludeInDiagnosticCounts()) {
644 ++Diag.NumErrors;
645 ++Diag.NumErrorsSuppressed;
646 }
647
648 return false;
649 }
650
651 // If the client doesn't care about this message, don't issue it. If this is
652 // a note and the last real diagnostic was ignored, ignore it too.
653 if (DiagLevel == DiagnosticIDs::Ignored ||
654 (DiagLevel == DiagnosticIDs::Note &&
655 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
656 return false;
657
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000658 if (DiagLevel >= DiagnosticIDs::Error) {
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000659 if (isUnrecoverable(DiagID))
Douglas Gregor85bea972011-07-06 17:40:26 +0000660 Diag.UnrecoverableErrorOccurred = true;
Douglas Gregor85bea972011-07-06 17:40:26 +0000661
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000662 if (Diag.Client->IncludeInDiagnosticCounts()) {
663 Diag.ErrorOccurred = true;
664 ++Diag.NumErrors;
665 }
666
Douglas Gregorf1d59482011-08-17 19:13:00 +0000667 // If we've emitted a lot of errors, emit a fatal error instead of it to
668 // stop a flood of bogus errors.
669 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
670 DiagLevel == DiagnosticIDs::Error) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000671 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
Douglas Gregorf1d59482011-08-17 19:13:00 +0000672 return false;
673 }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000674 }
675
Douglas Gregor4814fb52011-02-03 23:41:12 +0000676 // If we have any Fix-Its, make sure that all of the Fix-Its point into
Chandler Carruth3201f382011-07-26 05:17:23 +0000677 // source locations that aren't macro expansions. If any point into macro
678 // expansions, remove all of the Fix-Its.
Argyrios Kyrtzidiscbf46a02012-02-03 05:58:22 +0000679 for (unsigned I = 0, N = Diag.FixItHints.size(); I != N; ++I) {
Douglas Gregor4814fb52011-02-03 23:41:12 +0000680 const FixItHint &FixIt = Diag.FixItHints[I];
681 if (FixIt.RemoveRange.isInvalid() ||
682 FixIt.RemoveRange.getBegin().isMacroID() ||
683 FixIt.RemoveRange.getEnd().isMacroID()) {
Argyrios Kyrtzidiscbf46a02012-02-03 05:58:22 +0000684 Diag.FixItHints.clear();
Douglas Gregor4814fb52011-02-03 23:41:12 +0000685 break;
686 }
687 }
688
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000689 // Finally, report it.
David Blaikied6471f72011-09-25 23:23:43 +0000690 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000691 if (Diag.Client->IncludeInDiagnosticCounts()) {
692 if (DiagLevel == DiagnosticIDs::Warning)
693 ++Diag.NumWarnings;
694 }
695
696 Diag.CurDiagID = ~0U;
697
698 return true;
699}
John McCall923cd572011-06-15 21:46:43 +0000700
701bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
702 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
703 // Custom diagnostics.
704 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
705 }
706
707 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000708 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000709 return false;
710
711 if (DiagID == diag::err_unavailable ||
712 DiagID == diag::err_unavailable_message)
713 return false;
714
John McCallf85e1932011-06-15 23:02:42 +0000715 // Currently we consider all ARC errors as recoverable.
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000716 if (isARCDiagnostic(DiagID))
John McCallf85e1932011-06-15 23:02:42 +0000717 return false;
718
John McCall923cd572011-06-15 21:46:43 +0000719 return true;
720}
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000721
722bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
723 unsigned cat = getCategoryNumberForDiag(DiagID);
724 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
725}
726