Argyrios Kyrtzidis | d004064 | 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 | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 14 | #include "clang/Basic/DiagnosticIDs.h" |
David Blaikie | af90ec1 | 2012-02-15 21:58:34 +0000 | [diff] [blame] | 15 | #include "clang/Basic/AllDiagnostics.h" |
John McCall | a877d92 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 16 | #include "clang/Basic/DiagnosticCategories.h" |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
David Majnemer | 38af2a2 | 2013-07-20 07:15:15 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
David Blaikie | 76bd3c8 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Argyrios Kyrtzidis | d004064 | 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, |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 33 | CLASS_REMARK = 0x02, |
| 34 | CLASS_WARNING = 0x03, |
| 35 | CLASS_EXTENSION = 0x04, |
| 36 | CLASS_ERROR = 0x05 |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct StaticDiagInfoRec { |
Craig Topper | ea6caba | 2013-07-21 21:56:18 +0000 | [diff] [blame] | 40 | uint16_t DiagID; |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 41 | unsigned DefaultSeverity : 3; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 42 | unsigned Class : 3; |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 43 | unsigned SFINAE : 2; |
Daniel Dunbar | 6de48ee | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 44 | unsigned WarnNoWerror : 1; |
| 45 | unsigned WarnShowInSystemHeader : 1; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 46 | unsigned Category : 5; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 47 | |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 48 | uint16_t OptionGroupIndex; |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 49 | |
| 50 | uint16_t DescriptionLen; |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 51 | const char *DescriptionStr; |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 52 | |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 53 | unsigned getOptionGroupIndex() const { |
| 54 | return OptionGroupIndex; |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 57 | StringRef getDescription() const { |
| 58 | return StringRef(DescriptionStr, DescriptionLen); |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 59 | } |
Douglas Gregor | 46ce91a | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 60 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 61 | diag::Flavor getFlavor() const { |
| 62 | return Class == CLASS_REMARK ? diag::Flavor::Remark |
| 63 | : diag::Flavor::WarningOrError; |
| 64 | } |
| 65 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 66 | bool operator<(const StaticDiagInfoRec &RHS) const { |
| 67 | return DiagID < RHS.DiagID; |
| 68 | } |
| 69 | }; |
| 70 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 71 | } // namespace anonymous |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 72 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 73 | static const StaticDiagInfoRec StaticDiagInfo[] = { |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [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 | d004064 | 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 | 22a11b7 | 2011-12-09 00:02:23 +0000 | [diff] [blame] | 84 | #include "clang/Basic/DiagnosticSerializationKinds.inc" |
Argyrios Kyrtzidis | d004064 | 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 | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 88 | #include "clang/Basic/DiagnosticCommentKinds.inc" |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 89 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
| 90 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 91 | #undef DIAG |
Douglas Gregor | 46ce91a | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
David Majnemer | 38af2a2 | 2013-07-20 07:15:15 +0000 | [diff] [blame] | 94 | static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo); |
Douglas Gregor | 46ce91a | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 95 | |
Argyrios Kyrtzidis | d004064 | 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 | d004064 | 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 | 31e71a3 | 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) { |
Craig Topper | 2685cbe | 2015-10-18 05:29:21 +0000 | [diff] [blame] | 103 | assert(std::is_sorted(std::begin(StaticDiagInfo), |
| 104 | std::end(StaticDiagInfo)) && |
| 105 | "Diag ID conflict, the enums at the start of clang::diag (in " |
| 106 | "DiagnosticIDs.h) probably need to be increased"); |
Craig Topper | 31e71a3 | 2013-07-21 18:58:40 +0000 | [diff] [blame] | 107 | IsFirst = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 111 | // Out of bounds diag. Can't be in the table. |
| 112 | using namespace diag; |
David Majnemer | 38af2a2 | 2013-07-20 07:15:15 +0000 | [diff] [blame] | 113 | if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 114 | return nullptr; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 115 | |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 116 | // Compute the index of the requested diagnostic in the static table. |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 117 | // 1. Add the number of diagnostics in each category preceding the |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 118 | // diagnostic and of the category the diagnostic is in. This gives us |
| 119 | // the offset of the category in the table. |
| 120 | // 2. Subtract the number of IDs in each category from our ID. This gives us |
| 121 | // the offset of the diagnostic in the category. |
| 122 | // This is cheaper than a binary search on the table as it doesn't touch |
| 123 | // memory at all. |
| 124 | unsigned Offset = 0; |
David Majnemer | 38af2a2 | 2013-07-20 07:15:15 +0000 | [diff] [blame] | 125 | unsigned ID = DiagID - DIAG_START_COMMON - 1; |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 126 | #define CATEGORY(NAME, PREV) \ |
| 127 | if (DiagID > DIAG_START_##NAME) { \ |
Argyrios Kyrtzidis | f86e8cc | 2013-01-02 22:26:07 +0000 | [diff] [blame] | 128 | Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \ |
| 129 | ID -= DIAG_START_##NAME - DIAG_START_##PREV; \ |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 130 | } |
| 131 | CATEGORY(DRIVER, COMMON) |
| 132 | CATEGORY(FRONTEND, DRIVER) |
| 133 | CATEGORY(SERIALIZATION, FRONTEND) |
| 134 | CATEGORY(LEX, SERIALIZATION) |
| 135 | CATEGORY(PARSE, LEX) |
| 136 | CATEGORY(AST, PARSE) |
| 137 | CATEGORY(COMMENT, AST) |
| 138 | CATEGORY(SEMA, COMMENT) |
| 139 | CATEGORY(ANALYSIS, SEMA) |
| 140 | #undef CATEGORY |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 141 | |
| 142 | // Avoid out of bounds reads. |
| 143 | if (ID + Offset >= StaticDiagInfoSize) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 144 | return nullptr; |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 145 | |
Argyrios Kyrtzidis | f86e8cc | 2013-01-02 22:26:07 +0000 | [diff] [blame] | 146 | assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize); |
| 147 | |
Benjamin Kramer | 256f1dd | 2012-12-11 18:00:22 +0000 | [diff] [blame] | 148 | const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset]; |
| 149 | // If the diag id doesn't match we found a different diag, abort. This can |
| 150 | // happen when this function is called with an ID that points into a hole in |
| 151 | // the diagID space. |
| 152 | if (Found->DiagID != DiagID) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 153 | return nullptr; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 154 | return Found; |
| 155 | } |
| 156 | |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 157 | static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) { |
| 158 | DiagnosticMapping Info = DiagnosticMapping::Make( |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 159 | diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false); |
Daniel Dunbar | 6de48ee | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | fffcf21 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 161 | if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) { |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 162 | Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity); |
Daniel Dunbar | fffcf21 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 163 | |
| 164 | if (StaticInfo->WarnNoWerror) { |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 165 | assert(Info.getSeverity() == diag::Severity::Warning && |
Daniel Dunbar | 6de48ee | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 166 | "Unexpected mapping with no-Werror bit!"); |
Daniel Dunbar | fffcf21 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 167 | Info.setNoWarningAsError(true); |
Daniel Dunbar | 6de48ee | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 168 | } |
Daniel Dunbar | 6de48ee | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 169 | } |
Daniel Dunbar | fffcf21 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 170 | |
| 171 | return Info; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Douglas Gregor | 46ce91a | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 174 | /// getCategoryNumberForDiag - Return the category number that a specified |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 175 | /// DiagID belongs to, or 0 if no category. |
| 176 | unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) { |
| 177 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 178 | return Info->Category; |
| 179 | return 0; |
| 180 | } |
| 181 | |
Benjamin Kramer | dc0a46d | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 182 | namespace { |
| 183 | // The diagnostic category names. |
| 184 | struct StaticDiagCategoryRec { |
| 185 | const char *NameStr; |
| 186 | uint8_t NameLen; |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 188 | StringRef getName() const { |
| 189 | return StringRef(NameStr, NameLen); |
Benjamin Kramer | dc0a46d | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 190 | } |
| 191 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 192 | } |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | e8c12a2 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 194 | // Unfortunately, the split between DiagnosticIDs and Diagnostic is not |
| 195 | // particularly clean, but for now we just implement this method here so we can |
| 196 | // access GetDefaultDiagMapping. |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 197 | DiagnosticMapping & |
| 198 | DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) { |
| 199 | std::pair<iterator, bool> Result = |
| 200 | DiagMap.insert(std::make_pair(Diag, DiagnosticMapping())); |
Daniel Dunbar | e8c12a2 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 201 | |
| 202 | // Initialize the entry if we added it. |
Daniel Dunbar | 866fcd3 | 2011-09-29 02:03:01 +0000 | [diff] [blame] | 203 | if (Result.second) |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 204 | Result.first->second = GetDefaultDiagMapping(Diag); |
Daniel Dunbar | e8c12a2 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 205 | |
| 206 | return Result.first->second; |
| 207 | } |
| 208 | |
Benjamin Kramer | dc0a46d | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 209 | static const StaticDiagCategoryRec CategoryNameTable[] = { |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 210 | #define GET_CATEGORY_TABLE |
John McCall | a877d92 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 211 | #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) }, |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 212 | #include "clang/Basic/DiagnosticGroups.inc" |
| 213 | #undef GET_CATEGORY_TABLE |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 214 | { nullptr, 0 } |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /// getNumberOfCategories - Return the number of categories |
| 218 | unsigned DiagnosticIDs::getNumberOfCategories() { |
Craig Topper | e5ce831 | 2013-07-15 03:38:40 +0000 | [diff] [blame] | 219 | return llvm::array_lengthof(CategoryNameTable) - 1; |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 222 | /// getCategoryNameFromID - Given a category ID, return the name of the |
| 223 | /// category, an empty string if CategoryID is zero, or null if CategoryID is |
| 224 | /// invalid. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 225 | StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) { |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 226 | if (CategoryID >= getNumberOfCategories()) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 227 | return StringRef(); |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 228 | return CategoryNameTable[CategoryID].getName(); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 233 | DiagnosticIDs::SFINAEResponse |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 234 | DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) { |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 235 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 236 | return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 237 | return SFINAE_Report; |
| 238 | } |
| 239 | |
Douglas Gregor | 46ce91a | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 240 | /// getBuiltinDiagClass - Return the class field of the diagnostic. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 241 | /// |
| 242 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 243 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 244 | return Info->Class; |
| 245 | return ~0U; |
| 246 | } |
| 247 | |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | // Custom Diagnostic information |
| 250 | //===----------------------------------------------------------------------===// |
| 251 | |
| 252 | namespace clang { |
| 253 | namespace diag { |
| 254 | class CustomDiagInfo { |
| 255 | typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc; |
| 256 | std::vector<DiagDesc> DiagInfo; |
| 257 | std::map<DiagDesc, unsigned> DiagIDs; |
| 258 | public: |
| 259 | |
| 260 | /// getDescription - Return the description of the specified custom |
| 261 | /// diagnostic. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | StringRef getDescription(unsigned DiagID) const { |
Richard Trieu | 428058f | 2014-08-01 01:42:01 +0000 | [diff] [blame] | 263 | assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 264 | "Invalid diagnostic ID"); |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 265 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /// getLevel - Return the level of the specified custom diagnostic. |
| 269 | DiagnosticIDs::Level getLevel(unsigned DiagID) const { |
Richard Trieu | 428058f | 2014-08-01 01:42:01 +0000 | [diff] [blame] | 270 | assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 271 | "Invalid diagnostic ID"); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 272 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
| 273 | } |
| 274 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 275 | unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 276 | DiagnosticIDs &Diags) { |
| 277 | DiagDesc D(L, Message); |
| 278 | // Check to see if it already exists. |
| 279 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 280 | if (I != DiagIDs.end() && I->first == D) |
| 281 | return I->second; |
| 282 | |
| 283 | // If not, assign a new ID. |
| 284 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
| 285 | DiagIDs.insert(std::make_pair(D, ID)); |
| 286 | DiagInfo.push_back(D); |
| 287 | return ID; |
| 288 | } |
| 289 | }; |
| 290 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 291 | } // end diag namespace |
| 292 | } // end clang namespace |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 293 | |
| 294 | |
| 295 | //===----------------------------------------------------------------------===// |
| 296 | // Common Diagnostic implementation |
| 297 | //===----------------------------------------------------------------------===// |
| 298 | |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 299 | DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; } |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 300 | |
| 301 | DiagnosticIDs::~DiagnosticIDs() { |
| 302 | delete CustomDiagInfo; |
| 303 | } |
| 304 | |
| 305 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 306 | /// and level. If this is the first request for this diagnostic, it is |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 307 | /// registered and created, otherwise the existing ID is returned. |
Alp Toker | 61d41af | 2013-12-23 21:00:35 +0000 | [diff] [blame] | 308 | /// |
Alp Toker | 9e6d27d | 2014-01-26 06:41:58 +0000 | [diff] [blame] | 309 | /// \param FormatString A fixed diagnostic format string that will be hashed and |
Alp Toker | 61d41af | 2013-12-23 21:00:35 +0000 | [diff] [blame] | 310 | /// mapped to a unique DiagID. |
Alp Toker | 9e6d27d | 2014-01-26 06:41:58 +0000 | [diff] [blame] | 311 | unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) { |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 312 | if (!CustomDiagInfo) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 313 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Alp Toker | 9e6d27d | 2014-01-26 06:41:58 +0000 | [diff] [blame] | 314 | return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
| 318 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 319 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 320 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 321 | /// call on NOTEs. |
| 322 | bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) { |
| 323 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 324 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
| 325 | } |
| 326 | |
| 327 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 328 | /// Note. |
| 329 | bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) { |
| 330 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 331 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
| 332 | } |
| 333 | |
| 334 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
| 335 | /// ID is for an extension of some sort. This also returns EnabledByDefault, |
| 336 | /// which is set to indicate whether the diagnostic is ignored by default (in |
| 337 | /// which case -pedantic enables it) or treated as a warning/error by default. |
| 338 | /// |
| 339 | bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID, |
| 340 | bool &EnabledByDefault) { |
| 341 | if (DiagID >= diag::DIAG_UPPER_LIMIT || |
| 342 | getBuiltinDiagClass(DiagID) != CLASS_EXTENSION) |
| 343 | return false; |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 344 | |
Daniel Dunbar | fffcf21 | 2011-09-29 01:52:06 +0000 | [diff] [blame] | 345 | EnabledByDefault = |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 346 | GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 347 | return true; |
| 348 | } |
| 349 | |
Daniel Dunbar | aa11138 | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 350 | bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) { |
| 351 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 352 | return false; |
| 353 | |
Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 354 | return GetDefaultDiagMapping(DiagID).getSeverity() >= diag::Severity::Error; |
Daniel Dunbar | aa11138 | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 357 | /// getDescription - Given a diagnostic ID, return a description of the |
| 358 | /// issue. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 359 | StringRef DiagnosticIDs::getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 360 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 361 | return Info->getDescription(); |
Richard Trieu | 428058f | 2014-08-01 01:42:01 +0000 | [diff] [blame] | 362 | assert(CustomDiagInfo && "Invalid CustomDiagInfo"); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 363 | return CustomDiagInfo->getDescription(DiagID); |
| 364 | } |
| 365 | |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 366 | static DiagnosticIDs::Level toLevel(diag::Severity SV) { |
| 367 | switch (SV) { |
| 368 | case diag::Severity::Ignored: |
| 369 | return DiagnosticIDs::Ignored; |
| 370 | case diag::Severity::Remark: |
| 371 | return DiagnosticIDs::Remark; |
| 372 | case diag::Severity::Warning: |
| 373 | return DiagnosticIDs::Warning; |
| 374 | case diag::Severity::Error: |
| 375 | return DiagnosticIDs::Error; |
| 376 | case diag::Severity::Fatal: |
| 377 | return DiagnosticIDs::Fatal; |
| 378 | } |
Saleem Abdulrasool | fbfbaf6 | 2014-06-12 19:33:26 +0000 | [diff] [blame] | 379 | llvm_unreachable("unexpected severity"); |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 380 | } |
| 381 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 382 | /// getDiagnosticLevel - Based on the way the client configured the |
| 383 | /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level, |
| 384 | /// by consumable the DiagnosticClient. |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 385 | DiagnosticIDs::Level |
| 386 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc, |
Daniel Dunbar | 400e7e3 | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 387 | const DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 388 | // Handle custom diagnostics, which cannot be mapped. |
Richard Trieu | 428058f | 2014-08-01 01:42:01 +0000 | [diff] [blame] | 389 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 390 | assert(CustomDiagInfo && "Invalid CustomDiagInfo"); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 391 | return CustomDiagInfo->getLevel(DiagID); |
Richard Trieu | 428058f | 2014-08-01 01:42:01 +0000 | [diff] [blame] | 392 | } |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 393 | |
| 394 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Jordan Rose | 6f524ac | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 395 | if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note; |
Alp Toker | 04278ec | 2014-06-16 13:56:47 +0000 | [diff] [blame] | 396 | return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag)); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 399 | /// \brief Based on the way the client configured the Diagnostic |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 400 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 401 | /// the DiagnosticClient. |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 402 | /// |
| 403 | /// \param Loc The source location we are interested in finding out the |
| 404 | /// diagnostic state. Can be null in order to query the latest state. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 405 | diag::Severity |
Alp Toker | 04278ec | 2014-06-16 13:56:47 +0000 | [diff] [blame] | 406 | DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc, |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 407 | const DiagnosticsEngine &Diag) const { |
Alp Toker | 04278ec | 2014-06-16 13:56:47 +0000 | [diff] [blame] | 408 | assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE); |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 409 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 410 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 411 | // to error. Errors can only be mapped to fatal. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 412 | diag::Severity Result = diag::Severity::Fatal; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 413 | |
Daniel Dunbar | e8c12a2 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 414 | // Get the mapping information, or compute it lazily. |
Richard Smith | d230de2 | 2017-01-26 01:01:01 +0000 | [diff] [blame] | 415 | DiagnosticsEngine::DiagState *State = Diag.GetDiagStateForLoc(Loc); |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 416 | DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 417 | |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 418 | // TODO: Can a null severity really get here? |
| 419 | if (Mapping.getSeverity() != diag::Severity()) |
| 420 | Result = Mapping.getSeverity(); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 422 | // Upgrade ignored diagnostics if -Weverything is enabled. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 423 | if (Diag.EnableAllWarnings && Result == diag::Severity::Ignored && |
Richard Smith | e423ed3 | 2014-08-21 20:44:44 +0000 | [diff] [blame] | 424 | !Mapping.isUser() && getBuiltinDiagClass(DiagID) != CLASS_REMARK) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 425 | Result = diag::Severity::Warning; |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 426 | |
Bob Wilson | 73a4deb | 2011-10-12 19:55:31 +0000 | [diff] [blame] | 427 | // Ignore -pedantic diagnostics inside __extension__ blocks. |
| 428 | // (The diagnostics controlled by -pedantic are the extension diagnostics |
| 429 | // that are not enabled by default.) |
Daniel Dunbar | 787032b | 2011-11-28 22:19:36 +0000 | [diff] [blame] | 430 | bool EnabledByDefault = false; |
Bob Wilson | 73a4deb | 2011-10-12 19:55:31 +0000 | [diff] [blame] | 431 | bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault); |
| 432 | if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 433 | return diag::Severity::Ignored; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 434 | |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 435 | // For extension diagnostics that haven't been explicitly mapped, check if we |
| 436 | // should upgrade the diagnostic. |
Alp Toker | ac4e8e5 | 2014-06-22 21:58:33 +0000 | [diff] [blame] | 437 | if (IsExtensionDiag && !Mapping.isUser()) |
| 438 | Result = std::max(Result, Diag.ExtBehavior); |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 439 | |
| 440 | // At this point, ignored errors can no longer be upgraded. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 441 | if (Result == diag::Severity::Ignored) |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 442 | return Result; |
| 443 | |
| 444 | // Honor -w, which is lower in priority than pedantic-errors, but higher than |
| 445 | // -Werror. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 446 | if (Result == diag::Severity::Warning && Diag.IgnoreAllWarnings) |
| 447 | return diag::Severity::Ignored; |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 448 | |
| 449 | // If -Werror is enabled, map warnings to errors unless explicitly disabled. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 450 | if (Result == diag::Severity::Warning) { |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 451 | if (Diag.WarningsAsErrors && !Mapping.hasNoWarningAsError()) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 452 | Result = diag::Severity::Error; |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // If -Wfatal-errors is enabled, map errors to fatal unless explicity |
| 456 | // disabled. |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 457 | if (Result == diag::Severity::Error) { |
Alp Toker | c726c36 | 2014-06-10 09:31:37 +0000 | [diff] [blame] | 458 | if (Diag.ErrorsAsFatal && !Mapping.hasNoErrorAsFatal()) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 459 | Result = diag::Severity::Fatal; |
Daniel Dunbar | 58d0af6 | 2011-09-29 01:58:05 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 462 | // If explicitly requested, map fatal errors to errors. |
| 463 | if (Result == diag::Severity::Fatal) { |
| 464 | if (Diag.FatalsAsError) |
| 465 | Result = diag::Severity::Error; |
| 466 | } |
| 467 | |
Alp Toker | ed2c033 | 2014-06-10 06:09:00 +0000 | [diff] [blame] | 468 | // Custom diagnostics always are emitted in system headers. |
| 469 | bool ShowInSystemHeader = |
| 470 | !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader; |
| 471 | |
Daniel Dunbar | 866fcd3 | 2011-09-29 02:03:01 +0000 | [diff] [blame] | 472 | // If we are in a system header, we ignore it. We look at the diagnostic class |
| 473 | // because we also want to ignore extensions and warnings in -Werror and |
Argyrios Kyrtzidis | 8a8b877 | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 474 | // -pedantic-errors modes, which *map* warnings/extensions to errors. |
Alp Toker | 04278ec | 2014-06-16 13:56:47 +0000 | [diff] [blame] | 475 | if (Diag.SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() && |
Argyrios Kyrtzidis | 8a8b877 | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 476 | Diag.getSourceManager().isInSystemHeader( |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 477 | Diag.getSourceManager().getExpansionLoc(Loc))) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 478 | return diag::Severity::Ignored; |
Argyrios Kyrtzidis | 8a8b877 | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 479 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 480 | return Result; |
| 481 | } |
| 482 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 483 | #define GET_DIAG_ARRAYS |
| 484 | #include "clang/Basic/DiagnosticGroups.inc" |
| 485 | #undef GET_DIAG_ARRAYS |
| 486 | |
Craig Topper | b78e9d9 | 2013-08-29 06:06:18 +0000 | [diff] [blame] | 487 | namespace { |
| 488 | struct WarningOption { |
| 489 | uint16_t NameOffset; |
| 490 | uint16_t Members; |
| 491 | uint16_t SubGroups; |
Craig Topper | da7cf8a | 2013-08-29 05:18:04 +0000 | [diff] [blame] | 492 | |
Craig Topper | b78e9d9 | 2013-08-29 06:06:18 +0000 | [diff] [blame] | 493 | // String is stored with a pascal-style length byte. |
| 494 | StringRef getName() const { |
| 495 | return StringRef(DiagGroupNames + NameOffset + 1, |
| 496 | DiagGroupNames[NameOffset]); |
| 497 | } |
| 498 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 499 | } |
Craig Topper | da7cf8a | 2013-08-29 05:18:04 +0000 | [diff] [blame] | 500 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 501 | // Second the table of options, sorted by name for fast binary lookup. |
| 502 | static const WarningOption OptionTable[] = { |
| 503 | #define GET_DIAG_TABLE |
| 504 | #include "clang/Basic/DiagnosticGroups.inc" |
| 505 | #undef GET_DIAG_TABLE |
| 506 | }; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 507 | |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 508 | /// getWarningOptionForDiag - Return the lowest-level warning option that |
| 509 | /// enables the specified diagnostic. If there is no -Wfoo flag that controls |
| 510 | /// the diagnostic, this returns null. |
| 511 | StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) { |
| 512 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 513 | return OptionTable[Info->getOptionGroupIndex()].getName(); |
| 514 | return StringRef(); |
| 515 | } |
| 516 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 517 | /// Return \c true if any diagnostics were found in this group, even if they |
| 518 | /// were filtered out due to having the wrong flavor. |
| 519 | static bool getDiagnosticsInGroup(diag::Flavor Flavor, |
| 520 | const WarningOption *Group, |
Craig Topper | b78e9d9 | 2013-08-29 06:06:18 +0000 | [diff] [blame] | 521 | SmallVectorImpl<diag::kind> &Diags) { |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 522 | // An empty group is considered to be a warning group: we have empty groups |
| 523 | // for GCC compatibility, and GCC does not have remarks. |
| 524 | if (!Group->Members && !Group->SubGroups) |
David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 525 | return Flavor == diag::Flavor::Remark; |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 526 | |
| 527 | bool NotFound = true; |
| 528 | |
Daniel Dunbar | d908c12 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 529 | // Add the members of the option diagnostic set. |
Craig Topper | d80c17e | 2013-08-28 04:02:50 +0000 | [diff] [blame] | 530 | const int16_t *Member = DiagArrays + Group->Members; |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 531 | for (; *Member != -1; ++Member) { |
| 532 | if (GetDiagInfo(*Member)->getFlavor() == Flavor) { |
| 533 | NotFound = false; |
| 534 | Diags.push_back(*Member); |
| 535 | } |
| 536 | } |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | d908c12 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 538 | // Add the members of the subgroups. |
Craig Topper | d80c17e | 2013-08-28 04:02:50 +0000 | [diff] [blame] | 539 | const int16_t *SubGroups = DiagSubGroups + Group->SubGroups; |
| 540 | for (; *SubGroups != (int16_t)-1; ++SubGroups) |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 541 | NotFound &= getDiagnosticsInGroup(Flavor, &OptionTable[(short)*SubGroups], |
| 542 | Diags); |
| 543 | |
| 544 | return NotFound; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 547 | bool |
| 548 | DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group, |
| 549 | SmallVectorImpl<diag::kind> &Diags) const { |
Craig Topper | 924f6db | 2015-10-17 20:18:46 +0000 | [diff] [blame] | 550 | auto Found = std::lower_bound(std::begin(OptionTable), std::end(OptionTable), |
| 551 | Group, |
| 552 | [](const WarningOption &LHS, StringRef RHS) { |
| 553 | return LHS.getName() < RHS; |
| 554 | }); |
Craig Topper | c00e953 | 2015-10-17 17:10:43 +0000 | [diff] [blame] | 555 | if (Found == std::end(OptionTable) || Found->getName() != Group) |
Daniel Dunbar | d908c12 | 2011-09-29 01:47:16 +0000 | [diff] [blame] | 556 | return true; // Option not found. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 557 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 558 | return ::getDiagnosticsInGroup(Flavor, Found, Diags); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 561 | void DiagnosticIDs::getAllDiagnostics(diag::Flavor Flavor, |
| 562 | SmallVectorImpl<diag::kind> &Diags) const { |
Argyrios Kyrtzidis | 9ffada9 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 563 | for (unsigned i = 0; i != StaticDiagInfoSize; ++i) |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 564 | if (StaticDiagInfo[i].getFlavor() == Flavor) |
| 565 | Diags.push_back(StaticDiagInfo[i].DiagID); |
Argyrios Kyrtzidis | 9ffada9 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 568 | StringRef DiagnosticIDs::getNearestOption(diag::Flavor Flavor, |
| 569 | StringRef Group) { |
Benjamin Kramer | 116d887 | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 570 | StringRef Best; |
Benjamin Kramer | 176a5cb | 2011-11-15 12:26:39 +0000 | [diff] [blame] | 571 | unsigned BestDistance = Group.size() + 1; // Sanity threshold. |
Craig Topper | e353a72 | 2015-10-18 05:29:23 +0000 | [diff] [blame] | 572 | for (const WarningOption &O : OptionTable) { |
Benjamin Kramer | 116d887 | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 573 | // Don't suggest ignored warning flags. |
Craig Topper | c00e953 | 2015-10-17 17:10:43 +0000 | [diff] [blame] | 574 | if (!O.Members && !O.SubGroups) |
Benjamin Kramer | 116d887 | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 575 | continue; |
| 576 | |
Craig Topper | c00e953 | 2015-10-17 17:10:43 +0000 | [diff] [blame] | 577 | unsigned Distance = O.getName().edit_distance(Group, true, BestDistance); |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 578 | if (Distance > BestDistance) |
| 579 | continue; |
| 580 | |
| 581 | // Don't suggest groups that are not of this kind. |
| 582 | llvm::SmallVector<diag::kind, 8> Diags; |
Craig Topper | c00e953 | 2015-10-17 17:10:43 +0000 | [diff] [blame] | 583 | if (::getDiagnosticsInGroup(Flavor, &O, Diags) || Diags.empty()) |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 584 | continue; |
| 585 | |
Benjamin Kramer | 176a5cb | 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. |
Craig Topper | c00e953 | 2015-10-17 17:10:43 +0000 | [diff] [blame] | 591 | Best = O.getName(); |
Benjamin Kramer | 116d887 | 2011-11-14 23:30:34 +0000 | [diff] [blame] | 592 | BestDistance = Distance; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | return Best; |
| 597 | } |
| 598 | |
Argyrios Kyrtzidis | d004064 | 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 | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 601 | bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const { |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 602 | Diagnostic Info(&Diag); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 603 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 604 | assert(Diag.getClient() && "DiagnosticClient not set!"); |
| 605 | |
| 606 | // Figure out the diagnostic level of this message. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 607 | unsigned DiagID = Info.getID(); |
Jordan Rose | 6f524ac | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 608 | DiagnosticIDs::Level DiagLevel |
| 609 | = getDiagnosticLevel(DiagID, Info.getLocation(), Diag); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 610 | |
Richard Smith | a268671 | 2014-12-05 21:52:58 +0000 | [diff] [blame] | 611 | // Update counts for DiagnosticErrorTrap even if a fatal error occurred |
| 612 | // or diagnostics are suppressed. |
| 613 | if (DiagLevel >= DiagnosticIDs::Error) { |
| 614 | ++Diag.TrapNumErrorsOccurred; |
| 615 | if (isUnrecoverable(DiagID)) |
| 616 | ++Diag.TrapNumUnrecoverableErrorsOccurred; |
| 617 | } |
| 618 | |
| 619 | if (Diag.SuppressAllDiagnostics) |
| 620 | return false; |
| 621 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 622 | if (DiagLevel != DiagnosticIDs::Note) { |
| 623 | // Record that a fatal error occurred only when we see a second |
| 624 | // non-note diagnostic. This allows notes to be attached to the |
| 625 | // fatal error, but suppresses any diagnostics that follow those |
| 626 | // notes. |
| 627 | if (Diag.LastDiagLevel == DiagnosticIDs::Fatal) |
| 628 | Diag.FatalErrorOccurred = true; |
| 629 | |
| 630 | Diag.LastDiagLevel = DiagLevel; |
| 631 | } |
| 632 | |
| 633 | // If a fatal error has already been emitted, silence all subsequent |
| 634 | // diagnostics. |
| 635 | if (Diag.FatalErrorOccurred) { |
| 636 | if (DiagLevel >= DiagnosticIDs::Error && |
| 637 | Diag.Client->IncludeInDiagnosticCounts()) { |
| 638 | ++Diag.NumErrors; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 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 | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 651 | if (DiagLevel >= DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | 1fa8b4b | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 652 | if (isUnrecoverable(DiagID)) |
Douglas Gregor | 8a60bbe | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 653 | Diag.UnrecoverableErrorOccurred = true; |
Daniel Jasper | d2e6f65 | 2012-09-28 15:45:07 +0000 | [diff] [blame] | 654 | |
DeLesley Hutchins | 8ecd491 | 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 | d2e6f65 | 2012-09-28 15:45:07 +0000 | [diff] [blame] | 659 | Diag.ErrorOccurred = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 660 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 661 | ++Diag.NumErrors; |
| 662 | } |
| 663 | |
Douglas Gregor | 1420880 | 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 | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 668 | Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors); |
Douglas Gregor | 1420880 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 673 | // Finally, report it. |
Jordan Rose | 6f524ac | 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 | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 682 | Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info); |
Argyrios Kyrtzidis | d004064 | 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 | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 689 | } |
John McCall | a877d92 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 690 | |
| 691 | bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const { |
| 692 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
Richard Trieu | 428058f | 2014-08-01 01:42:01 +0000 | [diff] [blame] | 693 | assert(CustomDiagInfo && "Invalid CustomDiagInfo"); |
John McCall | a877d92 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 694 | // Custom diagnostics. |
| 695 | return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error; |
| 696 | } |
| 697 | |
| 698 | // Only errors may be unrecoverable. |
Douglas Gregor | 8a60bbe | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 699 | if (getBuiltinDiagClass(DiagID) < CLASS_ERROR) |
John McCall | a877d92 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 700 | return false; |
| 701 | |
| 702 | if (DiagID == diag::err_unavailable || |
| 703 | DiagID == diag::err_unavailable_message) |
| 704 | return false; |
| 705 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 706 | // Currently we consider all ARC errors as recoverable. |
Ted Kremenek | 337c5b8 | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 707 | if (isARCDiagnostic(DiagID)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 708 | return false; |
| 709 | |
John McCall | a877d92 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 710 | return true; |
| 711 | } |
Ted Kremenek | 337c5b8 | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 712 | |
| 713 | bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) { |
| 714 | unsigned cat = getCategoryNumberForDiag(DiagID); |
| 715 | return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC "); |
| 716 | } |