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