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