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