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 | |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 16 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 21 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 22 | const char *Modifier, unsigned ML, |
| 23 | const char *Argument, unsigned ArgLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 24 | const Diagnostic::ArgumentValue *PrevArgs, |
| 25 | unsigned NumPrevArgs, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 26 | llvm::SmallVectorImpl<char> &Output, |
| 27 | void *Cookie) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 28 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 29 | Output.append(Str, Str+strlen(Str)); |
| 30 | } |
| 31 | |
| 32 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 33 | Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags, |
| 34 | DiagnosticClient *client, bool ShouldOwnClient) |
| 35 | : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient), |
| 36 | SourceMgr(0) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 37 | ArgToStringFn = DummyArgToStringFn; |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 38 | ArgToStringCookie = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 40 | AllExtensionsSilenced = 0; |
| 41 | IgnoreAllWarnings = false; |
| 42 | WarningsAsErrors = false; |
| 43 | ErrorsAsFatal = false; |
| 44 | SuppressSystemWarnings = false; |
| 45 | SuppressAllDiagnostics = false; |
| 46 | ShowOverloads = Ovl_All; |
| 47 | ExtBehavior = Ext_Ignore; |
| 48 | |
| 49 | ErrorLimit = 0; |
| 50 | TemplateBacktraceLimit = 0; |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 51 | |
| 52 | // Set all mappings to 'unset'. |
| 53 | DiagMappingsStack.clear(); |
| 54 | DiagMappingsStack.push_back(DiagMappings()); |
| 55 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 56 | Reset(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 59 | Diagnostic::~Diagnostic() { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 60 | if (OwnsDiagClient) |
| 61 | delete Client; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 64 | |
| 65 | void Diagnostic::pushMappings() { |
John Thompson | ca2c3e2 | 2009-10-23 02:21:17 +0000 | [diff] [blame] | 66 | // Avoids undefined behavior when the stack has to resize. |
| 67 | DiagMappingsStack.reserve(DiagMappingsStack.size() + 1); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 68 | DiagMappingsStack.push_back(DiagMappingsStack.back()); |
| 69 | } |
| 70 | |
| 71 | bool Diagnostic::popMappings() { |
| 72 | if (DiagMappingsStack.size() == 1) |
| 73 | return false; |
| 74 | |
| 75 | DiagMappingsStack.pop_back(); |
| 76 | return true; |
| 77 | } |
| 78 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 79 | void Diagnostic::Reset() { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 80 | ErrorOccurred = false; |
| 81 | FatalErrorOccurred = false; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 82 | |
| 83 | NumWarnings = 0; |
| 84 | NumErrors = 0; |
| 85 | NumErrorsSuppressed = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 86 | CurDiagID = ~0U; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 87 | // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes |
| 88 | // using a Diagnostic associated to a translation unit that follow |
| 89 | // diagnostics from a Diagnostic associated to anoter t.u. will not be |
| 90 | // displayed. |
| 91 | LastDiagLevel = (DiagnosticIDs::Level)-1; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 92 | DelayedDiagID = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 93 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 95 | void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1, |
| 96 | llvm::StringRef Arg2) { |
| 97 | if (DelayedDiagID) |
| 98 | return; |
| 99 | |
| 100 | DelayedDiagID = DiagID; |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 101 | DelayedDiagArg1 = Arg1.str(); |
| 102 | DelayedDiagArg2 = Arg2.str(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void Diagnostic::ReportDelayed() { |
| 106 | Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2; |
| 107 | DelayedDiagID = 0; |
| 108 | DelayedDiagArg1.clear(); |
| 109 | DelayedDiagArg2.clear(); |
| 110 | } |
| 111 | |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 112 | void DiagnosticBuilder::FlushCounts() { |
| 113 | DiagObj->NumDiagArgs = NumArgs; |
| 114 | DiagObj->NumDiagRanges = NumRanges; |
| 115 | DiagObj->NumFixItHints = NumFixItHints; |
| 116 | } |
| 117 | |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 118 | bool DiagnosticBuilder::Emit() { |
| 119 | // If DiagObj is null, then its soul was stolen by the copy ctor |
| 120 | // or the user called Emit(). |
| 121 | if (DiagObj == 0) return false; |
| 122 | |
| 123 | // When emitting diagnostics, we set the final argument count into |
| 124 | // the Diagnostic object. |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 125 | FlushCounts(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 126 | |
| 127 | // Process the diagnostic, sending the accumulated information to the |
| 128 | // DiagnosticClient. |
| 129 | bool Emitted = DiagObj->ProcessDiag(); |
| 130 | |
| 131 | // Clear out the current diagnostic object. |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 132 | unsigned DiagID = DiagObj->CurDiagID; |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 133 | DiagObj->Clear(); |
| 134 | |
| 135 | // If there was a delayed diagnostic, emit it now. |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 136 | if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID) |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 137 | DiagObj->ReportDelayed(); |
| 138 | |
| 139 | // This diagnostic is dead. |
| 140 | DiagObj = 0; |
| 141 | |
| 142 | return Emitted; |
| 143 | } |
| 144 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 145 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 147 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 148 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 149 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 150 | template <std::size_t StrLen> |
| 151 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 152 | const char (&Str)[StrLen]) { |
| 153 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 154 | } |
| 155 | |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 156 | /// ScanForward - Scans forward, looking for the given character, skipping |
| 157 | /// nested clauses and escaped characters. |
| 158 | static const char *ScanFormat(const char *I, const char *E, char Target) { |
| 159 | unsigned Depth = 0; |
| 160 | |
| 161 | for ( ; I != E; ++I) { |
| 162 | if (Depth == 0 && *I == Target) return I; |
| 163 | if (Depth != 0 && *I == '}') Depth--; |
| 164 | |
| 165 | if (*I == '%') { |
| 166 | I++; |
| 167 | if (I == E) break; |
| 168 | |
| 169 | // Escaped characters get implicitly skipped here. |
| 170 | |
| 171 | // Format specifier. |
| 172 | if (!isdigit(*I) && !ispunct(*I)) { |
| 173 | for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ; |
| 174 | if (I == E) break; |
| 175 | if (*I == '{') |
| 176 | Depth++; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return E; |
| 181 | } |
| 182 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 183 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 184 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 185 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 186 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 187 | /// This is very useful for certain classes of variant diagnostics. |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 188 | static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo, |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 189 | const char *Argument, unsigned ArgumentLen, |
| 190 | llvm::SmallVectorImpl<char> &OutStr) { |
| 191 | const char *ArgumentEnd = Argument+ArgumentLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 193 | // Skip over 'ValNo' |'s. |
| 194 | while (ValNo) { |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 195 | const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 196 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 197 | " larger than the number of options in the diagnostic string!"); |
| 198 | Argument = NextVal+1; // Skip this string. |
| 199 | --ValNo; |
| 200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 202 | // Get the end of the value. This is either the } or the |. |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 203 | const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 204 | |
| 205 | // Recursively format the result of the select clause into the output string. |
| 206 | DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 210 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 211 | /// this: "you idiot, you have %4 parameter%s4!". |
| 212 | static void HandleIntegerSModifier(unsigned ValNo, |
| 213 | llvm::SmallVectorImpl<char> &OutStr) { |
| 214 | if (ValNo != 1) |
| 215 | OutStr.push_back('s'); |
| 216 | } |
| 217 | |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 218 | /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This |
| 219 | /// prints the ordinal form of the given integer, with 1 corresponding |
| 220 | /// to the first ordinal. Currently this is hard-coded to use the |
| 221 | /// English form. |
| 222 | static void HandleOrdinalModifier(unsigned ValNo, |
| 223 | llvm::SmallVectorImpl<char> &OutStr) { |
| 224 | assert(ValNo != 0 && "ValNo must be strictly positive!"); |
| 225 | |
| 226 | llvm::raw_svector_ostream Out(OutStr); |
| 227 | |
| 228 | // We could use text forms for the first N ordinals, but the numeric |
| 229 | // forms are actually nicer in diagnostics because they stand out. |
| 230 | Out << ValNo; |
| 231 | |
| 232 | // It is critically important that we do this perfectly for |
| 233 | // user-written sequences with over 100 elements. |
| 234 | switch (ValNo % 100) { |
| 235 | case 11: |
| 236 | case 12: |
| 237 | case 13: |
| 238 | Out << "th"; return; |
| 239 | default: |
| 240 | switch (ValNo % 10) { |
| 241 | case 1: Out << "st"; return; |
| 242 | case 2: Out << "nd"; return; |
| 243 | case 3: Out << "rd"; return; |
| 244 | default: Out << "th"; return; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 249 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 250 | /// PluralNumber - Parse an unsigned integer and advance Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 251 | static unsigned PluralNumber(const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 252 | // Programming 101: Parse a decimal number :-) |
| 253 | unsigned Val = 0; |
| 254 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 255 | Val *= 10; |
| 256 | Val += *Start - '0'; |
| 257 | ++Start; |
| 258 | } |
| 259 | return Val; |
| 260 | } |
| 261 | |
| 262 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 263 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 264 | if (*Start != '[') { |
| 265 | unsigned Ref = PluralNumber(Start, End); |
| 266 | return Ref == Val; |
| 267 | } |
| 268 | |
| 269 | ++Start; |
| 270 | unsigned Low = PluralNumber(Start, End); |
| 271 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 272 | ++Start; |
| 273 | unsigned High = PluralNumber(Start, End); |
| 274 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 275 | ++Start; |
| 276 | return Low <= Val && Val <= High; |
| 277 | } |
| 278 | |
| 279 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 280 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 281 | // Empty condition? |
| 282 | if (*Start == ':') |
| 283 | return true; |
| 284 | |
| 285 | while (1) { |
| 286 | char C = *Start; |
| 287 | if (C == '%') { |
| 288 | // Modulo expression |
| 289 | ++Start; |
| 290 | unsigned Arg = PluralNumber(Start, End); |
| 291 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 292 | ++Start; |
| 293 | unsigned ValMod = ValNo % Arg; |
| 294 | if (TestPluralRange(ValMod, Start, End)) |
| 295 | return true; |
| 296 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 297 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 298 | "Bad plural expression syntax: unexpected character"); |
| 299 | // Range expression |
| 300 | if (TestPluralRange(ValNo, Start, End)) |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | // Scan for next or-expr part. |
| 305 | Start = std::find(Start, End, ','); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | if (Start == End) |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 307 | break; |
| 308 | ++Start; |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 314 | /// for complex plural forms, or in languages where all plurals are complex. |
| 315 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 316 | /// conditions that are tested in order, the form corresponding to the first |
| 317 | /// that applies being emitted. The empty condition is always true, making the |
| 318 | /// last form a default case. |
| 319 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 320 | /// Here are the rules. |
| 321 | /// condition := expression | empty |
| 322 | /// empty := -> always true |
| 323 | /// expression := numeric [',' expression] -> logical or |
| 324 | /// numeric := range -> true if n in range |
| 325 | /// | '%' number '=' range -> true if n % number in range |
| 326 | /// range := number |
| 327 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 328 | /// |
| 329 | /// Here are some examples from the GNU gettext manual written in this form: |
| 330 | /// English: |
| 331 | /// {1:form0|:form1} |
| 332 | /// Latvian: |
| 333 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 334 | /// Gaeilge: |
| 335 | /// {1:form0|2:form1|:form2} |
| 336 | /// Romanian: |
| 337 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 338 | /// Lithuanian: |
| 339 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 340 | /// Russian (requires repeated form): |
| 341 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 342 | /// Slovak |
| 343 | /// {1:form0|[2,4]:form1|:form2} |
| 344 | /// Polish (requires repeated form): |
| 345 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 346 | static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo, |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 347 | const char *Argument, unsigned ArgumentLen, |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 348 | llvm::SmallVectorImpl<char> &OutStr) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 349 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 350 | while (1) { |
| 351 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 352 | const char *ExprEnd = Argument; |
| 353 | while (*ExprEnd != ':') { |
| 354 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 355 | ++ExprEnd; |
| 356 | } |
| 357 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 358 | Argument = ExprEnd + 1; |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 359 | ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 360 | |
| 361 | // Recursively format the result of the plural clause into the |
| 362 | // output string. |
| 363 | DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 366 | Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 371 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 372 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 373 | /// array. |
| 374 | void DiagnosticInfo:: |
| 375 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 376 | const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID()); |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 377 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 379 | FormatDiagnostic(DiagStr, DiagEnd, OutStr); |
| 380 | } |
| 381 | |
| 382 | void DiagnosticInfo:: |
| 383 | FormatDiagnostic(const char *DiagStr, const char *DiagEnd, |
| 384 | llvm::SmallVectorImpl<char> &OutStr) const { |
| 385 | |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 386 | /// FormattedArgs - Keep track of all of the arguments formatted by |
| 387 | /// ConvertArgToString and pass them into subsequent calls to |
| 388 | /// ConvertArgToString, allowing the implementation to avoid redundancies in |
| 389 | /// obvious cases. |
| 390 | llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs; |
| 391 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 392 | while (DiagStr != DiagEnd) { |
| 393 | if (DiagStr[0] != '%') { |
| 394 | // Append non-%0 substrings to Str if we have one. |
| 395 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 396 | OutStr.append(DiagStr, StrEnd); |
| 397 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 398 | continue; |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 399 | } else if (ispunct(DiagStr[1])) { |
| 400 | OutStr.push_back(DiagStr[1]); // %% -> %. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 401 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 402 | continue; |
| 403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 405 | // Skip the %. |
| 406 | ++DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 408 | // This must be a placeholder for a diagnostic argument. The format for a |
| 409 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 410 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 411 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 412 | // brace enclosed string. |
| 413 | const char *Modifier = 0, *Argument = 0; |
| 414 | unsigned ModifierLen = 0, ArgumentLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 416 | // Check to see if we have a modifier. If so eat it. |
| 417 | if (!isdigit(DiagStr[0])) { |
| 418 | Modifier = DiagStr; |
| 419 | while (DiagStr[0] == '-' || |
| 420 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 421 | ++DiagStr; |
| 422 | ModifierLen = DiagStr-Modifier; |
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 | // If we have an argument, get it next. |
| 425 | if (DiagStr[0] == '{') { |
| 426 | ++DiagStr; // Skip {. |
| 427 | Argument = DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 429 | DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); |
| 430 | assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 431 | ArgumentLen = DiagStr-Argument; |
| 432 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 433 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 436 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 437 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 438 | |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 439 | Diagnostic::ArgumentKind Kind = getArgKind(ArgNo); |
| 440 | |
| 441 | switch (Kind) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 442 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 443 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 444 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 445 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 446 | OutStr.append(S.begin(), S.end()); |
| 447 | break; |
| 448 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 449 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 450 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 451 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 452 | |
| 453 | // Don't crash if get passed a null pointer by accident. |
| 454 | if (!S) |
| 455 | S = "(null)"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 457 | OutStr.append(S, S + strlen(S)); |
| 458 | break; |
| 459 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 460 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 461 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 462 | int Val = getArgSInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 464 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 465 | HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 466 | OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 467 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 468 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 469 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 470 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 471 | OutStr); |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 472 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
| 473 | HandleOrdinalModifier((unsigned)Val, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 474 | } else { |
| 475 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 476 | llvm::raw_svector_ostream(OutStr) << Val; |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 478 | break; |
| 479 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 480 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 481 | unsigned Val = getArgUInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 483 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 484 | HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 485 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 486 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 487 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 488 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 489 | OutStr); |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 490 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
| 491 | HandleOrdinalModifier(Val, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 492 | } else { |
| 493 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 494 | llvm::raw_svector_ostream(OutStr) << Val; |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 496 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 498 | // ---- NAMES and TYPES ---- |
| 499 | case Diagnostic::ak_identifierinfo: { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 500 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 501 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 502 | |
| 503 | // Don't crash if get passed a null pointer by accident. |
| 504 | if (!II) { |
| 505 | const char *S = "(null)"; |
| 506 | OutStr.append(S, S + strlen(S)); |
| 507 | continue; |
| 508 | } |
| 509 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 510 | llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 511 | break; |
| 512 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 513 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 514 | case Diagnostic::ak_declarationname: |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 515 | case Diagnostic::ak_nameddecl: |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 516 | case Diagnostic::ak_nestednamespec: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 517 | case Diagnostic::ak_declcontext: |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 518 | getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 519 | Modifier, ModifierLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 520 | Argument, ArgumentLen, |
| 521 | FormattedArgs.data(), FormattedArgs.size(), |
| 522 | OutStr); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 523 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 524 | } |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 525 | |
| 526 | // Remember this argument info for subsequent formatting operations. Turn |
| 527 | // std::strings into a null terminated string to make it be the same case as |
| 528 | // all the other ones. |
| 529 | if (Kind != Diagnostic::ak_std_string) |
| 530 | FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); |
| 531 | else |
| 532 | FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string, |
| 533 | (intptr_t)getArgStdStr(ArgNo).c_str())); |
| 534 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 535 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 536 | } |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 537 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 538 | StoredDiagnostic::StoredDiagnostic() { } |
| 539 | |
| 540 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, |
| 541 | llvm::StringRef Message) |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 542 | : Level(Level), Loc(), Message(Message) { } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 543 | |
| 544 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, |
| 545 | const DiagnosticInfo &Info) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 546 | : Level(Level) { |
| 547 | assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) && |
| 548 | "Valid source location without setting a source manager for diagnostic"); |
| 549 | if (Info.getLocation().isValid()) |
| 550 | Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 551 | llvm::SmallString<64> Message; |
| 552 | Info.FormatDiagnostic(Message); |
| 553 | this->Message.assign(Message.begin(), Message.end()); |
| 554 | |
| 555 | Ranges.reserve(Info.getNumRanges()); |
| 556 | for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I) |
| 557 | Ranges.push_back(Info.getRange(I)); |
| 558 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 559 | FixIts.reserve(Info.getNumFixItHints()); |
| 560 | for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I) |
| 561 | FixIts.push_back(Info.getFixItHint(I)); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | StoredDiagnostic::~StoredDiagnostic() { } |
| 565 | |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 566 | /// IncludeInDiagnosticCounts - This method (whose default implementation |
| 567 | /// returns true) indicates whether the diagnostics handled by this |
| 568 | /// DiagnosticClient should be included in the number of diagnostics |
| 569 | /// reported by Diagnostic. |
| 570 | bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 571 | |
| 572 | PartialDiagnostic::StorageAllocator::StorageAllocator() { |
| 573 | for (unsigned I = 0; I != NumCached; ++I) |
| 574 | FreeList[I] = Cached + I; |
| 575 | NumFreeListEntries = NumCached; |
| 576 | } |
| 577 | |
| 578 | PartialDiagnostic::StorageAllocator::~StorageAllocator() { |
| 579 | assert(NumFreeListEntries == NumCached && "A partial is on the lamb"); |
| 580 | } |