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