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