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