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