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