Diagnostic.h revision e29cc89f82261ad899791f47d1c1d9ef91e172b6
1//===--- Diagnostic.h - C Language Family Diagnostic Handling ---*- C++ -*-===//
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 defines the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_DIAGNOSTIC_H
15#define LLVM_CLANG_DIAGNOSTIC_H
16
17#include "clang/Basic/SourceLocation.h"
18#include <string>
19#include <cassert>
20
21namespace llvm {
22  template <typename T> class SmallVectorImpl;
23}
24
25namespace clang {
26  class DiagnosticClient;
27  class SourceRange;
28  class DiagnosticBuilder;
29  class IdentifierInfo;
30
31  // Import the diagnostic enums themselves.
32  namespace diag {
33    // Start position for diagnostics.
34    enum {
35      DIAG_START_DRIVER   =                        300,
36      DIAG_START_FRONTEND = DIAG_START_DRIVER   +  100,
37      DIAG_START_LEX      = DIAG_START_FRONTEND +  100,
38      DIAG_START_PARSE    = DIAG_START_LEX      +  300,
39      DIAG_START_AST      = DIAG_START_PARSE    +  300,
40      DIAG_START_SEMA     = DIAG_START_AST      +  100,
41      DIAG_START_ANALYSIS = DIAG_START_SEMA     + 1000,
42      DIAG_UPPER_LIMIT    = DIAG_START_ANALYSIS +  100
43    };
44
45    class CustomDiagInfo;
46
47    /// diag::kind - All of the diagnostics that can be emitted by the frontend.
48    typedef unsigned kind;
49
50    // Get typedefs for common diagnostics.
51    enum {
52#define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC) ENUM,
53#include "clang/Basic/DiagnosticCommonKinds.inc"
54      NUM_BUILTIN_COMMON_DIAGNOSTICS
55#undef DIAG
56    };
57
58    /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
59    /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR
60    /// (emit as an error).  It allows clients to map errors to
61    /// MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop emitting diagnostics after this
62    /// one).
63    enum Mapping {
64      MAP_IGNORE  = 0,     //< Map this diagnostic to nothing, ignore it.
65      MAP_WARNING = 1,     //< Map this diagnostic to a warning.
66      MAP_ERROR   = 2,     //< Map this diagnostic to an error.
67      MAP_FATAL   = 3      //< Map this diagnostic to a fatal error.
68    };
69  }
70
71/// \brief Annotates a diagnostic with some code that should be
72/// inserted, removed, or replaced to fix the problem.
73///
74/// This kind of hint should be used when we are certain that the
75/// introduction, removal, or modification of a particular (small!)
76/// amount of code will correct a compilation error. The compiler
77/// should also provide full recovery from such errors, such that
78/// suppressing the diagnostic output can still result in successful
79/// compilation.
80class CodeModificationHint {
81public:
82  /// \brief Tokens that should be removed to correct the error.
83  SourceRange RemoveRange;
84
85  /// \brief The location at which we should insert code to correct
86  /// the error.
87  SourceLocation InsertionLoc;
88
89  /// \brief The actual code to insert at the insertion location, as a
90  /// string.
91  std::string CodeToInsert;
92
93  /// \brief Empty code modification hint, indicating that no code
94  /// modification is known.
95  CodeModificationHint() : RemoveRange(), InsertionLoc() { }
96
97  /// \brief Create a code modification hint that inserts the given
98  /// code string at a specific location.
99  static CodeModificationHint CreateInsertion(SourceLocation InsertionLoc,
100                                              const std::string &Code) {
101    CodeModificationHint Hint;
102    Hint.InsertionLoc = InsertionLoc;
103    Hint.CodeToInsert = Code;
104    return Hint;
105  }
106
107  /// \brief Create a code modification hint that removes the given
108  /// source range.
109  static CodeModificationHint CreateRemoval(SourceRange RemoveRange) {
110    CodeModificationHint Hint;
111    Hint.RemoveRange = RemoveRange;
112    return Hint;
113  }
114
115  /// \brief Create a code modification hint that replaces the given
116  /// source range with the given code string.
117  static CodeModificationHint CreateReplacement(SourceRange RemoveRange,
118                                                const std::string &Code) {
119    CodeModificationHint Hint;
120    Hint.RemoveRange = RemoveRange;
121    Hint.InsertionLoc = RemoveRange.getBegin();
122    Hint.CodeToInsert = Code;
123    return Hint;
124  }
125};
126
127/// Diagnostic - This concrete class is used by the front-end to report
128/// problems and issues.  It massages the diagnostics (e.g. handling things like
129/// "report warnings as errors" and passes them off to the DiagnosticClient for
130/// reporting to the user.
131class Diagnostic {
132public:
133  /// Level - The level of the diagnostic, after it has been through mapping.
134  enum Level {
135    Ignored, Note, Warning, Error, Fatal
136  };
137
138  enum ArgumentKind {
139    ak_std_string,      // std::string
140    ak_c_string,        // const char *
141    ak_sint,            // int
142    ak_uint,            // unsigned
143    ak_identifierinfo,  // IdentifierInfo
144    ak_qualtype,        // QualType
145    ak_declarationname, // DeclarationName
146    ak_nameddecl        // NamedDecl *
147  };
148
149private:
150  unsigned char AllExtensionsSilenced; // Used by __extension__
151  bool IgnoreAllWarnings;     // Ignore all warnings: -w
152  bool WarningsAsErrors;      // Treat warnings like errors:
153  bool SuppressSystemWarnings;// Suppress warnings in system headers.
154  DiagnosticClient *Client;
155
156  /// DiagMappings - Mapping information for diagnostics.  Mapping info is
157  /// packed into two bits per diagnostic.
158  unsigned char DiagMappings[diag::DIAG_UPPER_LIMIT/2];
159
160  /// ErrorOccurred / FatalErrorOccurred - This is set to true when an error or
161  /// fatal error is emitted, and is sticky.
162  bool ErrorOccurred;
163  bool FatalErrorOccurred;
164
165  /// LastDiagLevel - This is the level of the last diagnostic emitted.  This is
166  /// used to emit continuation diagnostics with the same level as the
167  /// diagnostic that they follow.
168  Diagnostic::Level LastDiagLevel;
169
170  unsigned NumDiagnostics;    // Number of diagnostics reported
171  unsigned NumErrors;         // Number of diagnostics that are errors
172
173  /// CustomDiagInfo - Information for uniquing and looking up custom diags.
174  diag::CustomDiagInfo *CustomDiagInfo;
175
176  /// ArgToStringFn - A function pointer that converts an opaque diagnostic
177  /// argument to a strings.  This takes the modifiers and argument that was
178  /// present in the diagnostic.
179  /// This is a hack to avoid a layering violation between libbasic and libsema.
180  typedef void (*ArgToStringFnTy)(ArgumentKind Kind, intptr_t Val,
181                                  const char *Modifier, unsigned ModifierLen,
182                                  const char *Argument, unsigned ArgumentLen,
183                                  llvm::SmallVectorImpl<char> &Output,
184                                  void *Cookie);
185  void *ArgToStringCookie;
186  ArgToStringFnTy ArgToStringFn;
187public:
188  explicit Diagnostic(DiagnosticClient *client = 0);
189  ~Diagnostic();
190
191  //===--------------------------------------------------------------------===//
192  //  Diagnostic characterization methods, used by a client to customize how
193  //
194
195  DiagnosticClient *getClient() { return Client; };
196  const DiagnosticClient *getClient() const { return Client; };
197
198  void setClient(DiagnosticClient* client) { Client = client; }
199
200  /// setIgnoreAllWarnings - When set to true, any unmapped warnings are
201  /// ignored.  If this and WarningsAsErrors are both set, then this one wins.
202  void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }
203  bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; }
204
205  /// setWarningsAsErrors - When set to true, any warnings reported are issued
206  /// as errors.
207  void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
208  bool getWarningsAsErrors() const { return WarningsAsErrors; }
209
210  /// setSuppressSystemWarnings - When set to true mask warnings that
211  /// come from system headers.
212  void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
213  bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; }
214
215  /// AllExtensionsSilenced - This is a counter bumped when an __extension__
216  /// block is encountered.  When non-zero, all extension diagnostics are
217  /// entirely silenced, no matter how they are mapped.
218  void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
219  void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
220
221  /// setDiagnosticMapping - This allows the client to specify that certain
222  /// warnings are ignored.  Only WARNINGs and EXTENSIONs can be mapped.
223  void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map) {
224    assert(Diag < diag::DIAG_UPPER_LIMIT &&
225           "Can only map builtin diagnostics");
226    assert((isBuiltinWarningOrExtension(Diag) || Map == diag::MAP_FATAL) &&
227           "Cannot map errors!");
228    setDiagnosticMappingInternal(Diag, Map);
229  }
230
231  /// getDiagnosticMapping - Return the mapping currently set for the specified
232  /// diagnostic.
233  diag::Mapping getDiagnosticMapping(diag::kind Diag) const {
234    return (diag::Mapping)((DiagMappings[Diag/2] >> (Diag & 1)*4) & 7);
235  }
236
237  bool hasErrorOccurred() const { return ErrorOccurred; }
238  bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
239
240  unsigned getNumErrors() const { return NumErrors; }
241  unsigned getNumDiagnostics() const { return NumDiagnostics; }
242
243  /// getCustomDiagID - Return an ID for a diagnostic with the specified message
244  /// and level.  If this is the first request for this diagnosic, it is
245  /// registered and created, otherwise the existing ID is returned.
246  unsigned getCustomDiagID(Level L, const char *Message);
247
248
249  /// ConvertArgToString - This method converts a diagnostic argument (as an
250  /// intptr_t) into the string that represents it.
251  void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
252                          const char *Modifier, unsigned ModLen,
253                          const char *Argument, unsigned ArgLen,
254                          llvm::SmallVectorImpl<char> &Output) const {
255    ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen, Output,
256                  ArgToStringCookie);
257  }
258
259  void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {
260    ArgToStringFn = Fn;
261    ArgToStringCookie = Cookie;
262  }
263
264  //===--------------------------------------------------------------------===//
265  // Diagnostic classification and reporting interfaces.
266  //
267
268  /// getDescription - Given a diagnostic ID, return a description of the
269  /// issue.
270  const char *getDescription(unsigned DiagID) const;
271
272  /// isNoteWarningOrExtension - Return true if the unmapped diagnostic
273  /// level of the specified diagnostic ID is a Warning or Extension.
274  /// This only works on builtin diagnostics, not custom ones, and is not legal to
275  /// call on NOTEs.
276  static bool isBuiltinWarningOrExtension(unsigned DiagID);
277
278  /// \brief Determine whether the given built-in diagnostic ID is a
279  /// Note.
280  static bool isBuiltinNote(unsigned DiagID);
281
282  /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
283  /// ID is for an extension of some sort.
284  ///
285  static bool isBuiltinExtensionDiag(unsigned DiagID);
286
287
288  /// getDiagnosticLevel - Based on the way the client configured the Diagnostic
289  /// object, classify the specified diagnostic ID into a Level, consumable by
290  /// the DiagnosticClient.
291  Level getDiagnosticLevel(unsigned DiagID) const;
292
293  /// Report - Issue the message to the client.  @c DiagID is a member of the
294  /// @c diag::kind enum.  This actually returns aninstance of DiagnosticBuilder
295  /// which emits the diagnostics (through @c ProcessDiag) when it is destroyed.
296  /// @c Pos represents the source location associated with the diagnostic,
297  /// which can be an invalid location if no position information is available.
298  inline DiagnosticBuilder Report(FullSourceLoc Pos, unsigned DiagID);
299
300  /// \brief Clear out the current diagnostic.
301  void Clear() { CurDiagID = ~0U; }
302
303private:
304  void setDiagnosticMappingInternal(unsigned Diag, unsigned Map) {
305    unsigned char &Slot = DiagMappings[Diag/2];
306    unsigned Bits = (Diag & 1)*4;
307    Slot &= ~(7 << Bits);
308    Slot |= Map << Bits;
309  }
310
311  /// getDiagnosticLevel - This is an internal implementation helper used when
312  /// DiagClass is already known.
313  Level getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const;
314
315  // This is private state used by DiagnosticBuilder.  We put it here instead of
316  // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight
317  // object.  This implementation choice means that we can only have one
318  // diagnostic "in flight" at a time, but this seems to be a reasonable
319  // tradeoff to keep these objects small.  Assertions verify that only one
320  // diagnostic is in flight at a time.
321  friend class DiagnosticBuilder;
322  friend class DiagnosticInfo;
323
324  /// CurDiagLoc - This is the location of the current diagnostic that is in
325  /// flight.
326  FullSourceLoc CurDiagLoc;
327
328  /// CurDiagID - This is the ID of the current diagnostic that is in flight.
329  /// This is set to ~0U when there is no diagnostic in flight.
330  unsigned CurDiagID;
331
332  enum {
333    /// MaxArguments - The maximum number of arguments we can hold. We currently
334    /// only support up to 10 arguments (%0-%9).  A single diagnostic with more
335    /// than that almost certainly has to be simplified anyway.
336    MaxArguments = 10
337  };
338
339  /// NumDiagArgs - This contains the number of entries in Arguments.
340  signed char NumDiagArgs;
341  /// NumRanges - This is the number of ranges in the DiagRanges array.
342  unsigned char NumDiagRanges;
343  /// \brief The number of code modifications hints in the
344  /// CodeModificationHints array.
345  unsigned char NumCodeModificationHints;
346
347  /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
348  /// values, with one for each argument.  This specifies whether the argument
349  /// is in DiagArgumentsStr or in DiagArguments.
350  unsigned char DiagArgumentsKind[MaxArguments];
351
352  /// DiagArgumentsStr - This holds the values of each string argument for the
353  /// current diagnostic.  This value is only used when the corresponding
354  /// ArgumentKind is ak_std_string.
355  std::string DiagArgumentsStr[MaxArguments];
356
357  /// DiagArgumentsVal - The values for the various substitution positions. This
358  /// is used when the argument is not an std::string.  The specific value is
359  /// mangled into an intptr_t and the intepretation depends on exactly what
360  /// sort of argument kind it is.
361  intptr_t DiagArgumentsVal[MaxArguments];
362
363  /// DiagRanges - The list of ranges added to this diagnostic.  It currently
364  /// only support 10 ranges, could easily be extended if needed.
365  const SourceRange *DiagRanges[10];
366
367  enum { MaxCodeModificationHints = 3 };
368
369  /// CodeModificationHints - If valid, provides a hint with some code
370  /// to insert, remove, or modify at a particular position.
371  CodeModificationHint CodeModificationHints[MaxCodeModificationHints];
372
373  /// ProcessDiag - This is the method used to report a diagnostic that is
374  /// finally fully formed.
375  void ProcessDiag();
376};
377
378//===----------------------------------------------------------------------===//
379// DiagnosticBuilder
380//===----------------------------------------------------------------------===//
381
382/// DiagnosticBuilder - This is a little helper class used to produce
383/// diagnostics.  This is constructed by the Diagnostic::Report method, and
384/// allows insertion of extra information (arguments and source ranges) into the
385/// currently "in flight" diagnostic.  When the temporary for the builder is
386/// destroyed, the diagnostic is issued.
387///
388/// Note that many of these will be created as temporary objects (many call
389/// sites), so we want them to be small and we never want their address taken.
390/// This ensures that compilers with somewhat reasonable optimizers will promote
391/// the common fields to registers, eliminating increments of the NumArgs field,
392/// for example.
393class DiagnosticBuilder {
394  mutable Diagnostic *DiagObj;
395  mutable unsigned NumArgs, NumRanges, NumCodeModificationHints;
396
397  void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT
398  friend class Diagnostic;
399  explicit DiagnosticBuilder(Diagnostic *diagObj)
400    : DiagObj(diagObj), NumArgs(0), NumRanges(0),
401      NumCodeModificationHints(0) {}
402
403public:
404  /// Copy constructor.  When copied, this "takes" the diagnostic info from the
405  /// input and neuters it.
406  DiagnosticBuilder(const DiagnosticBuilder &D) {
407    DiagObj = D.DiagObj;
408    D.DiagObj = 0;
409    NumArgs = D.NumArgs;
410    NumRanges = D.NumRanges;
411    NumCodeModificationHints = D.NumCodeModificationHints;
412  }
413
414  /// \brief Force the diagnostic builder to emit the diagnostic now.
415  ///
416  /// Once this function has been called, the DiagnosticBuilder object
417  /// should not be used again before it is destroyed.
418  void Emit() {
419    // If DiagObj is null, then its soul was stolen by the copy ctor
420    // or the user called Emit().
421    if (DiagObj == 0) return;
422
423    // When emitting diagnostics, we set the final argument count into
424    // the Diagnostic object.
425    DiagObj->NumDiagArgs = NumArgs;
426    DiagObj->NumDiagRanges = NumRanges;
427    DiagObj->NumCodeModificationHints = NumCodeModificationHints;
428
429    // Process the diagnostic, sending the accumulated information to the
430    // DiagnosticClient.
431    DiagObj->ProcessDiag();
432
433    // Clear out the current diagnostic object.
434    DiagObj->Clear();
435
436    // This diagnostic is dead.
437    DiagObj = 0;
438  }
439
440  /// Destructor - The dtor emits the diagnostic if it hasn't already
441  /// been emitted.
442  ~DiagnosticBuilder() { Emit(); }
443
444  /// Operator bool: conversion of DiagnosticBuilder to bool always returns
445  /// true.  This allows is to be used in boolean error contexts like:
446  /// return Diag(...);
447  operator bool() const { return true; }
448
449  void AddString(const std::string &S) const {
450    assert(NumArgs < Diagnostic::MaxArguments &&
451           "Too many arguments to diagnostic!");
452    DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string;
453    DiagObj->DiagArgumentsStr[NumArgs++] = S;
454  }
455
456  void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const {
457    assert(NumArgs < Diagnostic::MaxArguments &&
458           "Too many arguments to diagnostic!");
459    DiagObj->DiagArgumentsKind[NumArgs] = Kind;
460    DiagObj->DiagArgumentsVal[NumArgs++] = V;
461  }
462
463  void AddSourceRange(const SourceRange &R) const {
464    assert(NumRanges <
465           sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) &&
466           "Too many arguments to diagnostic!");
467    DiagObj->DiagRanges[NumRanges++] = &R;
468  }
469
470  void AddCodeModificationHint(const CodeModificationHint &Hint) const {
471    assert(NumCodeModificationHints < Diagnostic::MaxCodeModificationHints &&
472           "Too many code modification hints!");
473    DiagObj->CodeModificationHints[NumCodeModificationHints++] = Hint;
474  }
475};
476
477inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
478                                           const std::string &S) {
479  DB.AddString(S);
480  return DB;
481}
482
483inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
484                                           const char *Str) {
485  DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str),
486                  Diagnostic::ak_c_string);
487  return DB;
488}
489
490inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) {
491  DB.AddTaggedVal(I, Diagnostic::ak_sint);
492  return DB;
493}
494
495inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,bool I) {
496  DB.AddTaggedVal(I, Diagnostic::ak_sint);
497  return DB;
498}
499
500inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
501                                           unsigned I) {
502  DB.AddTaggedVal(I, Diagnostic::ak_uint);
503  return DB;
504}
505
506inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
507                                           const IdentifierInfo *II) {
508  DB.AddTaggedVal(reinterpret_cast<intptr_t>(II),
509                  Diagnostic::ak_identifierinfo);
510  return DB;
511}
512
513inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
514                                           const SourceRange &R) {
515  DB.AddSourceRange(R);
516  return DB;
517}
518
519inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
520                                           const CodeModificationHint &Hint) {
521  DB.AddCodeModificationHint(Hint);
522  return DB;
523}
524
525/// Report - Issue the message to the client.  DiagID is a member of the
526/// diag::kind enum.  This actually returns a new instance of DiagnosticBuilder
527/// which emits the diagnostics (through ProcessDiag) when it is destroyed.
528inline DiagnosticBuilder Diagnostic::Report(FullSourceLoc Loc, unsigned DiagID){
529  assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
530  CurDiagLoc = Loc;
531  CurDiagID = DiagID;
532  return DiagnosticBuilder(this);
533}
534
535//===----------------------------------------------------------------------===//
536// DiagnosticInfo
537//===----------------------------------------------------------------------===//
538
539/// DiagnosticInfo - This is a little helper class (which is basically a smart
540/// pointer that forward info from Diagnostic) that allows clients to enquire
541/// about the currently in-flight diagnostic.
542class DiagnosticInfo {
543  const Diagnostic *DiagObj;
544public:
545  explicit DiagnosticInfo(const Diagnostic *DO) : DiagObj(DO) {}
546
547  const Diagnostic *getDiags() const { return DiagObj; }
548  unsigned getID() const { return DiagObj->CurDiagID; }
549  const FullSourceLoc &getLocation() const { return DiagObj->CurDiagLoc; }
550
551  unsigned getNumArgs() const { return DiagObj->NumDiagArgs; }
552
553  /// getArgKind - Return the kind of the specified index.  Based on the kind
554  /// of argument, the accessors below can be used to get the value.
555  Diagnostic::ArgumentKind getArgKind(unsigned Idx) const {
556    assert(Idx < getNumArgs() && "Argument index out of range!");
557    return (Diagnostic::ArgumentKind)DiagObj->DiagArgumentsKind[Idx];
558  }
559
560  /// getArgStdStr - Return the provided argument string specified by Idx.
561  const std::string &getArgStdStr(unsigned Idx) const {
562    assert(getArgKind(Idx) == Diagnostic::ak_std_string &&
563           "invalid argument accessor!");
564    return DiagObj->DiagArgumentsStr[Idx];
565  }
566
567  /// getArgCStr - Return the specified C string argument.
568  const char *getArgCStr(unsigned Idx) const {
569    assert(getArgKind(Idx) == Diagnostic::ak_c_string &&
570           "invalid argument accessor!");
571    return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]);
572  }
573
574  /// getArgSInt - Return the specified signed integer argument.
575  int getArgSInt(unsigned Idx) const {
576    assert(getArgKind(Idx) == Diagnostic::ak_sint &&
577           "invalid argument accessor!");
578    return (int)DiagObj->DiagArgumentsVal[Idx];
579  }
580
581  /// getArgUInt - Return the specified unsigned integer argument.
582  unsigned getArgUInt(unsigned Idx) const {
583    assert(getArgKind(Idx) == Diagnostic::ak_uint &&
584           "invalid argument accessor!");
585    return (unsigned)DiagObj->DiagArgumentsVal[Idx];
586  }
587
588  /// getArgIdentifier - Return the specified IdentifierInfo argument.
589  const IdentifierInfo *getArgIdentifier(unsigned Idx) const {
590    assert(getArgKind(Idx) == Diagnostic::ak_identifierinfo &&
591           "invalid argument accessor!");
592    return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]);
593  }
594
595  /// getRawArg - Return the specified non-string argument in an opaque form.
596  intptr_t getRawArg(unsigned Idx) const {
597    assert(getArgKind(Idx) != Diagnostic::ak_std_string &&
598           "invalid argument accessor!");
599    return DiagObj->DiagArgumentsVal[Idx];
600  }
601
602
603  /// getNumRanges - Return the number of source ranges associated with this
604  /// diagnostic.
605  unsigned getNumRanges() const {
606    return DiagObj->NumDiagRanges;
607  }
608
609  const SourceRange &getRange(unsigned Idx) const {
610    assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!");
611    return *DiagObj->DiagRanges[Idx];
612  }
613
614  unsigned getNumCodeModificationHints() const {
615    return DiagObj->NumCodeModificationHints;
616  }
617
618  const CodeModificationHint &getCodeModificationHint(unsigned Idx) const {
619    return DiagObj->CodeModificationHints[Idx];
620  }
621
622  const CodeModificationHint *getCodeModificationHints() const {
623    return DiagObj->NumCodeModificationHints?
624             &DiagObj->CodeModificationHints[0] : 0;
625  }
626
627  /// FormatDiagnostic - Format this diagnostic into a string, substituting the
628  /// formal arguments into the %0 slots.  The result is appended onto the Str
629  /// array.
630  void FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const;
631};
632
633/// DiagnosticClient - This is an abstract interface implemented by clients of
634/// the front-end, which formats and prints fully processed diagnostics.
635class DiagnosticClient {
636public:
637  virtual ~DiagnosticClient();
638
639  /// IncludeInDiagnosticCounts - This method (whose default implementation
640  ///  returns true) indicates whether the diagnostics handled by this
641  ///  DiagnosticClient should be included in the number of diagnostics
642  ///  reported by Diagnostic.
643  virtual bool IncludeInDiagnosticCounts() const;
644
645  /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
646  /// capturing it to a log as needed.
647  virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
648                                const DiagnosticInfo &Info) = 0;
649};
650
651}  // end namespace clang
652
653#endif
654