blob: fbeec272d343041bc15cdce205e26bff6341515b [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"
24
25#include <map>
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Builtin Diagnostic information
30//===----------------------------------------------------------------------===//
31
32namespace {
33
34// Diagnostic classes.
35enum {
36 CLASS_NOTE = 0x01,
37 CLASS_WARNING = 0x02,
38 CLASS_EXTENSION = 0x03,
39 CLASS_ERROR = 0x04
40};
41
42struct StaticDiagInfoRec {
43 unsigned short DiagID;
44 unsigned Mapping : 3;
45 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000046 unsigned SFINAE : 1;
47 unsigned AccessControl : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000048 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000049
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000050 uint8_t NameLen;
51 uint8_t OptionGroupLen;
52
53 uint16_t DescriptionLen;
54 uint16_t BriefExplanationLen;
55 uint16_t FullExplanationLen;
56
57 const char *NameStr;
58 const char *OptionGroupStr;
59
60 const char *DescriptionStr;
61 const char *BriefExplanationStr;
62 const char *FullExplanationStr;
63
Chris Lattner5f9e2722011-07-23 10:55:15 +000064 StringRef getName() const {
65 return StringRef(NameStr, NameLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000066 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000067 StringRef getOptionGroup() const {
68 return StringRef(OptionGroupStr, OptionGroupLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000069 }
70
Chris Lattner5f9e2722011-07-23 10:55:15 +000071 StringRef getDescription() const {
72 return StringRef(DescriptionStr, DescriptionLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000073 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000074 StringRef getBriefExplanation() const {
75 return StringRef(BriefExplanationStr, BriefExplanationLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000076 }
Chris Lattner5f9e2722011-07-23 10:55:15 +000077 StringRef getFullExplanation() const {
78 return StringRef(FullExplanationStr, FullExplanationLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000079 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000080
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000081 bool operator<(const StaticDiagInfoRec &RHS) const {
82 return DiagID < RHS.DiagID;
83 }
84};
85
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000086struct StaticDiagNameIndexRec {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000087 const char *NameStr;
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000088 unsigned short DiagID;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000089 uint8_t NameLen;
90
Chris Lattner5f9e2722011-07-23 10:55:15 +000091 StringRef getName() const {
92 return StringRef(NameStr, NameLen);
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000093 }
94
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000095 bool operator<(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000096 return getName() < RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000097 }
98
99 bool operator==(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000100 return getName() == RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000101 }
102};
103
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000104template <size_t SizeOfStr, typename FieldType>
105class StringSizerHelper {
106 char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
107public:
108 enum { Size = SizeOfStr };
109};
110
111} // namespace anonymous
112
113#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000114
115static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000116#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
117 SFINAE,ACCESS,CATEGORY,BRIEF,FULL) \
118 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, CATEGORY, \
119 STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t), \
120 STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t), \
121 STR_SIZE(FULL, uint16_t), \
122 #ENUM, GROUP, DESC, BRIEF, FULL },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000123#include "clang/Basic/DiagnosticCommonKinds.inc"
124#include "clang/Basic/DiagnosticDriverKinds.inc"
125#include "clang/Basic/DiagnosticFrontendKinds.inc"
126#include "clang/Basic/DiagnosticLexKinds.inc"
127#include "clang/Basic/DiagnosticParseKinds.inc"
128#include "clang/Basic/DiagnosticASTKinds.inc"
129#include "clang/Basic/DiagnosticSemaKinds.inc"
130#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000131#undef DIAG
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000132 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000133};
134
135static const unsigned StaticDiagInfoSize =
136 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
137
138/// To be sorted before first use (since it's splitted among multiple files)
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000139static const StaticDiagNameIndexRec StaticDiagNameIndex[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000140#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000141#include "clang/Basic/DiagnosticIndexName.inc"
142#undef DIAG_NAME_INDEX
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000143 { 0, 0, 0 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000144};
145
146static const unsigned StaticDiagNameIndexSize =
147 sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000148
149/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
150/// or null if the ID is invalid.
151static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000152 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
153#ifndef NDEBUG
154 static bool IsFirst = true;
155 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000156 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000157 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
158 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000159 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000160
161 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
162 "Improperly sorted diag info");
163 }
164 IsFirst = false;
165 }
166#endif
167
168 // Search the diagnostic table with a binary search.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000169 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000170
171 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000172 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
173 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000174 Found->DiagID != DiagID)
175 return 0;
176
177 return Found;
178}
179
180static unsigned GetDefaultDiagMapping(unsigned DiagID) {
181 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
182 return Info->Mapping;
183 return diag::MAP_FATAL;
184}
185
186/// getWarningOptionForDiag - Return the lowest-level warning option that
187/// enables the specified diagnostic. If there is no -Wfoo flag that controls
188/// the diagnostic, this returns null.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000190 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000191 return Info->getOptionGroup();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000192 return StringRef();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000193}
194
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000195/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000196/// DiagID belongs to, or 0 if no category.
197unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
198 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
199 return Info->Category;
200 return 0;
201}
202
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000203namespace {
204 // The diagnostic category names.
205 struct StaticDiagCategoryRec {
206 const char *NameStr;
207 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000208
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 StringRef getName() const {
210 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000211 }
212 };
213}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000214
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000215static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000216#define GET_CATEGORY_TABLE
John McCall923cd572011-06-15 21:46:43 +0000217#define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000218#include "clang/Basic/DiagnosticGroups.inc"
219#undef GET_CATEGORY_TABLE
220 { 0, 0 }
221};
222
223/// getNumberOfCategories - Return the number of categories
224unsigned DiagnosticIDs::getNumberOfCategories() {
225 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
226}
227
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000228/// getCategoryNameFromID - Given a category ID, return the name of the
229/// category, an empty string if CategoryID is zero, or null if CategoryID is
230/// invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000232 if (CategoryID >= getNumberOfCategories())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233 return StringRef();
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000234 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000235}
236
237
238
239DiagnosticIDs::SFINAEResponse
240DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
241 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000242 if (Info->AccessControl)
243 return SFINAE_AccessControl;
244
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000245 if (!Info->SFINAE)
246 return SFINAE_Report;
247
248 if (Info->Class == CLASS_ERROR)
249 return SFINAE_SubstitutionFailure;
250
251 // Suppress notes, warnings, and extensions;
252 return SFINAE_Suppress;
253 }
254
255 return SFINAE_Report;
256}
257
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000258/// getName - Given a diagnostic ID, return its name
Chris Lattner5f9e2722011-07-23 10:55:15 +0000259StringRef DiagnosticIDs::getName(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000260 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000261 return Info->getName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000263}
264
265/// getIdFromName - Given a diagnostic name, return its ID, or 0
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266unsigned DiagnosticIDs::getIdFromName(StringRef Name) {
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000267 const StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000268 StaticDiagNameIndex + StaticDiagNameIndexSize;
269
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000270 if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000271
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000272 StaticDiagNameIndexRec Find = { Name.data(), 0, Name.size() };
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000273
274 const StaticDiagNameIndexRec *Found =
275 std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
276 if (Found == StaticDiagNameIndexEnd ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000277 Found->getName() != Name)
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000278 return diag::DIAG_UPPER_LIMIT;
279
280 return Found->DiagID;
281}
282
283/// getBriefExplanation - Given a diagnostic ID, return a brief explanation
284/// of the issue
Chris Lattner5f9e2722011-07-23 10:55:15 +0000285StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000286 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000287 return Info->getBriefExplanation();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000289}
290
291/// getFullExplanation - Given a diagnostic ID, return a full explanation
292/// of the issue
Chris Lattner5f9e2722011-07-23 10:55:15 +0000293StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000294 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000295 return Info->getFullExplanation();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296 return StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000297}
298
299/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000300///
301static unsigned getBuiltinDiagClass(unsigned DiagID) {
302 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
303 return Info->Class;
304 return ~0U;
305}
306
307//===----------------------------------------------------------------------===//
308// Custom Diagnostic information
309//===----------------------------------------------------------------------===//
310
311namespace clang {
312 namespace diag {
313 class CustomDiagInfo {
314 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
315 std::vector<DiagDesc> DiagInfo;
316 std::map<DiagDesc, unsigned> DiagIDs;
317 public:
318
319 /// getDescription - Return the description of the specified custom
320 /// diagnostic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000321 StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000322 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
323 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000324 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000325 }
326
327 /// getLevel - Return the level of the specified custom diagnostic.
328 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
329 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
330 "Invalid diagnosic ID");
331 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
332 }
333
Chris Lattner5f9e2722011-07-23 10:55:15 +0000334 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000335 DiagnosticIDs &Diags) {
336 DiagDesc D(L, Message);
337 // Check to see if it already exists.
338 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
339 if (I != DiagIDs.end() && I->first == D)
340 return I->second;
341
342 // If not, assign a new ID.
343 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
344 DiagIDs.insert(std::make_pair(D, ID));
345 DiagInfo.push_back(D);
346 return ID;
347 }
348 };
349
350 } // end diag namespace
351} // end clang namespace
352
353
354//===----------------------------------------------------------------------===//
355// Common Diagnostic implementation
356//===----------------------------------------------------------------------===//
357
358DiagnosticIDs::DiagnosticIDs() {
359 CustomDiagInfo = 0;
360}
361
362DiagnosticIDs::~DiagnosticIDs() {
363 delete CustomDiagInfo;
364}
365
366/// getCustomDiagID - Return an ID for a diagnostic with the specified message
367/// and level. If this is the first request for this diagnosic, it is
368/// registered and created, otherwise the existing ID is returned.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000369unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000370 if (CustomDiagInfo == 0)
371 CustomDiagInfo = new diag::CustomDiagInfo();
372 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
373}
374
375
376/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
377/// level of the specified diagnostic ID is a Warning or Extension.
378/// This only works on builtin diagnostics, not custom ones, and is not legal to
379/// call on NOTEs.
380bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
381 return DiagID < diag::DIAG_UPPER_LIMIT &&
382 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
383}
384
385/// \brief Determine whether the given built-in diagnostic ID is a
386/// Note.
387bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
388 return DiagID < diag::DIAG_UPPER_LIMIT &&
389 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
390}
391
392/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
393/// ID is for an extension of some sort. This also returns EnabledByDefault,
394/// which is set to indicate whether the diagnostic is ignored by default (in
395/// which case -pedantic enables it) or treated as a warning/error by default.
396///
397bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
398 bool &EnabledByDefault) {
399 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
400 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
401 return false;
402
403 EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE;
404 return true;
405}
406
407/// getDescription - Given a diagnostic ID, return a description of the
408/// issue.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000409StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000410 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000411 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000412 return CustomDiagInfo->getDescription(DiagID);
413}
414
415/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
416/// object, classify the specified diagnostic ID into a Level, consumable by
417/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000418DiagnosticIDs::Level
419DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000420 const Diagnostic &Diag,
421 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000422 // Handle custom diagnostics, which cannot be mapped.
423 if (DiagID >= diag::DIAG_UPPER_LIMIT)
424 return CustomDiagInfo->getLevel(DiagID);
425
426 unsigned DiagClass = getBuiltinDiagClass(DiagID);
427 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Ted Kremenek7decebf2011-02-25 01:28:26 +0000428 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag, mapping);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000429}
430
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000431/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000432/// object, classify the specified diagnostic ID into a Level, consumable by
433/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000434///
435/// \param Loc The source location we are interested in finding out the
436/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000437DiagnosticIDs::Level
438DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000439 SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000440 const Diagnostic &Diag,
441 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000442 // Specific non-error diagnostics may be mapped to various levels from ignored
443 // to error. Errors can only be mapped to fatal.
444 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
445
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000446 Diagnostic::DiagStatePointsTy::iterator
447 Pos = Diag.GetDiagStatePointForLoc(Loc);
448 Diagnostic::DiagState *State = Pos->State;
449
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000450 // Get the mapping information, if unset, compute it lazily.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000451 unsigned MappingInfo = Diag.getDiagnosticMappingInfo((diag::kind)DiagID,
452 State);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000453 if (MappingInfo == 0) {
454 MappingInfo = GetDefaultDiagMapping(DiagID);
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000455 Diag.setDiagnosticMappingInternal(DiagID, MappingInfo, State, false, false);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000456 }
Ted Kremenek7decebf2011-02-25 01:28:26 +0000457
458 if (mapping)
459 *mapping = (diag::Mapping) (MappingInfo & 7);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000460
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000461 bool ShouldEmitInSystemHeader = false;
462
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000463 switch (MappingInfo & 7) {
464 default: assert(0 && "Unknown mapping!");
465 case diag::MAP_IGNORE:
466 // Ignore this, unless this is an extension diagnostic and we're mapping
467 // them onto warnings or errors.
468 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
469 Diag.ExtBehavior == Diagnostic::Ext_Ignore || // Ext ignored
470 (MappingInfo & 8) != 0) // User explicitly mapped it.
471 return DiagnosticIDs::Ignored;
472 Result = DiagnosticIDs::Warning;
473 if (Diag.ExtBehavior == Diagnostic::Ext_Error) Result = DiagnosticIDs::Error;
474 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
475 Result = DiagnosticIDs::Fatal;
476 break;
477 case diag::MAP_ERROR:
478 Result = DiagnosticIDs::Error;
479 if (Diag.ErrorsAsFatal)
480 Result = DiagnosticIDs::Fatal;
481 break;
482 case diag::MAP_FATAL:
483 Result = DiagnosticIDs::Fatal;
484 break;
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000485 case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER:
486 ShouldEmitInSystemHeader = true;
487 // continue as MAP_WARNING.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000488 case diag::MAP_WARNING:
489 // If warnings are globally mapped to ignore or error, do it.
490 if (Diag.IgnoreAllWarnings)
491 return DiagnosticIDs::Ignored;
492
493 Result = DiagnosticIDs::Warning;
494
495 // If this is an extension diagnostic and we're in -pedantic-error mode, and
496 // if the user didn't explicitly map it, upgrade to an error.
497 if (Diag.ExtBehavior == Diagnostic::Ext_Error &&
498 (MappingInfo & 8) == 0 &&
499 isBuiltinExtensionDiag(DiagID))
500 Result = DiagnosticIDs::Error;
501
502 if (Diag.WarningsAsErrors)
503 Result = DiagnosticIDs::Error;
504 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
505 Result = DiagnosticIDs::Fatal;
506 break;
507
508 case diag::MAP_WARNING_NO_WERROR:
509 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
510 // not be adjusted by -Werror or -pedantic-errors.
511 Result = DiagnosticIDs::Warning;
512
513 // If warnings are globally mapped to ignore or error, do it.
514 if (Diag.IgnoreAllWarnings)
515 return DiagnosticIDs::Ignored;
516
517 break;
518
519 case diag::MAP_ERROR_NO_WFATAL:
520 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
521 // unaffected by -Wfatal-errors.
522 Result = DiagnosticIDs::Error;
523 break;
524 }
525
526 // Okay, we're about to return this as a "diagnostic to emit" one last check:
527 // if this is any sort of extension warning, and if we're in an __extension__
528 // block, silence it.
529 if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
530 return DiagnosticIDs::Ignored;
531
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000532 // If we are in a system header, we ignore it.
533 // We also want to ignore extensions and warnings in -Werror and
534 // -pedantic-errors modes, which *map* warnings/extensions to errors.
535 if (Result >= DiagnosticIDs::Warning &&
536 DiagClass != CLASS_ERROR &&
537 // Custom diagnostics always are emitted in system headers.
538 DiagID < diag::DIAG_UPPER_LIMIT &&
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000539 !ShouldEmitInSystemHeader &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000540 Diag.SuppressSystemWarnings &&
541 Loc.isValid() &&
542 Diag.getSourceManager().isInSystemHeader(
Chandler Carruth40278532011-07-25 16:49:02 +0000543 Diag.getSourceManager().getExpansionLoc(Loc)))
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000544 return DiagnosticIDs::Ignored;
545
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000546 return Result;
547}
548
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000549namespace {
550 struct WarningOption {
551 // Be safe with the size of 'NameLen' because we don't statically check if
552 // the size will fit in the field; the struct size won't decrease with a
553 // shorter type anyway.
554 size_t NameLen;
555 const char *NameStr;
556 const short *Members;
557 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000558
Chris Lattner5f9e2722011-07-23 10:55:15 +0000559 StringRef getName() const {
560 return StringRef(NameStr, NameLen);
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000561 }
562 };
563}
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000564
565#define GET_DIAG_ARRAYS
566#include "clang/Basic/DiagnosticGroups.inc"
567#undef GET_DIAG_ARRAYS
568
569// Second the table of options, sorted by name for fast binary lookup.
570static const WarningOption OptionTable[] = {
571#define GET_DIAG_TABLE
572#include "clang/Basic/DiagnosticGroups.inc"
573#undef GET_DIAG_TABLE
574};
575static const size_t OptionTableSize =
576sizeof(OptionTable) / sizeof(OptionTable[0]);
577
578static bool WarningOptionCompare(const WarningOption &LHS,
579 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000580 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000581}
582
583static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000584 SourceLocation Loc, Diagnostic &Diag) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000585 // Option exists, poke all the members of its diagnostic set.
586 if (const short *Member = Group->Members) {
587 for (; *Member != -1; ++Member)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000588 Diag.setDiagnosticMapping(*Member, Mapping, Loc);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000589 }
590
591 // Enable/disable all subgroups along with this one.
592 if (const short *SubGroups = Group->SubGroups) {
593 for (; *SubGroups != (short)-1; ++SubGroups)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000594 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000595 }
596}
597
598/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
599/// "unknown-pragmas" to have the specified mapping. This returns true and
600/// ignores the request if "Group" was unknown, false otherwise.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000601bool DiagnosticIDs::setDiagnosticGroupMapping(StringRef Group,
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000602 diag::Mapping Map,
603 SourceLocation Loc,
604 Diagnostic &Diag) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000605 assert((Loc.isValid() ||
606 Diag.DiagStatePoints.empty() ||
607 Diag.DiagStatePoints.back().Loc.isInvalid()) &&
608 "Loc should be invalid only when the mapping comes from command-line");
609 assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
610 Diag.DiagStatePoints.back().Loc.isInvalid() ||
611 !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
612 Diag.DiagStatePoints.back().Loc)) &&
613 "Source location of new mapping is before the previous one!");
614
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000615 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000616 const WarningOption *Found =
617 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
618 WarningOptionCompare);
619 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000620 Found->getName() != Group)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000621 return true; // Option not found.
622
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000623 MapGroupMembers(Found, Map, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000624 return false;
625}
626
627/// ProcessDiag - This is the method used to report a diagnostic that is
628/// finally fully formed.
629bool DiagnosticIDs::ProcessDiag(Diagnostic &Diag) const {
630 DiagnosticInfo Info(&Diag);
631
632 if (Diag.SuppressAllDiagnostics)
633 return false;
634
635 assert(Diag.getClient() && "DiagnosticClient not set!");
636
637 // Figure out the diagnostic level of this message.
638 DiagnosticIDs::Level DiagLevel;
639 unsigned DiagID = Info.getID();
640
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000641 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
642 // Handle custom diagnostics, which cannot be mapped.
643 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000644 } else {
645 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
646 // the diagnostic level was for the previous diagnostic so that it is
647 // filtered the same as the previous diagnostic.
648 unsigned DiagClass = getBuiltinDiagClass(DiagID);
649 if (DiagClass == CLASS_NOTE) {
650 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000651 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000652 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
653 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000654 }
655 }
656
657 if (DiagLevel != DiagnosticIDs::Note) {
658 // Record that a fatal error occurred only when we see a second
659 // non-note diagnostic. This allows notes to be attached to the
660 // fatal error, but suppresses any diagnostics that follow those
661 // notes.
662 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
663 Diag.FatalErrorOccurred = true;
664
665 Diag.LastDiagLevel = DiagLevel;
666 }
667
668 // If a fatal error has already been emitted, silence all subsequent
669 // diagnostics.
670 if (Diag.FatalErrorOccurred) {
671 if (DiagLevel >= DiagnosticIDs::Error &&
672 Diag.Client->IncludeInDiagnosticCounts()) {
673 ++Diag.NumErrors;
674 ++Diag.NumErrorsSuppressed;
675 }
676
677 return false;
678 }
679
680 // If the client doesn't care about this message, don't issue it. If this is
681 // a note and the last real diagnostic was ignored, ignore it too.
682 if (DiagLevel == DiagnosticIDs::Ignored ||
683 (DiagLevel == DiagnosticIDs::Note &&
684 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
685 return false;
686
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000687 if (DiagLevel >= DiagnosticIDs::Error) {
John McCall923cd572011-06-15 21:46:43 +0000688 Diag.TrapErrorOccurred = true;
Douglas Gregor85bea972011-07-06 17:40:26 +0000689 if (isUnrecoverable(DiagID)) {
John McCall923cd572011-06-15 21:46:43 +0000690 Diag.TrapUnrecoverableErrorOccurred = true;
Douglas Gregor85bea972011-07-06 17:40:26 +0000691 Diag.UnrecoverableErrorOccurred = true;
692 }
693
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000694 if (Diag.Client->IncludeInDiagnosticCounts()) {
695 Diag.ErrorOccurred = true;
696 ++Diag.NumErrors;
697 }
698
699 // If we've emitted a lot of errors, emit a fatal error after it to stop a
700 // flood of bogus errors.
701 if (Diag.ErrorLimit && Diag.NumErrors >= Diag.ErrorLimit &&
702 DiagLevel == DiagnosticIDs::Error)
703 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
704 }
705
Douglas Gregor4814fb52011-02-03 23:41:12 +0000706 // If we have any Fix-Its, make sure that all of the Fix-Its point into
Chandler Carruth3201f382011-07-26 05:17:23 +0000707 // source locations that aren't macro expansions. If any point into macro
708 // expansions, remove all of the Fix-Its.
Douglas Gregor4814fb52011-02-03 23:41:12 +0000709 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
710 const FixItHint &FixIt = Diag.FixItHints[I];
711 if (FixIt.RemoveRange.isInvalid() ||
712 FixIt.RemoveRange.getBegin().isMacroID() ||
713 FixIt.RemoveRange.getEnd().isMacroID()) {
714 Diag.NumFixItHints = 0;
715 break;
716 }
717 }
718
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000719 // Finally, report it.
720 Diag.Client->HandleDiagnostic((Diagnostic::Level)DiagLevel, Info);
721 if (Diag.Client->IncludeInDiagnosticCounts()) {
722 if (DiagLevel == DiagnosticIDs::Warning)
723 ++Diag.NumWarnings;
724 }
725
726 Diag.CurDiagID = ~0U;
727
728 return true;
729}
John McCall923cd572011-06-15 21:46:43 +0000730
731bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
732 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
733 // Custom diagnostics.
734 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
735 }
736
737 // Only errors may be unrecoverable.
Douglas Gregor85bea972011-07-06 17:40:26 +0000738 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
John McCall923cd572011-06-15 21:46:43 +0000739 return false;
740
741 if (DiagID == diag::err_unavailable ||
742 DiagID == diag::err_unavailable_message)
743 return false;
744
John McCallf85e1932011-06-15 23:02:42 +0000745 // Currently we consider all ARC errors as recoverable.
746 if (getCategoryNumberForDiag(DiagID) ==
747 diag::DiagCat_Automatic_Reference_Counting_Issue)
748 return false;
749
John McCall923cd572011-06-15 21:46:43 +0000750 return true;
751}