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