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/Basic/DiagnosticIDs.h"
15 #include "clang/Basic/AllDiagnostics.h"
16 #include "clang/Basic/DiagnosticCategories.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 #include <map>
22 using namespace clang;
23
24 //===----------------------------------------------------------------------===//
25 // Builtin Diagnostic information
26 //===----------------------------------------------------------------------===//
27
28 namespace {
29
30 // Diagnostic classes.
31 enum {
32 CLASS_NOTE = 0x01,
33 CLASS_WARNING = 0x02,
34 CLASS_EXTENSION = 0x03,
35 CLASS_ERROR = 0x04
36 };
37
38 struct StaticDiagInfoRec {
39 unsigned short DiagID;
40 unsigned Mapping : 3;
41 unsigned Class : 3;
42 unsigned SFINAE : 1;
43 unsigned AccessControl : 1;
44 unsigned WarnNoWerror : 1;
45 unsigned WarnShowInSystemHeader : 1;
46 unsigned Category : 5;
47
48 uint16_t OptionGroupIndex;
49
50 uint16_t DescriptionLen;
51 const char *DescriptionStr;
52
getOptionGroupIndex__anon557f21980111::StaticDiagInfoRec53 unsigned getOptionGroupIndex() const {
54 return OptionGroupIndex;
55 }
56
getDescription__anon557f21980111::StaticDiagInfoRec57 StringRef getDescription() const {
58 return StringRef(DescriptionStr, DescriptionLen);
59 }
60
operator <__anon557f21980111::StaticDiagInfoRec61 bool operator<(const StaticDiagInfoRec &RHS) const {
62 return DiagID < RHS.DiagID;
63 }
64 };
65
66 } // namespace anonymous
67
68 static const StaticDiagInfoRec StaticDiagInfo[] = {
69 #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \
70 SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \
71 CATEGORY) \
72 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \
73 NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \
74 STR_SIZE(DESC, uint16_t), DESC },
75 #include "clang/Basic/DiagnosticCommonKinds.inc"
76 #include "clang/Basic/DiagnosticDriverKinds.inc"
77 #include "clang/Basic/DiagnosticFrontendKinds.inc"
78 #include "clang/Basic/DiagnosticSerializationKinds.inc"
79 #include "clang/Basic/DiagnosticLexKinds.inc"
80 #include "clang/Basic/DiagnosticParseKinds.inc"
81 #include "clang/Basic/DiagnosticASTKinds.inc"
82 #include "clang/Basic/DiagnosticCommentKinds.inc"
83 #include "clang/Basic/DiagnosticSemaKinds.inc"
84 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
85 #undef DIAG
86 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
87 };
88
89 static const unsigned StaticDiagInfoSize =
90 sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
91
92 /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
93 /// or null if the ID is invalid.
GetDiagInfo(unsigned DiagID)94 static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
95 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
96 #ifndef NDEBUG
97 static bool IsFirst = true;
98 if (IsFirst) {
99 for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
100 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
101 "Diag ID conflict, the enums at the start of clang::diag (in "
102 "DiagnosticIDs.h) probably need to be increased");
103
104 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
105 "Improperly sorted diag info");
106 }
107 IsFirst = false;
108 }
109 #endif
110
111 // Search the diagnostic table with a binary search.
112 StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID),
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
114
115 const StaticDiagInfoRec *Found =
116 std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
117 if (Found == StaticDiagInfo + StaticDiagInfoSize ||
118 Found->DiagID != DiagID)
119 return 0;
120
121 return Found;
122 }
123
GetDefaultDiagMappingInfo(unsigned DiagID)124 static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) {
125 DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make(
126 diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false);
127
128 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
129 Info.setMapping((diag::Mapping) StaticInfo->Mapping);
130
131 if (StaticInfo->WarnNoWerror) {
132 assert(Info.getMapping() == diag::MAP_WARNING &&
133 "Unexpected mapping with no-Werror bit!");
134 Info.setNoWarningAsError(true);
135 }
136
137 if (StaticInfo->WarnShowInSystemHeader) {
138 assert(Info.getMapping() == diag::MAP_WARNING &&
139 "Unexpected mapping with show-in-system-header bit!");
140 Info.setShowInSystemHeader(true);
141 }
142 }
143
144 return Info;
145 }
146
147 /// getCategoryNumberForDiag - Return the category number that a specified
148 /// DiagID belongs to, or 0 if no category.
getCategoryNumberForDiag(unsigned DiagID)149 unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
150 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
151 return Info->Category;
152 return 0;
153 }
154
155 namespace {
156 // The diagnostic category names.
157 struct StaticDiagCategoryRec {
158 const char *NameStr;
159 uint8_t NameLen;
160
getName__anon557f21980311::StaticDiagCategoryRec161 StringRef getName() const {
162 return StringRef(NameStr, NameLen);
163 }
164 };
165 }
166
167 // Unfortunately, the split between DiagnosticIDs and Diagnostic is not
168 // particularly clean, but for now we just implement this method here so we can
169 // access GetDefaultDiagMapping.
getOrAddMappingInfo(diag::kind Diag)170 DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo(
171 diag::kind Diag)
172 {
173 std::pair<iterator, bool> Result = DiagMap.insert(
174 std::make_pair(Diag, DiagnosticMappingInfo()));
175
176 // Initialize the entry if we added it.
177 if (Result.second)
178 Result.first->second = GetDefaultDiagMappingInfo(Diag);
179
180 return Result.first->second;
181 }
182
183 static const StaticDiagCategoryRec CategoryNameTable[] = {
184 #define GET_CATEGORY_TABLE
185 #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
186 #include "clang/Basic/DiagnosticGroups.inc"
187 #undef GET_CATEGORY_TABLE
188 { 0, 0 }
189 };
190
191 /// getNumberOfCategories - Return the number of categories
getNumberOfCategories()192 unsigned DiagnosticIDs::getNumberOfCategories() {
193 return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
194 }
195
196 /// getCategoryNameFromID - Given a category ID, return the name of the
197 /// category, an empty string if CategoryID is zero, or null if CategoryID is
198 /// invalid.
getCategoryNameFromID(unsigned CategoryID)199 StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
200 if (CategoryID >= getNumberOfCategories())
201 return StringRef();
202 return CategoryNameTable[CategoryID].getName();
203 }
204
205
206
207 DiagnosticIDs::SFINAEResponse
getDiagnosticSFINAEResponse(unsigned DiagID)208 DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
209 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
210 if (Info->AccessControl)
211 return SFINAE_AccessControl;
212
213 if (!Info->SFINAE)
214 return SFINAE_Report;
215
216 if (Info->Class == CLASS_ERROR)
217 return SFINAE_SubstitutionFailure;
218
219 // Suppress notes, warnings, and extensions;
220 return SFINAE_Suppress;
221 }
222
223 return SFINAE_Report;
224 }
225
226 /// getBuiltinDiagClass - Return the class field of the diagnostic.
227 ///
getBuiltinDiagClass(unsigned DiagID)228 static unsigned getBuiltinDiagClass(unsigned DiagID) {
229 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
230 return Info->Class;
231 return ~0U;
232 }
233
234 //===----------------------------------------------------------------------===//
235 // Custom Diagnostic information
236 //===----------------------------------------------------------------------===//
237
238 namespace clang {
239 namespace diag {
240 class CustomDiagInfo {
241 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
242 std::vector<DiagDesc> DiagInfo;
243 std::map<DiagDesc, unsigned> DiagIDs;
244 public:
245
246 /// getDescription - Return the description of the specified custom
247 /// diagnostic.
getDescription(unsigned DiagID) const248 StringRef getDescription(unsigned DiagID) const {
249 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
250 "Invalid diagnosic ID");
251 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
252 }
253
254 /// getLevel - Return the level of the specified custom diagnostic.
getLevel(unsigned DiagID) const255 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
256 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
257 "Invalid diagnosic ID");
258 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
259 }
260
getOrCreateDiagID(DiagnosticIDs::Level L,StringRef Message,DiagnosticIDs & Diags)261 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
262 DiagnosticIDs &Diags) {
263 DiagDesc D(L, Message);
264 // Check to see if it already exists.
265 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
266 if (I != DiagIDs.end() && I->first == D)
267 return I->second;
268
269 // If not, assign a new ID.
270 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
271 DiagIDs.insert(std::make_pair(D, ID));
272 DiagInfo.push_back(D);
273 return ID;
274 }
275 };
276
277 } // end diag namespace
278 } // end clang namespace
279
280
281 //===----------------------------------------------------------------------===//
282 // Common Diagnostic implementation
283 //===----------------------------------------------------------------------===//
284
DiagnosticIDs()285 DiagnosticIDs::DiagnosticIDs() {
286 CustomDiagInfo = 0;
287 }
288
~DiagnosticIDs()289 DiagnosticIDs::~DiagnosticIDs() {
290 delete CustomDiagInfo;
291 }
292
293 /// getCustomDiagID - Return an ID for a diagnostic with the specified message
294 /// and level. If this is the first request for this diagnosic, it is
295 /// registered and created, otherwise the existing ID is returned.
getCustomDiagID(Level L,StringRef Message)296 unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) {
297 if (CustomDiagInfo == 0)
298 CustomDiagInfo = new diag::CustomDiagInfo();
299 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
300 }
301
302
303 /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
304 /// level of the specified diagnostic ID is a Warning or Extension.
305 /// This only works on builtin diagnostics, not custom ones, and is not legal to
306 /// call on NOTEs.
isBuiltinWarningOrExtension(unsigned DiagID)307 bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
308 return DiagID < diag::DIAG_UPPER_LIMIT &&
309 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
310 }
311
312 /// \brief Determine whether the given built-in diagnostic ID is a
313 /// Note.
isBuiltinNote(unsigned DiagID)314 bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
315 return DiagID < diag::DIAG_UPPER_LIMIT &&
316 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
317 }
318
319 /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
320 /// ID is for an extension of some sort. This also returns EnabledByDefault,
321 /// which is set to indicate whether the diagnostic is ignored by default (in
322 /// which case -pedantic enables it) or treated as a warning/error by default.
323 ///
isBuiltinExtensionDiag(unsigned DiagID,bool & EnabledByDefault)324 bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
325 bool &EnabledByDefault) {
326 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
327 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
328 return false;
329
330 EnabledByDefault =
331 GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE;
332 return true;
333 }
334
isDefaultMappingAsError(unsigned DiagID)335 bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
336 if (DiagID >= diag::DIAG_UPPER_LIMIT)
337 return false;
338
339 return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
340 }
341
342 /// getDescription - Given a diagnostic ID, return a description of the
343 /// issue.
getDescription(unsigned DiagID) const344 StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
345 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
346 return Info->getDescription();
347 return CustomDiagInfo->getDescription(DiagID);
348 }
349
350 /// getDiagnosticLevel - Based on the way the client configured the
351 /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
352 /// by consumable the DiagnosticClient.
353 DiagnosticIDs::Level
getDiagnosticLevel(unsigned DiagID,SourceLocation Loc,const DiagnosticsEngine & Diag) const354 DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
355 const DiagnosticsEngine &Diag) const {
356 // Handle custom diagnostics, which cannot be mapped.
357 if (DiagID >= diag::DIAG_UPPER_LIMIT)
358 return CustomDiagInfo->getLevel(DiagID);
359
360 unsigned DiagClass = getBuiltinDiagClass(DiagID);
361 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
362 return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag);
363 }
364
365 /// \brief Based on the way the client configured the Diagnostic
366 /// object, classify the specified diagnostic ID into a Level, consumable by
367 /// the DiagnosticClient.
368 ///
369 /// \param Loc The source location we are interested in finding out the
370 /// diagnostic state. Can be null in order to query the latest state.
371 DiagnosticIDs::Level
getDiagnosticLevel(unsigned DiagID,unsigned DiagClass,SourceLocation Loc,const DiagnosticsEngine & Diag) const372 DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
373 SourceLocation Loc,
374 const DiagnosticsEngine &Diag) const {
375 // Specific non-error diagnostics may be mapped to various levels from ignored
376 // to error. Errors can only be mapped to fatal.
377 DiagnosticIDs::Level Result = DiagnosticIDs::Fatal;
378
379 DiagnosticsEngine::DiagStatePointsTy::iterator
380 Pos = Diag.GetDiagStatePointForLoc(Loc);
381 DiagnosticsEngine::DiagState *State = Pos->State;
382
383 // Get the mapping information, or compute it lazily.
384 DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo(
385 (diag::kind)DiagID);
386
387 switch (MappingInfo.getMapping()) {
388 case diag::MAP_IGNORE:
389 Result = DiagnosticIDs::Ignored;
390 break;
391 case diag::MAP_WARNING:
392 Result = DiagnosticIDs::Warning;
393 break;
394 case diag::MAP_ERROR:
395 Result = DiagnosticIDs::Error;
396 break;
397 case diag::MAP_FATAL:
398 Result = DiagnosticIDs::Fatal;
399 break;
400 }
401
402 // Upgrade ignored diagnostics if -Weverything is enabled.
403 if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored &&
404 !MappingInfo.isUser())
405 Result = DiagnosticIDs::Warning;
406
407 // Ignore -pedantic diagnostics inside __extension__ blocks.
408 // (The diagnostics controlled by -pedantic are the extension diagnostics
409 // that are not enabled by default.)
410 bool EnabledByDefault = false;
411 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
412 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
413 return DiagnosticIDs::Ignored;
414
415 // For extension diagnostics that haven't been explicitly mapped, check if we
416 // should upgrade the diagnostic.
417 if (IsExtensionDiag && !MappingInfo.isUser()) {
418 switch (Diag.ExtBehavior) {
419 case DiagnosticsEngine::Ext_Ignore:
420 break;
421 case DiagnosticsEngine::Ext_Warn:
422 // Upgrade ignored diagnostics to warnings.
423 if (Result == DiagnosticIDs::Ignored)
424 Result = DiagnosticIDs::Warning;
425 break;
426 case DiagnosticsEngine::Ext_Error:
427 // Upgrade ignored or warning diagnostics to errors.
428 if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning)
429 Result = DiagnosticIDs::Error;
430 break;
431 }
432 }
433
434 // At this point, ignored errors can no longer be upgraded.
435 if (Result == DiagnosticIDs::Ignored)
436 return Result;
437
438 // Honor -w, which is lower in priority than pedantic-errors, but higher than
439 // -Werror.
440 if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings)
441 return DiagnosticIDs::Ignored;
442
443 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
444 if (Result == DiagnosticIDs::Warning) {
445 if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError())
446 Result = DiagnosticIDs::Error;
447 }
448
449 // If -Wfatal-errors is enabled, map errors to fatal unless explicity
450 // disabled.
451 if (Result == DiagnosticIDs::Error) {
452 if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal())
453 Result = DiagnosticIDs::Fatal;
454 }
455
456 // If we are in a system header, we ignore it. We look at the diagnostic class
457 // because we also want to ignore extensions and warnings in -Werror and
458 // -pedantic-errors modes, which *map* warnings/extensions to errors.
459 if (Result >= DiagnosticIDs::Warning &&
460 DiagClass != CLASS_ERROR &&
461 // Custom diagnostics always are emitted in system headers.
462 DiagID < diag::DIAG_UPPER_LIMIT &&
463 !MappingInfo.hasShowInSystemHeader() &&
464 Diag.SuppressSystemWarnings &&
465 Loc.isValid() &&
466 Diag.getSourceManager().isInSystemHeader(
467 Diag.getSourceManager().getExpansionLoc(Loc)))
468 return DiagnosticIDs::Ignored;
469
470 return Result;
471 }
472
473 struct clang::WarningOption {
474 // Be safe with the size of 'NameLen' because we don't statically check if
475 // the size will fit in the field; the struct size won't decrease with a
476 // shorter type anyway.
477 size_t NameLen;
478 const char *NameStr;
479 const short *Members;
480 const short *SubGroups;
481
getNameclang::WarningOption482 StringRef getName() const {
483 return StringRef(NameStr, NameLen);
484 }
485 };
486
487 #define GET_DIAG_ARRAYS
488 #include "clang/Basic/DiagnosticGroups.inc"
489 #undef GET_DIAG_ARRAYS
490
491 // Second the table of options, sorted by name for fast binary lookup.
492 static const WarningOption OptionTable[] = {
493 #define GET_DIAG_TABLE
494 #include "clang/Basic/DiagnosticGroups.inc"
495 #undef GET_DIAG_TABLE
496 };
497 static const size_t OptionTableSize =
498 sizeof(OptionTable) / sizeof(OptionTable[0]);
499
WarningOptionCompare(const WarningOption & LHS,const WarningOption & RHS)500 static bool WarningOptionCompare(const WarningOption &LHS,
501 const WarningOption &RHS) {
502 return LHS.getName() < RHS.getName();
503 }
504
505 /// getWarningOptionForDiag - Return the lowest-level warning option that
506 /// enables the specified diagnostic. If there is no -Wfoo flag that controls
507 /// the diagnostic, this returns null.
getWarningOptionForDiag(unsigned DiagID)508 StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
509 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
510 return OptionTable[Info->getOptionGroupIndex()].getName();
511 return StringRef();
512 }
513
getDiagnosticsInGroup(const WarningOption * Group,llvm::SmallVectorImpl<diag::kind> & Diags) const514 void DiagnosticIDs::getDiagnosticsInGroup(
515 const WarningOption *Group,
516 llvm::SmallVectorImpl<diag::kind> &Diags) const
517 {
518 // Add the members of the option diagnostic set.
519 if (const short *Member = Group->Members) {
520 for (; *Member != -1; ++Member)
521 Diags.push_back(*Member);
522 }
523
524 // Add the members of the subgroups.
525 if (const short *SubGroups = Group->SubGroups) {
526 for (; *SubGroups != (short)-1; ++SubGroups)
527 getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags);
528 }
529 }
530
getDiagnosticsInGroup(StringRef Group,llvm::SmallVectorImpl<diag::kind> & Diags) const531 bool DiagnosticIDs::getDiagnosticsInGroup(
532 StringRef Group,
533 llvm::SmallVectorImpl<diag::kind> &Diags) const
534 {
535 WarningOption Key = { Group.size(), Group.data(), 0, 0 };
536 const WarningOption *Found =
537 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
538 WarningOptionCompare);
539 if (Found == OptionTable + OptionTableSize ||
540 Found->getName() != Group)
541 return true; // Option not found.
542
543 getDiagnosticsInGroup(Found, Diags);
544 return false;
545 }
546
getAllDiagnostics(llvm::SmallVectorImpl<diag::kind> & Diags) const547 void DiagnosticIDs::getAllDiagnostics(
548 llvm::SmallVectorImpl<diag::kind> &Diags) const {
549 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
550 Diags.push_back(StaticDiagInfo[i].DiagID);
551 }
552
getNearestWarningOption(StringRef Group)553 StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
554 StringRef Best;
555 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
556 for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
557 i != e; ++i) {
558 // Don't suggest ignored warning flags.
559 if (!i->Members && !i->SubGroups)
560 continue;
561
562 unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
563 if (Distance == BestDistance) {
564 // Two matches with the same distance, don't prefer one over the other.
565 Best = "";
566 } else if (Distance < BestDistance) {
567 // This is a better match.
568 Best = i->getName();
569 BestDistance = Distance;
570 }
571 }
572
573 return Best;
574 }
575
576 /// ProcessDiag - This is the method used to report a diagnostic that is
577 /// finally fully formed.
ProcessDiag(DiagnosticsEngine & Diag) const578 bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
579 Diagnostic Info(&Diag);
580
581 if (Diag.SuppressAllDiagnostics)
582 return false;
583
584 assert(Diag.getClient() && "DiagnosticClient not set!");
585
586 // Figure out the diagnostic level of this message.
587 unsigned DiagID = Info.getID();
588 DiagnosticIDs::Level DiagLevel
589 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
590
591 if (DiagLevel != DiagnosticIDs::Note) {
592 // Record that a fatal error occurred only when we see a second
593 // non-note diagnostic. This allows notes to be attached to the
594 // fatal error, but suppresses any diagnostics that follow those
595 // notes.
596 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
597 Diag.FatalErrorOccurred = true;
598
599 Diag.LastDiagLevel = DiagLevel;
600 }
601
602 // Update counts for DiagnosticErrorTrap even if a fatal error occurred.
603 if (DiagLevel >= DiagnosticIDs::Error) {
604 ++Diag.TrapNumErrorsOccurred;
605 if (isUnrecoverable(DiagID))
606 ++Diag.TrapNumUnrecoverableErrorsOccurred;
607 }
608
609 // If a fatal error has already been emitted, silence all subsequent
610 // diagnostics.
611 if (Diag.FatalErrorOccurred) {
612 if (DiagLevel >= DiagnosticIDs::Error &&
613 Diag.Client->IncludeInDiagnosticCounts()) {
614 ++Diag.NumErrors;
615 ++Diag.NumErrorsSuppressed;
616 }
617
618 return false;
619 }
620
621 // If the client doesn't care about this message, don't issue it. If this is
622 // a note and the last real diagnostic was ignored, ignore it too.
623 if (DiagLevel == DiagnosticIDs::Ignored ||
624 (DiagLevel == DiagnosticIDs::Note &&
625 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
626 return false;
627
628 if (DiagLevel >= DiagnosticIDs::Error) {
629 if (isUnrecoverable(DiagID))
630 Diag.UnrecoverableErrorOccurred = true;
631
632 if (Diag.Client->IncludeInDiagnosticCounts()) {
633 Diag.ErrorOccurred = true;
634 ++Diag.NumErrors;
635 }
636
637 // If we've emitted a lot of errors, emit a fatal error instead of it to
638 // stop a flood of bogus errors.
639 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
640 DiagLevel == DiagnosticIDs::Error) {
641 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
642 return false;
643 }
644 }
645
646 // Finally, report it.
647 EmitDiag(Diag, DiagLevel);
648 return true;
649 }
650
EmitDiag(DiagnosticsEngine & Diag,Level DiagLevel) const651 void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
652 Diagnostic Info(&Diag);
653 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
654
655 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
656 if (Diag.Client->IncludeInDiagnosticCounts()) {
657 if (DiagLevel == DiagnosticIDs::Warning)
658 ++Diag.NumWarnings;
659 }
660
661 Diag.CurDiagID = ~0U;
662 }
663
isUnrecoverable(unsigned DiagID) const664 bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
665 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
666 // Custom diagnostics.
667 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
668 }
669
670 // Only errors may be unrecoverable.
671 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
672 return false;
673
674 if (DiagID == diag::err_unavailable ||
675 DiagID == diag::err_unavailable_message)
676 return false;
677
678 // Currently we consider all ARC errors as recoverable.
679 if (isARCDiagnostic(DiagID))
680 return false;
681
682 return true;
683 }
684
isARCDiagnostic(unsigned DiagID)685 bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
686 unsigned cat = getCategoryNumberForDiag(DiagID);
687 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
688 }
689
690