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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | #define DIAG(ENUM,FLAGS,DESC) FLAGS, |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 42 | static unsigned char DiagnosticFlagsCommon[] = { |
| 43 | #include "clang/Basic/DiagnosticCommonKinds.def" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | 0 |
| 45 | }; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 46 | static unsigned char DiagnosticFlagsLex[] = { |
| 47 | #include "clang/Basic/DiagnosticLexKinds.def" |
| 48 | 0 |
| 49 | }; |
| 50 | static unsigned char DiagnosticFlagsParse[] = { |
| 51 | #include "clang/Basic/DiagnosticParseKinds.def" |
| 52 | 0 |
| 53 | }; |
| 54 | static unsigned char DiagnosticFlagsAST[] = { |
| 55 | #include "clang/Basic/DiagnosticASTKinds.def" |
| 56 | 0 |
| 57 | }; |
| 58 | static unsigned char DiagnosticFlagsSema[] = { |
| 59 | #include "clang/Basic/DiagnosticSemaKinds.def" |
| 60 | 0 |
| 61 | }; |
| 62 | static unsigned char DiagnosticFlagsAnalysis[] = { |
| 63 | #include "clang/Basic/DiagnosticAnalysisKinds.def" |
| 64 | 0 |
| 65 | }; |
| 66 | #undef DIAG |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | |
| 68 | /// getDiagClass - Return the class field of the diagnostic. |
| 69 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 70 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 71 | assert(DiagID < diag::DIAG_UPPER_LIMIT && |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 72 | "Diagnostic ID out of range!"); |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 73 | unsigned res; |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 74 | if (DiagID < diag::DIAG_START_LEX) |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 75 | res = DiagnosticFlagsCommon[DiagID]; |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 76 | else if (DiagID < diag::DIAG_START_PARSE) |
| 77 | res = DiagnosticFlagsLex[DiagID - diag::DIAG_START_LEX - 1]; |
| 78 | else if (DiagID < diag::DIAG_START_AST) |
| 79 | res = DiagnosticFlagsParse[DiagID - diag::DIAG_START_PARSE - 1]; |
| 80 | else if (DiagID < diag::DIAG_START_SEMA) |
| 81 | res = DiagnosticFlagsAST[DiagID - diag::DIAG_START_AST - 1]; |
| 82 | else if (DiagID < diag::DIAG_START_ANALYSIS) |
| 83 | res = DiagnosticFlagsSema[DiagID - diag::DIAG_START_SEMA - 1]; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 84 | else |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 85 | res = DiagnosticFlagsAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1]; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 86 | return res & class_mask; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /// DiagnosticText - An english message to print for the diagnostic. These |
| 90 | /// should be localized. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | #define DIAG(ENUM,FLAGS,DESC) DESC, |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 92 | static const char * const DiagnosticTextCommon[] = { |
| 93 | #include "clang/Basic/DiagnosticCommonKinds.def" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | 0 |
| 95 | }; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 96 | static const char * const DiagnosticTextLex[] = { |
| 97 | #include "clang/Basic/DiagnosticLexKinds.def" |
| 98 | 0 |
| 99 | }; |
| 100 | static const char * const DiagnosticTextParse[] = { |
| 101 | #include "clang/Basic/DiagnosticParseKinds.def" |
| 102 | 0 |
| 103 | }; |
| 104 | static const char * const DiagnosticTextAST[] = { |
| 105 | #include "clang/Basic/DiagnosticASTKinds.def" |
| 106 | 0 |
| 107 | }; |
| 108 | static const char * const DiagnosticTextSema[] = { |
| 109 | #include "clang/Basic/DiagnosticSemaKinds.def" |
| 110 | 0 |
| 111 | }; |
| 112 | static const char * const DiagnosticTextAnalysis[] = { |
| 113 | #include "clang/Basic/DiagnosticAnalysisKinds.def" |
| 114 | 0 |
| 115 | }; |
| 116 | #undef DIAG |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 118 | //===----------------------------------------------------------------------===// |
| 119 | // Custom Diagnostic information |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | namespace clang { |
| 123 | namespace diag { |
| 124 | class CustomDiagInfo { |
| 125 | typedef std::pair<Diagnostic::Level, std::string> DiagDesc; |
| 126 | std::vector<DiagDesc> DiagInfo; |
| 127 | std::map<DiagDesc, unsigned> DiagIDs; |
| 128 | public: |
| 129 | |
| 130 | /// getDescription - Return the description of the specified custom |
| 131 | /// diagnostic. |
| 132 | const char *getDescription(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 133 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 134 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 135 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str(); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /// getLevel - Return the level of the specified custom diagnostic. |
| 139 | Diagnostic::Level getLevel(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 140 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 141 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 142 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 145 | unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message, |
| 146 | Diagnostic &Diags) { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 147 | DiagDesc D(L, Message); |
| 148 | // Check to see if it already exists. |
| 149 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 150 | if (I != DiagIDs.end() && I->first == D) |
| 151 | return I->second; |
| 152 | |
| 153 | // If not, assign a new ID. |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 154 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 155 | DiagIDs.insert(std::make_pair(D, ID)); |
| 156 | DiagInfo.push_back(D); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 157 | |
| 158 | // If this is a warning, and all warnings are supposed to map to errors, |
| 159 | // insert the mapping now. |
| 160 | if (L == Diagnostic::Warning && Diags.getWarningsAsErrors()) |
| 161 | Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 162 | return ID; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | } // end diag namespace |
| 167 | } // end clang namespace |
| 168 | |
| 169 | |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | // Common Diagnostic implementation |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 174 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 175 | const char *Modifier, unsigned ML, |
| 176 | const char *Argument, unsigned ArgLen, |
| 177 | llvm::SmallVectorImpl<char> &Output) { |
| 178 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 179 | Output.append(Str, Str+strlen(Str)); |
| 180 | } |
| 181 | |
| 182 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 183 | Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 184 | IgnoreAllWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | WarningsAsErrors = false; |
| 186 | WarnOnExtensions = false; |
| 187 | ErrorOnExtensions = false; |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 188 | SuppressSystemWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | // Clear all mappings, setting them to MAP_DEFAULT. |
| 190 | memset(DiagMappings, 0, sizeof(DiagMappings)); |
| 191 | |
| 192 | ErrorOccurred = false; |
| 193 | NumDiagnostics = 0; |
| 194 | NumErrors = 0; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 195 | CustomDiagInfo = 0; |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 196 | CurDiagID = ~0U; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 198 | ArgToStringFn = DummyArgToStringFn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 201 | Diagnostic::~Diagnostic() { |
| 202 | delete CustomDiagInfo; |
| 203 | } |
| 204 | |
| 205 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 206 | /// and level. If this is the first request for this diagnosic, it is |
| 207 | /// registered and created, otherwise the existing ID is returned. |
| 208 | unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) { |
| 209 | if (CustomDiagInfo == 0) |
| 210 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 211 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 215 | /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic |
| 216 | /// level of the specified diagnostic ID is a Note, Warning, or Extension. |
| 217 | /// Note that this only works on builtin diagnostics, not custom ones. |
| 218 | bool Diagnostic::isBuiltinNoteWarningOrExtension(unsigned DiagID) { |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 219 | return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) < ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
| 223 | /// getDescription - Given a diagnostic ID, return a description of the |
| 224 | /// issue. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 225 | const char *Diagnostic::getDescription(unsigned DiagID) const { |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 226 | if (DiagID < diag::DIAG_UPPER_LIMIT) { |
| 227 | if (DiagID < diag::DIAG_START_LEX) |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 228 | return DiagnosticTextCommon[DiagID]; |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 229 | else if (DiagID < diag::DIAG_START_PARSE) |
| 230 | return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1]; |
| 231 | else if (DiagID < diag::DIAG_START_AST) |
| 232 | return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1]; |
| 233 | else if (DiagID < diag::DIAG_START_SEMA) |
| 234 | return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1]; |
| 235 | else if (DiagID < diag::DIAG_START_ANALYSIS) |
| 236 | return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1]; |
| 237 | else if (DiagID < diag::DIAG_UPPER_LIMIT) |
| 238 | return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1]; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return CustomDiagInfo->getDescription(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 245 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 246 | /// the DiagnosticClient. |
| 247 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 248 | // Handle custom diagnostics, which cannot be mapped. |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 249 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 250 | return CustomDiagInfo->getLevel(DiagID); |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 251 | |
| 252 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | |
| 254 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 255 | // to error. |
| 256 | if (DiagClass < ERROR) { |
| 257 | switch (getDiagnosticMapping((diag::kind)DiagID)) { |
| 258 | case diag::MAP_DEFAULT: break; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 259 | case diag::MAP_IGNORE: return Diagnostic::Ignored; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | case diag::MAP_WARNING: DiagClass = WARNING; break; |
| 261 | case diag::MAP_ERROR: DiagClass = ERROR; break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Map diagnostic classes based on command line argument settings. |
| 266 | if (DiagClass == EXTENSION) { |
| 267 | if (ErrorOnExtensions) |
| 268 | DiagClass = ERROR; |
| 269 | else if (WarnOnExtensions) |
| 270 | DiagClass = WARNING; |
| 271 | else |
| 272 | return Ignored; |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 273 | } else if (DiagClass == EXTWARN) { |
| 274 | DiagClass = ErrorOnExtensions ? ERROR : WARNING; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 277 | // If warnings are globally mapped to ignore or error, do it. |
| 278 | if (DiagClass == WARNING) { |
| 279 | if (IgnoreAllWarnings) |
| 280 | return Diagnostic::Ignored; |
| 281 | if (WarningsAsErrors) |
| 282 | DiagClass = ERROR; |
| 283 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | |
| 285 | switch (DiagClass) { |
| 286 | default: assert(0 && "Unknown diagnostic class!"); |
| 287 | case NOTE: return Diagnostic::Note; |
| 288 | case WARNING: return Diagnostic::Warning; |
| 289 | case ERROR: return Diagnostic::Error; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 293 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 294 | /// finally fully formed. |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 295 | void Diagnostic::ProcessDiag() { |
| 296 | DiagnosticInfo Info(this); |
| 297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | // Figure out the diagnostic level of this message. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 299 | Diagnostic::Level DiagLevel = getDiagnosticLevel(Info.getID()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | |
| 301 | // If the client doesn't care about this message, don't issue it. |
| 302 | if (DiagLevel == Diagnostic::Ignored) |
| 303 | return; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 304 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 305 | // 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] | 306 | // 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] | 307 | // ignore extensions and warnings in -Werror and -pedantic-errors modes, |
| 308 | // which *map* warnings/extensions to errors. |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 309 | if (SuppressSystemWarnings && |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 310 | Info.getID() < diag::DIAG_UPPER_LIMIT && |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 311 | getBuiltinDiagClass(Info.getID()) != ERROR && |
| 312 | Info.getLocation().isValid() && |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 313 | Info.getLocation().getSpellingLoc().isInSystemHeader()) |
Chris Lattner | 7097d91 | 2008-02-03 09:00:04 +0000 | [diff] [blame] | 314 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | |
| 316 | if (DiagLevel >= Diagnostic::Error) { |
| 317 | ErrorOccurred = true; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 319 | ++NumErrors; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | // Finally, report it. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 323 | Client->HandleDiagnostic(DiagLevel, Info); |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 324 | if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 329 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 330 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 331 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 332 | template <std::size_t StrLen> |
| 333 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 334 | const char (&Str)[StrLen]) { |
| 335 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 336 | } |
| 337 | |
| 338 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 339 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 340 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 341 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 342 | /// This is very useful for certain classes of variant diagnostics. |
| 343 | static void HandleSelectModifier(unsigned ValNo, |
| 344 | const char *Argument, unsigned ArgumentLen, |
| 345 | llvm::SmallVectorImpl<char> &OutStr) { |
| 346 | const char *ArgumentEnd = Argument+ArgumentLen; |
| 347 | |
| 348 | // Skip over 'ValNo' |'s. |
| 349 | while (ValNo) { |
| 350 | const char *NextVal = std::find(Argument, ArgumentEnd, '|'); |
| 351 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 352 | " larger than the number of options in the diagnostic string!"); |
| 353 | Argument = NextVal+1; // Skip this string. |
| 354 | --ValNo; |
| 355 | } |
| 356 | |
| 357 | // Get the end of the value. This is either the } or the |. |
| 358 | const char *EndPtr = std::find(Argument, ArgumentEnd, '|'); |
| 359 | // Add the value to the output string. |
| 360 | OutStr.append(Argument, EndPtr); |
| 361 | } |
| 362 | |
| 363 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 364 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 365 | /// this: "you idiot, you have %4 parameter%s4!". |
| 366 | static void HandleIntegerSModifier(unsigned ValNo, |
| 367 | llvm::SmallVectorImpl<char> &OutStr) { |
| 368 | if (ValNo != 1) |
| 369 | OutStr.push_back('s'); |
| 370 | } |
| 371 | |
| 372 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 373 | /// PluralNumber - Parse an unsigned integer and advance Start. |
| 374 | static unsigned PluralNumber(const char *&Start, const char *End) |
| 375 | { |
| 376 | // Programming 101: Parse a decimal number :-) |
| 377 | unsigned Val = 0; |
| 378 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 379 | Val *= 10; |
| 380 | Val += *Start - '0'; |
| 381 | ++Start; |
| 382 | } |
| 383 | return Val; |
| 384 | } |
| 385 | |
| 386 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
| 387 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) |
| 388 | { |
| 389 | if (*Start != '[') { |
| 390 | unsigned Ref = PluralNumber(Start, End); |
| 391 | return Ref == Val; |
| 392 | } |
| 393 | |
| 394 | ++Start; |
| 395 | unsigned Low = PluralNumber(Start, End); |
| 396 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 397 | ++Start; |
| 398 | unsigned High = PluralNumber(Start, End); |
| 399 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 400 | ++Start; |
| 401 | return Low <= Val && Val <= High; |
| 402 | } |
| 403 | |
| 404 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
| 405 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) |
| 406 | { |
| 407 | // Empty condition? |
| 408 | if (*Start == ':') |
| 409 | return true; |
| 410 | |
| 411 | while (1) { |
| 412 | char C = *Start; |
| 413 | if (C == '%') { |
| 414 | // Modulo expression |
| 415 | ++Start; |
| 416 | unsigned Arg = PluralNumber(Start, End); |
| 417 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 418 | ++Start; |
| 419 | unsigned ValMod = ValNo % Arg; |
| 420 | if (TestPluralRange(ValMod, Start, End)) |
| 421 | return true; |
| 422 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 423 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 424 | "Bad plural expression syntax: unexpected character"); |
| 425 | // Range expression |
| 426 | if (TestPluralRange(ValNo, Start, End)) |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | // Scan for next or-expr part. |
| 431 | Start = std::find(Start, End, ','); |
| 432 | if(Start == End) |
| 433 | break; |
| 434 | ++Start; |
| 435 | } |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 440 | /// for complex plural forms, or in languages where all plurals are complex. |
| 441 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 442 | /// conditions that are tested in order, the form corresponding to the first |
| 443 | /// that applies being emitted. The empty condition is always true, making the |
| 444 | /// last form a default case. |
| 445 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 446 | /// Here are the rules. |
| 447 | /// condition := expression | empty |
| 448 | /// empty := -> always true |
| 449 | /// expression := numeric [',' expression] -> logical or |
| 450 | /// numeric := range -> true if n in range |
| 451 | /// | '%' number '=' range -> true if n % number in range |
| 452 | /// range := number |
| 453 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 454 | /// |
| 455 | /// Here are some examples from the GNU gettext manual written in this form: |
| 456 | /// English: |
| 457 | /// {1:form0|:form1} |
| 458 | /// Latvian: |
| 459 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 460 | /// Gaeilge: |
| 461 | /// {1:form0|2:form1|:form2} |
| 462 | /// Romanian: |
| 463 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 464 | /// Lithuanian: |
| 465 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 466 | /// Russian (requires repeated form): |
| 467 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 468 | /// Slovak |
| 469 | /// {1:form0|[2,4]:form1|:form2} |
| 470 | /// Polish (requires repeated form): |
| 471 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
| 472 | static void HandlePluralModifier(unsigned ValNo, |
| 473 | const char *Argument, unsigned ArgumentLen, |
| 474 | llvm::SmallVectorImpl<char> &OutStr) |
| 475 | { |
| 476 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 477 | while (1) { |
| 478 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 479 | const char *ExprEnd = Argument; |
| 480 | while (*ExprEnd != ':') { |
| 481 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 482 | ++ExprEnd; |
| 483 | } |
| 484 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 485 | Argument = ExprEnd + 1; |
| 486 | ExprEnd = std::find(Argument, ArgumentEnd, '|'); |
| 487 | OutStr.append(Argument, ExprEnd); |
| 488 | return; |
| 489 | } |
| 490 | Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 495 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 496 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 497 | /// array. |
| 498 | void DiagnosticInfo:: |
| 499 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
| 500 | const char *DiagStr = getDiags()->getDescription(getID()); |
| 501 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 502 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 503 | while (DiagStr != DiagEnd) { |
| 504 | if (DiagStr[0] != '%') { |
| 505 | // Append non-%0 substrings to Str if we have one. |
| 506 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 507 | OutStr.append(DiagStr, StrEnd); |
| 508 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 509 | continue; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 510 | } else if (DiagStr[1] == '%') { |
| 511 | OutStr.push_back('%'); // %% -> %. |
| 512 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 513 | continue; |
| 514 | } |
| 515 | |
| 516 | // Skip the %. |
| 517 | ++DiagStr; |
| 518 | |
| 519 | // This must be a placeholder for a diagnostic argument. The format for a |
| 520 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 521 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 522 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 523 | // brace enclosed string. |
| 524 | const char *Modifier = 0, *Argument = 0; |
| 525 | unsigned ModifierLen = 0, ArgumentLen = 0; |
| 526 | |
| 527 | // Check to see if we have a modifier. If so eat it. |
| 528 | if (!isdigit(DiagStr[0])) { |
| 529 | Modifier = DiagStr; |
| 530 | while (DiagStr[0] == '-' || |
| 531 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 532 | ++DiagStr; |
| 533 | ModifierLen = DiagStr-Modifier; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 534 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 535 | // If we have an argument, get it next. |
| 536 | if (DiagStr[0] == '{') { |
| 537 | ++DiagStr; // Skip {. |
| 538 | Argument = DiagStr; |
| 539 | |
| 540 | for (; DiagStr[0] != '}'; ++DiagStr) |
| 541 | assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!"); |
| 542 | ArgumentLen = DiagStr-Argument; |
| 543 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 548 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 550 | switch (getArgKind(ArgNo)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 551 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 552 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 553 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 554 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 555 | OutStr.append(S.begin(), S.end()); |
| 556 | break; |
| 557 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 558 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 559 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 560 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 561 | OutStr.append(S, S + strlen(S)); |
| 562 | break; |
| 563 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 564 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 565 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 566 | int Val = getArgSInt(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 567 | |
| 568 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 569 | HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
| 570 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 571 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 572 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 573 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 574 | } else { |
| 575 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 576 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 577 | std::string S = llvm::itostr(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 578 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 579 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 580 | break; |
| 581 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 582 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 583 | unsigned Val = getArgUInt(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 584 | |
| 585 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 586 | HandleSelectModifier(Val, Argument, ArgumentLen, OutStr); |
| 587 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 588 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 589 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 590 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 591 | } else { |
| 592 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
| 593 | |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 594 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 595 | std::string S = llvm::utostr_32(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 596 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 597 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 598 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 599 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 600 | // ---- NAMES and TYPES ---- |
| 601 | case Diagnostic::ak_identifierinfo: { |
| 602 | OutStr.push_back('\''); |
| 603 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 604 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 605 | OutStr.append(II->getName(), II->getName() + II->getLength()); |
| 606 | OutStr.push_back('\''); |
| 607 | break; |
| 608 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 609 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 610 | case Diagnostic::ak_declarationname: |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 611 | case Diagnostic::ak_nameddecl: |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 612 | OutStr.push_back('\''); |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 613 | getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo), |
| 614 | Modifier, ModifierLen, |
| 615 | Argument, ArgumentLen, OutStr); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 616 | OutStr.push_back('\''); |
| 617 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 620 | } |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 621 | |
| 622 | /// IncludeInDiagnosticCounts - This method (whose default implementation |
| 623 | /// returns true) indicates whether the diagnostics handled by this |
| 624 | /// DiagnosticClient should be included in the number of diagnostics |
| 625 | /// reported by Diagnostic. |
| 626 | bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } |