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