Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1 | //===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic IDs-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTDiagnostic.h" |
| 15 | #include "clang/Analysis/AnalysisDiagnostic.h" |
| 16 | #include "clang/Basic/DiagnosticIDs.h" |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 17 | #include "clang/Basic/DiagnosticCategories.h" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Driver/DriverDiagnostic.h" |
| 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Lex/LexDiagnostic.h" |
| 22 | #include "clang/Parse/ParseDiagnostic.h" |
| 23 | #include "clang/Sema/SemaDiagnostic.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 25 | |
| 26 | #include <map> |
| 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Builtin Diagnostic information |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | // Diagnostic classes. |
| 36 | enum { |
| 37 | CLASS_NOTE = 0x01, |
| 38 | CLASS_WARNING = 0x02, |
| 39 | CLASS_EXTENSION = 0x03, |
| 40 | CLASS_ERROR = 0x04 |
| 41 | }; |
| 42 | |
| 43 | struct StaticDiagInfoRec { |
| 44 | unsigned short DiagID; |
| 45 | unsigned Mapping : 3; |
| 46 | unsigned Class : 3; |
Douglas Gregor | 418df34 | 2011-01-27 21:06:28 +0000 | [diff] [blame] | 47 | unsigned SFINAE : 1; |
| 48 | unsigned AccessControl : 1; |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 49 | unsigned WarnNoWerror : 1; |
| 50 | unsigned WarnShowInSystemHeader : 1; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 51 | unsigned Category : 5; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 52 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 53 | uint8_t NameLen; |
| 54 | uint8_t OptionGroupLen; |
| 55 | |
| 56 | uint16_t DescriptionLen; |
| 57 | uint16_t BriefExplanationLen; |
| 58 | uint16_t FullExplanationLen; |
| 59 | |
| 60 | const char *NameStr; |
| 61 | const char *OptionGroupStr; |
| 62 | |
| 63 | const char *DescriptionStr; |
| 64 | const char *BriefExplanationStr; |
| 65 | const char *FullExplanationStr; |
| 66 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | StringRef getName() const { |
| 68 | return StringRef(NameStr, NameLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 70 | StringRef getOptionGroup() const { |
| 71 | return StringRef(OptionGroupStr, OptionGroupLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 74 | StringRef getDescription() const { |
| 75 | return StringRef(DescriptionStr, DescriptionLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 77 | StringRef getBriefExplanation() const { |
| 78 | return StringRef(BriefExplanationStr, BriefExplanationLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 80 | StringRef getFullExplanation() const { |
| 81 | return StringRef(FullExplanationStr, FullExplanationLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 82 | } |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 83 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 84 | bool operator<(const StaticDiagInfoRec &RHS) const { |
| 85 | return DiagID < RHS.DiagID; |
| 86 | } |
| 87 | }; |
| 88 | |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 89 | struct StaticDiagNameIndexRec { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 90 | const char *NameStr; |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 91 | unsigned short DiagID; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 92 | uint8_t NameLen; |
| 93 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 94 | StringRef getName() const { |
| 95 | return StringRef(NameStr, NameLen); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 98 | bool operator<(const StaticDiagNameIndexRec &RHS) const { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 99 | return getName() < RHS.getName(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool operator==(const StaticDiagNameIndexRec &RHS) const { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 103 | return getName() == RHS.getName(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 107 | template <size_t SizeOfStr, typename FieldType> |
| 108 | class StringSizerHelper { |
| 109 | char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1]; |
| 110 | public: |
| 111 | enum { Size = SizeOfStr }; |
| 112 | }; |
| 113 | |
| 114 | } // namespace anonymous |
| 115 | |
| 116 | #define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 117 | |
| 118 | static const StaticDiagInfoRec StaticDiagInfo[] = { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 119 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \ |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 120 | SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \ |
| 121 | CATEGORY,BRIEF,FULL) \ |
| 122 | { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \ |
| 123 | NOWERROR, SHOWINSYSHEADER, CATEGORY, \ |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 124 | STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t), \ |
| 125 | STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t), \ |
| 126 | STR_SIZE(FULL, uint16_t), \ |
| 127 | #ENUM, GROUP, DESC, BRIEF, FULL }, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 128 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
| 129 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
| 130 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
| 131 | #include "clang/Basic/DiagnosticLexKinds.inc" |
| 132 | #include "clang/Basic/DiagnosticParseKinds.inc" |
| 133 | #include "clang/Basic/DiagnosticASTKinds.inc" |
| 134 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
| 135 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 136 | #undef DIAG |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 137 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static const unsigned StaticDiagInfoSize = |
| 141 | sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1; |
| 142 | |
| 143 | /// To be sorted before first use (since it's splitted among multiple files) |
Benjamin Kramer | 81f9d14 | 2011-06-14 13:15:38 +0000 | [diff] [blame] | 144 | static const StaticDiagNameIndexRec StaticDiagNameIndex[] = { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 145 | #define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) }, |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 146 | #include "clang/Basic/DiagnosticIndexName.inc" |
| 147 | #undef DIAG_NAME_INDEX |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 148 | { 0, 0, 0 } |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | static const unsigned StaticDiagNameIndexSize = |
| 152 | sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 153 | |
| 154 | /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID, |
| 155 | /// or null if the ID is invalid. |
| 156 | static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 157 | // If assertions are enabled, verify that the StaticDiagInfo array is sorted. |
| 158 | #ifndef NDEBUG |
| 159 | static bool IsFirst = true; |
| 160 | if (IsFirst) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 161 | for (unsigned i = 1; i != StaticDiagInfoSize; ++i) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 162 | assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID && |
| 163 | "Diag ID conflict, the enums at the start of clang::diag (in " |
Fariborz Jahanian | f84109e | 2011-01-07 18:59:25 +0000 | [diff] [blame] | 164 | "DiagnosticIDs.h) probably need to be increased"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 165 | |
| 166 | assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] && |
| 167 | "Improperly sorted diag info"); |
| 168 | } |
| 169 | IsFirst = false; |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | // Search the diagnostic table with a binary search. |
Jeffrey Yasskin | 7c5109b | 2011-08-13 05:47:04 +0000 | [diff] [blame] | 174 | StaticDiagInfoRec Find = { static_cast<unsigned short>(DiagID), |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 175 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 176 | |
| 177 | const StaticDiagInfoRec *Found = |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 178 | std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find); |
| 179 | if (Found == StaticDiagInfo + StaticDiagInfoSize || |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 180 | Found->DiagID != DiagID) |
| 181 | return 0; |
| 182 | |
| 183 | return Found; |
| 184 | } |
| 185 | |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 186 | static diag::Mapping GetDefaultDiagMapping(unsigned DiagID) { |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 187 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) { |
| 188 | // Compute the effective mapping based on the extra bits. |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 189 | diag::Mapping Mapping = (diag::Mapping) Info->Mapping; |
Daniel Dunbar | 4213df3 | 2011-09-29 00:34:06 +0000 | [diff] [blame] | 190 | |
| 191 | if (Info->WarnNoWerror) { |
| 192 | assert(Mapping == diag::MAP_WARNING && |
| 193 | "Unexpected mapping with no-Werror bit!"); |
| 194 | Mapping = diag::MAP_WARNING_NO_WERROR; |
| 195 | } |
| 196 | |
| 197 | if (Info->WarnShowInSystemHeader) { |
| 198 | assert(Mapping == diag::MAP_WARNING && |
| 199 | "Unexpected mapping with show-in-system-header bit!"); |
| 200 | Mapping = diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER; |
| 201 | } |
| 202 | |
| 203 | return Mapping; |
| 204 | } |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 205 | return diag::MAP_FATAL; |
| 206 | } |
| 207 | |
| 208 | /// getWarningOptionForDiag - Return the lowest-level warning option that |
| 209 | /// enables the specified diagnostic. If there is no -Wfoo flag that controls |
| 210 | /// the diagnostic, this returns null. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 211 | StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 212 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 213 | return Info->getOptionGroup(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 214 | return StringRef(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 217 | /// getCategoryNumberForDiag - Return the category number that a specified |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 218 | /// DiagID belongs to, or 0 if no category. |
| 219 | unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) { |
| 220 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 221 | return Info->Category; |
| 222 | return 0; |
| 223 | } |
| 224 | |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 225 | namespace { |
| 226 | // The diagnostic category names. |
| 227 | struct StaticDiagCategoryRec { |
| 228 | const char *NameStr; |
| 229 | uint8_t NameLen; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 231 | StringRef getName() const { |
| 232 | return StringRef(NameStr, NameLen); |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 233 | } |
| 234 | }; |
| 235 | } |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 236 | |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 237 | static const StaticDiagCategoryRec CategoryNameTable[] = { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 238 | #define GET_CATEGORY_TABLE |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 239 | #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) }, |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 240 | #include "clang/Basic/DiagnosticGroups.inc" |
| 241 | #undef GET_CATEGORY_TABLE |
| 242 | { 0, 0 } |
| 243 | }; |
| 244 | |
| 245 | /// getNumberOfCategories - Return the number of categories |
| 246 | unsigned DiagnosticIDs::getNumberOfCategories() { |
| 247 | return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1; |
| 248 | } |
| 249 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 250 | /// getCategoryNameFromID - Given a category ID, return the name of the |
| 251 | /// category, an empty string if CategoryID is zero, or null if CategoryID is |
| 252 | /// invalid. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 253 | StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 254 | if (CategoryID >= getNumberOfCategories()) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | return StringRef(); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 256 | return CategoryNameTable[CategoryID].getName(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | DiagnosticIDs::SFINAEResponse |
| 262 | DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) { |
| 263 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) { |
Douglas Gregor | 418df34 | 2011-01-27 21:06:28 +0000 | [diff] [blame] | 264 | if (Info->AccessControl) |
| 265 | return SFINAE_AccessControl; |
| 266 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 267 | if (!Info->SFINAE) |
| 268 | return SFINAE_Report; |
| 269 | |
| 270 | if (Info->Class == CLASS_ERROR) |
| 271 | return SFINAE_SubstitutionFailure; |
| 272 | |
| 273 | // Suppress notes, warnings, and extensions; |
| 274 | return SFINAE_Suppress; |
| 275 | } |
| 276 | |
| 277 | return SFINAE_Report; |
| 278 | } |
| 279 | |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 280 | /// getName - Given a diagnostic ID, return its name |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 281 | StringRef DiagnosticIDs::getName(unsigned DiagID) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 282 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 283 | return Info->getName(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 284 | return StringRef(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /// getIdFromName - Given a diagnostic name, return its ID, or 0 |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 288 | unsigned DiagnosticIDs::getIdFromName(StringRef Name) { |
Benjamin Kramer | 81f9d14 | 2011-06-14 13:15:38 +0000 | [diff] [blame] | 289 | const StaticDiagNameIndexRec *StaticDiagNameIndexEnd = |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 290 | StaticDiagNameIndex + StaticDiagNameIndexSize; |
| 291 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 292 | if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; } |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 293 | |
Jeffrey Yasskin | 7c5109b | 2011-08-13 05:47:04 +0000 | [diff] [blame] | 294 | assert(Name.size() == static_cast<uint8_t>(Name.size()) && |
| 295 | "Name is too long"); |
| 296 | StaticDiagNameIndexRec Find = { Name.data(), 0, |
| 297 | static_cast<uint8_t>(Name.size()) }; |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 298 | |
| 299 | const StaticDiagNameIndexRec *Found = |
| 300 | std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find); |
| 301 | if (Found == StaticDiagNameIndexEnd || |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 302 | Found->getName() != Name) |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 303 | return diag::DIAG_UPPER_LIMIT; |
| 304 | |
| 305 | return Found->DiagID; |
| 306 | } |
| 307 | |
| 308 | /// getBriefExplanation - Given a diagnostic ID, return a brief explanation |
| 309 | /// of the issue |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 310 | StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 311 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 312 | return Info->getBriefExplanation(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 313 | return StringRef(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /// getFullExplanation - Given a diagnostic ID, return a full explanation |
| 317 | /// of the issue |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 318 | StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 319 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 320 | return Info->getFullExplanation(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 321 | return StringRef(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /// getBuiltinDiagClass - Return the class field of the diagnostic. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 325 | /// |
| 326 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 327 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 328 | return Info->Class; |
| 329 | return ~0U; |
| 330 | } |
| 331 | |
| 332 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6948bc4 | 2011-08-09 03:39:14 +0000 | [diff] [blame] | 333 | // diag_iterator |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
| 336 | llvm::StringRef DiagnosticIDs::diag_iterator::getDiagName() const { |
| 337 | return static_cast<const StaticDiagNameIndexRec*>(impl)->getName(); |
| 338 | } |
| 339 | |
| 340 | unsigned DiagnosticIDs::diag_iterator::getDiagID() const { |
| 341 | return static_cast<const StaticDiagNameIndexRec*>(impl)->DiagID; |
| 342 | } |
| 343 | |
| 344 | DiagnosticIDs::diag_iterator &DiagnosticIDs::diag_iterator::operator++() { |
| 345 | const StaticDiagNameIndexRec* ptr = |
| 346 | static_cast<const StaticDiagNameIndexRec*>(impl);; |
| 347 | ++ptr; |
| 348 | impl = ptr; |
| 349 | return *this; |
| 350 | } |
| 351 | |
| 352 | DiagnosticIDs::diag_iterator DiagnosticIDs::diags_begin() { |
| 353 | return DiagnosticIDs::diag_iterator(StaticDiagNameIndex); |
| 354 | } |
| 355 | |
| 356 | DiagnosticIDs::diag_iterator DiagnosticIDs::diags_end() { |
| 357 | return DiagnosticIDs::diag_iterator(StaticDiagNameIndex + |
| 358 | StaticDiagNameIndexSize); |
| 359 | } |
| 360 | |
| 361 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 362 | // Custom Diagnostic information |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | |
| 365 | namespace clang { |
| 366 | namespace diag { |
| 367 | class CustomDiagInfo { |
| 368 | typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc; |
| 369 | std::vector<DiagDesc> DiagInfo; |
| 370 | std::map<DiagDesc, unsigned> DiagIDs; |
| 371 | public: |
| 372 | |
| 373 | /// getDescription - Return the description of the specified custom |
| 374 | /// diagnostic. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 375 | StringRef getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 376 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
| 377 | "Invalid diagnosic ID"); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 378 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /// getLevel - Return the level of the specified custom diagnostic. |
| 382 | DiagnosticIDs::Level getLevel(unsigned DiagID) const { |
| 383 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
| 384 | "Invalid diagnosic ID"); |
| 385 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
| 386 | } |
| 387 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 388 | unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 389 | DiagnosticIDs &Diags) { |
| 390 | DiagDesc D(L, Message); |
| 391 | // Check to see if it already exists. |
| 392 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 393 | if (I != DiagIDs.end() && I->first == D) |
| 394 | return I->second; |
| 395 | |
| 396 | // If not, assign a new ID. |
| 397 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
| 398 | DiagIDs.insert(std::make_pair(D, ID)); |
| 399 | DiagInfo.push_back(D); |
| 400 | return ID; |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | } // end diag namespace |
| 405 | } // end clang namespace |
| 406 | |
| 407 | |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | // Common Diagnostic implementation |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | |
| 412 | DiagnosticIDs::DiagnosticIDs() { |
| 413 | CustomDiagInfo = 0; |
| 414 | } |
| 415 | |
| 416 | DiagnosticIDs::~DiagnosticIDs() { |
| 417 | delete CustomDiagInfo; |
| 418 | } |
| 419 | |
| 420 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 421 | /// and level. If this is the first request for this diagnosic, it is |
| 422 | /// registered and created, otherwise the existing ID is returned. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 423 | unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 424 | if (CustomDiagInfo == 0) |
| 425 | CustomDiagInfo = new diag::CustomDiagInfo(); |
| 426 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 431 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 432 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 433 | /// call on NOTEs. |
| 434 | bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) { |
| 435 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 436 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
| 437 | } |
| 438 | |
| 439 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 440 | /// Note. |
| 441 | bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) { |
| 442 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 443 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
| 444 | } |
| 445 | |
| 446 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
| 447 | /// ID is for an extension of some sort. This also returns EnabledByDefault, |
| 448 | /// which is set to indicate whether the diagnostic is ignored by default (in |
| 449 | /// which case -pedantic enables it) or treated as a warning/error by default. |
| 450 | /// |
| 451 | bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID, |
| 452 | bool &EnabledByDefault) { |
| 453 | if (DiagID >= diag::DIAG_UPPER_LIMIT || |
| 454 | getBuiltinDiagClass(DiagID) != CLASS_EXTENSION) |
| 455 | return false; |
| 456 | |
| 457 | EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE; |
| 458 | return true; |
| 459 | } |
| 460 | |
Daniel Dunbar | 76101cf | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 461 | bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) { |
| 462 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 463 | return false; |
| 464 | |
| 465 | return GetDefaultDiagMapping(DiagID) == diag::MAP_ERROR; |
| 466 | } |
| 467 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 468 | /// getDescription - Given a diagnostic ID, return a description of the |
| 469 | /// issue. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 470 | StringRef DiagnosticIDs::getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 471 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 472 | return Info->getDescription(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 473 | return CustomDiagInfo->getDescription(DiagID); |
| 474 | } |
| 475 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 476 | /// getDiagnosticLevel - Based on the way the client configured the |
| 477 | /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level, |
| 478 | /// by consumable the DiagnosticClient. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 479 | DiagnosticIDs::Level |
| 480 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc, |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 481 | const DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 482 | // Handle custom diagnostics, which cannot be mapped. |
| 483 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 484 | return CustomDiagInfo->getLevel(DiagID); |
| 485 | |
| 486 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
| 487 | assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!"); |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 488 | return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 491 | /// \brief Based on the way the client configured the Diagnostic |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 492 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 493 | /// the DiagnosticClient. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 494 | /// |
| 495 | /// \param Loc The source location we are interested in finding out the |
| 496 | /// diagnostic state. Can be null in order to query the latest state. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 497 | DiagnosticIDs::Level |
| 498 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass, |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 499 | SourceLocation Loc, |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 500 | const DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 501 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 502 | // to error. Errors can only be mapped to fatal. |
| 503 | DiagnosticIDs::Level Result = DiagnosticIDs::Fatal; |
| 504 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 505 | DiagnosticsEngine::DiagStatePointsTy::iterator |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 506 | Pos = Diag.GetDiagStatePointForLoc(Loc); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 507 | DiagnosticsEngine::DiagState *State = Pos->State; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 508 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 509 | // Get the mapping information, if unset, compute it lazily. |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 510 | DiagnosticMappingInfo MappingInfo = State->getMappingInfo((diag::kind)DiagID); |
| 511 | if (MappingInfo.isUnset()) { |
| 512 | MappingInfo = DiagnosticMappingInfo::MakeInfo( |
| 513 | GetDefaultDiagMapping(DiagID), /*IsUser=*/false, /*IsPragma=*/false); |
| 514 | State->setMappingInfo((diag::kind) DiagID, MappingInfo); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Argyrios Kyrtzidis | 144bc08 | 2011-04-21 23:08:23 +0000 | [diff] [blame] | 517 | bool ShouldEmitInSystemHeader = false; |
| 518 | |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 519 | switch (MappingInfo.getMapping()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 520 | default: llvm_unreachable("Unknown mapping!"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 521 | case diag::MAP_IGNORE: |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 522 | if (Diag.EnableAllWarnings) { |
| 523 | // Leave the warning disabled if it was explicitly ignored. |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 524 | if (MappingInfo.isUser()) |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 525 | return DiagnosticIDs::Ignored; |
| 526 | |
| 527 | Result = Diag.WarningsAsErrors ? DiagnosticIDs::Error |
| 528 | : DiagnosticIDs::Warning; |
| 529 | } |
| 530 | // Otherwise, ignore this diagnostic unless this is an extension diagnostic |
| 531 | // and we're mapping them onto warnings or errors. |
| 532 | else if (!isBuiltinExtensionDiag(DiagID) || // Not an extension |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 533 | Diag.ExtBehavior == DiagnosticsEngine::Ext_Ignore || // Ext ignored |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 534 | MappingInfo.isUser()) { // User explicitly mapped it. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 535 | return DiagnosticIDs::Ignored; |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 536 | } |
| 537 | else { |
| 538 | Result = DiagnosticIDs::Warning; |
| 539 | } |
| 540 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 541 | if (Diag.ExtBehavior == DiagnosticsEngine::Ext_Error) |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 542 | Result = DiagnosticIDs::Error; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 543 | if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal) |
| 544 | Result = DiagnosticIDs::Fatal; |
| 545 | break; |
| 546 | case diag::MAP_ERROR: |
| 547 | Result = DiagnosticIDs::Error; |
| 548 | if (Diag.ErrorsAsFatal) |
| 549 | Result = DiagnosticIDs::Fatal; |
| 550 | break; |
| 551 | case diag::MAP_FATAL: |
| 552 | Result = DiagnosticIDs::Fatal; |
| 553 | break; |
Argyrios Kyrtzidis | 144bc08 | 2011-04-21 23:08:23 +0000 | [diff] [blame] | 554 | case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER: |
| 555 | ShouldEmitInSystemHeader = true; |
| 556 | // continue as MAP_WARNING. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 557 | case diag::MAP_WARNING: |
| 558 | // If warnings are globally mapped to ignore or error, do it. |
| 559 | if (Diag.IgnoreAllWarnings) |
| 560 | return DiagnosticIDs::Ignored; |
| 561 | |
| 562 | Result = DiagnosticIDs::Warning; |
| 563 | |
| 564 | // If this is an extension diagnostic and we're in -pedantic-error mode, and |
| 565 | // if the user didn't explicitly map it, upgrade to an error. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 566 | if (Diag.ExtBehavior == DiagnosticsEngine::Ext_Error && |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame^] | 567 | !MappingInfo.isUser() && |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 568 | isBuiltinExtensionDiag(DiagID)) |
| 569 | Result = DiagnosticIDs::Error; |
| 570 | |
| 571 | if (Diag.WarningsAsErrors) |
| 572 | Result = DiagnosticIDs::Error; |
| 573 | if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal) |
| 574 | Result = DiagnosticIDs::Fatal; |
| 575 | break; |
| 576 | |
| 577 | case diag::MAP_WARNING_NO_WERROR: |
| 578 | // Diagnostics specified with -Wno-error=foo should be set to warnings, but |
| 579 | // not be adjusted by -Werror or -pedantic-errors. |
| 580 | Result = DiagnosticIDs::Warning; |
| 581 | |
| 582 | // If warnings are globally mapped to ignore or error, do it. |
| 583 | if (Diag.IgnoreAllWarnings) |
| 584 | return DiagnosticIDs::Ignored; |
| 585 | |
| 586 | break; |
| 587 | |
| 588 | case diag::MAP_ERROR_NO_WFATAL: |
| 589 | // Diagnostics specified as -Wno-fatal-error=foo should be errors, but |
| 590 | // unaffected by -Wfatal-errors. |
| 591 | Result = DiagnosticIDs::Error; |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | // Okay, we're about to return this as a "diagnostic to emit" one last check: |
| 596 | // if this is any sort of extension warning, and if we're in an __extension__ |
| 597 | // block, silence it. |
| 598 | if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID)) |
| 599 | return DiagnosticIDs::Ignored; |
| 600 | |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 601 | // If we are in a system header, we ignore it. |
| 602 | // We also want to ignore extensions and warnings in -Werror and |
| 603 | // -pedantic-errors modes, which *map* warnings/extensions to errors. |
| 604 | if (Result >= DiagnosticIDs::Warning && |
| 605 | DiagClass != CLASS_ERROR && |
| 606 | // Custom diagnostics always are emitted in system headers. |
| 607 | DiagID < diag::DIAG_UPPER_LIMIT && |
Argyrios Kyrtzidis | 144bc08 | 2011-04-21 23:08:23 +0000 | [diff] [blame] | 608 | !ShouldEmitInSystemHeader && |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 609 | Diag.SuppressSystemWarnings && |
| 610 | Loc.isValid() && |
| 611 | Diag.getSourceManager().isInSystemHeader( |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 612 | Diag.getSourceManager().getExpansionLoc(Loc))) |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 613 | return DiagnosticIDs::Ignored; |
| 614 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 615 | return Result; |
| 616 | } |
| 617 | |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 618 | namespace { |
| 619 | struct WarningOption { |
| 620 | // Be safe with the size of 'NameLen' because we don't statically check if |
| 621 | // the size will fit in the field; the struct size won't decrease with a |
| 622 | // shorter type anyway. |
| 623 | size_t NameLen; |
| 624 | const char *NameStr; |
| 625 | const short *Members; |
| 626 | const short *SubGroups; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 628 | StringRef getName() const { |
| 629 | return StringRef(NameStr, NameLen); |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 630 | } |
| 631 | }; |
| 632 | } |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 633 | |
| 634 | #define GET_DIAG_ARRAYS |
| 635 | #include "clang/Basic/DiagnosticGroups.inc" |
| 636 | #undef GET_DIAG_ARRAYS |
| 637 | |
| 638 | // Second the table of options, sorted by name for fast binary lookup. |
| 639 | static const WarningOption OptionTable[] = { |
| 640 | #define GET_DIAG_TABLE |
| 641 | #include "clang/Basic/DiagnosticGroups.inc" |
| 642 | #undef GET_DIAG_TABLE |
| 643 | }; |
| 644 | static const size_t OptionTableSize = |
| 645 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 646 | |
| 647 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 648 | const WarningOption &RHS) { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 649 | return LHS.getName() < RHS.getName(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 653 | SourceLocation Loc, DiagnosticsEngine &Diag) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 654 | // Option exists, poke all the members of its diagnostic set. |
| 655 | if (const short *Member = Group->Members) { |
| 656 | for (; *Member != -1; ++Member) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 657 | Diag.setDiagnosticMapping(*Member, Mapping, Loc); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // Enable/disable all subgroups along with this one. |
| 661 | if (const short *SubGroups = Group->SubGroups) { |
| 662 | for (; *SubGroups != (short)-1; ++SubGroups) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 663 | MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g. |
| 668 | /// "unknown-pragmas" to have the specified mapping. This returns true and |
| 669 | /// ignores the request if "Group" was unknown, false otherwise. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 670 | bool DiagnosticIDs::setDiagnosticGroupMapping(StringRef Group, |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 671 | diag::Mapping Map, |
| 672 | SourceLocation Loc, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 673 | DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 674 | assert((Loc.isValid() || |
| 675 | Diag.DiagStatePoints.empty() || |
| 676 | Diag.DiagStatePoints.back().Loc.isInvalid()) && |
| 677 | "Loc should be invalid only when the mapping comes from command-line"); |
| 678 | assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() || |
| 679 | Diag.DiagStatePoints.back().Loc.isInvalid() || |
| 680 | !Diag.SourceMgr->isBeforeInTranslationUnit(Loc, |
| 681 | Diag.DiagStatePoints.back().Loc)) && |
| 682 | "Source location of new mapping is before the previous one!"); |
| 683 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 684 | WarningOption Key = { Group.size(), Group.data(), 0, 0 }; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 685 | const WarningOption *Found = |
| 686 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 687 | WarningOptionCompare); |
| 688 | if (Found == OptionTable + OptionTableSize || |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 689 | Found->getName() != Group) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 690 | return true; // Option not found. |
| 691 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 692 | MapGroupMembers(Found, Map, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 693 | return false; |
| 694 | } |
| 695 | |
| 696 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 697 | /// finally fully formed. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 698 | bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const { |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 699 | Diagnostic Info(&Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 700 | |
| 701 | if (Diag.SuppressAllDiagnostics) |
| 702 | return false; |
| 703 | |
| 704 | assert(Diag.getClient() && "DiagnosticClient not set!"); |
| 705 | |
| 706 | // Figure out the diagnostic level of this message. |
| 707 | DiagnosticIDs::Level DiagLevel; |
| 708 | unsigned DiagID = Info.getID(); |
| 709 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 710 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 711 | // Handle custom diagnostics, which cannot be mapped. |
| 712 | DiagLevel = CustomDiagInfo->getLevel(DiagID); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 713 | } else { |
| 714 | // Get the class of the diagnostic. If this is a NOTE, map it onto whatever |
| 715 | // the diagnostic level was for the previous diagnostic so that it is |
| 716 | // filtered the same as the previous diagnostic. |
| 717 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
| 718 | if (DiagClass == CLASS_NOTE) { |
| 719 | DiagLevel = DiagnosticIDs::Note; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 720 | } else { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 721 | DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(), |
| 722 | Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | if (DiagLevel != DiagnosticIDs::Note) { |
| 727 | // Record that a fatal error occurred only when we see a second |
| 728 | // non-note diagnostic. This allows notes to be attached to the |
| 729 | // fatal error, but suppresses any diagnostics that follow those |
| 730 | // notes. |
| 731 | if (Diag.LastDiagLevel == DiagnosticIDs::Fatal) |
| 732 | Diag.FatalErrorOccurred = true; |
| 733 | |
| 734 | Diag.LastDiagLevel = DiagLevel; |
| 735 | } |
| 736 | |
Argyrios Kyrtzidis | c0a575f | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 737 | // Update counts for DiagnosticErrorTrap even if a fatal error occurred. |
| 738 | if (DiagLevel >= DiagnosticIDs::Error) { |
| 739 | ++Diag.TrapNumErrorsOccurred; |
| 740 | if (isUnrecoverable(DiagID)) |
| 741 | ++Diag.TrapNumUnrecoverableErrorsOccurred; |
| 742 | } |
| 743 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 744 | // If a fatal error has already been emitted, silence all subsequent |
| 745 | // diagnostics. |
| 746 | if (Diag.FatalErrorOccurred) { |
| 747 | if (DiagLevel >= DiagnosticIDs::Error && |
| 748 | Diag.Client->IncludeInDiagnosticCounts()) { |
| 749 | ++Diag.NumErrors; |
| 750 | ++Diag.NumErrorsSuppressed; |
| 751 | } |
| 752 | |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | // If the client doesn't care about this message, don't issue it. If this is |
| 757 | // a note and the last real diagnostic was ignored, ignore it too. |
| 758 | if (DiagLevel == DiagnosticIDs::Ignored || |
| 759 | (DiagLevel == DiagnosticIDs::Note && |
| 760 | Diag.LastDiagLevel == DiagnosticIDs::Ignored)) |
| 761 | return false; |
| 762 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 763 | if (DiagLevel >= DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | c0a575f | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 764 | if (isUnrecoverable(DiagID)) |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 765 | Diag.UnrecoverableErrorOccurred = true; |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 766 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 767 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
| 768 | Diag.ErrorOccurred = true; |
| 769 | ++Diag.NumErrors; |
| 770 | } |
| 771 | |
Douglas Gregor | f1d5948 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 772 | // If we've emitted a lot of errors, emit a fatal error instead of it to |
| 773 | // stop a flood of bogus errors. |
| 774 | if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit && |
| 775 | DiagLevel == DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 776 | Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors); |
Douglas Gregor | f1d5948 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 777 | return false; |
| 778 | } |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Douglas Gregor | 4814fb5 | 2011-02-03 23:41:12 +0000 | [diff] [blame] | 781 | // If we have any Fix-Its, make sure that all of the Fix-Its point into |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 782 | // source locations that aren't macro expansions. If any point into macro |
| 783 | // expansions, remove all of the Fix-Its. |
Douglas Gregor | 4814fb5 | 2011-02-03 23:41:12 +0000 | [diff] [blame] | 784 | for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) { |
| 785 | const FixItHint &FixIt = Diag.FixItHints[I]; |
| 786 | if (FixIt.RemoveRange.isInvalid() || |
| 787 | FixIt.RemoveRange.getBegin().isMacroID() || |
| 788 | FixIt.RemoveRange.getEnd().isMacroID()) { |
| 789 | Diag.NumFixItHints = 0; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 794 | // Finally, report it. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 795 | Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 796 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
| 797 | if (DiagLevel == DiagnosticIDs::Warning) |
| 798 | ++Diag.NumWarnings; |
| 799 | } |
| 800 | |
| 801 | Diag.CurDiagID = ~0U; |
| 802 | |
| 803 | return true; |
| 804 | } |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 805 | |
| 806 | bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const { |
| 807 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 808 | // Custom diagnostics. |
| 809 | return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error; |
| 810 | } |
| 811 | |
| 812 | // Only errors may be unrecoverable. |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 813 | if (getBuiltinDiagClass(DiagID) < CLASS_ERROR) |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 814 | return false; |
| 815 | |
| 816 | if (DiagID == diag::err_unavailable || |
| 817 | DiagID == diag::err_unavailable_message) |
| 818 | return false; |
| 819 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 820 | // Currently we consider all ARC errors as recoverable. |
| 821 | if (getCategoryNumberForDiag(DiagID) == |
| 822 | diag::DiagCat_Automatic_Reference_Counting_Issue) |
| 823 | return false; |
| 824 | |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 825 | return true; |
| 826 | } |