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 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 14 | #include "clang/Basic/DiagnosticIDs.h" |
David Blaikie | 6c44886 | 2012-02-15 21:58:34 +0000 | [diff] [blame] | 15 | #include "clang/Basic/AllDiagnostics.h" |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 16 | #include "clang/Basic/DiagnosticCategories.h" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 20 | #include <map> |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Builtin Diagnostic information |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // Diagnostic classes. |
| 30 | enum { |
| 31 | CLASS_NOTE = 0x01, |
| 32 | CLASS_WARNING = 0x02, |
| 33 | CLASS_EXTENSION = 0x03, |
| 34 | CLASS_ERROR = 0x04 |
| 35 | }; |
| 36 | |
| 37 | struct StaticDiagInfoRec { |
| 38 | unsigned short DiagID; |
| 39 | unsigned Mapping : 3; |
| 40 | unsigned Class : 3; |
Douglas Gregor | 418df34 | 2011-01-27 21:06:28 +0000 | [diff] [blame] | 41 | unsigned SFINAE : 1; |
| 42 | unsigned AccessControl : 1; |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 43 | unsigned WarnNoWerror : 1; |
| 44 | unsigned WarnShowInSystemHeader : 1; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 45 | unsigned Category : 5; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 46 | |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 47 | uint16_t OptionGroupIndex; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 48 | |
| 49 | uint16_t DescriptionLen; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 50 | const char *DescriptionStr; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 51 | |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 52 | unsigned getOptionGroupIndex() const { |
| 53 | return OptionGroupIndex; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 56 | StringRef getDescription() const { |
| 57 | return StringRef(DescriptionStr, DescriptionLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 58 | } |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 59 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 60 | bool operator<(const StaticDiagInfoRec &RHS) const { |
| 61 | return DiagID < RHS.DiagID; |
| 62 | } |
| 63 | }; |
| 64 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 65 | } // namespace anonymous |
| 66 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 67 | static const StaticDiagInfoRec StaticDiagInfo[] = { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 68 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \ |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 69 | SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \ |
Benjamin Kramer | f94d392 | 2012-02-09 19:38:26 +0000 | [diff] [blame] | 70 | CATEGORY) \ |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 71 | { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \ |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 72 | NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, \ |
| 73 | STR_SIZE(DESC, uint16_t), DESC }, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 74 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
| 75 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
| 76 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
Chandler Carruth | a2398d7 | 2011-12-09 00:02:23 +0000 | [diff] [blame] | 77 | #include "clang/Basic/DiagnosticSerializationKinds.inc" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 78 | #include "clang/Basic/DiagnosticLexKinds.inc" |
| 79 | #include "clang/Basic/DiagnosticParseKinds.inc" |
| 80 | #include "clang/Basic/DiagnosticASTKinds.inc" |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 81 | #include "clang/Basic/DiagnosticCommentKinds.inc" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 82 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
| 83 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 84 | #undef DIAG |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 85 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static const unsigned StaticDiagInfoSize = |
| 89 | sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1; |
| 90 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 91 | /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID, |
| 92 | /// or null if the ID is invalid. |
| 93 | static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 94 | // If assertions are enabled, verify that the StaticDiagInfo array is sorted. |
| 95 | #ifndef NDEBUG |
| 96 | static bool IsFirst = true; |
| 97 | if (IsFirst) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 98 | for (unsigned i = 1; i != StaticDiagInfoSize; ++i) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 99 | assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID && |
| 100 | "Diag ID conflict, the enums at the start of clang::diag (in " |
Fariborz Jahanian | f84109e | 2011-01-07 18:59:25 +0000 | [diff] [blame] | 101 | "DiagnosticIDs.h) probably need to be increased"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 102 | |
| 103 | assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] && |
| 104 | "Improperly sorted diag info"); |
| 105 | } |
| 106 | IsFirst = false; |
| 107 | } |
| 108 | #endif |
| 109 | |
Benjamin Kramer | a07b59e | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 110 | // Out of bounds diag. Can't be in the table. |
| 111 | using namespace diag; |
| 112 | if (DiagID >= DIAG_UPPER_LIMIT) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | |
Benjamin Kramer | a07b59e | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 115 | // Compute the index of the requested diagnostic in the static table. |
| 116 | // 1. Add the number of diagnostics in each category preceeding the |
| 117 | // diagnostic and of the category the diagnostic is in. This gives us |
| 118 | // the offset of the category in the table. |
| 119 | // 2. Subtract the number of IDs in each category from our ID. This gives us |
| 120 | // the offset of the diagnostic in the category. |
| 121 | // This is cheaper than a binary search on the table as it doesn't touch |
| 122 | // memory at all. |
| 123 | unsigned Offset = 0; |
| 124 | unsigned ID = DiagID; |
| 125 | #define DIAG_START_COMMON 0 // Sentinel value. |
| 126 | #define CATEGORY(NAME, PREV) \ |
| 127 | if (DiagID > DIAG_START_##NAME) { \ |
Argyrios Kyrtzidis | 35abfcd | 2013-01-02 22:26:07 +0000 | [diff] [blame] | 128 | Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \ |
| 129 | ID -= DIAG_START_##NAME - DIAG_START_##PREV; \ |
Benjamin Kramer | a07b59e | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 130 | } |
| 131 | CATEGORY(DRIVER, COMMON) |
| 132 | CATEGORY(FRONTEND, DRIVER) |
| 133 | CATEGORY(SERIALIZATION, FRONTEND) |
| 134 | CATEGORY(LEX, SERIALIZATION) |
| 135 | CATEGORY(PARSE, LEX) |
| 136 | CATEGORY(AST, PARSE) |
| 137 | CATEGORY(COMMENT, AST) |
| 138 | CATEGORY(SEMA, COMMENT) |
| 139 | CATEGORY(ANALYSIS, SEMA) |
| 140 | #undef CATEGORY |
| 141 | #undef DIAG_START_COMMON |
| 142 | |
| 143 | // Avoid out of bounds reads. |
| 144 | if (ID + Offset >= StaticDiagInfoSize) |
| 145 | return 0; |
| 146 | |
Argyrios Kyrtzidis | 35abfcd | 2013-01-02 22:26:07 +0000 | [diff] [blame] | 147 | assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize); |
| 148 | |
Benjamin Kramer | a07b59e | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 149 | const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset]; |
| 150 | // If the diag id doesn't match we found a different diag, abort. This can |
| 151 | // happen when this function is called with an ID that points into a hole in |
| 152 | // the diagID space. |
| 153 | if (Found->DiagID != DiagID) |
| 154 | return 0; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 155 | return Found; |
| 156 | } |
| 157 | |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 158 | static DiagnosticMappingInfo GetDefaultDiagMappingInfo(unsigned DiagID) { |
Daniel Dunbar | aeacae5 | 2011-09-29 02:03:01 +0000 | [diff] [blame] | 159 | DiagnosticMappingInfo Info = DiagnosticMappingInfo::Make( |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 160 | diag::MAP_FATAL, /*IsUser=*/false, /*IsPragma=*/false); |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 161 | |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 162 | if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) { |
| 163 | Info.setMapping((diag::Mapping) StaticInfo->Mapping); |
| 164 | |
| 165 | if (StaticInfo->WarnNoWerror) { |
| 166 | assert(Info.getMapping() == diag::MAP_WARNING && |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 167 | "Unexpected mapping with no-Werror bit!"); |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 168 | Info.setNoWarningAsError(true); |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 171 | if (StaticInfo->WarnShowInSystemHeader) { |
| 172 | assert(Info.getMapping() == diag::MAP_WARNING && |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 173 | "Unexpected mapping with show-in-system-header bit!"); |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 174 | Info.setShowInSystemHeader(true); |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 175 | } |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 176 | } |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 177 | |
| 178 | return Info; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 181 | /// getCategoryNumberForDiag - Return the category number that a specified |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 182 | /// DiagID belongs to, or 0 if no category. |
| 183 | unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) { |
| 184 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 185 | return Info->Category; |
| 186 | return 0; |
| 187 | } |
| 188 | |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 189 | namespace { |
| 190 | // The diagnostic category names. |
| 191 | struct StaticDiagCategoryRec { |
| 192 | const char *NameStr; |
| 193 | uint8_t NameLen; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 195 | StringRef getName() const { |
| 196 | return StringRef(NameStr, NameLen); |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | } |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 201 | // Unfortunately, the split between DiagnosticIDs and Diagnostic is not |
| 202 | // particularly clean, but for now we just implement this method here so we can |
| 203 | // access GetDefaultDiagMapping. |
| 204 | DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo( |
| 205 | diag::kind Diag) |
| 206 | { |
| 207 | std::pair<iterator, bool> Result = DiagMap.insert( |
Daniel Dunbar | aeacae5 | 2011-09-29 02:03:01 +0000 | [diff] [blame] | 208 | std::make_pair(Diag, DiagnosticMappingInfo())); |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 209 | |
| 210 | // Initialize the entry if we added it. |
Daniel Dunbar | aeacae5 | 2011-09-29 02:03:01 +0000 | [diff] [blame] | 211 | if (Result.second) |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 212 | Result.first->second = GetDefaultDiagMappingInfo(Diag); |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 213 | |
| 214 | return Result.first->second; |
| 215 | } |
| 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 | /// getBuiltinDiagClass - Return the class field of the diagnostic. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 261 | /// |
| 262 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 263 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 264 | return Info->Class; |
| 265 | return ~0U; |
| 266 | } |
| 267 | |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | // Custom Diagnostic information |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | namespace clang { |
| 273 | namespace diag { |
| 274 | class CustomDiagInfo { |
| 275 | typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc; |
| 276 | std::vector<DiagDesc> DiagInfo; |
| 277 | std::map<DiagDesc, unsigned> DiagIDs; |
| 278 | public: |
| 279 | |
| 280 | /// getDescription - Return the description of the specified custom |
| 281 | /// diagnostic. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 282 | StringRef getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 283 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
| 284 | "Invalid diagnosic ID"); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 285 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /// getLevel - Return the level of the specified custom diagnostic. |
| 289 | DiagnosticIDs::Level getLevel(unsigned DiagID) const { |
| 290 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
| 291 | "Invalid diagnosic ID"); |
| 292 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
| 293 | } |
| 294 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 295 | unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 296 | DiagnosticIDs &Diags) { |
| 297 | DiagDesc D(L, Message); |
| 298 | // Check to see if it already exists. |
| 299 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 300 | if (I != DiagIDs.end() && I->first == D) |
| 301 | return I->second; |
| 302 | |
| 303 | // If not, assign a new ID. |
| 304 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
| 305 | DiagIDs.insert(std::make_pair(D, ID)); |
| 306 | DiagInfo.push_back(D); |
| 307 | return ID; |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | } // end diag namespace |
| 312 | } // end clang namespace |
| 313 | |
| 314 | |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | // Common Diagnostic implementation |
| 317 | //===----------------------------------------------------------------------===// |
| 318 | |
| 319 | DiagnosticIDs::DiagnosticIDs() { |
| 320 | CustomDiagInfo = 0; |
| 321 | } |
| 322 | |
| 323 | DiagnosticIDs::~DiagnosticIDs() { |
| 324 | delete CustomDiagInfo; |
| 325 | } |
| 326 | |
| 327 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 328 | /// and level. If this is the first request for this diagnosic, it is |
| 329 | /// registered and created, otherwise the existing ID is returned. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 330 | unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 331 | if (CustomDiagInfo == 0) |
| 332 | CustomDiagInfo = new diag::CustomDiagInfo(); |
| 333 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 338 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 339 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 340 | /// call on NOTEs. |
| 341 | bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) { |
| 342 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 343 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
| 344 | } |
| 345 | |
| 346 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 347 | /// Note. |
| 348 | bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) { |
| 349 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 350 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
| 351 | } |
| 352 | |
| 353 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
| 354 | /// ID is for an extension of some sort. This also returns EnabledByDefault, |
| 355 | /// which is set to indicate whether the diagnostic is ignored by default (in |
| 356 | /// which case -pedantic enables it) or treated as a warning/error by default. |
| 357 | /// |
| 358 | bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID, |
| 359 | bool &EnabledByDefault) { |
| 360 | if (DiagID >= diag::DIAG_UPPER_LIMIT || |
| 361 | getBuiltinDiagClass(DiagID) != CLASS_EXTENSION) |
| 362 | return false; |
| 363 | |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 364 | EnabledByDefault = |
| 365 | GetDefaultDiagMappingInfo(DiagID).getMapping() != diag::MAP_IGNORE; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 366 | return true; |
| 367 | } |
| 368 | |
Daniel Dunbar | 76101cf | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 369 | bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) { |
| 370 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 371 | return false; |
| 372 | |
Daniel Dunbar | a5e4133 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 373 | return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR; |
Daniel Dunbar | 76101cf | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 376 | /// getDescription - Given a diagnostic ID, return a description of the |
| 377 | /// issue. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 378 | StringRef DiagnosticIDs::getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 379 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 380 | return Info->getDescription(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 381 | return CustomDiagInfo->getDescription(DiagID); |
| 382 | } |
| 383 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 384 | /// getDiagnosticLevel - Based on the way the client configured the |
| 385 | /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level, |
| 386 | /// by consumable the DiagnosticClient. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 387 | DiagnosticIDs::Level |
| 388 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc, |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 389 | const DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 390 | // Handle custom diagnostics, which cannot be mapped. |
| 391 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 392 | return CustomDiagInfo->getLevel(DiagID); |
| 393 | |
| 394 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Jordan Rose | c6d64a2 | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 395 | if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note; |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 396 | return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 399 | /// \brief Based on the way the client configured the Diagnostic |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 400 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 401 | /// the DiagnosticClient. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 402 | /// |
| 403 | /// \param Loc The source location we are interested in finding out the |
| 404 | /// diagnostic state. Can be null in order to query the latest state. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 405 | DiagnosticIDs::Level |
| 406 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass, |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 407 | SourceLocation Loc, |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 408 | const DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 409 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 410 | // to error. Errors can only be mapped to fatal. |
| 411 | DiagnosticIDs::Level Result = DiagnosticIDs::Fatal; |
| 412 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 413 | DiagnosticsEngine::DiagStatePointsTy::iterator |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 414 | Pos = Diag.GetDiagStatePointForLoc(Loc); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 415 | DiagnosticsEngine::DiagState *State = Pos->State; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 416 | |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 417 | // Get the mapping information, or compute it lazily. |
| 418 | DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo( |
| 419 | (diag::kind)DiagID); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 420 | |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame] | 421 | switch (MappingInfo.getMapping()) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 422 | case diag::MAP_IGNORE: |
Daniel Dunbar | be1aa41 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 423 | Result = DiagnosticIDs::Ignored; |
| 424 | break; |
| 425 | case diag::MAP_WARNING: |
| 426 | Result = DiagnosticIDs::Warning; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 427 | break; |
| 428 | case diag::MAP_ERROR: |
| 429 | Result = DiagnosticIDs::Error; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 430 | break; |
| 431 | case diag::MAP_FATAL: |
| 432 | Result = DiagnosticIDs::Fatal; |
| 433 | break; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Daniel Dunbar | be1aa41 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 436 | // Upgrade ignored diagnostics if -Weverything is enabled. |
| 437 | if (Diag.EnableAllWarnings && Result == DiagnosticIDs::Ignored && |
| 438 | !MappingInfo.isUser()) |
| 439 | Result = DiagnosticIDs::Warning; |
| 440 | |
Bob Wilson | 18c407f | 2011-10-12 19:55:31 +0000 | [diff] [blame] | 441 | // Ignore -pedantic diagnostics inside __extension__ blocks. |
| 442 | // (The diagnostics controlled by -pedantic are the extension diagnostics |
| 443 | // that are not enabled by default.) |
Daniel Dunbar | f3dee20 | 2011-11-28 22:19:36 +0000 | [diff] [blame] | 444 | bool EnabledByDefault = false; |
Bob Wilson | 18c407f | 2011-10-12 19:55:31 +0000 | [diff] [blame] | 445 | bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault); |
| 446 | if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 447 | return DiagnosticIDs::Ignored; |
| 448 | |
Daniel Dunbar | be1aa41 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 449 | // For extension diagnostics that haven't been explicitly mapped, check if we |
| 450 | // should upgrade the diagnostic. |
| 451 | if (IsExtensionDiag && !MappingInfo.isUser()) { |
| 452 | switch (Diag.ExtBehavior) { |
| 453 | case DiagnosticsEngine::Ext_Ignore: |
| 454 | break; |
| 455 | case DiagnosticsEngine::Ext_Warn: |
| 456 | // Upgrade ignored diagnostics to warnings. |
| 457 | if (Result == DiagnosticIDs::Ignored) |
| 458 | Result = DiagnosticIDs::Warning; |
| 459 | break; |
| 460 | case DiagnosticsEngine::Ext_Error: |
| 461 | // Upgrade ignored or warning diagnostics to errors. |
| 462 | if (Result == DiagnosticIDs::Ignored || Result == DiagnosticIDs::Warning) |
| 463 | Result = DiagnosticIDs::Error; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // At this point, ignored errors can no longer be upgraded. |
| 469 | if (Result == DiagnosticIDs::Ignored) |
| 470 | return Result; |
| 471 | |
| 472 | // Honor -w, which is lower in priority than pedantic-errors, but higher than |
| 473 | // -Werror. |
| 474 | if (Result == DiagnosticIDs::Warning && Diag.IgnoreAllWarnings) |
| 475 | return DiagnosticIDs::Ignored; |
| 476 | |
| 477 | // If -Werror is enabled, map warnings to errors unless explicitly disabled. |
| 478 | if (Result == DiagnosticIDs::Warning) { |
| 479 | if (Diag.WarningsAsErrors && !MappingInfo.hasNoWarningAsError()) |
| 480 | Result = DiagnosticIDs::Error; |
| 481 | } |
| 482 | |
| 483 | // If -Wfatal-errors is enabled, map errors to fatal unless explicity |
| 484 | // disabled. |
| 485 | if (Result == DiagnosticIDs::Error) { |
| 486 | if (Diag.ErrorsAsFatal && !MappingInfo.hasNoErrorAsFatal()) |
| 487 | Result = DiagnosticIDs::Fatal; |
| 488 | } |
| 489 | |
Daniel Dunbar | aeacae5 | 2011-09-29 02:03:01 +0000 | [diff] [blame] | 490 | // If we are in a system header, we ignore it. We look at the diagnostic class |
| 491 | // because we also want to ignore extensions and warnings in -Werror and |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 492 | // -pedantic-errors modes, which *map* warnings/extensions to errors. |
| 493 | if (Result >= DiagnosticIDs::Warning && |
| 494 | DiagClass != CLASS_ERROR && |
| 495 | // Custom diagnostics always are emitted in system headers. |
| 496 | DiagID < diag::DIAG_UPPER_LIMIT && |
Daniel Dunbar | be1aa41 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 497 | !MappingInfo.hasShowInSystemHeader() && |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 498 | Diag.SuppressSystemWarnings && |
| 499 | Loc.isValid() && |
| 500 | Diag.getSourceManager().isInSystemHeader( |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 501 | Diag.getSourceManager().getExpansionLoc(Loc))) |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 502 | return DiagnosticIDs::Ignored; |
| 503 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 504 | return Result; |
| 505 | } |
| 506 | |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 507 | struct clang::WarningOption { |
| 508 | // Be safe with the size of 'NameLen' because we don't statically check if |
| 509 | // the size will fit in the field; the struct size won't decrease with a |
| 510 | // shorter type anyway. |
| 511 | size_t NameLen; |
| 512 | const char *NameStr; |
| 513 | const short *Members; |
| 514 | const short *SubGroups; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 515 | |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 516 | StringRef getName() const { |
| 517 | return StringRef(NameStr, NameLen); |
| 518 | } |
| 519 | }; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 520 | |
| 521 | #define GET_DIAG_ARRAYS |
| 522 | #include "clang/Basic/DiagnosticGroups.inc" |
| 523 | #undef GET_DIAG_ARRAYS |
| 524 | |
| 525 | // Second the table of options, sorted by name for fast binary lookup. |
| 526 | static const WarningOption OptionTable[] = { |
| 527 | #define GET_DIAG_TABLE |
| 528 | #include "clang/Basic/DiagnosticGroups.inc" |
| 529 | #undef GET_DIAG_TABLE |
| 530 | }; |
| 531 | static const size_t OptionTableSize = |
| 532 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 533 | |
| 534 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 535 | const WarningOption &RHS) { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 536 | return LHS.getName() < RHS.getName(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 539 | /// getWarningOptionForDiag - Return the lowest-level warning option that |
| 540 | /// enables the specified diagnostic. If there is no -Wfoo flag that controls |
| 541 | /// the diagnostic, this returns null. |
| 542 | StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) { |
| 543 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 544 | return OptionTable[Info->getOptionGroupIndex()].getName(); |
| 545 | return StringRef(); |
| 546 | } |
| 547 | |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 548 | void DiagnosticIDs::getDiagnosticsInGroup( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 549 | const WarningOption *Group, |
| 550 | SmallVectorImpl<diag::kind> &Diags) const { |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 551 | // Add the members of the option diagnostic set. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 552 | if (const short *Member = Group->Members) { |
| 553 | for (; *Member != -1; ++Member) |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 554 | Diags.push_back(*Member); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 557 | // Add the members of the subgroups. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 558 | if (const short *SubGroups = Group->SubGroups) { |
| 559 | for (; *SubGroups != (short)-1; ++SubGroups) |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 560 | getDiagnosticsInGroup(&OptionTable[(short)*SubGroups], Diags); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 564 | bool DiagnosticIDs::getDiagnosticsInGroup( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 565 | StringRef Group, |
| 566 | SmallVectorImpl<diag::kind> &Diags) const { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 567 | WarningOption Key = { Group.size(), Group.data(), 0, 0 }; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 568 | const WarningOption *Found = |
| 569 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 570 | WarningOptionCompare); |
| 571 | if (Found == OptionTable + OptionTableSize || |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 572 | Found->getName() != Group) |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 573 | return true; // Option not found. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | 3f83946 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 575 | getDiagnosticsInGroup(Found, Diags); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | |
Argyrios Kyrtzidis | 11583c7 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 579 | void DiagnosticIDs::getAllDiagnostics( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 580 | SmallVectorImpl<diag::kind> &Diags) const { |
Argyrios Kyrtzidis | 11583c7 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 581 | for (unsigned i = 0; i != StaticDiagInfoSize; ++i) |
| 582 | Diags.push_back(StaticDiagInfo[i].DiagID); |
| 583 | } |
| 584 | |
Benjamin Kramer | a70cb9d | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 585 | StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) { |
| 586 | StringRef Best; |
Benjamin Kramer | dce6327 | 2011-11-15 12:26:39 +0000 | [diff] [blame] | 587 | unsigned BestDistance = Group.size() + 1; // Sanity threshold. |
Benjamin Kramer | a70cb9d | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 588 | for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize; |
| 589 | i != e; ++i) { |
| 590 | // Don't suggest ignored warning flags. |
| 591 | if (!i->Members && !i->SubGroups) |
| 592 | continue; |
| 593 | |
| 594 | unsigned Distance = i->getName().edit_distance(Group, true, BestDistance); |
Benjamin Kramer | dce6327 | 2011-11-15 12:26:39 +0000 | [diff] [blame] | 595 | if (Distance == BestDistance) { |
| 596 | // Two matches with the same distance, don't prefer one over the other. |
| 597 | Best = ""; |
| 598 | } else if (Distance < BestDistance) { |
| 599 | // This is a better match. |
Benjamin Kramer | a70cb9d | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 600 | Best = i->getName(); |
| 601 | BestDistance = Distance; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return Best; |
| 606 | } |
| 607 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 608 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 609 | /// finally fully formed. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 610 | bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const { |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 611 | Diagnostic Info(&Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 612 | |
| 613 | if (Diag.SuppressAllDiagnostics) |
| 614 | return false; |
| 615 | |
| 616 | assert(Diag.getClient() && "DiagnosticClient not set!"); |
| 617 | |
| 618 | // Figure out the diagnostic level of this message. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 619 | unsigned DiagID = Info.getID(); |
Jordan Rose | c6d64a2 | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 620 | DiagnosticIDs::Level DiagLevel |
| 621 | = getDiagnosticLevel(DiagID, Info.getLocation(), Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 622 | |
| 623 | if (DiagLevel != DiagnosticIDs::Note) { |
| 624 | // Record that a fatal error occurred only when we see a second |
| 625 | // non-note diagnostic. This allows notes to be attached to the |
| 626 | // fatal error, but suppresses any diagnostics that follow those |
| 627 | // notes. |
| 628 | if (Diag.LastDiagLevel == DiagnosticIDs::Fatal) |
| 629 | Diag.FatalErrorOccurred = true; |
| 630 | |
| 631 | Diag.LastDiagLevel = DiagLevel; |
| 632 | } |
| 633 | |
Argyrios Kyrtzidis | c0a575f | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 634 | // Update counts for DiagnosticErrorTrap even if a fatal error occurred. |
| 635 | if (DiagLevel >= DiagnosticIDs::Error) { |
| 636 | ++Diag.TrapNumErrorsOccurred; |
| 637 | if (isUnrecoverable(DiagID)) |
| 638 | ++Diag.TrapNumUnrecoverableErrorsOccurred; |
| 639 | } |
| 640 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 641 | // If a fatal error has already been emitted, silence all subsequent |
| 642 | // diagnostics. |
| 643 | if (Diag.FatalErrorOccurred) { |
| 644 | if (DiagLevel >= DiagnosticIDs::Error && |
| 645 | Diag.Client->IncludeInDiagnosticCounts()) { |
| 646 | ++Diag.NumErrors; |
| 647 | ++Diag.NumErrorsSuppressed; |
| 648 | } |
| 649 | |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | // If the client doesn't care about this message, don't issue it. If this is |
| 654 | // a note and the last real diagnostic was ignored, ignore it too. |
| 655 | if (DiagLevel == DiagnosticIDs::Ignored || |
| 656 | (DiagLevel == DiagnosticIDs::Note && |
| 657 | Diag.LastDiagLevel == DiagnosticIDs::Ignored)) |
| 658 | return false; |
| 659 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 660 | if (DiagLevel >= DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | c0a575f | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 661 | if (isUnrecoverable(DiagID)) |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 662 | Diag.UnrecoverableErrorOccurred = true; |
Daniel Jasper | 1c84c68 | 2012-09-28 15:45:07 +0000 | [diff] [blame] | 663 | |
DeLesley Hutchins | 12f37e4 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 664 | // Warnings which have been upgraded to errors do not prevent compilation. |
| 665 | if (isDefaultMappingAsError(DiagID)) |
| 666 | Diag.UncompilableErrorOccurred = true; |
| 667 | |
Daniel Jasper | 1c84c68 | 2012-09-28 15:45:07 +0000 | [diff] [blame] | 668 | Diag.ErrorOccurred = true; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 669 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 670 | ++Diag.NumErrors; |
| 671 | } |
| 672 | |
Douglas Gregor | f1d5948 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 673 | // If we've emitted a lot of errors, emit a fatal error instead of it to |
| 674 | // stop a flood of bogus errors. |
| 675 | if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit && |
| 676 | DiagLevel == DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 677 | Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors); |
Douglas Gregor | f1d5948 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 682 | // Finally, report it. |
Jordan Rose | c6d64a2 | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 683 | EmitDiag(Diag, DiagLevel); |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const { |
| 688 | Diagnostic Info(&Diag); |
| 689 | assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!"); |
| 690 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 691 | Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 692 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
| 693 | if (DiagLevel == DiagnosticIDs::Warning) |
| 694 | ++Diag.NumWarnings; |
| 695 | } |
| 696 | |
| 697 | Diag.CurDiagID = ~0U; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 698 | } |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 699 | |
| 700 | bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const { |
| 701 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 702 | // Custom diagnostics. |
| 703 | return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error; |
| 704 | } |
| 705 | |
| 706 | // Only errors may be unrecoverable. |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 707 | if (getBuiltinDiagClass(DiagID) < CLASS_ERROR) |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 708 | return false; |
| 709 | |
| 710 | if (DiagID == diag::err_unavailable || |
| 711 | DiagID == diag::err_unavailable_message) |
| 712 | return false; |
| 713 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 714 | // Currently we consider all ARC errors as recoverable. |
Ted Kremenek | afdc21a | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 715 | if (isARCDiagnostic(DiagID)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 716 | return false; |
| 717 | |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 718 | return true; |
| 719 | } |
Ted Kremenek | afdc21a | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 720 | |
| 721 | bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) { |
| 722 | unsigned cat = getCategoryNumberForDiag(DiagID); |
| 723 | return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC "); |
| 724 | } |