1 //===--- 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/DiagnosticCategories.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "clang/Frontend/FrontendDiagnostic.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Parse/ParseDiagnostic.h"
23 #include "clang/Sema/SemaDiagnostic.h"
24 
25 #include <map>
26 using namespace clang;
27 
28 //===----------------------------------------------------------------------===//
29 // Builtin Diagnostic information
30 //===----------------------------------------------------------------------===//
31 
32 namespace {
33 
34 // Diagnostic classes.
35 enum {
36   CLASS_NOTE       = 0x01,
37   CLASS_WARNING    = 0x02,
38   CLASS_EXTENSION  = 0x03,
39   CLASS_ERROR      = 0x04
40 };
41 
42 struct StaticDiagInfoRec {
43   unsigned short DiagID;
44   unsigned Mapping : 3;
45   unsigned Class : 3;
46   unsigned SFINAE : 1;
47   unsigned AccessControl : 1;
48   unsigned Category : 5;
49 
50   uint8_t  NameLen;
51   uint8_t  OptionGroupLen;
52 
53   uint16_t DescriptionLen;
54   uint16_t BriefExplanationLen;
55   uint16_t FullExplanationLen;
56 
57   const char *NameStr;
58   const char *OptionGroupStr;
59 
60   const char *DescriptionStr;
61   const char *BriefExplanationStr;
62   const char *FullExplanationStr;
63 
64   StringRef getName() const {
65     return StringRef(NameStr, NameLen);
66   }
67   StringRef getOptionGroup() const {
68     return StringRef(OptionGroupStr, OptionGroupLen);
69   }
70 
71   StringRef getDescription() const {
72     return StringRef(DescriptionStr, DescriptionLen);
73   }
74   StringRef getBriefExplanation() const {
75     return StringRef(BriefExplanationStr, BriefExplanationLen);
76   }
77   StringRef getFullExplanation() const {
78     return StringRef(FullExplanationStr, FullExplanationLen);
79   }
80 
81   bool operator<(const StaticDiagInfoRec &RHS) const {
82     return DiagID < RHS.DiagID;
83   }
84 };
85 
86 struct StaticDiagNameIndexRec {
87   const char *NameStr;
88   unsigned short DiagID;
89   uint8_t NameLen;
90 
91   StringRef getName() const {
92     return StringRef(NameStr, NameLen);
93   }
94 
95   bool operator<(const StaticDiagNameIndexRec &RHS) const {
96     return getName() < RHS.getName();
97   }
98 
99   bool operator==(const StaticDiagNameIndexRec &RHS) const {
100     return getName() == RHS.getName();
101   }
102 };
103 
104 template <size_t SizeOfStr, typename FieldType>
105 class StringSizerHelper {
106   char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
107 public:
108   enum { Size = SizeOfStr };
109 };
110 
111 } // namespace anonymous
112 
113 #define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
114 
115 static const StaticDiagInfoRec StaticDiagInfo[] = {
116 #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,               \
117              SFINAE,ACCESS,CATEGORY,BRIEF,FULL)                   \
118   { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, CATEGORY, \
119     STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t),           \
120     STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t),          \
121     STR_SIZE(FULL, uint16_t),                                     \
122     #ENUM, GROUP, DESC, BRIEF, FULL },
123 #include "clang/Basic/DiagnosticCommonKinds.inc"
124 #include "clang/Basic/DiagnosticDriverKinds.inc"
125 #include "clang/Basic/DiagnosticFrontendKinds.inc"
126 #include "clang/Basic/DiagnosticLexKinds.inc"
127 #include "clang/Basic/DiagnosticParseKinds.inc"
128 #include "clang/Basic/DiagnosticASTKinds.inc"
129 #include "clang/Basic/DiagnosticSemaKinds.inc"
130 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
131 #undef DIAG
132   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
133 };
134 
135 static const unsigned StaticDiagInfoSize =
136   sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
137 
138 /// To be sorted before first use (since it's splitted among multiple files)
139 static const StaticDiagNameIndexRec StaticDiagNameIndex[] = {
140 #define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
141 #include "clang/Basic/DiagnosticIndexName.inc"
142 #undef DIAG_NAME_INDEX
143   { 0, 0, 0 }
144 };
145 
146 static const unsigned StaticDiagNameIndexSize =
147   sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
148 
149 /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
150 /// or null if the ID is invalid.
151 static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
152   // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
153 #ifndef NDEBUG
154   static bool IsFirst = true;
155   if (IsFirst) {
156     for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
157       assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
158              "Diag ID conflict, the enums at the start of clang::diag (in "
159              "DiagnosticIDs.h) probably need to be increased");
160 
161       assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
162              "Improperly sorted diag info");
163     }
164     IsFirst = false;
165   }
166 #endif
167 
168   // Search the diagnostic table with a binary search.
169   StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
170                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
171 
172   const StaticDiagInfoRec *Found =
173     std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
174   if (Found == StaticDiagInfo + StaticDiagInfoSize ||
175       Found->DiagID != DiagID)
176     return 0;
177 
178   return Found;
179 }
180 
181 static unsigned GetDefaultDiagMapping(unsigned DiagID) {
182   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
183     return Info->Mapping;
184   return diag::MAP_FATAL;
185 }
186 
187 /// getWarningOptionForDiag - Return the lowest-level warning option that
188 /// enables the specified diagnostic.  If there is no -Wfoo flag that controls
189 /// the diagnostic, this returns null.
190 StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
191   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
192     return Info->getOptionGroup();
193   return StringRef();
194 }
195 
196 /// getCategoryNumberForDiag - Return the category number that a specified
197 /// DiagID belongs to, or 0 if no category.
198 unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
199   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
200     return Info->Category;
201   return 0;
202 }
203 
204 namespace {
205   // The diagnostic category names.
206   struct StaticDiagCategoryRec {
207     const char *NameStr;
208     uint8_t NameLen;
209 
210     StringRef getName() const {
211       return StringRef(NameStr, NameLen);
212     }
213   };
214 }
215 
216 static const StaticDiagCategoryRec CategoryNameTable[] = {
217 #define GET_CATEGORY_TABLE
218 #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
219 #include "clang/Basic/DiagnosticGroups.inc"
220 #undef GET_CATEGORY_TABLE
221   { 0, 0 }
222 };
223 
224 /// getNumberOfCategories - Return the number of categories
225 unsigned DiagnosticIDs::getNumberOfCategories() {
226   return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
227 }
228 
229 /// getCategoryNameFromID - Given a category ID, return the name of the
230 /// category, an empty string if CategoryID is zero, or null if CategoryID is
231 /// invalid.
232 StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
233   if (CategoryID >= getNumberOfCategories())
234    return StringRef();
235   return CategoryNameTable[CategoryID].getName();
236 }
237 
238 
239 
240 DiagnosticIDs::SFINAEResponse
241 DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
242   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
243     if (Info->AccessControl)
244       return SFINAE_AccessControl;
245 
246     if (!Info->SFINAE)
247       return SFINAE_Report;
248 
249     if (Info->Class == CLASS_ERROR)
250       return SFINAE_SubstitutionFailure;
251 
252     // Suppress notes, warnings, and extensions;
253     return SFINAE_Suppress;
254   }
255 
256   return SFINAE_Report;
257 }
258 
259 /// getName - Given a diagnostic ID, return its name
260 StringRef DiagnosticIDs::getName(unsigned DiagID) {
261   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
262     return Info->getName();
263   return StringRef();
264 }
265 
266 /// getIdFromName - Given a diagnostic name, return its ID, or 0
267 unsigned DiagnosticIDs::getIdFromName(StringRef Name) {
268   const StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
269     StaticDiagNameIndex + StaticDiagNameIndexSize;
270 
271   if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
272 
273   assert(Name.size() == static_cast<uint8_t>(Name.size()) &&
274          "Name is too long");
275   StaticDiagNameIndexRec Find = { Name.data(), 0,
276                                   static_cast<uint8_t>(Name.size()) };
277 
278   const StaticDiagNameIndexRec *Found =
279     std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
280   if (Found == StaticDiagNameIndexEnd ||
281       Found->getName() != Name)
282     return diag::DIAG_UPPER_LIMIT;
283 
284   return Found->DiagID;
285 }
286 
287 /// getBriefExplanation - Given a diagnostic ID, return a brief explanation
288 /// of the issue
289 StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
290   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
291     return Info->getBriefExplanation();
292   return StringRef();
293 }
294 
295 /// getFullExplanation - Given a diagnostic ID, return a full explanation
296 /// of the issue
297 StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
298   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
299     return Info->getFullExplanation();
300   return StringRef();
301 }
302 
303 /// getBuiltinDiagClass - Return the class field of the diagnostic.
304 ///
305 static unsigned getBuiltinDiagClass(unsigned DiagID) {
306   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
307     return Info->Class;
308   return ~0U;
309 }
310 
311 //===----------------------------------------------------------------------===//
312 // diag_iterator
313 //===----------------------------------------------------------------------===//
314 
315 llvm::StringRef DiagnosticIDs::diag_iterator::getDiagName() const {
316   return static_cast<const StaticDiagNameIndexRec*>(impl)->getName();
317 }
318 
319 unsigned DiagnosticIDs::diag_iterator::getDiagID() const {
320   return static_cast<const StaticDiagNameIndexRec*>(impl)->DiagID;
321 }
322 
323 DiagnosticIDs::diag_iterator &DiagnosticIDs::diag_iterator::operator++() {
324   const StaticDiagNameIndexRec* ptr =
325     static_cast<const StaticDiagNameIndexRec*>(impl);;
326   ++ptr;
327   impl = ptr;
328   return *this;
329 }
330 
331 DiagnosticIDs::diag_iterator DiagnosticIDs::diags_begin() {
332   return DiagnosticIDs::diag_iterator(StaticDiagNameIndex);
333 }
334 
335 DiagnosticIDs::diag_iterator DiagnosticIDs::diags_end() {
336   return DiagnosticIDs::diag_iterator(StaticDiagNameIndex +
337                                       StaticDiagNameIndexSize);
338 }
339 
340 //===----------------------------------------------------------------------===//
341 // Custom Diagnostic information
342 //===----------------------------------------------------------------------===//
343 
344 namespace clang {
345   namespace diag {
346     class CustomDiagInfo {
347       typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
348       std::vector<DiagDesc> DiagInfo;
349       std::map<DiagDesc, unsigned> DiagIDs;
350     public:
351 
352       /// getDescription - Return the description of the specified custom
353       /// diagnostic.
354       StringRef getDescription(unsigned DiagID) const {
355         assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
356                "Invalid diagnosic ID");
357         return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
358       }
359 
360       /// getLevel - Return the level of the specified custom diagnostic.
361       DiagnosticIDs::Level getLevel(unsigned DiagID) const {
362         assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
363                "Invalid diagnosic ID");
364         return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
365       }
366 
367       unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
368                                  DiagnosticIDs &Diags) {
369         DiagDesc D(L, Message);
370         // Check to see if it already exists.
371         std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
372         if (I != DiagIDs.end() && I->first == D)
373           return I->second;
374 
375         // If not, assign a new ID.
376         unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
377         DiagIDs.insert(std::make_pair(D, ID));
378         DiagInfo.push_back(D);
379         return ID;
380       }
381     };
382 
383   } // end diag namespace
384 } // end clang namespace
385 
386 
387 //===----------------------------------------------------------------------===//
388 // Common Diagnostic implementation
389 //===----------------------------------------------------------------------===//
390 
391 DiagnosticIDs::DiagnosticIDs() {
392   CustomDiagInfo = 0;
393 }
394 
395 DiagnosticIDs::~DiagnosticIDs() {
396   delete CustomDiagInfo;
397 }
398 
399 /// getCustomDiagID - Return an ID for a diagnostic with the specified message
400 /// and level.  If this is the first request for this diagnosic, it is
401 /// registered and created, otherwise the existing ID is returned.
402 unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
403   if (CustomDiagInfo == 0)
404     CustomDiagInfo = new diag::CustomDiagInfo();
405   return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
406 }
407 
408 
409 /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
410 /// level of the specified diagnostic ID is a Warning or Extension.
411 /// This only works on builtin diagnostics, not custom ones, and is not legal to
412 /// call on NOTEs.
413 bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
414   return DiagID < diag::DIAG_UPPER_LIMIT &&
415          getBuiltinDiagClass(DiagID) != CLASS_ERROR;
416 }
417 
418 /// \brief Determine whether the given built-in diagnostic ID is a
419 /// Note.
420 bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
421   return DiagID < diag::DIAG_UPPER_LIMIT &&
422     getBuiltinDiagClass(DiagID) == CLASS_NOTE;
423 }
424 
425 /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
426 /// ID is for an extension of some sort.  This also returns EnabledByDefault,
427 /// which is set to indicate whether the diagnostic is ignored by default (in
428 /// which case -pedantic enables it) or treated as a warning/error by default.
429 ///
430 bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
431                                         bool &EnabledByDefault) {
432   if (DiagID >= diag::DIAG_UPPER_LIMIT ||
433       getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
434     return false;
435 
436   EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE;
437   return true;
438 }
439 
440 /// getDescription - Given a diagnostic ID, return a description of the
441 /// issue.
442 StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
443   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
444     return Info->getDescription();
445   return CustomDiagInfo->getDescription(DiagID);
446 }
447 
448 /// getDiagnosticLevel - Based on the way the client configured the Diagnostic
449 /// object, classify the specified diagnostic ID into a Level, consumable by
450 /// the DiagnosticClient.
451 DiagnosticIDs::Level
452 DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
453                                   const Diagnostic &Diag,
454                                   diag::Mapping *mapping) const {
455   // Handle custom diagnostics, which cannot be mapped.
456   if (DiagID >= diag::DIAG_UPPER_LIMIT)
457     return CustomDiagInfo->getLevel(DiagID);
458 
459   unsigned DiagClass = getBuiltinDiagClass(DiagID);
460   assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
461   return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag, mapping);
462 }
463 
464 /// \brief Based on the way the client configured the Diagnostic
465 /// object, classify the specified diagnostic ID into a Level, consumable by
466 /// the DiagnosticClient.
467 ///
468 /// \param Loc The source location we are interested in finding out the
469 /// diagnostic state. Can be null in order to query the latest state.
470 DiagnosticIDs::Level
471 DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
472                                   SourceLocation Loc,
473                                   const Diagnostic &Diag,
474                                   diag::Mapping *mapping) const {
475   // Specific non-error diagnostics may be mapped to various levels from ignored
476   // to error.  Errors can only be mapped to fatal.
477   DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
478 
479   Diagnostic::DiagStatePointsTy::iterator
480     Pos = Diag.GetDiagStatePointForLoc(Loc);
481   Diagnostic::DiagState *State = Pos->State;
482 
483   // Get the mapping information, if unset, compute it lazily.
484   unsigned MappingInfo = Diag.getDiagnosticMappingInfo((diag::kind)DiagID,
485                                                        State);
486   if (MappingInfo == 0) {
487     MappingInfo = GetDefaultDiagMapping(DiagID);
488     Diag.setDiagnosticMappingInternal(DiagID, MappingInfo, State, false, false);
489   }
490 
491   if (mapping)
492     *mapping = (diag::Mapping) (MappingInfo & 7);
493 
494   bool ShouldEmitInSystemHeader = false;
495 
496   switch (MappingInfo & 7) {
497   default: assert(0 && "Unknown mapping!");
498   case diag::MAP_IGNORE:
499     if (Diag.EnableAllWarnings) {
500       // Leave the warning disabled if it was explicitly ignored.
501       if ((MappingInfo & 8) != 0)
502         return DiagnosticIDs::Ignored;
503 
504       Result = Diag.WarningsAsErrors ? DiagnosticIDs::Error
505                                      : DiagnosticIDs::Warning;
506     }
507     // Otherwise, ignore this diagnostic unless this is an extension diagnostic
508     // and we're mapping them onto warnings or errors.
509     else if (!isBuiltinExtensionDiag(DiagID) ||  // Not an extension
510              Diag.ExtBehavior == Diagnostic::Ext_Ignore || // Ext ignored
511              (MappingInfo & 8) != 0) {           // User explicitly mapped it.
512       return DiagnosticIDs::Ignored;
513     }
514     else {
515       Result = DiagnosticIDs::Warning;
516     }
517 
518     if (Diag.ExtBehavior == Diagnostic::Ext_Error)
519       Result = DiagnosticIDs::Error;
520     if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
521       Result = DiagnosticIDs::Fatal;
522     break;
523   case diag::MAP_ERROR:
524     Result = DiagnosticIDs::Error;
525     if (Diag.ErrorsAsFatal)
526       Result = DiagnosticIDs::Fatal;
527     break;
528   case diag::MAP_FATAL:
529     Result = DiagnosticIDs::Fatal;
530     break;
531   case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER:
532     ShouldEmitInSystemHeader = true;
533     // continue as MAP_WARNING.
534   case diag::MAP_WARNING:
535     // If warnings are globally mapped to ignore or error, do it.
536     if (Diag.IgnoreAllWarnings)
537       return DiagnosticIDs::Ignored;
538 
539     Result = DiagnosticIDs::Warning;
540 
541     // If this is an extension diagnostic and we're in -pedantic-error mode, and
542     // if the user didn't explicitly map it, upgrade to an error.
543     if (Diag.ExtBehavior == Diagnostic::Ext_Error &&
544         (MappingInfo & 8) == 0 &&
545         isBuiltinExtensionDiag(DiagID))
546       Result = DiagnosticIDs::Error;
547 
548     if (Diag.WarningsAsErrors)
549       Result = DiagnosticIDs::Error;
550     if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal)
551       Result = DiagnosticIDs::Fatal;
552     break;
553 
554   case diag::MAP_WARNING_NO_WERROR:
555     // Diagnostics specified with -Wno-error=foo should be set to warnings, but
556     // not be adjusted by -Werror or -pedantic-errors.
557     Result = DiagnosticIDs::Warning;
558 
559     // If warnings are globally mapped to ignore or error, do it.
560     if (Diag.IgnoreAllWarnings)
561       return DiagnosticIDs::Ignored;
562 
563     break;
564 
565   case diag::MAP_ERROR_NO_WFATAL:
566     // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
567     // unaffected by -Wfatal-errors.
568     Result = DiagnosticIDs::Error;
569     break;
570   }
571 
572   // Okay, we're about to return this as a "diagnostic to emit" one last check:
573   // if this is any sort of extension warning, and if we're in an __extension__
574   // block, silence it.
575   if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
576     return DiagnosticIDs::Ignored;
577 
578   // If we are in a system header, we ignore it.
579   // We also want to ignore extensions and warnings in -Werror and
580   // -pedantic-errors modes, which *map* warnings/extensions to errors.
581   if (Result >= DiagnosticIDs::Warning &&
582       DiagClass != CLASS_ERROR &&
583       // Custom diagnostics always are emitted in system headers.
584       DiagID < diag::DIAG_UPPER_LIMIT &&
585       !ShouldEmitInSystemHeader &&
586       Diag.SuppressSystemWarnings &&
587       Loc.isValid() &&
588       Diag.getSourceManager().isInSystemHeader(
589           Diag.getSourceManager().getExpansionLoc(Loc)))
590     return DiagnosticIDs::Ignored;
591 
592   return Result;
593 }
594 
595 namespace {
596   struct WarningOption {
597     // Be safe with the size of 'NameLen' because we don't statically check if
598     // the size will fit in the field; the struct size won't decrease with a
599     // shorter type anyway.
600     size_t NameLen;
601     const char *NameStr;
602     const short *Members;
603     const short *SubGroups;
604 
605     StringRef getName() const {
606       return StringRef(NameStr, NameLen);
607     }
608   };
609 }
610 
611 #define GET_DIAG_ARRAYS
612 #include "clang/Basic/DiagnosticGroups.inc"
613 #undef GET_DIAG_ARRAYS
614 
615 // Second the table of options, sorted by name for fast binary lookup.
616 static const WarningOption OptionTable[] = {
617 #define GET_DIAG_TABLE
618 #include "clang/Basic/DiagnosticGroups.inc"
619 #undef GET_DIAG_TABLE
620 };
621 static const size_t OptionTableSize =
622 sizeof(OptionTable) / sizeof(OptionTable[0]);
623 
624 static bool WarningOptionCompare(const WarningOption &LHS,
625                                  const WarningOption &RHS) {
626   return LHS.getName() < RHS.getName();
627 }
628 
629 static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
630                             SourceLocation Loc, Diagnostic &Diag) {
631   // Option exists, poke all the members of its diagnostic set.
632   if (const short *Member = Group->Members) {
633     for (; *Member != -1; ++Member)
634       Diag.setDiagnosticMapping(*Member, Mapping, Loc);
635   }
636 
637   // Enable/disable all subgroups along with this one.
638   if (const short *SubGroups = Group->SubGroups) {
639     for (; *SubGroups != (short)-1; ++SubGroups)
640       MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag);
641   }
642 }
643 
644 /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
645 /// "unknown-pragmas" to have the specified mapping.  This returns true and
646 /// ignores the request if "Group" was unknown, false otherwise.
647 bool DiagnosticIDs::setDiagnosticGroupMapping(StringRef Group,
648                                               diag::Mapping Map,
649                                               SourceLocation Loc,
650                                               Diagnostic &Diag) const {
651   assert((Loc.isValid() ||
652           Diag.DiagStatePoints.empty() ||
653           Diag.DiagStatePoints.back().Loc.isInvalid()) &&
654          "Loc should be invalid only when the mapping comes from command-line");
655   assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() ||
656           Diag.DiagStatePoints.back().Loc.isInvalid() ||
657           !Diag.SourceMgr->isBeforeInTranslationUnit(Loc,
658                                             Diag.DiagStatePoints.back().Loc)) &&
659          "Source location of new mapping is before the previous one!");
660 
661   WarningOption Key = { Group.size(), Group.data(), 0, 0 };
662   const WarningOption *Found =
663   std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
664                    WarningOptionCompare);
665   if (Found == OptionTable + OptionTableSize ||
666       Found->getName() != Group)
667     return true;  // Option not found.
668 
669   MapGroupMembers(Found, Map, Loc, Diag);
670   return false;
671 }
672 
673 /// ProcessDiag - This is the method used to report a diagnostic that is
674 /// finally fully formed.
675 bool DiagnosticIDs::ProcessDiag(Diagnostic &Diag) const {
676   DiagnosticInfo Info(&Diag);
677 
678   if (Diag.SuppressAllDiagnostics)
679     return false;
680 
681   assert(Diag.getClient() && "DiagnosticClient not set!");
682 
683   // Figure out the diagnostic level of this message.
684   DiagnosticIDs::Level DiagLevel;
685   unsigned DiagID = Info.getID();
686 
687   if (DiagID >= diag::DIAG_UPPER_LIMIT) {
688     // Handle custom diagnostics, which cannot be mapped.
689     DiagLevel = CustomDiagInfo->getLevel(DiagID);
690   } else {
691     // Get the class of the diagnostic.  If this is a NOTE, map it onto whatever
692     // the diagnostic level was for the previous diagnostic so that it is
693     // filtered the same as the previous diagnostic.
694     unsigned DiagClass = getBuiltinDiagClass(DiagID);
695     if (DiagClass == CLASS_NOTE) {
696       DiagLevel = DiagnosticIDs::Note;
697     } else {
698       DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(),
699                                      Diag);
700     }
701   }
702 
703   if (DiagLevel != DiagnosticIDs::Note) {
704     // Record that a fatal error occurred only when we see a second
705     // non-note diagnostic. This allows notes to be attached to the
706     // fatal error, but suppresses any diagnostics that follow those
707     // notes.
708     if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
709       Diag.FatalErrorOccurred = true;
710 
711     Diag.LastDiagLevel = DiagLevel;
712   }
713 
714   // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
715   if (DiagLevel >= DiagnosticIDs::Error) {
716     ++Diag.TrapNumErrorsOccurred;
717     if (isUnrecoverable(DiagID))
718       ++Diag.TrapNumUnrecoverableErrorsOccurred;
719   }
720 
721   // If a fatal error has already been emitted, silence all subsequent
722   // diagnostics.
723   if (Diag.FatalErrorOccurred) {
724     if (DiagLevel >= DiagnosticIDs::Error &&
725         Diag.Client->IncludeInDiagnosticCounts()) {
726       ++Diag.NumErrors;
727       ++Diag.NumErrorsSuppressed;
728     }
729 
730     return false;
731   }
732 
733   // If the client doesn't care about this message, don't issue it.  If this is
734   // a note and the last real diagnostic was ignored, ignore it too.
735   if (DiagLevel == DiagnosticIDs::Ignored ||
736       (DiagLevel == DiagnosticIDs::Note &&
737        Diag.LastDiagLevel == DiagnosticIDs::Ignored))
738     return false;
739 
740   if (DiagLevel >= DiagnosticIDs::Error) {
741     if (isUnrecoverable(DiagID))
742       Diag.UnrecoverableErrorOccurred = true;
743 
744     if (Diag.Client->IncludeInDiagnosticCounts()) {
745       Diag.ErrorOccurred = true;
746       ++Diag.NumErrors;
747     }
748 
749     // If we've emitted a lot of errors, emit a fatal error instead of it to
750     // stop a flood of bogus errors.
751     if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
752         DiagLevel == DiagnosticIDs::Error) {
753       Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
754       return false;
755     }
756   }
757 
758   // If we have any Fix-Its, make sure that all of the Fix-Its point into
759   // source locations that aren't macro expansions. If any point into macro
760   // expansions, remove all of the Fix-Its.
761   for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) {
762     const FixItHint &FixIt = Diag.FixItHints[I];
763     if (FixIt.RemoveRange.isInvalid() ||
764         FixIt.RemoveRange.getBegin().isMacroID() ||
765         FixIt.RemoveRange.getEnd().isMacroID()) {
766       Diag.NumFixItHints = 0;
767       break;
768     }
769   }
770 
771   // Finally, report it.
772   Diag.Client->HandleDiagnostic((Diagnostic::Level)DiagLevel, Info);
773   if (Diag.Client->IncludeInDiagnosticCounts()) {
774     if (DiagLevel == DiagnosticIDs::Warning)
775       ++Diag.NumWarnings;
776   }
777 
778   Diag.CurDiagID = ~0U;
779 
780   return true;
781 }
782 
783 bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
784   if (DiagID >= diag::DIAG_UPPER_LIMIT) {
785     // Custom diagnostics.
786     return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
787   }
788 
789   // Only errors may be unrecoverable.
790   if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
791     return false;
792 
793   if (DiagID == diag::err_unavailable ||
794       DiagID == diag::err_unavailable_message)
795     return false;
796 
797   // Currently we consider all ARC errors as recoverable.
798   if (getCategoryNumberForDiag(DiagID) ==
799         diag::DiagCat_Automatic_Reference_Counting_Issue)
800     return false;
801 
802   return true;
803 }
804