blob: 61c046066352f4424993337b72381a71f81ec805 [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"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Driver/DriverDiagnostic.h"
19#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Lex/LexDiagnostic.h"
21#include "clang/Parse/ParseDiagnostic.h"
22#include "clang/Sema/SemaDiagnostic.h"
23
24#include <map>
25using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Builtin Diagnostic information
29//===----------------------------------------------------------------------===//
30
31namespace {
32
33// Diagnostic classes.
34enum {
35 CLASS_NOTE = 0x01,
36 CLASS_WARNING = 0x02,
37 CLASS_EXTENSION = 0x03,
38 CLASS_ERROR = 0x04
39};
40
41struct StaticDiagInfoRec {
42 unsigned short DiagID;
43 unsigned Mapping : 3;
44 unsigned Class : 3;
Douglas Gregor418df342011-01-27 21:06:28 +000045 unsigned SFINAE : 1;
46 unsigned AccessControl : 1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000047 unsigned Category : 5;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000048
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000049 uint8_t NameLen;
50 uint8_t OptionGroupLen;
51
52 uint16_t DescriptionLen;
53 uint16_t BriefExplanationLen;
54 uint16_t FullExplanationLen;
55
56 const char *NameStr;
57 const char *OptionGroupStr;
58
59 const char *DescriptionStr;
60 const char *BriefExplanationStr;
61 const char *FullExplanationStr;
62
63 llvm::StringRef getName() const {
64 return llvm::StringRef(NameStr, NameLen);
65 }
66 llvm::StringRef getOptionGroup() const {
67 return llvm::StringRef(OptionGroupStr, OptionGroupLen);
68 }
69
70 llvm::StringRef getDescription() const {
71 return llvm::StringRef(DescriptionStr, DescriptionLen);
72 }
73 llvm::StringRef getBriefExplanation() const {
74 return llvm::StringRef(BriefExplanationStr, BriefExplanationLen);
75 }
76 llvm::StringRef getFullExplanation() const {
77 return llvm::StringRef(FullExplanationStr, FullExplanationLen);
78 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000079
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000080 bool operator<(const StaticDiagInfoRec &RHS) const {
81 return DiagID < RHS.DiagID;
82 }
83};
84
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000085struct StaticDiagNameIndexRec {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000086 const char *NameStr;
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000087 unsigned short DiagID;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000088 uint8_t NameLen;
89
90 llvm::StringRef getName() const {
91 return llvm::StringRef(NameStr, NameLen);
92 }
93
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000094 bool operator<(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000095 return getName() < RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000096 }
97
98 bool operator==(const StaticDiagNameIndexRec &RHS) const {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000099 return getName() == RHS.getName();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000100 }
101};
102
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000103template <size_t SizeOfStr, typename FieldType>
104class StringSizerHelper {
105 char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
106public:
107 enum { Size = SizeOfStr };
108};
109
110} // namespace anonymous
111
112#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000113
114static const StaticDiagInfoRec StaticDiagInfo[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000115#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
116 SFINAE,ACCESS,CATEGORY,BRIEF,FULL) \
117 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, CATEGORY, \
118 STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t), \
119 STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t), \
120 STR_SIZE(FULL, uint16_t), \
121 #ENUM, GROUP, DESC, BRIEF, FULL },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000122#include "clang/Basic/DiagnosticCommonKinds.inc"
123#include "clang/Basic/DiagnosticDriverKinds.inc"
124#include "clang/Basic/DiagnosticFrontendKinds.inc"
125#include "clang/Basic/DiagnosticLexKinds.inc"
126#include "clang/Basic/DiagnosticParseKinds.inc"
127#include "clang/Basic/DiagnosticASTKinds.inc"
128#include "clang/Basic/DiagnosticSemaKinds.inc"
129#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000130#undef DIAG
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000131 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000132};
133
134static const unsigned StaticDiagInfoSize =
135 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
136
137/// To be sorted before first use (since it's splitted among multiple files)
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000138static const StaticDiagNameIndexRec StaticDiagNameIndex[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000139#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000140#include "clang/Basic/DiagnosticIndexName.inc"
141#undef DIAG_NAME_INDEX
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000142 { 0, 0, 0 }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000143};
144
145static const unsigned StaticDiagNameIndexSize =
146 sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000147
148/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
149/// or null if the ID is invalid.
150static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000151 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
152#ifndef NDEBUG
153 static bool IsFirst = true;
154 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000155 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000156 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
157 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000158 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000159
160 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
161 "Improperly sorted diag info");
162 }
163 IsFirst = false;
164 }
165#endif
166
167 // Search the diagnostic table with a binary search.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000168 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 +0000169
170 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000171 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
172 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000173 Found->DiagID != DiagID)
174 return 0;
175
176 return Found;
177}
178
179static unsigned GetDefaultDiagMapping(unsigned DiagID) {
180 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
181 return Info->Mapping;
182 return diag::MAP_FATAL;
183}
184
185/// getWarningOptionForDiag - Return the lowest-level warning option that
186/// enables the specified diagnostic. If there is no -Wfoo flag that controls
187/// the diagnostic, this returns null.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000188llvm::StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000189 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000190 return Info->getOptionGroup();
191 return llvm::StringRef();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000192}
193
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000194/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000195/// DiagID belongs to, or 0 if no category.
196unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
197 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
198 return Info->Category;
199 return 0;
200}
201
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000202namespace {
203 // The diagnostic category names.
204 struct StaticDiagCategoryRec {
205 const char *NameStr;
206 uint8_t NameLen;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000207
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000208 llvm::StringRef getName() const {
209 return llvm::StringRef(NameStr, NameLen);
210 }
211 };
212}
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000213
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000214static const StaticDiagCategoryRec CategoryNameTable[] = {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000215#define GET_CATEGORY_TABLE
216#define CATEGORY(X) { X, STR_SIZE(X, uint8_t) },
217#include "clang/Basic/DiagnosticGroups.inc"
218#undef GET_CATEGORY_TABLE
219 { 0, 0 }
220};
221
222/// getNumberOfCategories - Return the number of categories
223unsigned DiagnosticIDs::getNumberOfCategories() {
224 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
225}
226
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000227/// getCategoryNameFromID - Given a category ID, return the name of the
228/// category, an empty string if CategoryID is zero, or null if CategoryID is
229/// invalid.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000230llvm::StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
231 if (CategoryID >= getNumberOfCategories())
232 return llvm::StringRef();
233 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000234}
235
236
237
238DiagnosticIDs::SFINAEResponse
239DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
240 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000241 if (Info->AccessControl)
242 return SFINAE_AccessControl;
243
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000244 if (!Info->SFINAE)
245 return SFINAE_Report;
246
247 if (Info->Class == CLASS_ERROR)
248 return SFINAE_SubstitutionFailure;
249
250 // Suppress notes, warnings, and extensions;
251 return SFINAE_Suppress;
252 }
253
254 return SFINAE_Report;
255}
256
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000257/// getName - Given a diagnostic ID, return its name
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000258llvm::StringRef DiagnosticIDs::getName(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000259 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000260 return Info->getName();
261 return llvm::StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000262}
263
264/// getIdFromName - Given a diagnostic name, return its ID, or 0
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000265unsigned DiagnosticIDs::getIdFromName(llvm::StringRef Name) {
Benjamin Kramer81f9d142011-06-14 13:15:38 +0000266 const StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000267 StaticDiagNameIndex + StaticDiagNameIndexSize;
268
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000269 if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000270
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000271 StaticDiagNameIndexRec Find = { Name.data(), 0, Name.size() };
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000272
273 const StaticDiagNameIndexRec *Found =
274 std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
275 if (Found == StaticDiagNameIndexEnd ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000276 Found->getName() != Name)
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000277 return diag::DIAG_UPPER_LIMIT;
278
279 return Found->DiagID;
280}
281
282/// getBriefExplanation - Given a diagnostic ID, return a brief explanation
283/// of the issue
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000284llvm::StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000285 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000286 return Info->getBriefExplanation();
287 return llvm::StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000288}
289
290/// getFullExplanation - Given a diagnostic ID, return a full explanation
291/// of the issue
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000292llvm::StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000293 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000294 return Info->getFullExplanation();
295 return llvm::StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000296}
297
298/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000299///
300static unsigned getBuiltinDiagClass(unsigned DiagID) {
301 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
302 return Info->Class;
303 return ~0U;
304}
305
306//===----------------------------------------------------------------------===//
307// Custom Diagnostic information
308//===----------------------------------------------------------------------===//
309
310namespace clang {
311 namespace diag {
312 class CustomDiagInfo {
313 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
314 std::vector<DiagDesc> DiagInfo;
315 std::map<DiagDesc, unsigned> DiagIDs;
316 public:
317
318 /// getDescription - Return the description of the specified custom
319 /// diagnostic.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000320 llvm::StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000321 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
322 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000323 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000324 }
325
326 /// getLevel - Return the level of the specified custom diagnostic.
327 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
328 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
329 "Invalid diagnosic ID");
330 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
331 }
332
333 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, llvm::StringRef Message,
334 DiagnosticIDs &Diags) {
335 DiagDesc D(L, Message);
336 // Check to see if it already exists.
337 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
338 if (I != DiagIDs.end() && I->first == D)
339 return I->second;
340
341 // If not, assign a new ID.
342 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
343 DiagIDs.insert(std::make_pair(D, ID));
344 DiagInfo.push_back(D);
345 return ID;
346 }
347 };
348
349 } // end diag namespace
350} // end clang namespace
351
352
353//===----------------------------------------------------------------------===//
354// Common Diagnostic implementation
355//===----------------------------------------------------------------------===//
356
357DiagnosticIDs::DiagnosticIDs() {
358 CustomDiagInfo = 0;
359}
360
361DiagnosticIDs::~DiagnosticIDs() {
362 delete CustomDiagInfo;
363}
364
365/// getCustomDiagID - Return an ID for a diagnostic with the specified message
366/// and level. If this is the first request for this diagnosic, it is
367/// registered and created, otherwise the existing ID is returned.
368unsigned DiagnosticIDs::getCustomDiagID(Level L, llvm::StringRef Message) {
369 if (CustomDiagInfo == 0)
370 CustomDiagInfo = new diag::CustomDiagInfo();
371 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
372}
373
374
375/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
376/// level of the specified diagnostic ID is a Warning or Extension.
377/// This only works on builtin diagnostics, not custom ones, and is not legal to
378/// call on NOTEs.
379bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
380 return DiagID < diag::DIAG_UPPER_LIMIT &&
381 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
382}
383
384/// \brief Determine whether the given built-in diagnostic ID is a
385/// Note.
386bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
387 return DiagID < diag::DIAG_UPPER_LIMIT &&
388 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
389}
390
391/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
392/// ID is for an extension of some sort. This also returns EnabledByDefault,
393/// which is set to indicate whether the diagnostic is ignored by default (in
394/// which case -pedantic enables it) or treated as a warning/error by default.
395///
396bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
397 bool &EnabledByDefault) {
398 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
399 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
400 return false;
401
402 EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE;
403 return true;
404}
405
406/// getDescription - Given a diagnostic ID, return a description of the
407/// issue.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000408llvm::StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000409 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000410 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000411 return CustomDiagInfo->getDescription(DiagID);
412}
413
414/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
415/// object, classify the specified diagnostic ID into a Level, consumable by
416/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000417DiagnosticIDs::Level
418DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000419 const Diagnostic &Diag,
420 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000421 // Handle custom diagnostics, which cannot be mapped.
422 if (DiagID >= diag::DIAG_UPPER_LIMIT)
423 return CustomDiagInfo->getLevel(DiagID);
424
425 unsigned DiagClass = getBuiltinDiagClass(DiagID);
426 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Ted Kremenek7decebf2011-02-25 01:28:26 +0000427 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag, mapping);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000428}
429
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000430/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000431/// object, classify the specified diagnostic ID into a Level, consumable by
432/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000433///
434/// \param Loc The source location we are interested in finding out the
435/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000436DiagnosticIDs::Level
437DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000438 SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000439 const Diagnostic &Diag,
440 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000441 // Specific non-error diagnostics may be mapped to various levels from ignored
442 // to error. Errors can only be mapped to fatal.
443 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
444
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000445 Diagnostic::DiagStatePointsTy::iterator
446 Pos = Diag.GetDiagStatePointForLoc(Loc);
447 Diagnostic::DiagState *State = Pos->State;
448
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000449 // Get the mapping information, if unset, compute it lazily.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000450 unsigned MappingInfo = Diag.getDiagnosticMappingInfo((diag::kind)DiagID,
451 State);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000452 if (MappingInfo == 0) {
453 MappingInfo = GetDefaultDiagMapping(DiagID);
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000454 Diag.setDiagnosticMappingInternal(DiagID, MappingInfo, State, false, false);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000455 }
Ted Kremenek7decebf2011-02-25 01:28:26 +0000456
457 if (mapping)
458 *mapping = (diag::Mapping) (MappingInfo & 7);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000459
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000460 bool ShouldEmitInSystemHeader = false;
461
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000462 switch (MappingInfo & 7) {
463 default: assert(0 && "Unknown mapping!");
464 case diag::MAP_IGNORE:
465 // Ignore this, unless this is an extension diagnostic and we're mapping
466 // them onto warnings or errors.
467 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
468 Diag.ExtBehavior == Diagnostic::Ext_Ignore || // Ext ignored
469 (MappingInfo & 8) != 0) // User explicitly mapped it.
470 return DiagnosticIDs::Ignored;
471 Result = DiagnosticIDs::Warning;
472 if (Diag.ExtBehavior == Diagnostic::Ext_Error) Result = DiagnosticIDs::Error;
473 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
474 Result = DiagnosticIDs::Fatal;
475 break;
476 case diag::MAP_ERROR:
477 Result = DiagnosticIDs::Error;
478 if (Diag.ErrorsAsFatal)
479 Result = DiagnosticIDs::Fatal;
480 break;
481 case diag::MAP_FATAL:
482 Result = DiagnosticIDs::Fatal;
483 break;
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000484 case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER:
485 ShouldEmitInSystemHeader = true;
486 // continue as MAP_WARNING.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000487 case diag::MAP_WARNING:
488 // If warnings are globally mapped to ignore or error, do it.
489 if (Diag.IgnoreAllWarnings)
490 return DiagnosticIDs::Ignored;
491
492 Result = DiagnosticIDs::Warning;
493
494 // If this is an extension diagnostic and we're in -pedantic-error mode, and
495 // if the user didn't explicitly map it, upgrade to an error.
496 if (Diag.ExtBehavior == Diagnostic::Ext_Error &&
497 (MappingInfo & 8) == 0 &&
498 isBuiltinExtensionDiag(DiagID))
499 Result = DiagnosticIDs::Error;
500
501 if (Diag.WarningsAsErrors)
502 Result = DiagnosticIDs::Error;
503 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
504 Result = DiagnosticIDs::Fatal;
505 break;
506
507 case diag::MAP_WARNING_NO_WERROR:
508 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
509 // not be adjusted by -Werror or -pedantic-errors.
510 Result = DiagnosticIDs::Warning;
511
512 // If warnings are globally mapped to ignore or error, do it.
513 if (Diag.IgnoreAllWarnings)
514 return DiagnosticIDs::Ignored;
515
516 break;
517
518 case diag::MAP_ERROR_NO_WFATAL:
519 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
520 // unaffected by -Wfatal-errors.
521 Result = DiagnosticIDs::Error;
522 break;
523 }
524
525 // Okay, we're about to return this as a "diagnostic to emit" one last check:
526 // if this is any sort of extension warning, and if we're in an __extension__
527 // block, silence it.
528 if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
529 return DiagnosticIDs::Ignored;
530
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000531 // If we are in a system header, we ignore it.
532 // We also want to ignore extensions and warnings in -Werror and
533 // -pedantic-errors modes, which *map* warnings/extensions to errors.
534 if (Result >= DiagnosticIDs::Warning &&
535 DiagClass != CLASS_ERROR &&
536 // Custom diagnostics always are emitted in system headers.
537 DiagID < diag::DIAG_UPPER_LIMIT &&
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000538 !ShouldEmitInSystemHeader &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000539 Diag.SuppressSystemWarnings &&
540 Loc.isValid() &&
541 Diag.getSourceManager().isInSystemHeader(
542 Diag.getSourceManager().getInstantiationLoc(Loc)))
543 return DiagnosticIDs::Ignored;
544
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000545 return Result;
546}
547
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000548namespace {
549 struct WarningOption {
550 // Be safe with the size of 'NameLen' because we don't statically check if
551 // the size will fit in the field; the struct size won't decrease with a
552 // shorter type anyway.
553 size_t NameLen;
554 const char *NameStr;
555 const short *Members;
556 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000557
Benjamin Kramerdbda5132011-06-13 18:38:45 +0000558 llvm::StringRef getName() const {
559 return llvm::StringRef(NameStr, NameLen);
560 }
561 };
562}
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000563
564#define GET_DIAG_ARRAYS
565#include "clang/Basic/DiagnosticGroups.inc"
566#undef GET_DIAG_ARRAYS
567
568// Second the table of options, sorted by name for fast binary lookup.
569static const WarningOption OptionTable[] = {
570#define GET_DIAG_TABLE
571#include "clang/Basic/DiagnosticGroups.inc"
572#undef GET_DIAG_TABLE
573};
574static const size_t OptionTableSize =
575sizeof(OptionTable) / sizeof(OptionTable[0]);
576
577static bool WarningOptionCompare(const WarningOption &LHS,
578 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000579 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000580}
581
582static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000583 SourceLocation Loc, Diagnostic &Diag) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000584 // Option exists, poke all the members of its diagnostic set.
585 if (const short *Member = Group->Members) {
586 for (; *Member != -1; ++Member)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000587 Diag.setDiagnosticMapping(*Member, Mapping, Loc);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000588 }
589
590 // Enable/disable all subgroups along with this one.
591 if (const short *SubGroups = Group->SubGroups) {
592 for (; *SubGroups != (short)-1; ++SubGroups)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000593 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000594 }
595}
596
597/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
598/// "unknown-pragmas" to have the specified mapping. This returns true and
599/// ignores the request if "Group" was unknown, false otherwise.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000600bool DiagnosticIDs::setDiagnosticGroupMapping(llvm::StringRef Group,
601 diag::Mapping Map,
602 SourceLocation Loc,
603 Diagnostic &Diag) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000604 assert((Loc.isValid() ||
605 Diag.DiagStatePoints.empty() ||
606 Diag.DiagStatePoints.back().Loc.isInvalid()) &&
607 "Loc should be invalid only when the mapping comes from command-line");
608 assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
609 Diag.DiagStatePoints.back().Loc.isInvalid() ||
610 !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
611 Diag.DiagStatePoints.back().Loc)) &&
612 "Source location of new mapping is before the previous one!");
613
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000614 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000615 const WarningOption *Found =
616 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
617 WarningOptionCompare);
618 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000619 Found->getName() != Group)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000620 return true; // Option not found.
621
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000622 MapGroupMembers(Found, Map, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000623 return false;
624}
625
626/// ProcessDiag - This is the method used to report a diagnostic that is
627/// finally fully formed.
628bool DiagnosticIDs::ProcessDiag(Diagnostic &Diag) const {
629 DiagnosticInfo Info(&Diag);
630
631 if (Diag.SuppressAllDiagnostics)
632 return false;
633
634 assert(Diag.getClient() && "DiagnosticClient not set!");
635
636 // Figure out the diagnostic level of this message.
637 DiagnosticIDs::Level DiagLevel;
638 unsigned DiagID = Info.getID();
639
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000640 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
641 // Handle custom diagnostics, which cannot be mapped.
642 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000643 } else {
644 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
645 // the diagnostic level was for the previous diagnostic so that it is
646 // filtered the same as the previous diagnostic.
647 unsigned DiagClass = getBuiltinDiagClass(DiagID);
648 if (DiagClass == CLASS_NOTE) {
649 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000650 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000651 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
652 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000653 }
654 }
655
656 if (DiagLevel != DiagnosticIDs::Note) {
657 // Record that a fatal error occurred only when we see a second
658 // non-note diagnostic. This allows notes to be attached to the
659 // fatal error, but suppresses any diagnostics that follow those
660 // notes.
661 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
662 Diag.FatalErrorOccurred = true;
663
664 Diag.LastDiagLevel = DiagLevel;
665 }
666
667 // If a fatal error has already been emitted, silence all subsequent
668 // diagnostics.
669 if (Diag.FatalErrorOccurred) {
670 if (DiagLevel >= DiagnosticIDs::Error &&
671 Diag.Client->IncludeInDiagnosticCounts()) {
672 ++Diag.NumErrors;
673 ++Diag.NumErrorsSuppressed;
674 }
675
676 return false;
677 }
678
679 // If the client doesn't care about this message, don't issue it. If this is
680 // a note and the last real diagnostic was ignored, ignore it too.
681 if (DiagLevel == DiagnosticIDs::Ignored ||
682 (DiagLevel == DiagnosticIDs::Note &&
683 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
684 return false;
685
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000686 if (DiagLevel >= DiagnosticIDs::Error) {
687 if (Diag.Client->IncludeInDiagnosticCounts()) {
688 Diag.ErrorOccurred = true;
689 ++Diag.NumErrors;
690 }
691
692 // If we've emitted a lot of errors, emit a fatal error after it to stop a
693 // flood of bogus errors.
694 if (Diag.ErrorLimit && Diag.NumErrors >= Diag.ErrorLimit &&
695 DiagLevel == DiagnosticIDs::Error)
696 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
697 }
698
Douglas Gregor4814fb52011-02-03 23:41:12 +0000699 // If we have any Fix-Its, make sure that all of the Fix-Its point into
700 // source locations that aren't macro instantiations. If any point into
701 // macro instantiations, remove all of the Fix-Its.
702 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
703 const FixItHint &FixIt = Diag.FixItHints[I];
704 if (FixIt.RemoveRange.isInvalid() ||
705 FixIt.RemoveRange.getBegin().isMacroID() ||
706 FixIt.RemoveRange.getEnd().isMacroID()) {
707 Diag.NumFixItHints = 0;
708 break;
709 }
710 }
711
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000712 // Finally, report it.
713 Diag.Client->HandleDiagnostic((Diagnostic::Level)DiagLevel, Info);
714 if (Diag.Client->IncludeInDiagnosticCounts()) {
715 if (DiagLevel == DiagnosticIDs::Warning)
716 ++Diag.NumWarnings;
717 }
718
719 Diag.CurDiagID = ~0U;
720
721 return true;
722}