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