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