blob: f92a34edd2653505e995635cb640b4e9e952773b [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;
48
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000049 const char *Name;
50
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000051 const char *Description;
52 const char *OptionGroup;
53
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000054 const char *BriefExplanation;
55 const char *FullExplanation;
56
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000057 bool operator<(const StaticDiagInfoRec &RHS) const {
58 return DiagID < RHS.DiagID;
59 }
60};
61
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000062struct StaticDiagNameIndexRec {
63 const char *Name;
64 unsigned short DiagID;
65
66 bool operator<(const StaticDiagNameIndexRec &RHS) const {
67 assert(Name && RHS.Name && "Null Diagnostic Name");
68 return strcmp(Name, RHS.Name) == -1;
69 }
70
71 bool operator==(const StaticDiagNameIndexRec &RHS) const {
72 assert(Name && RHS.Name && "Null Diagnostic Name");
73 return strcmp(Name, RHS.Name) == 0;
74 }
75};
76
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000077}
78
79static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000080#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
81 SFINAE,ACCESS,CATEGORY,BRIEF,FULL) \
82 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, \
83 ACCESS, CATEGORY, #ENUM, DESC, GROUP, BRIEF, FULL },
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000084#include "clang/Basic/DiagnosticCommonKinds.inc"
85#include "clang/Basic/DiagnosticDriverKinds.inc"
86#include "clang/Basic/DiagnosticFrontendKinds.inc"
87#include "clang/Basic/DiagnosticLexKinds.inc"
88#include "clang/Basic/DiagnosticParseKinds.inc"
89#include "clang/Basic/DiagnosticASTKinds.inc"
90#include "clang/Basic/DiagnosticSemaKinds.inc"
91#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000092#undef DIAG
Douglas Gregor7d2b8c12011-04-15 22:04:17 +000093 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
94};
95
96static const unsigned StaticDiagInfoSize =
97 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
98
99/// To be sorted before first use (since it's splitted among multiple files)
100static StaticDiagNameIndexRec StaticDiagNameIndex[] = {
101#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM },
102#include "clang/Basic/DiagnosticIndexName.inc"
103#undef DIAG_NAME_INDEX
104 { 0, 0 }
105};
106
107static const unsigned StaticDiagNameIndexSize =
108 sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000109
110/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
111/// or null if the ID is invalid.
112static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000113 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
114#ifndef NDEBUG
115 static bool IsFirst = true;
116 if (IsFirst) {
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000117 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000118 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
119 "Diag ID conflict, the enums at the start of clang::diag (in "
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000120 "DiagnosticIDs.h) probably need to be increased");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000121
122 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
123 "Improperly sorted diag info");
124 }
125 IsFirst = false;
126 }
127#endif
128
129 // Search the diagnostic table with a binary search.
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000130 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000131
132 const StaticDiagInfoRec *Found =
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000133 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
134 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000135 Found->DiagID != DiagID)
136 return 0;
137
138 return Found;
139}
140
141static unsigned GetDefaultDiagMapping(unsigned DiagID) {
142 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
143 return Info->Mapping;
144 return diag::MAP_FATAL;
145}
146
147/// getWarningOptionForDiag - Return the lowest-level warning option that
148/// enables the specified diagnostic. If there is no -Wfoo flag that controls
149/// the diagnostic, this returns null.
150const char *DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
151 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
152 return Info->OptionGroup;
153 return 0;
154}
155
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000156/// getCategoryNumberForDiag - Return the category number that a specified
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000157/// DiagID belongs to, or 0 if no category.
158unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
159 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
160 return Info->Category;
161 return 0;
162}
163
164/// getCategoryNameFromID - Given a category ID, return the name of the
165/// category, an empty string if CategoryID is zero, or null if CategoryID is
166/// invalid.
167const char *DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
168 // Second the table of options, sorted by name for fast binary lookup.
169 static const char *CategoryNameTable[] = {
170#define GET_CATEGORY_TABLE
171#define CATEGORY(X) X,
172#include "clang/Basic/DiagnosticGroups.inc"
173#undef GET_CATEGORY_TABLE
174 "<<END>>"
175 };
176 static const size_t CategoryNameTableSize =
177 sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
178
179 if (CategoryID >= CategoryNameTableSize) return 0;
180 return CategoryNameTable[CategoryID];
181}
182
183
184
185DiagnosticIDs::SFINAEResponse
186DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
187 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
Douglas Gregor418df342011-01-27 21:06:28 +0000188 if (Info->AccessControl)
189 return SFINAE_AccessControl;
190
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000191 if (!Info->SFINAE)
192 return SFINAE_Report;
193
194 if (Info->Class == CLASS_ERROR)
195 return SFINAE_SubstitutionFailure;
196
197 // Suppress notes, warnings, and extensions;
198 return SFINAE_Suppress;
199 }
200
201 return SFINAE_Report;
202}
203
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000204/// getName - Given a diagnostic ID, return its name
205const char *DiagnosticIDs::getName(unsigned DiagID) {
206 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
207 return Info->Name;
208 return 0;
209}
210
211/// getIdFromName - Given a diagnostic name, return its ID, or 0
212unsigned DiagnosticIDs::getIdFromName(char const *Name) {
213 StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
214 StaticDiagNameIndex + StaticDiagNameIndexSize;
215
216 if (Name == 0) { return diag::DIAG_UPPER_LIMIT; }
217
218 StaticDiagNameIndexRec Find = { Name, 0 };
219
220 const StaticDiagNameIndexRec *Found =
221 std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
222 if (Found == StaticDiagNameIndexEnd ||
223 strcmp(Found->Name, Name) != 0)
224 return diag::DIAG_UPPER_LIMIT;
225
226 return Found->DiagID;
227}
228
229/// getBriefExplanation - Given a diagnostic ID, return a brief explanation
230/// of the issue
231const char *DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
232 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
233 return Info->BriefExplanation;
234 return 0;
235}
236
237/// getFullExplanation - Given a diagnostic ID, return a full explanation
238/// of the issue
239const char *DiagnosticIDs::getFullExplanation(unsigned DiagID) {
240 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
241 return Info->FullExplanation;
242 return 0;
243}
244
245/// getBuiltinDiagClass - Return the class field of the diagnostic.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000246///
247static unsigned getBuiltinDiagClass(unsigned DiagID) {
248 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
249 return Info->Class;
250 return ~0U;
251}
252
253//===----------------------------------------------------------------------===//
254// Custom Diagnostic information
255//===----------------------------------------------------------------------===//
256
257namespace clang {
258 namespace diag {
259 class CustomDiagInfo {
260 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
261 std::vector<DiagDesc> DiagInfo;
262 std::map<DiagDesc, unsigned> DiagIDs;
263 public:
264
265 /// getDescription - Return the description of the specified custom
266 /// diagnostic.
267 const char *getDescription(unsigned DiagID) const {
268 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
269 "Invalid diagnosic ID");
270 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
271 }
272
273 /// getLevel - Return the level of the specified custom diagnostic.
274 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
275 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
276 "Invalid diagnosic ID");
277 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
278 }
279
280 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, llvm::StringRef Message,
281 DiagnosticIDs &Diags) {
282 DiagDesc D(L, Message);
283 // Check to see if it already exists.
284 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
285 if (I != DiagIDs.end() && I->first == D)
286 return I->second;
287
288 // If not, assign a new ID.
289 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
290 DiagIDs.insert(std::make_pair(D, ID));
291 DiagInfo.push_back(D);
292 return ID;
293 }
294 };
295
296 } // end diag namespace
297} // end clang namespace
298
299
300//===----------------------------------------------------------------------===//
301// Common Diagnostic implementation
302//===----------------------------------------------------------------------===//
303
304DiagnosticIDs::DiagnosticIDs() {
305 CustomDiagInfo = 0;
306}
307
308DiagnosticIDs::~DiagnosticIDs() {
309 delete CustomDiagInfo;
310}
311
312/// getCustomDiagID - Return an ID for a diagnostic with the specified message
313/// and level. If this is the first request for this diagnosic, it is
314/// registered and created, otherwise the existing ID is returned.
315unsigned DiagnosticIDs::getCustomDiagID(Level L, llvm::StringRef Message) {
316 if (CustomDiagInfo == 0)
317 CustomDiagInfo = new diag::CustomDiagInfo();
318 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
319}
320
321
322/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
323/// level of the specified diagnostic ID is a Warning or Extension.
324/// This only works on builtin diagnostics, not custom ones, and is not legal to
325/// call on NOTEs.
326bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
327 return DiagID < diag::DIAG_UPPER_LIMIT &&
328 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
329}
330
331/// \brief Determine whether the given built-in diagnostic ID is a
332/// Note.
333bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
334 return DiagID < diag::DIAG_UPPER_LIMIT &&
335 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
336}
337
338/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
339/// ID is for an extension of some sort. This also returns EnabledByDefault,
340/// which is set to indicate whether the diagnostic is ignored by default (in
341/// which case -pedantic enables it) or treated as a warning/error by default.
342///
343bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
344 bool &EnabledByDefault) {
345 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
346 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
347 return false;
348
349 EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE;
350 return true;
351}
352
353/// getDescription - Given a diagnostic ID, return a description of the
354/// issue.
355const char *DiagnosticIDs::getDescription(unsigned DiagID) const {
356 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
357 return Info->Description;
358 return CustomDiagInfo->getDescription(DiagID);
359}
360
361/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
362/// object, classify the specified diagnostic ID into a Level, consumable by
363/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000364DiagnosticIDs::Level
365DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000366 const Diagnostic &Diag,
367 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000368 // Handle custom diagnostics, which cannot be mapped.
369 if (DiagID >= diag::DIAG_UPPER_LIMIT)
370 return CustomDiagInfo->getLevel(DiagID);
371
372 unsigned DiagClass = getBuiltinDiagClass(DiagID);
373 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Ted Kremenek7decebf2011-02-25 01:28:26 +0000374 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag, mapping);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000375}
376
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000377/// \brief Based on the way the client configured the Diagnostic
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000378/// object, classify the specified diagnostic ID into a Level, consumable by
379/// the DiagnosticClient.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000380///
381/// \param Loc The source location we are interested in finding out the
382/// diagnostic state. Can be null in order to query the latest state.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000383DiagnosticIDs::Level
384DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000385 SourceLocation Loc,
Ted Kremenek7decebf2011-02-25 01:28:26 +0000386 const Diagnostic &Diag,
387 diag::Mapping *mapping) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000388 // Specific non-error diagnostics may be mapped to various levels from ignored
389 // to error. Errors can only be mapped to fatal.
390 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
391
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000392 Diagnostic::DiagStatePointsTy::iterator
393 Pos = Diag.GetDiagStatePointForLoc(Loc);
394 Diagnostic::DiagState *State = Pos->State;
395
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000396 // Get the mapping information, if unset, compute it lazily.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000397 unsigned MappingInfo = Diag.getDiagnosticMappingInfo((diag::kind)DiagID,
398 State);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000399 if (MappingInfo == 0) {
400 MappingInfo = GetDefaultDiagMapping(DiagID);
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000401 Diag.setDiagnosticMappingInternal(DiagID, MappingInfo, State, false, false);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000402 }
Ted Kremenek7decebf2011-02-25 01:28:26 +0000403
404 if (mapping)
405 *mapping = (diag::Mapping) (MappingInfo & 7);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000406
407 switch (MappingInfo & 7) {
408 default: assert(0 && "Unknown mapping!");
409 case diag::MAP_IGNORE:
410 // Ignore this, unless this is an extension diagnostic and we're mapping
411 // them onto warnings or errors.
412 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
413 Diag.ExtBehavior == Diagnostic::Ext_Ignore || // Ext ignored
414 (MappingInfo & 8) != 0) // User explicitly mapped it.
415 return DiagnosticIDs::Ignored;
416 Result = DiagnosticIDs::Warning;
417 if (Diag.ExtBehavior == Diagnostic::Ext_Error) Result = DiagnosticIDs::Error;
418 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
419 Result = DiagnosticIDs::Fatal;
420 break;
421 case diag::MAP_ERROR:
422 Result = DiagnosticIDs::Error;
423 if (Diag.ErrorsAsFatal)
424 Result = DiagnosticIDs::Fatal;
425 break;
426 case diag::MAP_FATAL:
427 Result = DiagnosticIDs::Fatal;
428 break;
429 case diag::MAP_WARNING:
430 // If warnings are globally mapped to ignore or error, do it.
431 if (Diag.IgnoreAllWarnings)
432 return DiagnosticIDs::Ignored;
433
434 Result = DiagnosticIDs::Warning;
435
436 // If this is an extension diagnostic and we're in -pedantic-error mode, and
437 // if the user didn't explicitly map it, upgrade to an error.
438 if (Diag.ExtBehavior == Diagnostic::Ext_Error &&
439 (MappingInfo & 8) == 0 &&
440 isBuiltinExtensionDiag(DiagID))
441 Result = DiagnosticIDs::Error;
442
443 if (Diag.WarningsAsErrors)
444 Result = DiagnosticIDs::Error;
445 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
446 Result = DiagnosticIDs::Fatal;
447 break;
448
449 case diag::MAP_WARNING_NO_WERROR:
450 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
451 // not be adjusted by -Werror or -pedantic-errors.
452 Result = DiagnosticIDs::Warning;
453
454 // If warnings are globally mapped to ignore or error, do it.
455 if (Diag.IgnoreAllWarnings)
456 return DiagnosticIDs::Ignored;
457
458 break;
459
460 case diag::MAP_ERROR_NO_WFATAL:
461 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
462 // unaffected by -Wfatal-errors.
463 Result = DiagnosticIDs::Error;
464 break;
465 }
466
467 // Okay, we're about to return this as a "diagnostic to emit" one last check:
468 // if this is any sort of extension warning, and if we're in an __extension__
469 // block, silence it.
470 if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
471 return DiagnosticIDs::Ignored;
472
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000473 // If we are in a system header, we ignore it.
474 // We also want to ignore extensions and warnings in -Werror and
475 // -pedantic-errors modes, which *map* warnings/extensions to errors.
476 if (Result >= DiagnosticIDs::Warning &&
477 DiagClass != CLASS_ERROR &&
478 // Custom diagnostics always are emitted in system headers.
479 DiagID < diag::DIAG_UPPER_LIMIT &&
480 Diag.SuppressSystemWarnings &&
481 Loc.isValid() &&
482 Diag.getSourceManager().isInSystemHeader(
483 Diag.getSourceManager().getInstantiationLoc(Loc)))
484 return DiagnosticIDs::Ignored;
485
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000486 return Result;
487}
488
489struct WarningOption {
490 const char *Name;
491 const short *Members;
492 const short *SubGroups;
493};
494
495#define GET_DIAG_ARRAYS
496#include "clang/Basic/DiagnosticGroups.inc"
497#undef GET_DIAG_ARRAYS
498
499// Second the table of options, sorted by name for fast binary lookup.
500static const WarningOption OptionTable[] = {
501#define GET_DIAG_TABLE
502#include "clang/Basic/DiagnosticGroups.inc"
503#undef GET_DIAG_TABLE
504};
505static const size_t OptionTableSize =
506sizeof(OptionTable) / sizeof(OptionTable[0]);
507
508static bool WarningOptionCompare(const WarningOption &LHS,
509 const WarningOption &RHS) {
510 return strcmp(LHS.Name, RHS.Name) < 0;
511}
512
513static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000514 SourceLocation Loc, Diagnostic &Diag) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000515 // Option exists, poke all the members of its diagnostic set.
516 if (const short *Member = Group->Members) {
517 for (; *Member != -1; ++Member)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000518 Diag.setDiagnosticMapping(*Member, Mapping, Loc);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000519 }
520
521 // Enable/disable all subgroups along with this one.
522 if (const short *SubGroups = Group->SubGroups) {
523 for (; *SubGroups != (short)-1; ++SubGroups)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000524 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000525 }
526}
527
528/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
529/// "unknown-pragmas" to have the specified mapping. This returns true and
530/// ignores the request if "Group" was unknown, false otherwise.
531bool DiagnosticIDs::setDiagnosticGroupMapping(const char *Group,
532 diag::Mapping Map,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000533 SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000534 Diagnostic &Diag) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000535 assert((Loc.isValid() ||
536 Diag.DiagStatePoints.empty() ||
537 Diag.DiagStatePoints.back().Loc.isInvalid()) &&
538 "Loc should be invalid only when the mapping comes from command-line");
539 assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
540 Diag.DiagStatePoints.back().Loc.isInvalid() ||
541 !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
542 Diag.DiagStatePoints.back().Loc)) &&
543 "Source location of new mapping is before the previous one!");
544
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000545 WarningOption Key = { Group, 0, 0 };
546 const WarningOption *Found =
547 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
548 WarningOptionCompare);
549 if (Found == OptionTable + OptionTableSize ||
550 strcmp(Found->Name, Group) != 0)
551 return true; // Option not found.
552
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000553 MapGroupMembers(Found, Map, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000554 return false;
555}
556
557/// ProcessDiag - This is the method used to report a diagnostic that is
558/// finally fully formed.
559bool DiagnosticIDs::ProcessDiag(Diagnostic &Diag) const {
560 DiagnosticInfo Info(&Diag);
561
562 if (Diag.SuppressAllDiagnostics)
563 return false;
564
565 assert(Diag.getClient() && "DiagnosticClient not set!");
566
567 // Figure out the diagnostic level of this message.
568 DiagnosticIDs::Level DiagLevel;
569 unsigned DiagID = Info.getID();
570
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000571 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
572 // Handle custom diagnostics, which cannot be mapped.
573 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000574 } else {
575 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
576 // the diagnostic level was for the previous diagnostic so that it is
577 // filtered the same as the previous diagnostic.
578 unsigned DiagClass = getBuiltinDiagClass(DiagID);
579 if (DiagClass == CLASS_NOTE) {
580 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000581 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000582 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
583 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000584 }
585 }
586
587 if (DiagLevel != DiagnosticIDs::Note) {
588 // Record that a fatal error occurred only when we see a second
589 // non-note diagnostic. This allows notes to be attached to the
590 // fatal error, but suppresses any diagnostics that follow those
591 // notes.
592 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
593 Diag.FatalErrorOccurred = true;
594
595 Diag.LastDiagLevel = DiagLevel;
596 }
597
598 // If a fatal error has already been emitted, silence all subsequent
599 // diagnostics.
600 if (Diag.FatalErrorOccurred) {
601 if (DiagLevel >= DiagnosticIDs::Error &&
602 Diag.Client->IncludeInDiagnosticCounts()) {
603 ++Diag.NumErrors;
604 ++Diag.NumErrorsSuppressed;
605 }
606
607 return false;
608 }
609
610 // If the client doesn't care about this message, don't issue it. If this is
611 // a note and the last real diagnostic was ignored, ignore it too.
612 if (DiagLevel == DiagnosticIDs::Ignored ||
613 (DiagLevel == DiagnosticIDs::Note &&
614 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
615 return false;
616
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000617 if (DiagLevel >= DiagnosticIDs::Error) {
618 if (Diag.Client->IncludeInDiagnosticCounts()) {
619 Diag.ErrorOccurred = true;
620 ++Diag.NumErrors;
621 }
622
623 // If we've emitted a lot of errors, emit a fatal error after it to stop a
624 // flood of bogus errors.
625 if (Diag.ErrorLimit && Diag.NumErrors >= Diag.ErrorLimit &&
626 DiagLevel == DiagnosticIDs::Error)
627 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
628 }
629
Douglas Gregor4814fb52011-02-03 23:41:12 +0000630 // If we have any Fix-Its, make sure that all of the Fix-Its point into
631 // source locations that aren't macro instantiations. If any point into
632 // macro instantiations, remove all of the Fix-Its.
633 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
634 const FixItHint &FixIt = Diag.FixItHints[I];
635 if (FixIt.RemoveRange.isInvalid() ||
636 FixIt.RemoveRange.getBegin().isMacroID() ||
637 FixIt.RemoveRange.getEnd().isMacroID()) {
638 Diag.NumFixItHints = 0;
639 break;
640 }
641 }
642
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000643 // Finally, report it.
644 Diag.Client->HandleDiagnostic((Diagnostic::Level)DiagLevel, Info);
645 if (Diag.Client->IncludeInDiagnosticCounts()) {
646 if (DiagLevel == DiagnosticIDs::Warning)
647 ++Diag.NumWarnings;
648 }
649
650 Diag.CurDiagID = ~0U;
651
652 return true;
653}