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