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