blob: 6d7e3204cba32ae55c1ab5f2a06e479da4a4a99f [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)
138static 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
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000202// The diagnostic category names.
203struct StaticDiagCategoryRec {
204 const char *NameStr;
205 uint8_t NameLen;
206
207 llvm::StringRef getName() const {
208 return llvm::StringRef(NameStr, NameLen);
209 }
210};
211
212static StaticDiagCategoryRec CategoryNameTable[] = {
213#define GET_CATEGORY_TABLE
214#define CATEGORY(X) { X, STR_SIZE(X, uint8_t) },
215#include "clang/Basic/DiagnosticGroups.inc"
216#undef GET_CATEGORY_TABLE
217 { 0, 0 }
218};
219
220/// getNumberOfCategories - Return the number of categories
221unsigned DiagnosticIDs::getNumberOfCategories() {
222 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
223}
224
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000225/// getCategoryNameFromID - Given a category ID, return the name of the
226/// category, an empty string if CategoryID is zero, or null if CategoryID is
227/// invalid.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000228llvm::StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
229 if (CategoryID >= getNumberOfCategories())
230 return llvm::StringRef();
231 return CategoryNameTable[CategoryID].getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000232}
233
234
235
236DiagnosticIDs::SFINAEResponse
237DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
238 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000239 if (Info->AccessControl)
240 return SFINAE_AccessControl;
241
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000242 if (!Info->SFINAE)
243 return SFINAE_Report;
244
245 if (Info->Class == CLASS_ERROR)
246 return SFINAE_SubstitutionFailure;
247
248 // Suppress notes, warnings, and extensions;
249 return SFINAE_Suppress;
250 }
251
252 return SFINAE_Report;
253}
254
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000255/// getName - Given a diagnostic ID, return its name
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000256llvm::StringRef DiagnosticIDs::getName(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000257 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000258 return Info->getName();
259 return llvm::StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000260}
261
262/// getIdFromName - Given a diagnostic name, return its ID, or 0
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000263unsigned DiagnosticIDs::getIdFromName(llvm::StringRef Name) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000264 StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
265 StaticDiagNameIndex + StaticDiagNameIndexSize;
266
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000267 if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000268
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000269 StaticDiagNameIndexRec Find = { Name.data(), 0, Name.size() };
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000270
271 const StaticDiagNameIndexRec *Found =
272 std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
273 if (Found == StaticDiagNameIndexEnd ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000274 Found->getName() != Name)
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000275 return diag::DIAG_UPPER_LIMIT;
276
277 return Found->DiagID;
278}
279
280/// getBriefExplanation - Given a diagnostic ID, return a brief explanation
281/// of the issue
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000282llvm::StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000283 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000284 return Info->getBriefExplanation();
285 return llvm::StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000286}
287
288/// getFullExplanation - Given a diagnostic ID, return a full explanation
289/// of the issue
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000290llvm::StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000291 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000292 return Info->getFullExplanation();
293 return llvm::StringRef();
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000294}
295
296/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000297///
298static unsigned getBuiltinDiagClass(unsigned DiagID) {
299 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
300 return Info->Class;
301 return ~0U;
302}
303
304//===----------------------------------------------------------------------===//
305// Custom Diagnostic information
306//===----------------------------------------------------------------------===//
307
308namespace clang {
309 namespace diag {
310 class CustomDiagInfo {
311 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
312 std::vector<DiagDesc> DiagInfo;
313 std::map<DiagDesc, unsigned> DiagIDs;
314 public:
315
316 /// getDescription - Return the description of the specified custom
317 /// diagnostic.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000318 llvm::StringRef getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000319 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
320 "Invalid diagnosic ID");
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000321 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000322 }
323
324 /// getLevel - Return the level of the specified custom diagnostic.
325 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
326 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
327 "Invalid diagnosic ID");
328 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
329 }
330
331 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, llvm::StringRef Message,
332 DiagnosticIDs &Diags) {
333 DiagDesc D(L, Message);
334 // Check to see if it already exists.
335 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
336 if (I != DiagIDs.end() && I->first == D)
337 return I->second;
338
339 // If not, assign a new ID.
340 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
341 DiagIDs.insert(std::make_pair(D, ID));
342 DiagInfo.push_back(D);
343 return ID;
344 }
345 };
346
347 } // end diag namespace
348} // end clang namespace
349
350
351//===----------------------------------------------------------------------===//
352// Common Diagnostic implementation
353//===----------------------------------------------------------------------===//
354
355DiagnosticIDs::DiagnosticIDs() {
356 CustomDiagInfo = 0;
357}
358
359DiagnosticIDs::~DiagnosticIDs() {
360 delete CustomDiagInfo;
361}
362
363/// getCustomDiagID - Return an ID for a diagnostic with the specified message
364/// and level. If this is the first request for this diagnosic, it is
365/// registered and created, otherwise the existing ID is returned.
366unsigned DiagnosticIDs::getCustomDiagID(Level L, llvm::StringRef Message) {
367 if (CustomDiagInfo == 0)
368 CustomDiagInfo = new diag::CustomDiagInfo();
369 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
370}
371
372
373/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
374/// level of the specified diagnostic ID is a Warning or Extension.
375/// This only works on builtin diagnostics, not custom ones, and is not legal to
376/// call on NOTEs.
377bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
378 return DiagID < diag::DIAG_UPPER_LIMIT &&
379 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
380}
381
382/// \brief Determine whether the given built-in diagnostic ID is a
383/// Note.
384bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
385 return DiagID < diag::DIAG_UPPER_LIMIT &&
386 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
387}
388
389/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
390/// ID is for an extension of some sort. This also returns EnabledByDefault,
391/// which is set to indicate whether the diagnostic is ignored by default (in
392/// which case -pedantic enables it) or treated as a warning/error by default.
393///
394bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
395 bool &EnabledByDefault) {
396 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
397 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
398 return false;
399
400 EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE;
401 return true;
402}
403
404/// getDescription - Given a diagnostic ID, return a description of the
405/// issue.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000406llvm::StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000407 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000408 return Info->getDescription();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000409 return CustomDiagInfo->getDescription(DiagID);
410}
411
412/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
413/// object, classify the specified diagnostic ID into a Level, consumable by
414/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000415DiagnosticIDs::Level
416DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000417 const Diagnostic &Diag,
418 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000419 // Handle custom diagnostics, which cannot be mapped.
420 if (DiagID >= diag::DIAG_UPPER_LIMIT)
421 return CustomDiagInfo->getLevel(DiagID);
422
423 unsigned DiagClass = getBuiltinDiagClass(DiagID);
424 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Ted Kremenek7decebf2011-02-25 01:28:26 +0000425 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag, mapping);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000426}
427
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000428/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000429/// object, classify the specified diagnostic ID into a Level, consumable by
430/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000431///
432/// \param Loc The source location we are interested in finding out the
433/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000434DiagnosticIDs::Level
435DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000436 SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000437 const Diagnostic &Diag,
438 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000439 // Specific non-error diagnostics may be mapped to various levels from ignored
440 // to error. Errors can only be mapped to fatal.
441 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
442
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000443 Diagnostic::DiagStatePointsTy::iterator
444 Pos = Diag.GetDiagStatePointForLoc(Loc);
445 Diagnostic::DiagState *State = Pos->State;
446
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000447 // Get the mapping information, if unset, compute it lazily.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000448 unsigned MappingInfo = Diag.getDiagnosticMappingInfo((diag::kind)DiagID,
449 State);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000450 if (MappingInfo == 0) {
451 MappingInfo = GetDefaultDiagMapping(DiagID);
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000452 Diag.setDiagnosticMappingInternal(DiagID, MappingInfo, State, false, false);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000453 }
Ted Kremenek7decebf2011-02-25 01:28:26 +0000454
455 if (mapping)
456 *mapping = (diag::Mapping) (MappingInfo & 7);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000457
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000458 bool ShouldEmitInSystemHeader = false;
459
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000460 switch (MappingInfo & 7) {
461 default: assert(0 && "Unknown mapping!");
462 case diag::MAP_IGNORE:
463 // Ignore this, unless this is an extension diagnostic and we're mapping
464 // them onto warnings or errors.
465 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
466 Diag.ExtBehavior == Diagnostic::Ext_Ignore || // Ext ignored
467 (MappingInfo & 8) != 0) // User explicitly mapped it.
468 return DiagnosticIDs::Ignored;
469 Result = DiagnosticIDs::Warning;
470 if (Diag.ExtBehavior == Diagnostic::Ext_Error) Result = DiagnosticIDs::Error;
471 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
472 Result = DiagnosticIDs::Fatal;
473 break;
474 case diag::MAP_ERROR:
475 Result = DiagnosticIDs::Error;
476 if (Diag.ErrorsAsFatal)
477 Result = DiagnosticIDs::Fatal;
478 break;
479 case diag::MAP_FATAL:
480 Result = DiagnosticIDs::Fatal;
481 break;
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000482 case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER:
483 ShouldEmitInSystemHeader = true;
484 // continue as MAP_WARNING.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000485 case diag::MAP_WARNING:
486 // If warnings are globally mapped to ignore or error, do it.
487 if (Diag.IgnoreAllWarnings)
488 return DiagnosticIDs::Ignored;
489
490 Result = DiagnosticIDs::Warning;
491
492 // If this is an extension diagnostic and we're in -pedantic-error mode, and
493 // if the user didn't explicitly map it, upgrade to an error.
494 if (Diag.ExtBehavior == Diagnostic::Ext_Error &&
495 (MappingInfo & 8) == 0 &&
496 isBuiltinExtensionDiag(DiagID))
497 Result = DiagnosticIDs::Error;
498
499 if (Diag.WarningsAsErrors)
500 Result = DiagnosticIDs::Error;
501 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
502 Result = DiagnosticIDs::Fatal;
503 break;
504
505 case diag::MAP_WARNING_NO_WERROR:
506 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
507 // not be adjusted by -Werror or -pedantic-errors.
508 Result = DiagnosticIDs::Warning;
509
510 // If warnings are globally mapped to ignore or error, do it.
511 if (Diag.IgnoreAllWarnings)
512 return DiagnosticIDs::Ignored;
513
514 break;
515
516 case diag::MAP_ERROR_NO_WFATAL:
517 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
518 // unaffected by -Wfatal-errors.
519 Result = DiagnosticIDs::Error;
520 break;
521 }
522
523 // Okay, we're about to return this as a "diagnostic to emit" one last check:
524 // if this is any sort of extension warning, and if we're in an __extension__
525 // block, silence it.
526 if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
527 return DiagnosticIDs::Ignored;
528
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000529 // If we are in a system header, we ignore it.
530 // We also want to ignore extensions and warnings in -Werror and
531 // -pedantic-errors modes, which *map* warnings/extensions to errors.
532 if (Result >= DiagnosticIDs::Warning &&
533 DiagClass != CLASS_ERROR &&
534 // Custom diagnostics always are emitted in system headers.
535 DiagID < diag::DIAG_UPPER_LIMIT &&
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000536 !ShouldEmitInSystemHeader &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000537 Diag.SuppressSystemWarnings &&
538 Loc.isValid() &&
539 Diag.getSourceManager().isInSystemHeader(
540 Diag.getSourceManager().getInstantiationLoc(Loc)))
541 return DiagnosticIDs::Ignored;
542
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000543 return Result;
544}
545
546struct WarningOption {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000547 // Be safe with the size of 'NameLen' because we don't statically check if the
548 // size will fit in the field; the struct size won't decrease with a shorter
549 // type anyway.
550 size_t NameLen;
551 const char *NameStr;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000552 const short *Members;
553 const short *SubGroups;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000554
555 llvm::StringRef getName() const { return llvm::StringRef(NameStr, NameLen); }
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000556};
557
558#define GET_DIAG_ARRAYS
559#include "clang/Basic/DiagnosticGroups.inc"
560#undef GET_DIAG_ARRAYS
561
562// Second the table of options, sorted by name for fast binary lookup.
563static const WarningOption OptionTable[] = {
564#define GET_DIAG_TABLE
565#include "clang/Basic/DiagnosticGroups.inc"
566#undef GET_DIAG_TABLE
567};
568static const size_t OptionTableSize =
569sizeof(OptionTable) / sizeof(OptionTable[0]);
570
571static bool WarningOptionCompare(const WarningOption &LHS,
572 const WarningOption &RHS) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000573 return LHS.getName() < RHS.getName();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000574}
575
576static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000577 SourceLocation Loc, Diagnostic &Diag) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000578 // Option exists, poke all the members of its diagnostic set.
579 if (const short *Member = Group->Members) {
580 for (; *Member != -1; ++Member)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000581 Diag.setDiagnosticMapping(*Member, Mapping, Loc);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000582 }
583
584 // Enable/disable all subgroups along with this one.
585 if (const short *SubGroups = Group->SubGroups) {
586 for (; *SubGroups != (short)-1; ++SubGroups)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000587 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000588 }
589}
590
591/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
592/// "unknown-pragmas" to have the specified mapping. This returns true and
593/// ignores the request if "Group" was unknown, false otherwise.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000594bool DiagnosticIDs::setDiagnosticGroupMapping(llvm::StringRef Group,
595 diag::Mapping Map,
596 SourceLocation Loc,
597 Diagnostic &Diag) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000598 assert((Loc.isValid() ||
599 Diag.DiagStatePoints.empty() ||
600 Diag.DiagStatePoints.back().Loc.isInvalid()) &&
601 "Loc should be invalid only when the mapping comes from command-line");
602 assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
603 Diag.DiagStatePoints.back().Loc.isInvalid() ||
604 !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
605 Diag.DiagStatePoints.back().Loc)) &&
606 "Source location of new mapping is before the previous one!");
607
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000608 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000609 const WarningOption *Found =
610 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
611 WarningOptionCompare);
612 if (Found == OptionTable + OptionTableSize ||
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000613 Found->getName() != Group)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000614 return true; // Option not found.
615
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000616 MapGroupMembers(Found, Map, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000617 return false;
618}
619
620/// ProcessDiag - This is the method used to report a diagnostic that is
621/// finally fully formed.
622bool DiagnosticIDs::ProcessDiag(Diagnostic &Diag) const {
623 DiagnosticInfo Info(&Diag);
624
625 if (Diag.SuppressAllDiagnostics)
626 return false;
627
628 assert(Diag.getClient() && "DiagnosticClient not set!");
629
630 // Figure out the diagnostic level of this message.
631 DiagnosticIDs::Level DiagLevel;
632 unsigned DiagID = Info.getID();
633
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000634 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
635 // Handle custom diagnostics, which cannot be mapped.
636 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000637 } else {
638 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
639 // the diagnostic level was for the previous diagnostic so that it is
640 // filtered the same as the previous diagnostic.
641 unsigned DiagClass = getBuiltinDiagClass(DiagID);
642 if (DiagClass == CLASS_NOTE) {
643 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000644 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000645 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
646 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000647 }
648 }
649
650 if (DiagLevel != DiagnosticIDs::Note) {
651 // Record that a fatal error occurred only when we see a second
652 // non-note diagnostic. This allows notes to be attached to the
653 // fatal error, but suppresses any diagnostics that follow those
654 // notes.
655 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
656 Diag.FatalErrorOccurred = true;
657
658 Diag.LastDiagLevel = DiagLevel;
659 }
660
661 // If a fatal error has already been emitted, silence all subsequent
662 // diagnostics.
663 if (Diag.FatalErrorOccurred) {
664 if (DiagLevel >= DiagnosticIDs::Error &&
665 Diag.Client->IncludeInDiagnosticCounts()) {
666 ++Diag.NumErrors;
667 ++Diag.NumErrorsSuppressed;
668 }
669
670 return false;
671 }
672
673 // If the client doesn't care about this message, don't issue it. If this is
674 // a note and the last real diagnostic was ignored, ignore it too.
675 if (DiagLevel == DiagnosticIDs::Ignored ||
676 (DiagLevel == DiagnosticIDs::Note &&
677 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
678 return false;
679
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000680 if (DiagLevel >= DiagnosticIDs::Error) {
681 if (Diag.Client->IncludeInDiagnosticCounts()) {
682 Diag.ErrorOccurred = true;
683 ++Diag.NumErrors;
684 }
685
686 // If we've emitted a lot of errors, emit a fatal error after it to stop a
687 // flood of bogus errors.
688 if (Diag.ErrorLimit && Diag.NumErrors >= Diag.ErrorLimit &&
689 DiagLevel == DiagnosticIDs::Error)
690 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
691 }
692
Douglas Gregor4814fb52011-02-03 23:41:12 +0000693 // If we have any Fix-Its, make sure that all of the Fix-Its point into
694 // source locations that aren't macro instantiations. If any point into
695 // macro instantiations, remove all of the Fix-Its.
696 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
697 const FixItHint &FixIt = Diag.FixItHints[I];
698 if (FixIt.RemoveRange.isInvalid() ||
699 FixIt.RemoveRange.getBegin().isMacroID() ||
700 FixIt.RemoveRange.getEnd().isMacroID()) {
701 Diag.NumFixItHints = 0;
702 break;
703 }
704 }
705
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000706 // Finally, report it.
707 Diag.Client->HandleDiagnostic((Diagnostic::Level)DiagLevel, Info);
708 if (Diag.Client->IncludeInDiagnosticCounts()) {
709 if (DiagLevel == DiagnosticIDs::Warning)
710 ++Diag.NumWarnings;
711 }
712
713 Diag.CurDiagID = ~0U;
714
715 return true;
716}