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