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 | |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame^] | 237 | // Unfortunately, the split between DiagnosticIDs and Diagnostic is not |
| 238 | // particularly clean, but for now we just implement this method here so we can |
| 239 | // access GetDefaultDiagMapping. |
| 240 | DiagnosticMappingInfo &DiagnosticsEngine::DiagState::getOrAddMappingInfo( |
| 241 | diag::kind Diag) |
| 242 | { |
| 243 | std::pair<iterator, bool> Result = DiagMap.insert( |
| 244 | std::make_pair(Diag, DiagnosticMappingInfo::MakeUnset())); |
| 245 | |
| 246 | // Initialize the entry if we added it. |
| 247 | if (Result.second) { |
| 248 | assert(Result.first->second.isUnset() && "unexpected unset entry"); |
| 249 | Result.first->second = DiagnosticMappingInfo::MakeInfo( |
| 250 | GetDefaultDiagMapping(Diag), /*IsUser=*/false, /*IsPragma=*/false); |
| 251 | } |
| 252 | |
| 253 | return Result.first->second; |
| 254 | } |
| 255 | |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 256 | static const StaticDiagCategoryRec CategoryNameTable[] = { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 257 | #define GET_CATEGORY_TABLE |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 258 | #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) }, |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 259 | #include "clang/Basic/DiagnosticGroups.inc" |
| 260 | #undef GET_CATEGORY_TABLE |
| 261 | { 0, 0 } |
| 262 | }; |
| 263 | |
| 264 | /// getNumberOfCategories - Return the number of categories |
| 265 | unsigned DiagnosticIDs::getNumberOfCategories() { |
| 266 | return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1; |
| 267 | } |
| 268 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 269 | /// getCategoryNameFromID - Given a category ID, return the name of the |
| 270 | /// category, an empty string if CategoryID is zero, or null if CategoryID is |
| 271 | /// invalid. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 272 | StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 273 | if (CategoryID >= getNumberOfCategories()) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 274 | return StringRef(); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 275 | return CategoryNameTable[CategoryID].getName(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | |
| 280 | DiagnosticIDs::SFINAEResponse |
| 281 | DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) { |
| 282 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) { |
Douglas Gregor | 418df34 | 2011-01-27 21:06:28 +0000 | [diff] [blame] | 283 | if (Info->AccessControl) |
| 284 | return SFINAE_AccessControl; |
| 285 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 286 | if (!Info->SFINAE) |
| 287 | return SFINAE_Report; |
| 288 | |
| 289 | if (Info->Class == CLASS_ERROR) |
| 290 | return SFINAE_SubstitutionFailure; |
| 291 | |
| 292 | // Suppress notes, warnings, and extensions; |
| 293 | return SFINAE_Suppress; |
| 294 | } |
| 295 | |
| 296 | return SFINAE_Report; |
| 297 | } |
| 298 | |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 299 | /// getName - Given a diagnostic ID, return its name |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 300 | StringRef DiagnosticIDs::getName(unsigned DiagID) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 301 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 302 | return Info->getName(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 303 | return StringRef(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /// getIdFromName - Given a diagnostic name, return its ID, or 0 |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 307 | unsigned DiagnosticIDs::getIdFromName(StringRef Name) { |
Benjamin Kramer | 81f9d14 | 2011-06-14 13:15:38 +0000 | [diff] [blame] | 308 | const StaticDiagNameIndexRec *StaticDiagNameIndexEnd = |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 309 | StaticDiagNameIndex + StaticDiagNameIndexSize; |
| 310 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 311 | if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; } |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 312 | |
Jeffrey Yasskin | 7c5109b | 2011-08-13 05:47:04 +0000 | [diff] [blame] | 313 | assert(Name.size() == static_cast<uint8_t>(Name.size()) && |
| 314 | "Name is too long"); |
| 315 | StaticDiagNameIndexRec Find = { Name.data(), 0, |
| 316 | static_cast<uint8_t>(Name.size()) }; |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 317 | |
| 318 | const StaticDiagNameIndexRec *Found = |
| 319 | std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find); |
| 320 | if (Found == StaticDiagNameIndexEnd || |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 321 | Found->getName() != Name) |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 322 | return diag::DIAG_UPPER_LIMIT; |
| 323 | |
| 324 | return Found->DiagID; |
| 325 | } |
| 326 | |
| 327 | /// getBriefExplanation - Given a diagnostic ID, return a brief explanation |
| 328 | /// of the issue |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 329 | StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 330 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 331 | return Info->getBriefExplanation(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 332 | return StringRef(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /// getFullExplanation - Given a diagnostic ID, return a full explanation |
| 336 | /// of the issue |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 337 | StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) { |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 338 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 339 | return Info->getFullExplanation(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 340 | return StringRef(); |
Douglas Gregor | 7d2b8c1 | 2011-04-15 22:04:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /// getBuiltinDiagClass - Return the class field of the diagnostic. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 344 | /// |
| 345 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 346 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 347 | return Info->Class; |
| 348 | return ~0U; |
| 349 | } |
| 350 | |
| 351 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6948bc4 | 2011-08-09 03:39:14 +0000 | [diff] [blame] | 352 | // diag_iterator |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | |
| 355 | llvm::StringRef DiagnosticIDs::diag_iterator::getDiagName() const { |
| 356 | return static_cast<const StaticDiagNameIndexRec*>(impl)->getName(); |
| 357 | } |
| 358 | |
| 359 | unsigned DiagnosticIDs::diag_iterator::getDiagID() const { |
| 360 | return static_cast<const StaticDiagNameIndexRec*>(impl)->DiagID; |
| 361 | } |
| 362 | |
| 363 | DiagnosticIDs::diag_iterator &DiagnosticIDs::diag_iterator::operator++() { |
| 364 | const StaticDiagNameIndexRec* ptr = |
| 365 | static_cast<const StaticDiagNameIndexRec*>(impl);; |
| 366 | ++ptr; |
| 367 | impl = ptr; |
| 368 | return *this; |
| 369 | } |
| 370 | |
| 371 | DiagnosticIDs::diag_iterator DiagnosticIDs::diags_begin() { |
| 372 | return DiagnosticIDs::diag_iterator(StaticDiagNameIndex); |
| 373 | } |
| 374 | |
| 375 | DiagnosticIDs::diag_iterator DiagnosticIDs::diags_end() { |
| 376 | return DiagnosticIDs::diag_iterator(StaticDiagNameIndex + |
| 377 | StaticDiagNameIndexSize); |
| 378 | } |
| 379 | |
| 380 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 381 | // Custom Diagnostic information |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | namespace clang { |
| 385 | namespace diag { |
| 386 | class CustomDiagInfo { |
| 387 | typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc; |
| 388 | std::vector<DiagDesc> DiagInfo; |
| 389 | std::map<DiagDesc, unsigned> DiagIDs; |
| 390 | public: |
| 391 | |
| 392 | /// getDescription - Return the description of the specified custom |
| 393 | /// diagnostic. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 394 | StringRef getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 395 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
| 396 | "Invalid diagnosic ID"); |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 397 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /// getLevel - Return the level of the specified custom diagnostic. |
| 401 | DiagnosticIDs::Level getLevel(unsigned DiagID) const { |
| 402 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
| 403 | "Invalid diagnosic ID"); |
| 404 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
| 405 | } |
| 406 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 407 | unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 408 | DiagnosticIDs &Diags) { |
| 409 | DiagDesc D(L, Message); |
| 410 | // Check to see if it already exists. |
| 411 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 412 | if (I != DiagIDs.end() && I->first == D) |
| 413 | return I->second; |
| 414 | |
| 415 | // If not, assign a new ID. |
| 416 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
| 417 | DiagIDs.insert(std::make_pair(D, ID)); |
| 418 | DiagInfo.push_back(D); |
| 419 | return ID; |
| 420 | } |
| 421 | }; |
| 422 | |
| 423 | } // end diag namespace |
| 424 | } // end clang namespace |
| 425 | |
| 426 | |
| 427 | //===----------------------------------------------------------------------===// |
| 428 | // Common Diagnostic implementation |
| 429 | //===----------------------------------------------------------------------===// |
| 430 | |
| 431 | DiagnosticIDs::DiagnosticIDs() { |
| 432 | CustomDiagInfo = 0; |
| 433 | } |
| 434 | |
| 435 | DiagnosticIDs::~DiagnosticIDs() { |
| 436 | delete CustomDiagInfo; |
| 437 | } |
| 438 | |
| 439 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 440 | /// and level. If this is the first request for this diagnosic, it is |
| 441 | /// registered and created, otherwise the existing ID is returned. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 442 | unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef Message) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 443 | if (CustomDiagInfo == 0) |
| 444 | CustomDiagInfo = new diag::CustomDiagInfo(); |
| 445 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 450 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 451 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 452 | /// call on NOTEs. |
| 453 | bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) { |
| 454 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 455 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
| 456 | } |
| 457 | |
| 458 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 459 | /// Note. |
| 460 | bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) { |
| 461 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 462 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
| 463 | } |
| 464 | |
| 465 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
| 466 | /// ID is for an extension of some sort. This also returns EnabledByDefault, |
| 467 | /// which is set to indicate whether the diagnostic is ignored by default (in |
| 468 | /// which case -pedantic enables it) or treated as a warning/error by default. |
| 469 | /// |
| 470 | bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID, |
| 471 | bool &EnabledByDefault) { |
| 472 | if (DiagID >= diag::DIAG_UPPER_LIMIT || |
| 473 | getBuiltinDiagClass(DiagID) != CLASS_EXTENSION) |
| 474 | return false; |
| 475 | |
| 476 | EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE; |
| 477 | return true; |
| 478 | } |
| 479 | |
Daniel Dunbar | 76101cf | 2011-09-29 01:01:08 +0000 | [diff] [blame] | 480 | bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) { |
| 481 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 482 | return false; |
| 483 | |
| 484 | return GetDefaultDiagMapping(DiagID) == diag::MAP_ERROR; |
| 485 | } |
| 486 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 487 | /// getDescription - Given a diagnostic ID, return a description of the |
| 488 | /// issue. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 489 | StringRef DiagnosticIDs::getDescription(unsigned DiagID) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 490 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 491 | return Info->getDescription(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 492 | return CustomDiagInfo->getDescription(DiagID); |
| 493 | } |
| 494 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 495 | /// getDiagnosticLevel - Based on the way the client configured the |
| 496 | /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level, |
| 497 | /// by consumable the DiagnosticClient. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 498 | DiagnosticIDs::Level |
| 499 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, 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 | // Handle custom diagnostics, which cannot be mapped. |
| 502 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
| 503 | return CustomDiagInfo->getLevel(DiagID); |
| 504 | |
| 505 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
| 506 | assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!"); |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 507 | return getDiagnosticLevel(DiagID, DiagClass, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 510 | /// \brief Based on the way the client configured the Diagnostic |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 511 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 512 | /// the DiagnosticClient. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 513 | /// |
| 514 | /// \param Loc The source location we are interested in finding out the |
| 515 | /// diagnostic state. Can be null in order to query the latest state. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 516 | DiagnosticIDs::Level |
| 517 | DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass, |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 518 | SourceLocation Loc, |
Daniel Dunbar | 1656aae | 2011-09-29 01:20:28 +0000 | [diff] [blame] | 519 | const DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 520 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 521 | // to error. Errors can only be mapped to fatal. |
| 522 | DiagnosticIDs::Level Result = DiagnosticIDs::Fatal; |
| 523 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 524 | DiagnosticsEngine::DiagStatePointsTy::iterator |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 525 | Pos = Diag.GetDiagStatePointForLoc(Loc); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 526 | DiagnosticsEngine::DiagState *State = Pos->State; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 527 | |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame^] | 528 | // Get the mapping information, or compute it lazily. |
| 529 | DiagnosticMappingInfo &MappingInfo = State->getOrAddMappingInfo( |
| 530 | (diag::kind)DiagID); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 531 | |
Argyrios Kyrtzidis | 144bc08 | 2011-04-21 23:08:23 +0000 | [diff] [blame] | 532 | bool ShouldEmitInSystemHeader = false; |
| 533 | |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame] | 534 | switch (MappingInfo.getMapping()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 535 | default: llvm_unreachable("Unknown mapping!"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 536 | case diag::MAP_IGNORE: |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 537 | if (Diag.EnableAllWarnings) { |
| 538 | // Leave the warning disabled if it was explicitly ignored. |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame] | 539 | if (MappingInfo.isUser()) |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 540 | return DiagnosticIDs::Ignored; |
| 541 | |
| 542 | Result = Diag.WarningsAsErrors ? DiagnosticIDs::Error |
| 543 | : DiagnosticIDs::Warning; |
| 544 | } |
| 545 | // Otherwise, ignore this diagnostic unless this is an extension diagnostic |
| 546 | // and we're mapping them onto warnings or errors. |
| 547 | else if (!isBuiltinExtensionDiag(DiagID) || // Not an extension |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 548 | Diag.ExtBehavior == DiagnosticsEngine::Ext_Ignore || // Ext ignored |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame] | 549 | MappingInfo.isUser()) { // User explicitly mapped it. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 550 | return DiagnosticIDs::Ignored; |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 551 | } |
| 552 | else { |
| 553 | Result = DiagnosticIDs::Warning; |
| 554 | } |
| 555 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 556 | if (Diag.ExtBehavior == DiagnosticsEngine::Ext_Error) |
Ted Kremenek | 1e473cc | 2011-08-18 01:12:56 +0000 | [diff] [blame] | 557 | Result = DiagnosticIDs::Error; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 558 | if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal) |
| 559 | Result = DiagnosticIDs::Fatal; |
| 560 | break; |
| 561 | case diag::MAP_ERROR: |
| 562 | Result = DiagnosticIDs::Error; |
| 563 | if (Diag.ErrorsAsFatal) |
| 564 | Result = DiagnosticIDs::Fatal; |
| 565 | break; |
| 566 | case diag::MAP_FATAL: |
| 567 | Result = DiagnosticIDs::Fatal; |
| 568 | break; |
Argyrios Kyrtzidis | 144bc08 | 2011-04-21 23:08:23 +0000 | [diff] [blame] | 569 | case diag::MAP_WARNING_SHOW_IN_SYSTEM_HEADER: |
| 570 | ShouldEmitInSystemHeader = true; |
| 571 | // continue as MAP_WARNING. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 572 | case diag::MAP_WARNING: |
| 573 | // If warnings are globally mapped to ignore or error, do it. |
| 574 | if (Diag.IgnoreAllWarnings) |
| 575 | return DiagnosticIDs::Ignored; |
| 576 | |
| 577 | Result = DiagnosticIDs::Warning; |
| 578 | |
| 579 | // If this is an extension diagnostic and we're in -pedantic-error mode, and |
| 580 | // if the user didn't explicitly map it, upgrade to an error. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 581 | if (Diag.ExtBehavior == DiagnosticsEngine::Ext_Error && |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame] | 582 | !MappingInfo.isUser() && |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 583 | isBuiltinExtensionDiag(DiagID)) |
| 584 | Result = DiagnosticIDs::Error; |
| 585 | |
| 586 | if (Diag.WarningsAsErrors) |
| 587 | Result = DiagnosticIDs::Error; |
| 588 | if (Result == DiagnosticIDs::Error && Diag.ErrorsAsFatal) |
| 589 | Result = DiagnosticIDs::Fatal; |
| 590 | break; |
| 591 | |
| 592 | case diag::MAP_WARNING_NO_WERROR: |
| 593 | // Diagnostics specified with -Wno-error=foo should be set to warnings, but |
| 594 | // not be adjusted by -Werror or -pedantic-errors. |
| 595 | Result = DiagnosticIDs::Warning; |
| 596 | |
| 597 | // If warnings are globally mapped to ignore or error, do it. |
| 598 | if (Diag.IgnoreAllWarnings) |
| 599 | return DiagnosticIDs::Ignored; |
| 600 | |
| 601 | break; |
| 602 | |
| 603 | case diag::MAP_ERROR_NO_WFATAL: |
| 604 | // Diagnostics specified as -Wno-fatal-error=foo should be errors, but |
| 605 | // unaffected by -Wfatal-errors. |
| 606 | Result = DiagnosticIDs::Error; |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | // Okay, we're about to return this as a "diagnostic to emit" one last check: |
| 611 | // if this is any sort of extension warning, and if we're in an __extension__ |
| 612 | // block, silence it. |
| 613 | if (Diag.AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID)) |
| 614 | return DiagnosticIDs::Ignored; |
| 615 | |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 616 | // If we are in a system header, we ignore it. |
| 617 | // We also want to ignore extensions and warnings in -Werror and |
| 618 | // -pedantic-errors modes, which *map* warnings/extensions to errors. |
| 619 | if (Result >= DiagnosticIDs::Warning && |
| 620 | DiagClass != CLASS_ERROR && |
| 621 | // Custom diagnostics always are emitted in system headers. |
| 622 | DiagID < diag::DIAG_UPPER_LIMIT && |
Argyrios Kyrtzidis | 144bc08 | 2011-04-21 23:08:23 +0000 | [diff] [blame] | 623 | !ShouldEmitInSystemHeader && |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 624 | Diag.SuppressSystemWarnings && |
| 625 | Loc.isValid() && |
| 626 | Diag.getSourceManager().isInSystemHeader( |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 627 | Diag.getSourceManager().getExpansionLoc(Loc))) |
Argyrios Kyrtzidis | cfdadfe | 2011-04-21 23:08:18 +0000 | [diff] [blame] | 628 | return DiagnosticIDs::Ignored; |
| 629 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 630 | return Result; |
| 631 | } |
| 632 | |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 633 | namespace { |
| 634 | struct WarningOption { |
| 635 | // Be safe with the size of 'NameLen' because we don't statically check if |
| 636 | // the size will fit in the field; the struct size won't decrease with a |
| 637 | // shorter type anyway. |
| 638 | size_t NameLen; |
| 639 | const char *NameStr; |
| 640 | const short *Members; |
| 641 | const short *SubGroups; |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 643 | StringRef getName() const { |
| 644 | return StringRef(NameStr, NameLen); |
Benjamin Kramer | dbda513 | 2011-06-13 18:38:45 +0000 | [diff] [blame] | 645 | } |
| 646 | }; |
| 647 | } |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 648 | |
| 649 | #define GET_DIAG_ARRAYS |
| 650 | #include "clang/Basic/DiagnosticGroups.inc" |
| 651 | #undef GET_DIAG_ARRAYS |
| 652 | |
| 653 | // Second the table of options, sorted by name for fast binary lookup. |
| 654 | static const WarningOption OptionTable[] = { |
| 655 | #define GET_DIAG_TABLE |
| 656 | #include "clang/Basic/DiagnosticGroups.inc" |
| 657 | #undef GET_DIAG_TABLE |
| 658 | }; |
| 659 | static const size_t OptionTableSize = |
| 660 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 661 | |
| 662 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 663 | const WarningOption &RHS) { |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 664 | return LHS.getName() < RHS.getName(); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 668 | SourceLocation Loc, DiagnosticsEngine &Diag) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 669 | // Option exists, poke all the members of its diagnostic set. |
| 670 | if (const short *Member = Group->Members) { |
| 671 | for (; *Member != -1; ++Member) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 672 | Diag.setDiagnosticMapping(*Member, Mapping, Loc); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // Enable/disable all subgroups along with this one. |
| 676 | if (const short *SubGroups = Group->SubGroups) { |
| 677 | for (; *SubGroups != (short)-1; ++SubGroups) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 678 | MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g. |
| 683 | /// "unknown-pragmas" to have the specified mapping. This returns true and |
| 684 | /// ignores the request if "Group" was unknown, false otherwise. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 685 | bool DiagnosticIDs::setDiagnosticGroupMapping(StringRef Group, |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 686 | diag::Mapping Map, |
| 687 | SourceLocation Loc, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 688 | DiagnosticsEngine &Diag) const { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 689 | assert((Loc.isValid() || |
| 690 | Diag.DiagStatePoints.empty() || |
| 691 | Diag.DiagStatePoints.back().Loc.isInvalid()) && |
| 692 | "Loc should be invalid only when the mapping comes from command-line"); |
| 693 | assert((Loc.isInvalid() || Diag.DiagStatePoints.empty() || |
| 694 | Diag.DiagStatePoints.back().Loc.isInvalid() || |
| 695 | !Diag.SourceMgr->isBeforeInTranslationUnit(Loc, |
| 696 | Diag.DiagStatePoints.back().Loc)) && |
| 697 | "Source location of new mapping is before the previous one!"); |
| 698 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 699 | WarningOption Key = { Group.size(), Group.data(), 0, 0 }; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 700 | const WarningOption *Found = |
| 701 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 702 | WarningOptionCompare); |
| 703 | if (Found == OptionTable + OptionTableSize || |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 704 | Found->getName() != Group) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 705 | return true; // Option not found. |
| 706 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 707 | MapGroupMembers(Found, Map, Loc, Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 708 | return false; |
| 709 | } |
| 710 | |
| 711 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 712 | /// finally fully formed. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 713 | bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const { |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 714 | Diagnostic Info(&Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 715 | |
| 716 | if (Diag.SuppressAllDiagnostics) |
| 717 | return false; |
| 718 | |
| 719 | assert(Diag.getClient() && "DiagnosticClient not set!"); |
| 720 | |
| 721 | // Figure out the diagnostic level of this message. |
| 722 | DiagnosticIDs::Level DiagLevel; |
| 723 | unsigned DiagID = Info.getID(); |
| 724 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 725 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 726 | // Handle custom diagnostics, which cannot be mapped. |
| 727 | DiagLevel = CustomDiagInfo->getLevel(DiagID); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 728 | } else { |
| 729 | // Get the class of the diagnostic. If this is a NOTE, map it onto whatever |
| 730 | // the diagnostic level was for the previous diagnostic so that it is |
| 731 | // filtered the same as the previous diagnostic. |
| 732 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
| 733 | if (DiagClass == CLASS_NOTE) { |
| 734 | DiagLevel = DiagnosticIDs::Note; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 735 | } else { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 736 | DiagLevel = getDiagnosticLevel(DiagID, DiagClass, Info.getLocation(), |
| 737 | Diag); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | if (DiagLevel != DiagnosticIDs::Note) { |
| 742 | // Record that a fatal error occurred only when we see a second |
| 743 | // non-note diagnostic. This allows notes to be attached to the |
| 744 | // fatal error, but suppresses any diagnostics that follow those |
| 745 | // notes. |
| 746 | if (Diag.LastDiagLevel == DiagnosticIDs::Fatal) |
| 747 | Diag.FatalErrorOccurred = true; |
| 748 | |
| 749 | Diag.LastDiagLevel = DiagLevel; |
| 750 | } |
| 751 | |
Argyrios Kyrtzidis | c0a575f | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 752 | // Update counts for DiagnosticErrorTrap even if a fatal error occurred. |
| 753 | if (DiagLevel >= DiagnosticIDs::Error) { |
| 754 | ++Diag.TrapNumErrorsOccurred; |
| 755 | if (isUnrecoverable(DiagID)) |
| 756 | ++Diag.TrapNumUnrecoverableErrorsOccurred; |
| 757 | } |
| 758 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 759 | // If a fatal error has already been emitted, silence all subsequent |
| 760 | // diagnostics. |
| 761 | if (Diag.FatalErrorOccurred) { |
| 762 | if (DiagLevel >= DiagnosticIDs::Error && |
| 763 | Diag.Client->IncludeInDiagnosticCounts()) { |
| 764 | ++Diag.NumErrors; |
| 765 | ++Diag.NumErrorsSuppressed; |
| 766 | } |
| 767 | |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | // If the client doesn't care about this message, don't issue it. If this is |
| 772 | // a note and the last real diagnostic was ignored, ignore it too. |
| 773 | if (DiagLevel == DiagnosticIDs::Ignored || |
| 774 | (DiagLevel == DiagnosticIDs::Note && |
| 775 | Diag.LastDiagLevel == DiagnosticIDs::Ignored)) |
| 776 | return false; |
| 777 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 778 | if (DiagLevel >= DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | c0a575f | 2011-07-29 01:25:44 +0000 | [diff] [blame] | 779 | if (isUnrecoverable(DiagID)) |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 780 | Diag.UnrecoverableErrorOccurred = true; |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 781 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 782 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
| 783 | Diag.ErrorOccurred = true; |
| 784 | ++Diag.NumErrors; |
| 785 | } |
| 786 | |
Douglas Gregor | f1d5948 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 787 | // If we've emitted a lot of errors, emit a fatal error instead of it to |
| 788 | // stop a flood of bogus errors. |
| 789 | if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit && |
| 790 | DiagLevel == DiagnosticIDs::Error) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 791 | Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors); |
Douglas Gregor | f1d5948 | 2011-08-17 19:13:00 +0000 | [diff] [blame] | 792 | return false; |
| 793 | } |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Douglas Gregor | 4814fb5 | 2011-02-03 23:41:12 +0000 | [diff] [blame] | 796 | // 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] | 797 | // source locations that aren't macro expansions. If any point into macro |
| 798 | // expansions, remove all of the Fix-Its. |
Douglas Gregor | 4814fb5 | 2011-02-03 23:41:12 +0000 | [diff] [blame] | 799 | for (unsigned I = 0, N = Diag.NumFixItHints; I != N; ++I) { |
| 800 | const FixItHint &FixIt = Diag.FixItHints[I]; |
| 801 | if (FixIt.RemoveRange.isInvalid() || |
| 802 | FixIt.RemoveRange.getBegin().isMacroID() || |
| 803 | FixIt.RemoveRange.getEnd().isMacroID()) { |
| 804 | Diag.NumFixItHints = 0; |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 809 | // Finally, report it. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 810 | Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 811 | if (Diag.Client->IncludeInDiagnosticCounts()) { |
| 812 | if (DiagLevel == DiagnosticIDs::Warning) |
| 813 | ++Diag.NumWarnings; |
| 814 | } |
| 815 | |
| 816 | Diag.CurDiagID = ~0U; |
| 817 | |
| 818 | return true; |
| 819 | } |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 820 | |
| 821 | bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const { |
| 822 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 823 | // Custom diagnostics. |
| 824 | return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error; |
| 825 | } |
| 826 | |
| 827 | // Only errors may be unrecoverable. |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 828 | if (getBuiltinDiagClass(DiagID) < CLASS_ERROR) |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 829 | return false; |
| 830 | |
| 831 | if (DiagID == diag::err_unavailable || |
| 832 | DiagID == diag::err_unavailable_message) |
| 833 | return false; |
| 834 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 835 | // Currently we consider all ARC errors as recoverable. |
| 836 | if (getCategoryNumberForDiag(DiagID) == |
| 837 | diag::DiagCat_Automatic_Reference_Counting_Issue) |
| 838 | return false; |
| 839 | |
John McCall | 923cd57 | 2011-06-15 21:46:43 +0000 | [diff] [blame] | 840 | return true; |
| 841 | } |