1 //===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Diagnostic IDs-related interfaces.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Basic/DiagnosticIDs.h"
14 #include "clang/Basic/AllDiagnostics.h"
15 #include "clang/Basic/DiagnosticCategories.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/ErrorHandling.h"
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
24 // Builtin Diagnostic information
25 //===----------------------------------------------------------------------===//
29 // Diagnostic classes.
34 CLASS_EXTENSION = 0x04,
38 struct StaticDiagInfoRec {
40 unsigned DefaultSeverity : 3;
43 unsigned WarnNoWerror : 1;
44 unsigned WarnShowInSystemHeader : 1;
45 unsigned Category : 6;
47 uint16_t OptionGroupIndex;
49 uint16_t DescriptionLen;
50 const char *DescriptionStr;
52 unsigned getOptionGroupIndex() const {
53 return OptionGroupIndex;
56 StringRef getDescription() const {
57 return StringRef(DescriptionStr, DescriptionLen);
60 diag::Flavor getFlavor() const {
61 return Class == CLASS_REMARK ? diag::Flavor::Remark
62 : diag::Flavor::WarningOrError;
65 bool operator<(const StaticDiagInfoRec &RHS) const {
66 return DiagID < RHS.DiagID;
70 #define STRINGIFY_NAME(NAME) #NAME
71 #define VALIDATE_DIAG_SIZE(NAME) \
73 static_cast<unsigned>(diag::NUM_BUILTIN_##NAME##_DIAGNOSTICS) < \
74 static_cast<unsigned>(diag::DIAG_START_##NAME) + \
75 static_cast<unsigned>(diag::DIAG_SIZE_##NAME), \
77 DIAG_SIZE_##NAME) " is insufficient to contain all " \
78 "diagnostics, it may need to be made larger in " \
80 VALIDATE_DIAG_SIZE(COMMON)
81 VALIDATE_DIAG_SIZE(DRIVER)
82 VALIDATE_DIAG_SIZE(FRONTEND)
83 VALIDATE_DIAG_SIZE(SERIALIZATION)
84 VALIDATE_DIAG_SIZE(LEX)
85 VALIDATE_DIAG_SIZE(PARSE)
86 VALIDATE_DIAG_SIZE(AST)
87 VALIDATE_DIAG_SIZE(COMMENT)
88 VALIDATE_DIAG_SIZE(CROSSTU)
89 VALIDATE_DIAG_SIZE(SEMA)
90 VALIDATE_DIAG_SIZE(ANALYSIS)
91 VALIDATE_DIAG_SIZE(REFACTORING)
92 #undef VALIDATE_DIAG_SIZE
95 } // namespace anonymous
97 static const StaticDiagInfoRec StaticDiagInfo[] = {
98 #define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
99 SHOWINSYSHEADER, CATEGORY) \
101 diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, NOWERROR, \
102 SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), DESC \
105 #include "clang/Basic/DiagnosticCommonKinds.inc"
106 #include "clang/Basic/DiagnosticDriverKinds.inc"
107 #include "clang/Basic/DiagnosticFrontendKinds.inc"
108 #include "clang/Basic/DiagnosticSerializationKinds.inc"
109 #include "clang/Basic/DiagnosticLexKinds.inc"
110 #include "clang/Basic/DiagnosticParseKinds.inc"
111 #include "clang/Basic/DiagnosticASTKinds.inc"
112 #include "clang/Basic/DiagnosticCommentKinds.inc"
113 #include "clang/Basic/DiagnosticCrossTUKinds.inc"
114 #include "clang/Basic/DiagnosticSemaKinds.inc"
115 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
116 #include "clang/Basic/DiagnosticRefactoringKinds.inc"
120 static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
122 /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
123 /// or null if the ID is invalid.
124 static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
125 // Out of bounds diag. Can't be in the table.
126 using namespace diag;
127 if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
130 // Compute the index of the requested diagnostic in the static table.
131 // 1. Add the number of diagnostics in each category preceding the
132 // diagnostic and of the category the diagnostic is in. This gives us
133 // the offset of the category in the table.
134 // 2. Subtract the number of IDs in each category from our ID. This gives us
135 // the offset of the diagnostic in the category.
136 // This is cheaper than a binary search on the table as it doesn't touch
139 unsigned ID = DiagID - DIAG_START_COMMON - 1;
140 #define CATEGORY(NAME, PREV) \
141 if (DiagID > DIAG_START_##NAME) { \
142 Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
143 ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
145 CATEGORY(DRIVER, COMMON)
146 CATEGORY(FRONTEND, DRIVER)
147 CATEGORY(SERIALIZATION, FRONTEND)
148 CATEGORY(LEX, SERIALIZATION)
151 CATEGORY(COMMENT, AST)
152 CATEGORY(CROSSTU, COMMENT)
153 CATEGORY(SEMA, CROSSTU)
154 CATEGORY(ANALYSIS, SEMA)
155 CATEGORY(REFACTORING, ANALYSIS)
158 // Avoid out of bounds reads.
159 if (ID + Offset >= StaticDiagInfoSize)
162 assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
164 const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
165 // If the diag id doesn't match we found a different diag, abort. This can
166 // happen when this function is called with an ID that points into a hole in
168 if (Found->DiagID != DiagID)
173 static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) {
174 DiagnosticMapping Info = DiagnosticMapping::Make(
175 diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
177 if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
178 Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
180 if (StaticInfo->WarnNoWerror) {
181 assert(Info.getSeverity() == diag::Severity::Warning &&
182 "Unexpected mapping with no-Werror bit!");
183 Info.setNoWarningAsError(true);
190 /// getCategoryNumberForDiag - Return the category number that a specified
191 /// DiagID belongs to, or 0 if no category.
192 unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
193 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
194 return Info->Category;
199 // The diagnostic category names.
200 struct StaticDiagCategoryRec {
204 StringRef getName() const {
205 return StringRef(NameStr, NameLen);
210 // Unfortunately, the split between DiagnosticIDs and Diagnostic is not
211 // particularly clean, but for now we just implement this method here so we can
212 // access GetDefaultDiagMapping.
214 DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
215 std::pair<iterator, bool> Result =
216 DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
218 // Initialize the entry if we added it.
220 Result.first->second = GetDefaultDiagMapping(Diag);
222 return Result.first->second;
225 static const StaticDiagCategoryRec CategoryNameTable[] = {
226 #define GET_CATEGORY_TABLE
227 #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
228 #include "clang/Basic/DiagnosticGroups.inc"
229 #undef GET_CATEGORY_TABLE
233 /// getNumberOfCategories - Return the number of categories
234 unsigned DiagnosticIDs::getNumberOfCategories() {
235 return llvm::array_lengthof(CategoryNameTable) - 1;
238 /// getCategoryNameFromID - Given a category ID, return the name of the
239 /// category, an empty string if CategoryID is zero, or null if CategoryID is
241 StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
242 if (CategoryID >= getNumberOfCategories())
244 return CategoryNameTable[CategoryID].getName();
249 DiagnosticIDs::SFINAEResponse
250 DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
251 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
252 return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
253 return SFINAE_Report;
256 /// getBuiltinDiagClass - Return the class field of the diagnostic.
258 static unsigned getBuiltinDiagClass(unsigned DiagID) {
259 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
264 //===----------------------------------------------------------------------===//
265 // Custom Diagnostic information
266 //===----------------------------------------------------------------------===//
270 class CustomDiagInfo {
271 typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
272 std::vector<DiagDesc> DiagInfo;
273 std::map<DiagDesc, unsigned> DiagIDs;
276 /// getDescription - Return the description of the specified custom
278 StringRef getDescription(unsigned DiagID) const {
279 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
280 "Invalid diagnostic ID");
281 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
284 /// getLevel - Return the level of the specified custom diagnostic.
285 DiagnosticIDs::Level getLevel(unsigned DiagID) const {
286 assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
287 "Invalid diagnostic ID");
288 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
291 unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
292 DiagnosticIDs &Diags) {
293 DiagDesc D(L, std::string(Message));
294 // Check to see if it already exists.
295 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
296 if (I != DiagIDs.end() && I->first == D)
299 // If not, assign a new ID.
300 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
301 DiagIDs.insert(std::make_pair(D, ID));
302 DiagInfo.push_back(D);
307 } // end diag namespace
308 } // end clang namespace
311 //===----------------------------------------------------------------------===//
312 // Common Diagnostic implementation
313 //===----------------------------------------------------------------------===//
315 DiagnosticIDs::DiagnosticIDs() {}
317 DiagnosticIDs::~DiagnosticIDs() {}
319 /// getCustomDiagID - Return an ID for a diagnostic with the specified message
320 /// and level. If this is the first request for this diagnostic, it is
321 /// registered and created, otherwise the existing ID is returned.
323 /// \param FormatString A fixed diagnostic format string that will be hashed and
324 /// mapped to a unique DiagID.
325 unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
327 CustomDiagInfo.reset(new diag::CustomDiagInfo());
328 return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
332 /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
333 /// level of the specified diagnostic ID is a Warning or Extension.
334 /// This only works on builtin diagnostics, not custom ones, and is not legal to
336 bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
337 return DiagID < diag::DIAG_UPPER_LIMIT &&
338 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
341 /// Determine whether the given built-in diagnostic ID is a
343 bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
344 return DiagID < diag::DIAG_UPPER_LIMIT &&
345 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
348 /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
349 /// ID is for an extension of some sort. This also returns EnabledByDefault,
350 /// which is set to indicate whether the diagnostic is ignored by default (in
351 /// which case -pedantic enables it) or treated as a warning/error by default.
353 bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
354 bool &EnabledByDefault) {
355 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
356 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
360 GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored;
364 bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
365 if (DiagID >= diag::DIAG_UPPER_LIMIT)
368 return GetDefaultDiagMapping(DiagID).getSeverity() >= diag::Severity::Error;
371 /// getDescription - Given a diagnostic ID, return a description of the
373 StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
374 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
375 return Info->getDescription();
376 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
377 return CustomDiagInfo->getDescription(DiagID);
380 static DiagnosticIDs::Level toLevel(diag::Severity SV) {
382 case diag::Severity::Ignored:
383 return DiagnosticIDs::Ignored;
384 case diag::Severity::Remark:
385 return DiagnosticIDs::Remark;
386 case diag::Severity::Warning:
387 return DiagnosticIDs::Warning;
388 case diag::Severity::Error:
389 return DiagnosticIDs::Error;
390 case diag::Severity::Fatal:
391 return DiagnosticIDs::Fatal;
393 llvm_unreachable("unexpected severity");
396 /// getDiagnosticLevel - Based on the way the client configured the
397 /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
398 /// by consumable the DiagnosticClient.
400 DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
401 const DiagnosticsEngine &Diag) const {
402 // Handle custom diagnostics, which cannot be mapped.
403 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
404 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
405 return CustomDiagInfo->getLevel(DiagID);
408 unsigned DiagClass = getBuiltinDiagClass(DiagID);
409 if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
410 return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
413 /// Based on the way the client configured the Diagnostic
414 /// object, classify the specified diagnostic ID into a Level, consumable by
415 /// the DiagnosticClient.
417 /// \param Loc The source location we are interested in finding out the
418 /// diagnostic state. Can be null in order to query the latest state.
420 DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
421 const DiagnosticsEngine &Diag) const {
422 assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE);
424 // Specific non-error diagnostics may be mapped to various levels from ignored
425 // to error. Errors can only be mapped to fatal.
426 diag::Severity Result = diag::Severity::Fatal;
428 // Get the mapping information, or compute it lazily.
429 DiagnosticsEngine::DiagState *State = Diag.GetDiagStateForLoc(Loc);
430 DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID);
432 // TODO: Can a null severity really get here?
433 if (Mapping.getSeverity() != diag::Severity())
434 Result = Mapping.getSeverity();
436 // Upgrade ignored diagnostics if -Weverything is enabled.
437 if (State->EnableAllWarnings && Result == diag::Severity::Ignored &&
438 !Mapping.isUser() && getBuiltinDiagClass(DiagID) != CLASS_REMARK)
439 Result = diag::Severity::Warning;
441 // Ignore -pedantic diagnostics inside __extension__ blocks.
442 // (The diagnostics controlled by -pedantic are the extension diagnostics
443 // that are not enabled by default.)
444 bool EnabledByDefault = false;
445 bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
446 if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
447 return diag::Severity::Ignored;
449 // For extension diagnostics that haven't been explicitly mapped, check if we
450 // should upgrade the diagnostic.
451 if (IsExtensionDiag && !Mapping.isUser())
452 Result = std::max(Result, State->ExtBehavior);
454 // At this point, ignored errors can no longer be upgraded.
455 if (Result == diag::Severity::Ignored)
458 // Honor -w: this disables all messages which which are not Error/Fatal by
459 // default (disregarding attempts to upgrade severity from Warning to Error),
460 // as well as disabling all messages which are currently mapped to Warning
461 // (whether by default or downgraded from Error via e.g. -Wno-error or #pragma
463 if (State->IgnoreAllWarnings) {
464 if (Result == diag::Severity::Warning ||
465 (Result >= diag::Severity::Error &&
466 !isDefaultMappingAsError((diag::kind)DiagID)))
467 return diag::Severity::Ignored;
470 // If -Werror is enabled, map warnings to errors unless explicitly disabled.
471 if (Result == diag::Severity::Warning) {
472 if (State->WarningsAsErrors && !Mapping.hasNoWarningAsError())
473 Result = diag::Severity::Error;
476 // If -Wfatal-errors is enabled, map errors to fatal unless explicitly
478 if (Result == diag::Severity::Error) {
479 if (State->ErrorsAsFatal && !Mapping.hasNoErrorAsFatal())
480 Result = diag::Severity::Fatal;
483 // If explicitly requested, map fatal errors to errors.
484 if (Result == diag::Severity::Fatal &&
485 Diag.CurDiagID != diag::fatal_too_many_errors && Diag.FatalsAsError)
486 Result = diag::Severity::Error;
488 // Custom diagnostics always are emitted in system headers.
489 bool ShowInSystemHeader =
490 !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
492 // If we are in a system header, we ignore it. We look at the diagnostic class
493 // because we also want to ignore extensions and warnings in -Werror and
494 // -pedantic-errors modes, which *map* warnings/extensions to errors.
495 if (State->SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
496 Diag.getSourceManager().isInSystemHeader(
497 Diag.getSourceManager().getExpansionLoc(Loc)))
498 return diag::Severity::Ignored;
503 #define GET_DIAG_ARRAYS
504 #include "clang/Basic/DiagnosticGroups.inc"
505 #undef GET_DIAG_ARRAYS
508 struct WarningOption {
513 // String is stored with a pascal-style length byte.
514 StringRef getName() const {
515 return StringRef(DiagGroupNames + NameOffset + 1,
516 DiagGroupNames[NameOffset]);
521 // Second the table of options, sorted by name for fast binary lookup.
522 static const WarningOption OptionTable[] = {
523 #define GET_DIAG_TABLE
524 #include "clang/Basic/DiagnosticGroups.inc"
525 #undef GET_DIAG_TABLE
528 /// getWarningOptionForDiag - Return the lowest-level warning option that
529 /// enables the specified diagnostic. If there is no -Wfoo flag that controls
530 /// the diagnostic, this returns null.
531 StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
532 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
533 return OptionTable[Info->getOptionGroupIndex()].getName();
537 std::vector<std::string> DiagnosticIDs::getDiagnosticFlags() {
538 std::vector<std::string> Res;
539 for (size_t I = 1; DiagGroupNames[I] != '\0';) {
540 std::string Diag(DiagGroupNames + I + 1, DiagGroupNames[I]);
541 I += DiagGroupNames[I] + 1;
542 Res.push_back("-W" + Diag);
543 Res.push_back("-Wno-" + Diag);
549 /// Return \c true if any diagnostics were found in this group, even if they
550 /// were filtered out due to having the wrong flavor.
551 static bool getDiagnosticsInGroup(diag::Flavor Flavor,
552 const WarningOption *Group,
553 SmallVectorImpl<diag::kind> &Diags) {
554 // An empty group is considered to be a warning group: we have empty groups
555 // for GCC compatibility, and GCC does not have remarks.
556 if (!Group->Members && !Group->SubGroups)
557 return Flavor == diag::Flavor::Remark;
559 bool NotFound = true;
561 // Add the members of the option diagnostic set.
562 const int16_t *Member = DiagArrays + Group->Members;
563 for (; *Member != -1; ++Member) {
564 if (GetDiagInfo(*Member)->getFlavor() == Flavor) {
566 Diags.push_back(*Member);
570 // Add the members of the subgroups.
571 const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
572 for (; *SubGroups != (int16_t)-1; ++SubGroups)
573 NotFound &= getDiagnosticsInGroup(Flavor, &OptionTable[(short)*SubGroups],
580 DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
581 SmallVectorImpl<diag::kind> &Diags) const {
582 auto Found = llvm::partition_point(
583 OptionTable, [=](const WarningOption &O) { return O.getName() < Group; });
584 if (Found == std::end(OptionTable) || Found->getName() != Group)
585 return true; // Option not found.
587 return ::getDiagnosticsInGroup(Flavor, Found, Diags);
590 void DiagnosticIDs::getAllDiagnostics(diag::Flavor Flavor,
591 std::vector<diag::kind> &Diags) {
592 for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
593 if (StaticDiagInfo[i].getFlavor() == Flavor)
594 Diags.push_back(StaticDiagInfo[i].DiagID);
597 StringRef DiagnosticIDs::getNearestOption(diag::Flavor Flavor,
600 unsigned BestDistance = Group.size() + 1; // Sanity threshold.
601 for (const WarningOption &O : OptionTable) {
602 // Don't suggest ignored warning flags.
603 if (!O.Members && !O.SubGroups)
606 unsigned Distance = O.getName().edit_distance(Group, true, BestDistance);
607 if (Distance > BestDistance)
610 // Don't suggest groups that are not of this kind.
611 llvm::SmallVector<diag::kind, 8> Diags;
612 if (::getDiagnosticsInGroup(Flavor, &O, Diags) || Diags.empty())
615 if (Distance == BestDistance) {
616 // Two matches with the same distance, don't prefer one over the other.
618 } else if (Distance < BestDistance) {
619 // This is a better match.
621 BestDistance = Distance;
628 /// ProcessDiag - This is the method used to report a diagnostic that is
629 /// finally fully formed.
630 bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
631 Diagnostic Info(&Diag);
633 assert(Diag.getClient() && "DiagnosticClient not set!");
635 // Figure out the diagnostic level of this message.
636 unsigned DiagID = Info.getID();
637 DiagnosticIDs::Level DiagLevel
638 = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
640 // Update counts for DiagnosticErrorTrap even if a fatal error occurred
641 // or diagnostics are suppressed.
642 if (DiagLevel >= DiagnosticIDs::Error) {
643 ++Diag.TrapNumErrorsOccurred;
644 if (isUnrecoverable(DiagID))
645 ++Diag.TrapNumUnrecoverableErrorsOccurred;
648 if (Diag.SuppressAllDiagnostics)
651 if (DiagLevel != DiagnosticIDs::Note) {
652 // Record that a fatal error occurred only when we see a second
653 // non-note diagnostic. This allows notes to be attached to the
654 // fatal error, but suppresses any diagnostics that follow those
656 if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
657 Diag.FatalErrorOccurred = true;
659 Diag.LastDiagLevel = DiagLevel;
662 // If a fatal error has already been emitted, silence all subsequent
664 if (Diag.FatalErrorOccurred) {
665 if (DiagLevel >= DiagnosticIDs::Error &&
666 Diag.Client->IncludeInDiagnosticCounts()) {
673 // If the client doesn't care about this message, don't issue it. If this is
674 // a note and the last real diagnostic was ignored, ignore it too.
675 if (DiagLevel == DiagnosticIDs::Ignored ||
676 (DiagLevel == DiagnosticIDs::Note &&
677 Diag.LastDiagLevel == DiagnosticIDs::Ignored))
680 if (DiagLevel >= DiagnosticIDs::Error) {
681 if (isUnrecoverable(DiagID))
682 Diag.UnrecoverableErrorOccurred = true;
684 // Warnings which have been upgraded to errors do not prevent compilation.
685 if (isDefaultMappingAsError(DiagID))
686 Diag.UncompilableErrorOccurred = true;
688 Diag.ErrorOccurred = true;
689 if (Diag.Client->IncludeInDiagnosticCounts()) {
693 // If we've emitted a lot of errors, emit a fatal error instead of it to
694 // stop a flood of bogus errors.
695 if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
696 DiagLevel == DiagnosticIDs::Error) {
697 Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
702 // Make sure we set FatalErrorOccurred to ensure that the notes from the
703 // diagnostic that caused `fatal_too_many_errors` won't be emitted.
704 if (Diag.CurDiagID == diag::fatal_too_many_errors)
705 Diag.FatalErrorOccurred = true;
706 // Finally, report it.
707 EmitDiag(Diag, DiagLevel);
711 void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
712 Diagnostic Info(&Diag);
713 assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
715 Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
716 if (Diag.Client->IncludeInDiagnosticCounts()) {
717 if (DiagLevel == DiagnosticIDs::Warning)
721 Diag.CurDiagID = ~0U;
724 bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
725 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
726 assert(CustomDiagInfo && "Invalid CustomDiagInfo");
727 // Custom diagnostics.
728 return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
731 // Only errors may be unrecoverable.
732 if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
735 if (DiagID == diag::err_unavailable ||
736 DiagID == diag::err_unavailable_message)
739 // Currently we consider all ARC errors as recoverable.
740 if (isARCDiagnostic(DiagID))
746 bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
747 unsigned cat = getCategoryNumberForDiag(DiagID);
748 return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");