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