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