blob: b4dd575a9684fa79204fc692ea8379c1179e2754 [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
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000407 bool ShouldEmitInSystemHeader = false;
408
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000409 switch (MappingInfo & 7) {
410 default: assert(0 && "Unknown mapping!");
411 case diag::MAP_IGNORE:
412 // Ignore this, unless this is an extension diagnostic and we're mapping
413 // them onto warnings or errors.
414 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
415 Diag.ExtBehavior == Diagnostic::Ext_Ignore || // Ext ignored
416 (MappingInfo & 8) != 0) // User explicitly mapped it.
417 return DiagnosticIDs::Ignored;
418 Result = DiagnosticIDs::Warning;
419 if (Diag.ExtBehavior == Diagnostic::Ext_Error) Result = DiagnosticIDs::Error;
420 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
421 Result = DiagnosticIDs::Fatal;
422 break;
423 case diag::MAP_ERROR:
424 Result = DiagnosticIDs::Error;
425 if (Diag.ErrorsAsFatal)
426 Result = DiagnosticIDs::Fatal;
427 break;
428 case diag::MAP_FATAL:
429 Result = DiagnosticIDs::Fatal;
430 break;
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000431 case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER:
432 ShouldEmitInSystemHeader = true;
433 // continue as MAP_WARNING.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000434 case diag::MAP_WARNING:
435 // If warnings are globally mapped to ignore or error, do it.
436 if (Diag.IgnoreAllWarnings)
437 return DiagnosticIDs::Ignored;
438
439 Result = DiagnosticIDs::Warning;
440
441 // If this is an extension diagnostic and we're in -pedantic-error mode, and
442 // if the user didn't explicitly map it, upgrade to an error.
443 if (Diag.ExtBehavior == Diagnostic::Ext_Error &&
444 (MappingInfo & 8) == 0 &&
445 isBuiltinExtensionDiag(DiagID))
446 Result = DiagnosticIDs::Error;
447
448 if (Diag.WarningsAsErrors)
449 Result = DiagnosticIDs::Error;
450 if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
451 Result = DiagnosticIDs::Fatal;
452 break;
453
454 case diag::MAP_WARNING_NO_WERROR:
455 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
456 // not be adjusted by -Werror or -pedantic-errors.
457 Result = DiagnosticIDs::Warning;
458
459 // If warnings are globally mapped to ignore or error, do it.
460 if (Diag.IgnoreAllWarnings)
461 return DiagnosticIDs::Ignored;
462
463 break;
464
465 case diag::MAP_ERROR_NO_WFATAL:
466 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
467 // unaffected by -Wfatal-errors.
468 Result = DiagnosticIDs::Error;
469 break;
470 }
471
472 // Okay, we're about to return this as a "diagnostic to emit" one last check:
473 // if this is any sort of extension warning, and if we're in an __extension__
474 // block, silence it.
475 if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
476 return DiagnosticIDs::Ignored;
477
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000478 // If we are in a system header, we ignore it.
479 // We also want to ignore extensions and warnings in -Werror and
480 // -pedantic-errors modes, which *map* warnings/extensions to errors.
481 if (Result >= DiagnosticIDs::Warning &&
482 DiagClass != CLASS_ERROR &&
483 // Custom diagnostics always are emitted in system headers.
484 DiagID < diag::DIAG_UPPER_LIMIT &&
Argyrios Kyrtzidis144bc082011-04-21 23:08:23 +0000485 !ShouldEmitInSystemHeader &&
Argyrios Kyrtzidiscfdadfe2011-04-21 23:08:18 +0000486 Diag.SuppressSystemWarnings &&
487 Loc.isValid() &&
488 Diag.getSourceManager().isInSystemHeader(
489 Diag.getSourceManager().getInstantiationLoc(Loc)))
490 return DiagnosticIDs::Ignored;
491
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000492 return Result;
493}
494
495struct WarningOption {
496 const char *Name;
497 const short *Members;
498 const short *SubGroups;
499};
500
501#define GET_DIAG_ARRAYS
502#include "clang/Basic/DiagnosticGroups.inc"
503#undef GET_DIAG_ARRAYS
504
505// Second the table of options, sorted by name for fast binary lookup.
506static const WarningOption OptionTable[] = {
507#define GET_DIAG_TABLE
508#include "clang/Basic/DiagnosticGroups.inc"
509#undef GET_DIAG_TABLE
510};
511static const size_t OptionTableSize =
512sizeof(OptionTable) / sizeof(OptionTable[0]);
513
514static bool WarningOptionCompare(const WarningOption &LHS,
515 const WarningOption &RHS) {
516 return strcmp(LHS.Name, RHS.Name) < 0;
517}
518
519static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000520 SourceLocation Loc, Diagnostic &Diag) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000521 // Option exists, poke all the members of its diagnostic set.
522 if (const short *Member = Group->Members) {
523 for (; *Member != -1; ++Member)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000524 Diag.setDiagnosticMapping(*Member, Mapping, Loc);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000525 }
526
527 // Enable/disable all subgroups along with this one.
528 if (const short *SubGroups = Group->SubGroups) {
529 for (; *SubGroups != (short)-1; ++SubGroups)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000530 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000531 }
532}
533
534/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
535/// "unknown-pragmas" to have the specified mapping. This returns true and
536/// ignores the request if "Group" was unknown, false otherwise.
537bool DiagnosticIDs::setDiagnosticGroupMapping(const char *Group,
538 diag::Mapping Map,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000539 SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000540 Diagnostic &Diag) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000541 assert((Loc.isValid() ||
542 Diag.DiagStatePoints.empty() ||
543 Diag.DiagStatePoints.back().Loc.isInvalid()) &&
544 "Loc should be invalid only when the mapping comes from command-line");
545 assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
546 Diag.DiagStatePoints.back().Loc.isInvalid() ||
547 !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
548 Diag.DiagStatePoints.back().Loc)) &&
549 "Source location of new mapping is before the previous one!");
550
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000551 WarningOption Key = { Group, 0, 0 };
552 const WarningOption *Found =
553 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
554 WarningOptionCompare);
555 if (Found == OptionTable + OptionTableSize ||
556 strcmp(Found->Name, Group) != 0)
557 return true; // Option not found.
558
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000559 MapGroupMembers(Found, Map, Loc, Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000560 return false;
561}
562
563/// ProcessDiag - This is the method used to report a diagnostic that is
564/// finally fully formed.
565bool DiagnosticIDs::ProcessDiag(Diagnostic &Diag) const {
566 DiagnosticInfo Info(&Diag);
567
568 if (Diag.SuppressAllDiagnostics)
569 return false;
570
571 assert(Diag.getClient() && "DiagnosticClient not set!");
572
573 // Figure out the diagnostic level of this message.
574 DiagnosticIDs::Level DiagLevel;
575 unsigned DiagID = Info.getID();
576
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000577 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
578 // Handle custom diagnostics, which cannot be mapped.
579 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000580 } else {
581 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
582 // the diagnostic level was for the previous diagnostic so that it is
583 // filtered the same as the previous diagnostic.
584 unsigned DiagClass = getBuiltinDiagClass(DiagID);
585 if (DiagClass == CLASS_NOTE) {
586 DiagLevel = DiagnosticIDs::Note;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000587 } else {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000588 DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
589 Diag);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000590 }
591 }
592
593 if (DiagLevel != DiagnosticIDs::Note) {
594 // Record that a fatal error occurred only when we see a second
595 // non-note diagnostic. This allows notes to be attached to the
596 // fatal error, but suppresses any diagnostics that follow those
597 // notes.
598 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
599 Diag.FatalErrorOccurred = true;
600
601 Diag.LastDiagLevel = DiagLevel;
602 }
603
604 // If a fatal error has already been emitted, silence all subsequent
605 // diagnostics.
606 if (Diag.FatalErrorOccurred) {
607 if (DiagLevel >= DiagnosticIDs::Error &&
608 Diag.Client->IncludeInDiagnosticCounts()) {
609 ++Diag.NumErrors;
610 ++Diag.NumErrorsSuppressed;
611 }
612
613 return false;
614 }
615
616 // If the client doesn't care about this message, don't issue it. If this is
617 // a note and the last real diagnostic was ignored, ignore it too.
618 if (DiagLevel == DiagnosticIDs::Ignored ||
619 (DiagLevel == DiagnosticIDs::Note &&
620 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
621 return false;
622
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000623 if (DiagLevel >= DiagnosticIDs::Error) {
624 if (Diag.Client->IncludeInDiagnosticCounts()) {
625 Diag.ErrorOccurred = true;
626 ++Diag.NumErrors;
627 }
628
629 // If we've emitted a lot of errors, emit a fatal error after it to stop a
630 // flood of bogus errors.
631 if (Diag.ErrorLimit && Diag.NumErrors >= Diag.ErrorLimit &&
632 DiagLevel == DiagnosticIDs::Error)
633 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
634 }
635
Douglas Gregor4814fb52011-02-03 23:41:12 +0000636 // If we have any Fix-Its, make sure that all of the Fix-Its point into
637 // source locations that aren't macro instantiations. If any point into
638 // macro instantiations, remove all of the Fix-Its.
639 for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
640 const FixItHint &FixIt = Diag.FixItHints[I];
641 if (FixIt.RemoveRange.isInvalid() ||
642 FixIt.RemoveRange.getBegin().isMacroID() ||
643 FixIt.RemoveRange.getEnd().isMacroID()) {
644 Diag.NumFixItHints = 0;
645 break;
646 }
647 }
648
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000649 // Finally, report it.
650 Diag.Client->HandleDiagnostic((Diagnostic::Level)DiagLevel, Info);
651 if (Diag.Client->IncludeInDiagnosticCounts()) {
652 if (DiagLevel == DiagnosticIDs::Warning)
653 ++Diag.NumWarnings;
654 }
655
656 Diag.CurDiagID = ~0U;
657
658 return true;
659}