]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Basic/Diagnostic.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Basic / Diagnostic.cpp
1 //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the Diagnostic-related interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/IdentifierTable.h"
16 #include "clang/Basic/PartialDiagnostic.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Support/CrashRecoveryContext.h"
20
21 using namespace clang;
22
23 static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
24                                const char *Modifier, unsigned ML,
25                                const char *Argument, unsigned ArgLen,
26                                const Diagnostic::ArgumentValue *PrevArgs,
27                                unsigned NumPrevArgs,
28                                llvm::SmallVectorImpl<char> &Output,
29                                void *Cookie,
30                                llvm::SmallVectorImpl<intptr_t> &QualTypeVals) {
31   const char *Str = "<can't format argument>";
32   Output.append(Str, Str+strlen(Str));
33 }
34
35
36 Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
37                        DiagnosticClient *client, bool ShouldOwnClient)
38   : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
39     SourceMgr(0) {
40   ArgToStringFn = DummyArgToStringFn;
41   ArgToStringCookie = 0;
42
43   AllExtensionsSilenced = 0;
44   IgnoreAllWarnings = false;
45   WarningsAsErrors = false;
46   ErrorsAsFatal = false;
47   SuppressSystemWarnings = false;
48   SuppressAllDiagnostics = false;
49   ShowOverloads = Ovl_All;
50   ExtBehavior = Ext_Ignore;
51
52   ErrorLimit = 0;
53   TemplateBacktraceLimit = 0;
54
55   Reset();
56 }
57
58 Diagnostic::~Diagnostic() {
59   if (OwnsDiagClient)
60     delete Client;
61 }
62
63 void Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) {
64   if (OwnsDiagClient && Client)
65     delete Client;
66   
67   Client = client;
68   OwnsDiagClient = ShouldOwnClient;
69 }
70
71 void Diagnostic::pushMappings(SourceLocation Loc) {
72   DiagStateOnPushStack.push_back(GetCurDiagState());
73 }
74
75 bool Diagnostic::popMappings(SourceLocation Loc) {
76   if (DiagStateOnPushStack.empty())
77     return false;
78
79   if (DiagStateOnPushStack.back() != GetCurDiagState()) {
80     // State changed at some point between push/pop.
81     PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
82   }
83   DiagStateOnPushStack.pop_back();
84   return true;
85 }
86
87 void Diagnostic::Reset() {
88   ErrorOccurred = false;
89   FatalErrorOccurred = false;
90   UnrecoverableErrorOccurred = false;
91   
92   NumWarnings = 0;
93   NumErrors = 0;
94   NumErrorsSuppressed = 0;
95   
96   CurDiagID = ~0U;
97   // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
98   // using a Diagnostic associated to a translation unit that follow
99   // diagnostics from a Diagnostic associated to anoter t.u. will not be
100   // displayed.
101   LastDiagLevel = (DiagnosticIDs::Level)-1;
102   DelayedDiagID = 0;
103
104   // Clear state related to #pragma diagnostic.
105   DiagStates.clear();
106   DiagStatePoints.clear();
107   DiagStateOnPushStack.clear();
108
109   // Create a DiagState and DiagStatePoint representing diagnostic changes
110   // through command-line.
111   DiagStates.push_back(DiagState());
112   PushDiagStatePoint(&DiagStates.back(), SourceLocation());
113 }
114
115 void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
116                                       llvm::StringRef Arg2) {
117   if (DelayedDiagID)
118     return;
119
120   DelayedDiagID = DiagID;
121   DelayedDiagArg1 = Arg1.str();
122   DelayedDiagArg2 = Arg2.str();
123 }
124
125 void Diagnostic::ReportDelayed() {
126   Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
127   DelayedDiagID = 0;
128   DelayedDiagArg1.clear();
129   DelayedDiagArg2.clear();
130 }
131
132 Diagnostic::DiagStatePointsTy::iterator
133 Diagnostic::GetDiagStatePointForLoc(SourceLocation L) const {
134   assert(!DiagStatePoints.empty());
135   assert(DiagStatePoints.front().Loc.isInvalid() &&
136          "Should have created a DiagStatePoint for command-line");
137
138   FullSourceLoc Loc(L, *SourceMgr);
139   if (Loc.isInvalid())
140     return DiagStatePoints.end() - 1;
141
142   DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
143   FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
144   if (LastStateChangePos.isValid() &&
145       Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
146     Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
147                            DiagStatePoint(0, Loc));
148   --Pos;
149   return Pos;
150 }
151
152 /// \brief This allows the client to specify that certain
153 /// warnings are ignored.  Notes can never be mapped, errors can only be
154 /// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
155 ///
156 /// \param The source location that this change of diagnostic state should
157 /// take affect. It can be null if we are setting the latest state.
158 void Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
159                                       SourceLocation L) {
160   assert(Diag < diag::DIAG_UPPER_LIMIT &&
161          "Can only map builtin diagnostics");
162   assert((Diags->isBuiltinWarningOrExtension(Diag) ||
163           (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
164          "Cannot map errors into warnings!");
165   assert(!DiagStatePoints.empty());
166
167   bool isPragma = L.isValid();
168   FullSourceLoc Loc(L, *SourceMgr);
169   FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
170
171   // Common case; setting all the diagnostics of a group in one place.
172   if (Loc.isInvalid() || Loc == LastStateChangePos) {
173     setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
174     return;
175   }
176
177   // Another common case; modifying diagnostic state in a source location
178   // after the previous one.
179   if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
180       LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
181     // A diagnostic pragma occurred, create a new DiagState initialized with
182     // the current one and a new DiagStatePoint to record at which location
183     // the new state became active.
184     DiagStates.push_back(*GetCurDiagState());
185     PushDiagStatePoint(&DiagStates.back(), Loc);
186     setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
187     return;
188   }
189
190   // We allow setting the diagnostic state in random source order for
191   // completeness but it should not be actually happening in normal practice.
192
193   DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
194   assert(Pos != DiagStatePoints.end());
195
196   // Update all diagnostic states that are active after the given location.
197   for (DiagStatePointsTy::iterator
198          I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
199     setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma);
200   }
201
202   // If the location corresponds to an existing point, just update its state.
203   if (Pos->Loc == Loc) {
204     setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma);
205     return;
206   }
207
208   // Create a new state/point and fit it into the vector of DiagStatePoints
209   // so that the vector is always ordered according to location.
210   Pos->Loc.isBeforeInTranslationUnitThan(Loc);
211   DiagStates.push_back(*Pos->State);
212   DiagState *NewState = &DiagStates.back();
213   setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma);
214   DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
215                                                FullSourceLoc(Loc, *SourceMgr)));
216 }
217
218 void Diagnostic::Report(const StoredDiagnostic &storedDiag) {
219   assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
220
221   CurDiagLoc = storedDiag.getLocation();
222   CurDiagID = storedDiag.getID();
223   NumDiagArgs = 0;
224
225   NumDiagRanges = storedDiag.range_size();
226   assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) &&
227          "Too many arguments to diagnostic!");
228   unsigned i = 0;
229   for (StoredDiagnostic::range_iterator
230          RI = storedDiag.range_begin(),
231          RE = storedDiag.range_end(); RI != RE; ++RI)
232     DiagRanges[i++] = *RI;
233
234   NumFixItHints = storedDiag.fixit_size();
235   assert(NumFixItHints < Diagnostic::MaxFixItHints && "Too many fix-it hints!");
236   i = 0;
237   for (StoredDiagnostic::fixit_iterator
238          FI = storedDiag.fixit_begin(),
239          FE = storedDiag.fixit_end(); FI != FE; ++FI)
240     FixItHints[i++] = *FI;
241
242   assert(Client && "DiagnosticClient not set!");
243   Level DiagLevel = storedDiag.getLevel();
244   DiagnosticInfo Info(this, storedDiag.getMessage());
245   Client->HandleDiagnostic(DiagLevel, Info);
246   if (Client->IncludeInDiagnosticCounts()) {
247     if (DiagLevel == Diagnostic::Warning)
248       ++NumWarnings;
249   }
250
251   CurDiagID = ~0U;
252 }
253
254 void DiagnosticBuilder::FlushCounts() {
255   DiagObj->NumDiagArgs = NumArgs;
256   DiagObj->NumDiagRanges = NumRanges;
257   DiagObj->NumFixItHints = NumFixItHints;
258 }
259
260 bool DiagnosticBuilder::Emit() {
261   // If DiagObj is null, then its soul was stolen by the copy ctor
262   // or the user called Emit().
263   if (DiagObj == 0) return false;
264
265   // When emitting diagnostics, we set the final argument count into
266   // the Diagnostic object.
267   FlushCounts();
268
269   // Process the diagnostic, sending the accumulated information to the
270   // DiagnosticClient.
271   bool Emitted = DiagObj->ProcessDiag();
272
273   // Clear out the current diagnostic object.
274   unsigned DiagID = DiagObj->CurDiagID;
275   DiagObj->Clear();
276
277   // If there was a delayed diagnostic, emit it now.
278   if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
279     DiagObj->ReportDelayed();
280
281   // This diagnostic is dead.
282   DiagObj = 0;
283
284   return Emitted;
285 }
286
287
288 DiagnosticClient::~DiagnosticClient() {}
289
290 void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
291                                         const DiagnosticInfo &Info) {
292   if (!IncludeInDiagnosticCounts())
293     return;
294
295   if (DiagLevel == Diagnostic::Warning)
296     ++NumWarnings;
297   else if (DiagLevel >= Diagnostic::Error)
298     ++NumErrors;
299 }
300
301 /// ModifierIs - Return true if the specified modifier matches specified string.
302 template <std::size_t StrLen>
303 static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
304                        const char (&Str)[StrLen]) {
305   return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
306 }
307
308 /// ScanForward - Scans forward, looking for the given character, skipping
309 /// nested clauses and escaped characters.
310 static const char *ScanFormat(const char *I, const char *E, char Target) {
311   unsigned Depth = 0;
312
313   for ( ; I != E; ++I) {
314     if (Depth == 0 && *I == Target) return I;
315     if (Depth != 0 && *I == '}') Depth--;
316
317     if (*I == '%') {
318       I++;
319       if (I == E) break;
320
321       // Escaped characters get implicitly skipped here.
322
323       // Format specifier.
324       if (!isdigit(*I) && !ispunct(*I)) {
325         for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
326         if (I == E) break;
327         if (*I == '{')
328           Depth++;
329       }
330     }
331   }
332   return E;
333 }
334
335 /// HandleSelectModifier - Handle the integer 'select' modifier.  This is used
336 /// like this:  %select{foo|bar|baz}2.  This means that the integer argument
337 /// "%2" has a value from 0-2.  If the value is 0, the diagnostic prints 'foo'.
338 /// If the value is 1, it prints 'bar'.  If it has the value 2, it prints 'baz'.
339 /// This is very useful for certain classes of variant diagnostics.
340 static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
341                                  const char *Argument, unsigned ArgumentLen,
342                                  llvm::SmallVectorImpl<char> &OutStr) {
343   const char *ArgumentEnd = Argument+ArgumentLen;
344
345   // Skip over 'ValNo' |'s.
346   while (ValNo) {
347     const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
348     assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
349            " larger than the number of options in the diagnostic string!");
350     Argument = NextVal+1;  // Skip this string.
351     --ValNo;
352   }
353
354   // Get the end of the value.  This is either the } or the |.
355   const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
356
357   // Recursively format the result of the select clause into the output string.
358   DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
359 }
360
361 /// HandleIntegerSModifier - Handle the integer 's' modifier.  This adds the
362 /// letter 's' to the string if the value is not 1.  This is used in cases like
363 /// this:  "you idiot, you have %4 parameter%s4!".
364 static void HandleIntegerSModifier(unsigned ValNo,
365                                    llvm::SmallVectorImpl<char> &OutStr) {
366   if (ValNo != 1)
367     OutStr.push_back('s');
368 }
369
370 /// HandleOrdinalModifier - Handle the integer 'ord' modifier.  This
371 /// prints the ordinal form of the given integer, with 1 corresponding
372 /// to the first ordinal.  Currently this is hard-coded to use the
373 /// English form.
374 static void HandleOrdinalModifier(unsigned ValNo,
375                                   llvm::SmallVectorImpl<char> &OutStr) {
376   assert(ValNo != 0 && "ValNo must be strictly positive!");
377
378   llvm::raw_svector_ostream Out(OutStr);
379
380   // We could use text forms for the first N ordinals, but the numeric
381   // forms are actually nicer in diagnostics because they stand out.
382   Out << ValNo;
383
384   // It is critically important that we do this perfectly for
385   // user-written sequences with over 100 elements.
386   switch (ValNo % 100) {
387   case 11:
388   case 12:
389   case 13:
390     Out << "th"; return;
391   default:
392     switch (ValNo % 10) {
393     case 1: Out << "st"; return;
394     case 2: Out << "nd"; return;
395     case 3: Out << "rd"; return;
396     default: Out << "th"; return;
397     }
398   }
399 }
400
401
402 /// PluralNumber - Parse an unsigned integer and advance Start.
403 static unsigned PluralNumber(const char *&Start, const char *End) {
404   // Programming 101: Parse a decimal number :-)
405   unsigned Val = 0;
406   while (Start != End && *Start >= '0' && *Start <= '9') {
407     Val *= 10;
408     Val += *Start - '0';
409     ++Start;
410   }
411   return Val;
412 }
413
414 /// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
415 static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
416   if (*Start != '[') {
417     unsigned Ref = PluralNumber(Start, End);
418     return Ref == Val;
419   }
420
421   ++Start;
422   unsigned Low = PluralNumber(Start, End);
423   assert(*Start == ',' && "Bad plural expression syntax: expected ,");
424   ++Start;
425   unsigned High = PluralNumber(Start, End);
426   assert(*Start == ']' && "Bad plural expression syntax: expected )");
427   ++Start;
428   return Low <= Val && Val <= High;
429 }
430
431 /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
432 static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
433   // Empty condition?
434   if (*Start == ':')
435     return true;
436
437   while (1) {
438     char C = *Start;
439     if (C == '%') {
440       // Modulo expression
441       ++Start;
442       unsigned Arg = PluralNumber(Start, End);
443       assert(*Start == '=' && "Bad plural expression syntax: expected =");
444       ++Start;
445       unsigned ValMod = ValNo % Arg;
446       if (TestPluralRange(ValMod, Start, End))
447         return true;
448     } else {
449       assert((C == '[' || (C >= '0' && C <= '9')) &&
450              "Bad plural expression syntax: unexpected character");
451       // Range expression
452       if (TestPluralRange(ValNo, Start, End))
453         return true;
454     }
455
456     // Scan for next or-expr part.
457     Start = std::find(Start, End, ',');
458     if (Start == End)
459       break;
460     ++Start;
461   }
462   return false;
463 }
464
465 /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
466 /// for complex plural forms, or in languages where all plurals are complex.
467 /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
468 /// conditions that are tested in order, the form corresponding to the first
469 /// that applies being emitted. The empty condition is always true, making the
470 /// last form a default case.
471 /// Conditions are simple boolean expressions, where n is the number argument.
472 /// Here are the rules.
473 /// condition  := expression | empty
474 /// empty      :=                             -> always true
475 /// expression := numeric [',' expression]    -> logical or
476 /// numeric    := range                       -> true if n in range
477 ///             | '%' number '=' range        -> true if n % number in range
478 /// range      := number
479 ///             | '[' number ',' number ']'   -> ranges are inclusive both ends
480 ///
481 /// Here are some examples from the GNU gettext manual written in this form:
482 /// English:
483 /// {1:form0|:form1}
484 /// Latvian:
485 /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
486 /// Gaeilge:
487 /// {1:form0|2:form1|:form2}
488 /// Romanian:
489 /// {1:form0|0,%100=[1,19]:form1|:form2}
490 /// Lithuanian:
491 /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
492 /// Russian (requires repeated form):
493 /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
494 /// Slovak
495 /// {1:form0|[2,4]:form1|:form2}
496 /// Polish (requires repeated form):
497 /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
498 static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
499                                  const char *Argument, unsigned ArgumentLen,
500                                  llvm::SmallVectorImpl<char> &OutStr) {
501   const char *ArgumentEnd = Argument + ArgumentLen;
502   while (1) {
503     assert(Argument < ArgumentEnd && "Plural expression didn't match.");
504     const char *ExprEnd = Argument;
505     while (*ExprEnd != ':') {
506       assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
507       ++ExprEnd;
508     }
509     if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
510       Argument = ExprEnd + 1;
511       ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
512
513       // Recursively format the result of the plural clause into the
514       // output string.
515       DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
516       return;
517     }
518     Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
519   }
520 }
521
522
523 /// FormatDiagnostic - Format this diagnostic into a string, substituting the
524 /// formal arguments into the %0 slots.  The result is appended onto the Str
525 /// array.
526 void DiagnosticInfo::
527 FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
528   if (!StoredDiagMessage.empty()) {
529     OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
530     return;
531   }
532
533   llvm::StringRef Diag = 
534     getDiags()->getDiagnosticIDs()->getDescription(getID());
535
536   FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
537 }
538
539 void DiagnosticInfo::
540 FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
541                  llvm::SmallVectorImpl<char> &OutStr) const {
542
543   /// FormattedArgs - Keep track of all of the arguments formatted by
544   /// ConvertArgToString and pass them into subsequent calls to
545   /// ConvertArgToString, allowing the implementation to avoid redundancies in
546   /// obvious cases.
547   llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
548
549   /// QualTypeVals - Pass a vector of arrays so that QualType names can be
550   /// compared to see if more information is needed to be printed.
551   llvm::SmallVector<intptr_t, 2> QualTypeVals;
552   for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
553     if (getArgKind(i) == Diagnostic::ak_qualtype)
554       QualTypeVals.push_back(getRawArg(i));
555
556   while (DiagStr != DiagEnd) {
557     if (DiagStr[0] != '%') {
558       // Append non-%0 substrings to Str if we have one.
559       const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
560       OutStr.append(DiagStr, StrEnd);
561       DiagStr = StrEnd;
562       continue;
563     } else if (ispunct(DiagStr[1])) {
564       OutStr.push_back(DiagStr[1]);  // %% -> %.
565       DiagStr += 2;
566       continue;
567     }
568
569     // Skip the %.
570     ++DiagStr;
571
572     // This must be a placeholder for a diagnostic argument.  The format for a
573     // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
574     // The digit is a number from 0-9 indicating which argument this comes from.
575     // The modifier is a string of digits from the set [-a-z]+, arguments is a
576     // brace enclosed string.
577     const char *Modifier = 0, *Argument = 0;
578     unsigned ModifierLen = 0, ArgumentLen = 0;
579
580     // Check to see if we have a modifier.  If so eat it.
581     if (!isdigit(DiagStr[0])) {
582       Modifier = DiagStr;
583       while (DiagStr[0] == '-' ||
584              (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
585         ++DiagStr;
586       ModifierLen = DiagStr-Modifier;
587
588       // If we have an argument, get it next.
589       if (DiagStr[0] == '{') {
590         ++DiagStr; // Skip {.
591         Argument = DiagStr;
592
593         DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
594         assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
595         ArgumentLen = DiagStr-Argument;
596         ++DiagStr;  // Skip }.
597       }
598     }
599
600     assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
601     unsigned ArgNo = *DiagStr++ - '0';
602
603     Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
604     
605     switch (Kind) {
606     // ---- STRINGS ----
607     case Diagnostic::ak_std_string: {
608       const std::string &S = getArgStdStr(ArgNo);
609       assert(ModifierLen == 0 && "No modifiers for strings yet");
610       OutStr.append(S.begin(), S.end());
611       break;
612     }
613     case Diagnostic::ak_c_string: {
614       const char *S = getArgCStr(ArgNo);
615       assert(ModifierLen == 0 && "No modifiers for strings yet");
616
617       // Don't crash if get passed a null pointer by accident.
618       if (!S)
619         S = "(null)";
620
621       OutStr.append(S, S + strlen(S));
622       break;
623     }
624     // ---- INTEGERS ----
625     case Diagnostic::ak_sint: {
626       int Val = getArgSInt(ArgNo);
627
628       if (ModifierIs(Modifier, ModifierLen, "select")) {
629         HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
630                              OutStr);
631       } else if (ModifierIs(Modifier, ModifierLen, "s")) {
632         HandleIntegerSModifier(Val, OutStr);
633       } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
634         HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
635                              OutStr);
636       } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
637         HandleOrdinalModifier((unsigned)Val, OutStr);
638       } else {
639         assert(ModifierLen == 0 && "Unknown integer modifier");
640         llvm::raw_svector_ostream(OutStr) << Val;
641       }
642       break;
643     }
644     case Diagnostic::ak_uint: {
645       unsigned Val = getArgUInt(ArgNo);
646
647       if (ModifierIs(Modifier, ModifierLen, "select")) {
648         HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
649       } else if (ModifierIs(Modifier, ModifierLen, "s")) {
650         HandleIntegerSModifier(Val, OutStr);
651       } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
652         HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
653                              OutStr);
654       } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
655         HandleOrdinalModifier(Val, OutStr);
656       } else {
657         assert(ModifierLen == 0 && "Unknown integer modifier");
658         llvm::raw_svector_ostream(OutStr) << Val;
659       }
660       break;
661     }
662     // ---- NAMES and TYPES ----
663     case Diagnostic::ak_identifierinfo: {
664       const IdentifierInfo *II = getArgIdentifier(ArgNo);
665       assert(ModifierLen == 0 && "No modifiers for strings yet");
666
667       // Don't crash if get passed a null pointer by accident.
668       if (!II) {
669         const char *S = "(null)";
670         OutStr.append(S, S + strlen(S));
671         continue;
672       }
673
674       llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
675       break;
676     }
677     case Diagnostic::ak_qualtype:
678     case Diagnostic::ak_declarationname:
679     case Diagnostic::ak_nameddecl:
680     case Diagnostic::ak_nestednamespec:
681     case Diagnostic::ak_declcontext:
682       getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
683                                      Modifier, ModifierLen,
684                                      Argument, ArgumentLen,
685                                      FormattedArgs.data(), FormattedArgs.size(),
686                                      OutStr, QualTypeVals);
687       break;
688     }
689     
690     // Remember this argument info for subsequent formatting operations.  Turn
691     // std::strings into a null terminated string to make it be the same case as
692     // all the other ones.
693     if (Kind != Diagnostic::ak_std_string)
694       FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
695     else
696       FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
697                                         (intptr_t)getArgStdStr(ArgNo).c_str()));
698     
699   }
700 }
701
702 StoredDiagnostic::StoredDiagnostic() { }
703
704 StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
705                                    llvm::StringRef Message)
706   : ID(ID), Level(Level), Loc(), Message(Message) { }
707
708 StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
709                                    const DiagnosticInfo &Info)
710   : ID(Info.getID()), Level(Level) 
711 {
712   assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
713        "Valid source location without setting a source manager for diagnostic");
714   if (Info.getLocation().isValid())
715     Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
716   llvm::SmallString<64> Message;
717   Info.FormatDiagnostic(Message);
718   this->Message.assign(Message.begin(), Message.end());
719
720   Ranges.reserve(Info.getNumRanges());
721   for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
722     Ranges.push_back(Info.getRange(I));
723
724   FixIts.reserve(Info.getNumFixItHints());
725   for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
726     FixIts.push_back(Info.getFixItHint(I));
727 }
728
729 StoredDiagnostic::~StoredDiagnostic() { }
730
731 /// IncludeInDiagnosticCounts - This method (whose default implementation
732 ///  returns true) indicates whether the diagnostics handled by this
733 ///  DiagnosticClient should be included in the number of diagnostics
734 ///  reported by Diagnostic.
735 bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
736
737 PartialDiagnostic::StorageAllocator::StorageAllocator() {
738   for (unsigned I = 0; I != NumCached; ++I)
739     FreeList[I] = Cached + I;
740   NumFreeListEntries = NumCached;
741 }
742
743 PartialDiagnostic::StorageAllocator::~StorageAllocator() {
744   // Don't assert if we are in a CrashRecovery context, as this
745   // invariant may be invalidated during a crash.
746   assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb");
747 }