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