Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame^] | 15 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | #include <map> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 21 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Builtin Diagnostic information |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | /// Flag values for diagnostics. |
| 29 | enum { |
| 30 | // Diagnostic classes. |
| 31 | NOTE = 0x01, |
| 32 | WARNING = 0x02, |
| 33 | EXTENSION = 0x03, |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 34 | EXTWARN = 0x04, |
| 35 | ERROR = 0x05, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | class_mask = 0x07 |
| 37 | }; |
| 38 | |
| 39 | /// DiagnosticFlags - A set of flags, or'd together, that describe the |
| 40 | /// diagnostic. |
| 41 | static unsigned char DiagnosticFlags[] = { |
| 42 | #define DIAG(ENUM,FLAGS,DESC) FLAGS, |
| 43 | #include "clang/Basic/DiagnosticKinds.def" |
| 44 | 0 |
| 45 | }; |
| 46 | |
| 47 | /// getDiagClass - Return the class field of the diagnostic. |
| 48 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 49 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 50 | assert(DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 51 | "Diagnostic ID out of range!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | return DiagnosticFlags[DiagID] & class_mask; |
| 53 | } |
| 54 | |
| 55 | /// DiagnosticText - An english message to print for the diagnostic. These |
| 56 | /// should be localized. |
| 57 | static const char * const DiagnosticText[] = { |
| 58 | #define DIAG(ENUM,FLAGS,DESC) DESC, |
| 59 | #include "clang/Basic/DiagnosticKinds.def" |
| 60 | 0 |
| 61 | }; |
| 62 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 63 | //===----------------------------------------------------------------------===// |
| 64 | // Custom Diagnostic information |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
| 67 | namespace clang { |
| 68 | namespace diag { |
| 69 | class CustomDiagInfo { |
| 70 | typedef std::pair<Diagnostic::Level, std::string> DiagDesc; |
| 71 | std::vector<DiagDesc> DiagInfo; |
| 72 | std::map<DiagDesc, unsigned> DiagIDs; |
| 73 | public: |
| 74 | |
| 75 | /// getDescription - Return the description of the specified custom |
| 76 | /// diagnostic. |
| 77 | const char *getDescription(unsigned DiagID) const { |
| 78 | assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() && |
| 79 | "Invalid diagnosic ID"); |
| 80 | return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].second.c_str(); |
| 81 | } |
| 82 | |
| 83 | /// getLevel - Return the level of the specified custom diagnostic. |
| 84 | Diagnostic::Level getLevel(unsigned DiagID) const { |
| 85 | assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() && |
| 86 | "Invalid diagnosic ID"); |
| 87 | return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].first; |
| 88 | } |
| 89 | |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 90 | unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message, |
| 91 | Diagnostic &Diags) { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 92 | DiagDesc D(L, Message); |
| 93 | // Check to see if it already exists. |
| 94 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 95 | if (I != DiagIDs.end() && I->first == D) |
| 96 | return I->second; |
| 97 | |
| 98 | // If not, assign a new ID. |
| 99 | unsigned ID = DiagInfo.size()+diag::NUM_BUILTIN_DIAGNOSTICS; |
| 100 | DiagIDs.insert(std::make_pair(D, ID)); |
| 101 | DiagInfo.push_back(D); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 102 | |
| 103 | // If this is a warning, and all warnings are supposed to map to errors, |
| 104 | // insert the mapping now. |
| 105 | if (L == Diagnostic::Warning && Diags.getWarningsAsErrors()) |
| 106 | Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 107 | return ID; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | } // end diag namespace |
| 112 | } // end clang namespace |
| 113 | |
| 114 | |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | // Common Diagnostic implementation |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 119 | Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 120 | IgnoreAllWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | WarningsAsErrors = false; |
| 122 | WarnOnExtensions = false; |
| 123 | ErrorOnExtensions = false; |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 124 | SuppressSystemWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | // Clear all mappings, setting them to MAP_DEFAULT. |
| 126 | memset(DiagMappings, 0, sizeof(DiagMappings)); |
| 127 | |
| 128 | ErrorOccurred = false; |
| 129 | NumDiagnostics = 0; |
| 130 | NumErrors = 0; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 131 | CustomDiagInfo = 0; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 132 | NumDiagArgs = -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 135 | Diagnostic::~Diagnostic() { |
| 136 | delete CustomDiagInfo; |
| 137 | } |
| 138 | |
| 139 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 140 | /// and level. If this is the first request for this diagnosic, it is |
| 141 | /// registered and created, otherwise the existing ID is returned. |
| 142 | unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) { |
| 143 | if (CustomDiagInfo == 0) |
| 144 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 145 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 149 | /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic |
| 150 | /// level of the specified diagnostic ID is a Note, Warning, or Extension. |
| 151 | /// Note that this only works on builtin diagnostics, not custom ones. |
| 152 | bool Diagnostic::isBuiltinNoteWarningOrExtension(unsigned DiagID) { |
| 153 | return DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 154 | getBuiltinDiagClass(DiagID) < ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | /// getDescription - Given a diagnostic ID, return a description of the |
| 159 | /// issue. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 160 | const char *Diagnostic::getDescription(unsigned DiagID) const { |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 161 | if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS) |
| 162 | return DiagnosticText[DiagID]; |
| 163 | else |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 164 | return CustomDiagInfo->getDescription(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 168 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 169 | /// the DiagnosticClient. |
| 170 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 171 | // Handle custom diagnostics, which cannot be mapped. |
| 172 | if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS) |
| 173 | return CustomDiagInfo->getLevel(DiagID); |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 174 | |
| 175 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | |
| 177 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 178 | // to error. |
| 179 | if (DiagClass < ERROR) { |
| 180 | switch (getDiagnosticMapping((diag::kind)DiagID)) { |
| 181 | case diag::MAP_DEFAULT: break; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 182 | case diag::MAP_IGNORE: return Diagnostic::Ignored; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | case diag::MAP_WARNING: DiagClass = WARNING; break; |
| 184 | case diag::MAP_ERROR: DiagClass = ERROR; break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Map diagnostic classes based on command line argument settings. |
| 189 | if (DiagClass == EXTENSION) { |
| 190 | if (ErrorOnExtensions) |
| 191 | DiagClass = ERROR; |
| 192 | else if (WarnOnExtensions) |
| 193 | DiagClass = WARNING; |
| 194 | else |
| 195 | return Ignored; |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 196 | } else if (DiagClass == EXTWARN) { |
| 197 | DiagClass = ErrorOnExtensions ? ERROR : WARNING; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 200 | // If warnings are globally mapped to ignore or error, do it. |
| 201 | if (DiagClass == WARNING) { |
| 202 | if (IgnoreAllWarnings) |
| 203 | return Diagnostic::Ignored; |
| 204 | if (WarningsAsErrors) |
| 205 | DiagClass = ERROR; |
| 206 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | |
| 208 | switch (DiagClass) { |
| 209 | default: assert(0 && "Unknown diagnostic class!"); |
| 210 | case NOTE: return Diagnostic::Note; |
| 211 | case WARNING: return Diagnostic::Warning; |
| 212 | case ERROR: return Diagnostic::Error; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 216 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 217 | /// finally fully formed. |
| 218 | void Diagnostic::ProcessDiag(const DiagnosticInfo &Info) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | // Figure out the diagnostic level of this message. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 220 | Diagnostic::Level DiagLevel = getDiagnosticLevel(Info.getID()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | |
| 222 | // If the client doesn't care about this message, don't issue it. |
| 223 | if (DiagLevel == Diagnostic::Ignored) |
| 224 | return; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 225 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 226 | // If this is not an error and we are in a system header, ignore it. We |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 227 | // have to check on the original Diag ID here, because we also want to |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 228 | // ignore extensions and warnings in -Werror and -pedantic-errors modes, |
| 229 | // which *map* warnings/extensions to errors. |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 230 | if (SuppressSystemWarnings && |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 231 | Info.getID() < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 232 | getBuiltinDiagClass(Info.getID()) != ERROR && |
| 233 | Info.getLocation().isValid() && |
| 234 | Info.getLocation().getPhysicalLoc().isInSystemHeader()) |
Chris Lattner | 7097d91 | 2008-02-03 09:00:04 +0000 | [diff] [blame] | 235 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | |
| 237 | if (DiagLevel >= Diagnostic::Error) { |
| 238 | ErrorOccurred = true; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 240 | ++NumErrors; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | // Finally, report it. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 244 | Client->HandleDiagnostic(DiagLevel, Info); |
| 245 | ++NumDiagnostics; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 248 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 250 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 251 | |
| 252 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 253 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 254 | /// array. |
| 255 | void DiagnosticInfo:: |
| 256 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
| 257 | const char *DiagStr = getDiags()->getDescription(getID()); |
| 258 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 259 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 260 | while (DiagStr != DiagEnd) { |
| 261 | if (DiagStr[0] != '%') { |
| 262 | // Append non-%0 substrings to Str if we have one. |
| 263 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 264 | OutStr.append(DiagStr, StrEnd); |
| 265 | DiagStr = StrEnd; |
| 266 | } else if (DiagStr[1] == '%') { |
| 267 | OutStr.push_back('%'); // %% -> %. |
| 268 | DiagStr += 2; |
| 269 | } else { |
| 270 | assert(isdigit(DiagStr[1]) && "Must escape % with %%"); |
| 271 | unsigned StrNo = DiagStr[1] - '0'; |
| 272 | |
| 273 | switch (getArgKind(StrNo)) { |
| 274 | case DiagnosticInfo::ak_std_string: { |
| 275 | const std::string &S = getArgStdStr(StrNo); |
| 276 | OutStr.append(S.begin(), S.end()); |
| 277 | break; |
| 278 | } |
| 279 | case DiagnosticInfo::ak_c_string: { |
| 280 | const char *S = getArgCStr(StrNo); |
| 281 | OutStr.append(S, S + strlen(S)); |
| 282 | break; |
| 283 | } |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 284 | case DiagnosticInfo::ak_sint: { |
| 285 | // FIXME: Optimize |
| 286 | std::string S = llvm::itostr(getArgSInt(StrNo)); |
| 287 | OutStr.append(S.begin(), S.end()); |
| 288 | break; |
| 289 | } |
| 290 | case DiagnosticInfo::ak_uint: { |
| 291 | // FIXME: Optimize |
| 292 | std::string S = llvm::utostr_32(getArgUInt(StrNo)); |
| 293 | OutStr.append(S.begin(), S.end()); |
| 294 | break; |
| 295 | } |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame^] | 296 | case DiagnosticInfo::ak_identifierinfo: { |
| 297 | const IdentifierInfo *II = getArgIdentifier(StrNo); |
| 298 | OutStr.append(II->getName(), II->getName() + II->getLength()); |
| 299 | break; |
| 300 | } |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 301 | } |
| 302 | DiagStr += 2; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 305 | } |