Diagnostic.h revision 28409289193afca1a3178a9796f8e8aebcb76896
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Diagnostic.h - C Language Family Diagnostic Handling ---*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the Diagnostic-related interfaces.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_DIAGNOSTIC_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_DIAGNOSTIC_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis#include "clang/Basic/DiagnosticIDs.h"
182eefd8657c233bc7c9330acfe475fc270bbe7cabTed Kremenek#include "clang/Basic/SourceLocation.h"
19f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor#include "llvm/ADT/ArrayRef.h"
200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include "llvm/ADT/DenseMap.h"
2128019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
22bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor#include "llvm/ADT/OwningPtr.h"
233f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor#include "llvm/Support/type_traits.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis#include <vector>
260827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include <list>
27f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
2978ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  class DiagnosticConsumer;
3033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DiagnosticBuilder;
31d42ffbd22fc7eb61321f6a88173ee424991f01c6Ted Kremenek  class IdentifierInfo;
3233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DeclContext;
3340469651a3f8379dc0f32df69e9bade06a2aad36Chris Lattner  class LangOptions;
347d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  class Preprocessor;
359c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  class DiagnosticErrorTrap;
36e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  class StoredDiagnostic;
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
384b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Annotates a diagnostic with some code that should be
394b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// inserted, removed, or replaced to fix the problem.
404b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
414b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// This kind of hint should be used when we are certain that the
424b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// introduction, removal, or modification of a particular (small!)
434b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// amount of code will correct a compilation error. The compiler
444b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// should also provide full recovery from such errors, such that
45b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor/// suppressing the diagnostic output can still result in successful
464b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// compilation.
47849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregorclass FixItHint {
484b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorpublic:
49783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// \brief Code that should be replaced to correct the error. Empty for an
50783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// insertion hint.
510a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  CharSourceRange RemoveRange;
524b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
534b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief The actual code to insert at the insertion location, as a
544b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// string.
554b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  std::string CodeToInsert;
564b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
574b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Empty code modification hint, indicating that no code
584b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// modification is known.
59783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  FixItHint() : RemoveRange() { }
604b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
612f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  bool isNull() const {
62783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    return !RemoveRange.isValid();
632f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  }
642f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson
654b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that inserts the given
664b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// code string at a specific location.
67849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  static FixItHint CreateInsertion(SourceLocation InsertionLoc,
68686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                   StringRef Code) {
69849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
70783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    Hint.RemoveRange =
71783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor      CharSourceRange(SourceRange(InsertionLoc, InsertionLoc), false);
72b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
73b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
74b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
754b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
764b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that removes the given
774b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range.
780a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(CharSourceRange RemoveRange) {
79849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
80b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
81b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
82b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
830a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(SourceRange RemoveRange) {
840a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateRemoval(CharSourceRange::getTokenRange(RemoveRange));
850a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
860a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
874b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that replaces the given
884b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range with the given code string.
890a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(CharSourceRange RemoveRange,
90686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                     StringRef Code) {
91849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
92b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
93b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
94b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
95b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
960a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
970a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(SourceRange RemoveRange,
98686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                     StringRef Code) {
990a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateReplacement(CharSourceRange::getTokenRange(RemoveRange), Code);
1000a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
1014b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor};
1024b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
103d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie/// DiagnosticsEngine - This concrete class is used by the front-end to report
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// problems and issues.  It massages the diagnostics (e.g. handling things like
10578ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie/// "report warnings as errors" and passes them off to the DiagnosticConsumer
10678ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie/// for reporting to the user. DiagnosticsEngine is tied to one translation unit
10778ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie/// and one SourceManager.
108d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine : public llvm::RefCountedBase<DiagnosticsEngine> {
109182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattnerpublic:
110182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// Level - The level of the diagnostic, after it has been through mapping.
111182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  enum Level {
11233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Ignored = DiagnosticIDs::Ignored,
11333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Note = DiagnosticIDs::Note,
11433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Warning = DiagnosticIDs::Warning,
11533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Error = DiagnosticIDs::Error,
11633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Fatal = DiagnosticIDs::Fatal
117182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  };
1181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// ExtensionHandling - How do we handle otherwise-unmapped extension?  This
120b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// is controlled by -pedantic and -pedantic-errors.
121b54b276a920246c595a0498da281821eb9d22996Chris Lattner  enum ExtensionHandling {
122b54b276a920246c595a0498da281821eb9d22996Chris Lattner    Ext_Ignore, Ext_Warn, Ext_Error
123b54b276a920246c595a0498da281821eb9d22996Chris Lattner  };
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1253fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  enum ArgumentKind {
12647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_std_string,      // std::string
12747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_c_string,        // const char *
12847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_sint,            // int
12947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_uint,            // unsigned
13047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_identifierinfo,  // IdentifierInfo
13147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_qualtype,        // QualType
13247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_declarationname, // DeclarationName
133dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    ak_nameddecl,       // NamedDecl *
1343f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor    ak_nestednamespec,  // NestedNameSpecifier *
1353f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor    ak_declcontext      // DeclContext *
1363fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  };
1375edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
1385edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// Specifies which overload candidates to display when overload resolution
1395edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// fails.
1405edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  enum OverloadsShown {
1415edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    Ovl_All,  ///< Show all overloads.
1425edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    Ovl_Best  ///< Show just the "best" overload candidates.
1435edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  };
1445edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
145b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ArgumentValue - This typedef represents on argument value, which is a
146b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// union discriminated by ArgumentKind, with a value.
147b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  typedef std::pair<ArgumentKind, intptr_t> ArgumentValue;
148ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpprivate:
15027ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  unsigned char AllExtensionsSilenced; // Used by __extension__
151b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool IgnoreAllWarnings;        // Ignore all warnings: -w
1521e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool WarningsAsErrors;         // Treat warnings like errors.
1531e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool EnableAllWarnings;        // Enable all warnings.
154e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool ErrorsAsFatal;            // Treat errors like fatal errors.
155b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool SuppressSystemWarnings;   // Suppress warnings in system headers.
15681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool SuppressAllDiagnostics;   // Suppress all diagnostics.
1575edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown ShowOverloads;  // Which overload candidates to show.
158c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  unsigned ErrorLimit;           // Cap of # errors emitted, 0 -> no limit.
159575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned TemplateBacktraceLimit; // Cap on depth of template backtrace stack,
160575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor                                   // 0 -> no limit.
161b54b276a920246c595a0498da281821eb9d22996Chris Lattner  ExtensionHandling ExtBehavior; // Map extensions onto warnings or errors?
16233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  llvm::IntrusiveRefCntPtr<DiagnosticIDs> Diags;
16378ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *Client;
16433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool OwnsDiagClient;
16533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager *SourceMgr;
1660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Mapping information for diagnostics.  Mapping info is
168691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// packed into four bits per diagnostic.  The low three bits are the mapping
169691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// (an instance of diag::Mapping), or zero if unset.  The high bit is set
170691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// when the mapping was established as a user mapping.  If the high bit is
171691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// clear, then the low bits are set to the default value, and should be
172691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// mapped with -pedantic, -Werror, etc.
1730827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
1743efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// A new DiagState is created and kept around when diagnostic pragmas modify
1753efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// the state so that we know what is the diagnostic state at any given
1763efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// source location.
1770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  class DiagState {
178b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    llvm::DenseMap<unsigned, DiagnosticMappingInfo> DiagMap;
1793c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
1803c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  public:
181ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    typedef llvm::DenseMap<unsigned, DiagnosticMappingInfo>::iterator
182b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar      iterator;
183ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    typedef llvm::DenseMap<unsigned, DiagnosticMappingInfo>::const_iterator
184ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar      const_iterator;
1853efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis
186b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    void setMappingInfo(diag::kind Diag, DiagnosticMappingInfo Info) {
187b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar      DiagMap[Diag] = Info;
188b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    }
1893c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
190ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    DiagnosticMappingInfo &getOrAddMappingInfo(diag::kind Diag);
191ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar
192ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    const_iterator begin() const { return DiagMap.begin(); }
193ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    const_iterator end() const { return DiagMap.end(); }
1940827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  };
1953c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
1960827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Keeps and automatically disposes all DiagStates that we create.
1970827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  std::list<DiagState> DiagStates;
1980827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1990827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Represents a point in source where the diagnostic state was
2000827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// modified because of a pragma. 'Loc' can be null if the point represents
2010827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the diagnostic state modifications done through the command-line.
2020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  struct DiagStatePoint {
2030827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagState *State;
2040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    FullSourceLoc Loc;
2050827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStatePoint(DiagState *State, FullSourceLoc Loc)
2060827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      : State(State), Loc(Loc) { }
2070827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2080827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    bool operator<(const DiagStatePoint &RHS) const {
2090827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      // If Loc is invalid it means it came from <command-line>, in which case
2100827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      // we regard it as coming before any valid source location.
2110827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      if (RHS.Loc.isInvalid())
2120827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis        return false;
2130827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      if (Loc.isInvalid())
2140827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis        return true;
2150827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      return Loc.isBeforeInTranslationUnitThan(RHS.Loc);
2163c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer    }
2173c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  };
21804ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
2190827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief A vector of all DiagStatePoints representing changes in diagnostic
2200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// state due to diagnostic pragmas. The vector is always sorted according to
2210827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the SourceLocation of the DiagStatePoint.
2220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  typedef std::vector<DiagStatePoint> DiagStatePointsTy;
2230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  mutable DiagStatePointsTy DiagStatePoints;
2240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Keeps the DiagState that was active during each diagnostic 'push'
2260827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// so we can get back at it when we 'pop'.
2270827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  std::vector<DiagState *> DiagStateOnPushStack;
2280827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2290827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagState *GetCurDiagState() const {
2300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert(!DiagStatePoints.empty());
2310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return DiagStatePoints.back().State;
2320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2330827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void PushDiagStatePoint(DiagState *State, SourceLocation L) {
2350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    FullSourceLoc Loc(L, *SourceMgr);
2360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // Make sure that DiagStatePoints is always sorted according to Loc.
2370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert((Loc.isValid() || DiagStatePoints.empty()) &&
2380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis           "Adding invalid loc point after another point");
2390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert((Loc.isInvalid() || DiagStatePoints.empty() ||
2400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis            DiagStatePoints.back().Loc.isInvalid() ||
2410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis            DiagStatePoints.back().Loc.isBeforeInTranslationUnitThan(Loc)) &&
2420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis           "Previous point loc comes after or is the same as new one");
2430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStatePoints.push_back(DiagStatePoint(State,
2440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                             FullSourceLoc(Loc, *SourceMgr)));
2450827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2460827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Finds the DiagStatePoint that contains the diagnostic state of
2480827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the given source location.
2490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator GetDiagStatePointForLoc(SourceLocation Loc) const;
25004ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
25115221422eda7bac679e38b07512feda49715ef66Chris Lattner  /// ErrorOccurred / FatalErrorOccurred - This is set to true when an error or
25215221422eda7bac679e38b07512feda49715ef66Chris Lattner  /// fatal error is emitted, and is sticky.
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool ErrorOccurred;
25415221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool FatalErrorOccurred;
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25685bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  /// \brief Indicates that an unrecoverable error has occurred.
25785bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  bool UnrecoverableErrorOccurred;
25885bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
259c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  /// \brief Counts for DiagnosticErrorTrap to check whether an error occurred
260add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// during a parsing section, e.g. during parsing a function.
261c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned TrapNumErrorsOccurred;
262c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned TrapNumUnrecoverableErrorsOccurred;
263add80bb3957dba1d9389304453c0a4c975768bd7John McCall
264f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// LastDiagLevel - This is the level of the last diagnostic emitted.  This is
265f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// used to emit continuation diagnostics with the same level as the
266f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// diagnostic that they follow.
26733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticIDs::Level LastDiagLevel;
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26953eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned NumWarnings;       // Number of warnings reported
27053eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned NumErrors;         // Number of errors reported
2711864f2eb59471d07db51adfc3e5b1a229eed631aDouglas Gregor  unsigned NumErrorsSuppressed; // Number of errors suppressed
2727bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
2733fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// ArgToStringFn - A function pointer that converts an opaque diagnostic
2743fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// argument to a strings.  This takes the modifiers and argument that was
2753fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// present in the diagnostic.
276b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
277b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// The PrevArgs array (whose length is NumPrevArgs) indicates the previous
278b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// arguments formatted for this diagnostic.  Implementations of this function
279b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// can use this information to avoid redundancy across arguments.
280b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
28122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  /// This is a hack to avoid a layering violation between libbasic and libsema.
2820673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth  typedef void (*ArgToStringFnTy)(
2830673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      ArgumentKind Kind, intptr_t Val,
2840673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      const char *Modifier, unsigned ModifierLen,
2850673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      const char *Argument, unsigned ArgumentLen,
2860673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      const ArgumentValue *PrevArgs,
2870673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      unsigned NumPrevArgs,
288686775deca8b8685eb90801495880e3abdd844c2Chris Lattner      SmallVectorImpl<char> &Output,
2890673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      void *Cookie,
290686775deca8b8685eb90801495880e3abdd844c2Chris Lattner      SmallVectorImpl<intptr_t> &QualTypeVals);
29192dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void *ArgToStringCookie;
2923fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFnTy ArgToStringFn;
29393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
29493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief ID of the "delayed" diagnostic, which is a (typically
29593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// fatal) diagnostic that had to be delayed because it was found
29693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// while emitting another diagnostic.
29793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  unsigned DelayedDiagID;
29893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
29993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief First string argument for the delayed diagnostic.
30093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg1;
30193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
30293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Second string argument for the delayed diagnostic.
30393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg2;
30493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
306d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticsEngine(
307d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                      const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &Diags,
30878ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie                      DiagnosticConsumer *client = 0,
30933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                      bool ShouldOwnClient = true);
310d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  ~DiagnosticsEngine();
3111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &getDiagnosticIDs() const {
31333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags;
31433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
31533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
31678ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *getClient() { return Client; }
31778ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  const DiagnosticConsumer *getClient() const { return Client; }
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
319d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief Determine whether this \c DiagnosticsEngine object own its client.
32078243658c533168d51fd076fba328437932ba6f1Douglas Gregor  bool ownsClient() const { return OwnsDiagClient; }
32178243658c533168d51fd076fba328437932ba6f1Douglas Gregor
322bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Return the current diagnostic client along with ownership of that
323bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// client.
32478ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *takeClient() {
32533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    OwnsDiagClient = false;
32633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Client;
32733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
32833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
32933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool hasSourceManager() const { return SourceMgr != 0; }
33033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const {
33133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    assert(SourceMgr && "SourceManager not set!");
33233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return *SourceMgr;
33333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
33433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  void setSourceManager(SourceManager *SrcMgr) { SourceMgr = SrcMgr; }
33533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
33633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
337d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  //  DiagnosticsEngine characterization methods, used by a client to customize
338d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  //  how diagnostics are emitted.
33933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //
34004ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// pushMappings - Copies the current DiagMappings and pushes the new copy
34204ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// onto the top of the stack.
3430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void pushMappings(SourceLocation Loc);
34404ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
34504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// popMappings - Pops the current DiagMappings off the top of the stack
34604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// causing the new top of the stack to be the active mappings. Returns
34704ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// true if the pop happens, false if there is only one DiagMapping on the
34804ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// stack.
3490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  bool popMappings(SourceLocation Loc);
35004ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
351bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Set the diagnostic client associated with this diagnostic object.
352bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  ///
35333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// \param ShouldOwnClient true if the diagnostic object should take
35433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// ownership of \c client.
35578ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  void setClient(DiagnosticConsumer *client, bool ShouldOwnClient = true);
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
357c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  /// setErrorLimit - Specify a limit for the number of errors we should
358b205ac9fcd22b87b41697172d1983c5ae9dabaafChris Lattner  /// emit before giving up.  Zero disables the limit.
359c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  void setErrorLimit(unsigned Limit) { ErrorLimit = Limit; }
360b205ac9fcd22b87b41697172d1983c5ae9dabaafChris Lattner
361575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Specify the maximum number of template instantiation
362575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// notes to emit along with a given diagnostic.
363575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  void setTemplateBacktraceLimit(unsigned Limit) {
364575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    TemplateBacktraceLimit = Limit;
365575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
3666c1cb9916e9988dcdd65b9266dbe24afd173427aDouglas Gregor
367575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Retrieve the maximum number of template instantiation
368575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// nodes to emit along with a given diagnostic.
369575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned getTemplateBacktraceLimit() const {
370575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    return TemplateBacktraceLimit;
371575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
3726c1cb9916e9988dcdd65b9266dbe24afd173427aDouglas Gregor
3735b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  /// setIgnoreAllWarnings - When set to true, any unmapped warnings are
3745b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  /// ignored.  If this and WarningsAsErrors are both set, then this one wins.
3755b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }
3765b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; }
3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3781e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  /// setEnableAllWarnings - When set to true, any unmapped ignored warnings
3791e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  /// are no longer ignored.  If this and IgnoreAllWarnings are both set,
3801e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  /// then that one wins.
3811e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  void setEnableAllWarnings(bool Val) { EnableAllWarnings = Val; }
3821e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool getEnableAllWarnngs() const { return EnableAllWarnings; }
3831e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// setWarningsAsErrors - When set to true, any warnings reported are issued
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// as errors.
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getWarningsAsErrors() const { return WarningsAsErrors; }
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
389e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  /// setErrorsAsFatal - When set to true, any error reported is made a
390e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  /// fatal error.
391e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  void setErrorsAsFatal(bool Val) { ErrorsAsFatal = Val; }
392e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool getErrorsAsFatal() const { return ErrorsAsFatal; }
393e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner
3942fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  /// setSuppressSystemWarnings - When set to true mask warnings that
3952fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  /// come from system headers.
3962fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
3972fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; }
3982fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar
39981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Suppress all diagnostics, to silence the front end when we
40081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// know that we don't want any more diagnostics to be passed along to the
40181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// client
40281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  void setSuppressAllDiagnostics(bool Val = true) {
40381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor    SuppressAllDiagnostics = Val;
40481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  }
40581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
40681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
4075edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// \brief Specify which overload candidates to show when overload resolution
4085edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// fails.  By default, we show all candidates.
4095edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  void setShowOverloads(OverloadsShown Val) {
4105edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    ShowOverloads = Val;
4115edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  }
4125edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown getShowOverloads() const { return ShowOverloads; }
4135edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
4140b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  /// \brief Pretend that the last diagnostic issued was ignored. This can
4150b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  /// be used by clients who suppress diagnostics themselves.
4160b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  void setLastDiagnosticIgnored() {
41733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    LastDiagLevel = DiagnosticIDs::Ignored;
4180b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  }
4190b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor
420b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// setExtensionHandlingBehavior - This controls whether otherwise-unmapped
421b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// extension diagnostics are mapped onto ignore/warning/error.  This
422b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// corresponds to the GCC -pedantic and -pedantic-errors option.
423b54b276a920246c595a0498da281821eb9d22996Chris Lattner  void setExtensionHandlingBehavior(ExtensionHandling H) {
424b54b276a920246c595a0498da281821eb9d22996Chris Lattner    ExtBehavior = H;
425b54b276a920246c595a0498da281821eb9d22996Chris Lattner  }
426c1b5fa41f09512c74030b9a2a0d1564535e22a76Peter Collingbourne  ExtensionHandling getExtensionHandlingBehavior() const { return ExtBehavior; }
4271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42827ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  /// AllExtensionsSilenced - This is a counter bumped when an __extension__
42927ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  /// block is encountered.  When non-zero, all extension diagnostics are
43027ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  /// entirely silenced, no matter how they are mapped.
43127ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
43227ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
43391a2886d558ea6ca7a0bed73ab5acea5ae78eac2Douglas Gregor  bool hasAllExtensionsSilenced() { return AllExtensionsSilenced != 0; }
4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief This allows the client to specify that certain
436691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// warnings are ignored.  Notes can never be mapped, errors can only be
437691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
4380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
4390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location that this change of diagnostic state should
4400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the latest state.
4410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
4420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                            SourceLocation Loc);
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4443bc172bc6a787842db6fea351cf6929539fca70dChris Lattner  /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
4453bc172bc6a787842db6fea351cf6929539fca70dChris Lattner  /// "unknown-pragmas" to have the specified mapping.  This returns true and
4463bc172bc6a787842db6fea351cf6929539fca70dChris Lattner  /// ignores the request if "Group" was unknown, false otherwise.
4470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
4480827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// 'Loc' is the source location that this change of diagnostic state should
4490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the state from command-line.
450686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  bool setDiagnosticGroupMapping(StringRef Group, diag::Mapping Map,
4513f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar                                 SourceLocation Loc = SourceLocation());
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
453aeacae523a1c6e5bb99d12754415bbbc3ccb86c9Daniel Dunbar  /// \brief Set the warning-as-error flag for the given diagnostic group. This
454aeacae523a1c6e5bb99d12754415bbbc3ccb86c9Daniel Dunbar  /// function always only operates on the current diagnostic state.
4554aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  ///
4564aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  /// \returns True if the given group is unknown, false otherwise.
4574aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled);
4584aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
459aeacae523a1c6e5bb99d12754415bbbc3ccb86c9Daniel Dunbar  /// \brief Set the error-as-fatal flag for the given diagnostic group. This
460aeacae523a1c6e5bb99d12754415bbbc3ccb86c9Daniel Dunbar  /// function always only operates on the current diagnostic state.
4614aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  ///
4624aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  /// \returns True if the given group is unknown, false otherwise.
4634aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled);
4644aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool hasErrorOccurred() const { return ErrorOccurred; }
46615221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
46785bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
46885bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  /// \brief Determine whether any kind of unrecoverable error has occurred.
46985bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  bool hasUnrecoverableErrorOccurred() const {
47085bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor    return FatalErrorOccurred || UnrecoverableErrorOccurred;
47185bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  }
47285bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
47353eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned getNumWarnings() const { return NumWarnings; }
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
475c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  void setNumWarnings(unsigned NumWarnings) {
476c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    this->NumWarnings = NumWarnings;
477c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  }
478c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
479182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// getCustomDiagID - Return an ID for a diagnostic with the specified message
480182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// and level.  If this is the first request for this diagnosic, it is
481182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// registered and created, otherwise the existing ID is returned.
482686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getCustomDiagID(Level L, StringRef Message) {
48333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->getCustomDiagID((DiagnosticIDs::Level)L, Message);
48433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4863fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// ConvertArgToString - This method converts a diagnostic argument (as an
4873fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// intptr_t) into the string that represents it.
4883fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
4893fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          const char *Modifier, unsigned ModLen,
4903fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          const char *Argument, unsigned ArgLen,
491b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                          const ArgumentValue *PrevArgs, unsigned NumPrevArgs,
492686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                          SmallVectorImpl<char> &Output,
493686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                          SmallVectorImpl<intptr_t> &QualTypeVals) const {
494b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen,
4950673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth                  PrevArgs, NumPrevArgs, Output, ArgToStringCookie,
4960673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth                  QualTypeVals);
49722caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49992dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {
5003fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner    ArgToStringFn = Fn;
50192dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner    ArgToStringCookie = Cookie;
50222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
504abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reset the state of the diagnostic object to its initial
505abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// configuration.
506abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void Reset();
507abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
509d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  // DiagnosticsEngine classification and reporting interfaces.
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
512d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief Based on the way the client configured the DiagnosticsEngine
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// object, classify the specified diagnostic ID into a Level, consumable by
51478ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  /// the DiagnosticConsumer.
5150827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
5160827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location we are interested in finding out the
5170827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// diagnostic state. Can be null in order to query the latest state.
5181656aaec223949245fdbebf306b719cba0b687caDaniel Dunbar  Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc) const {
5191656aaec223949245fdbebf306b719cba0b687caDaniel Dunbar    return (Level)Diags->getDiagnosticLevel(DiagID, Loc, *this);
52033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
522509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// Report - Issue the message to the client.  @c DiagID is a member of the
523509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// @c diag::kind enum.  This actually returns aninstance of DiagnosticBuilder
524509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// which emits the diagnostics (through @c ProcessDiag) when it is destroyed.
525509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// @c Pos represents the source location associated with the diagnostic,
526509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// which can be an invalid location if no position information is available.
52733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  inline DiagnosticBuilder Report(SourceLocation Pos, unsigned DiagID);
5280f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbar  inline DiagnosticBuilder Report(unsigned DiagID);
529de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor
530e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  void Report(const StoredDiagnostic &storedDiag);
531e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
53293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Determine whethere there is already a diagnostic in flight.
5335800f7ea9cf5621280089d690f677bd28064d6b5Ted Kremenek  bool isDiagnosticInFlight() const { return CurDiagID != ~0U; }
53493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
53593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Set the "delayed" diagnostic that will be emitted once
53693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// the current diagnostic completes.
53793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
53893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  If a diagnostic is already in-flight but the front end must
53993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  report a problem (e.g., with an inconsistent file system
54093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  state), this routine sets a "delayed" diagnostic that will be
54193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  emitted after the current diagnostic completes. This should
54293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  only be used for fatal errors detected at inconvenient
54393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  times. If emitting a delayed diagnostic causes a second delayed
54493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  diagnostic to be introduced, that second delayed diagnostic
54593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  will be ignored.
54693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
54793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param DiagID The ID of the diagnostic being delayed.
54893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
54993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param Arg1 A string argument that will be provided to the
55093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// diagnostic. A copy of this string will be stored in the
551d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// DiagnosticsEngine object itself.
55293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
55393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param Arg2 A string argument that will be provided to the
55493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// diagnostic. A copy of this string will be stored in the
555d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// DiagnosticsEngine object itself.
556686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1 = "",
557686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            StringRef Arg2 = "");
55893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
559de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor  /// \brief Clear out the current diagnostic.
560de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor  void Clear() { CurDiagID = ~0U; }
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5620a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattnerprivate:
56393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Report the delayed diagnostic.
56493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  void ReportDelayed();
56593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
5663cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  // This is private state used by DiagnosticBuilder.  We put it here instead of
5673cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight
5680a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // object.  This implementation choice means that we can only have one
5690a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // diagnostic "in flight" at a time, but this seems to be a reasonable
5700a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // tradeoff to keep these objects small.  Assertions verify that only one
5710a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // diagnostic is in flight at a time.
57233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  friend class DiagnosticIDs;
5733cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  friend class DiagnosticBuilder;
57440847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  friend class Diagnostic;
5759b623639378d53a675921ddfa7316034d571881eDouglas Gregor  friend class PartialDiagnostic;
5769c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  friend class DiagnosticErrorTrap;
5779b623639378d53a675921ddfa7316034d571881eDouglas Gregor
578b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  /// CurDiagLoc - This is the location of the current diagnostic that is in
579b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  /// flight.
58033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceLocation CurDiagLoc;
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
582b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  /// CurDiagID - This is the ID of the current diagnostic that is in flight.
5833cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// This is set to ~0U when there is no diagnostic in flight.
584b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  unsigned CurDiagID;
585b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner
58673d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  enum {
58773d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    /// MaxArguments - The maximum number of arguments we can hold. We currently
58873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    /// only support up to 10 arguments (%0-%9).  A single diagnostic with more
58973d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    /// than that almost certainly has to be simplified anyway.
59073d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    MaxArguments = 10
59173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  };
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5933cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// NumDiagArgs - This contains the number of entries in Arguments.
594730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  signed char NumDiagArgs;
595730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  /// NumRanges - This is the number of ranges in the DiagRanges array.
596730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  unsigned char NumDiagRanges;
5974b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief The number of code modifications hints in the
598849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  /// FixItHints array.
599849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  unsigned char NumFixItHints;
600ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
60173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
60273d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// values, with one for each argument.  This specifies whether the argument
60373d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// is in DiagArgumentsStr or in DiagArguments.
60473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  unsigned char DiagArgumentsKind[MaxArguments];
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60673d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// DiagArgumentsStr - This holds the values of each string argument for the
60773d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// current diagnostic.  This value is only used when the corresponding
60873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// ArgumentKind is ak_std_string.
60973d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  std::string DiagArgumentsStr[MaxArguments];
61073d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner
61173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// DiagArgumentsVal - The values for the various substitution positions. This
61273d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// is used when the argument is not an std::string.  The specific value is
613fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  /// mangled into an intptr_t and the interpretation depends on exactly what
61473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// sort of argument kind it is.
61573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  intptr_t DiagArgumentsVal[MaxArguments];
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6170a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// DiagRanges - The list of ranges added to this diagnostic.  It currently
6180a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// only support 10 ranges, could easily be extended if needed.
6190a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  CharSourceRange DiagRanges[10];
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621b89fe6b04c5b8a2d080c1c5605b72f809fc9ee68Anna Zaks  enum { MaxFixItHints = 6 };
6224b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
623849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  /// FixItHints - If valid, provides a hint with some code
6244b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// to insert, remove, or modify at a particular position.
625849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  FixItHint FixItHints[MaxFixItHints];
6264b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
6270a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// ProcessDiag - This is the method used to report a diagnostic that is
6280a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// finally fully formed.
6295e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  ///
6305e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \returns true if the diagnostic was emitted, false if it was
6315e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// suppressed.
63233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool ProcessDiag() {
63333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->ProcessDiag(*this);
63433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
635f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis
636f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  friend class ASTReader;
637f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  friend class ASTWriter;
6383cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner};
6393cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
6409c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// \brief RAII class that determines when any errors have occurred
6419c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// between the time the instance was created and the time it was
6429c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// queried.
6439c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidisclass DiagnosticErrorTrap {
644d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &Diag;
645c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned NumErrors;
646c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned NumUnrecoverableErrors;
6479c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6489c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidispublic:
649d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticErrorTrap(DiagnosticsEngine &Diag)
650add80bb3957dba1d9389304453c0a4c975768bd7John McCall    : Diag(Diag) { reset(); }
6519c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6529c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  /// \brief Determine whether any errors have occurred since this
6539c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  /// object instance was created.
6549c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  bool hasErrorOccurred() const {
655c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    return Diag.TrapNumErrorsOccurred > NumErrors;
656add80bb3957dba1d9389304453c0a4c975768bd7John McCall  }
657add80bb3957dba1d9389304453c0a4c975768bd7John McCall
658add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// \brief Determine whether any unrecoverable errors have occurred since this
659add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// object instance was created.
660add80bb3957dba1d9389304453c0a4c975768bd7John McCall  bool hasUnrecoverableErrorOccurred() const {
661c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    return Diag.TrapNumUnrecoverableErrorsOccurred > NumUnrecoverableErrors;
6629c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  }
6639c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6649c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  // Set to initial state of "no errors occurred".
665add80bb3957dba1d9389304453c0a4c975768bd7John McCall  void reset() {
666c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    NumErrors = Diag.TrapNumErrorsOccurred;
667c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    NumUnrecoverableErrors = Diag.TrapNumUnrecoverableErrorsOccurred;
668add80bb3957dba1d9389304453c0a4c975768bd7John McCall  }
6699c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis};
6709c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6713cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
6723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner// DiagnosticBuilder
6733cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
6743cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
6753cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// DiagnosticBuilder - This is a little helper class used to produce
676d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie/// diagnostics.  This is constructed by the DiagnosticsEngine::Report method,
677d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie/// and allows insertion of extra information (arguments and source ranges) into
678d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie/// the currently "in flight" diagnostic.  When the temporary for the builder is
6793cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// destroyed, the diagnostic is issued.
6803cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner///
6813cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// Note that many of these will be created as temporary objects (many call
6823cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// sites), so we want them to be small and we never want their address taken.
6833cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// This ensures that compilers with somewhat reasonable optimizers will promote
6843cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// the common fields to registers, eliminating increments of the NumArgs field,
6853cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// for example.
6863cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerclass DiagnosticBuilder {
687d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  mutable DiagnosticsEngine *DiagObj;
688849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  mutable unsigned NumArgs, NumRanges, NumFixItHints;
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6903cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT
691d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  friend class DiagnosticsEngine;
692d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticBuilder(DiagnosticsEngine *diagObj)
693849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    : DiagObj(diagObj), NumArgs(0), NumRanges(0), NumFixItHints(0) {}
694ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
6959b623639378d53a675921ddfa7316034d571881eDouglas Gregor  friend class PartialDiagnostic;
696b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
697b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregorprotected:
698b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  void FlushCounts();
6999b623639378d53a675921ddfa7316034d571881eDouglas Gregor
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
7010a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// Copy constructor.  When copied, this "takes" the diagnostic info from the
7020a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// input and neuters it.
7033cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DiagnosticBuilder(const DiagnosticBuilder &D) {
7040a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    DiagObj = D.DiagObj;
7050a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    D.DiagObj = 0;
70625a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    NumArgs = D.NumArgs;
70725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    NumRanges = D.NumRanges;
708849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    NumFixItHints = D.NumFixItHints;
7090a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
71025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor
7115e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \brief Simple enumeration value used to give a name to the
7125e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// suppress-diagnostic constructor.
7135e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  enum SuppressKind { Suppress };
7145e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor
7155e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \brief Create an empty DiagnosticBuilder object that represents
7165e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// no actual diagnostic.
7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  explicit DiagnosticBuilder(SuppressKind)
718849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    : DiagObj(0), NumArgs(0), NumRanges(0), NumFixItHints(0) { }
7195e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor
72025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// \brief Force the diagnostic builder to emit the diagnostic now.
72125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  ///
72225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// Once this function has been called, the DiagnosticBuilder object
72325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// should not be used again before it is destroyed.
7245e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  ///
7255e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \returns true if a diagnostic was emitted, false if the
7265e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// diagnostic was suppressed.
72793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  bool Emit();
72825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor
72925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// Destructor - The dtor emits the diagnostic if it hasn't already
73025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// been emitted.
73125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  ~DiagnosticBuilder() { Emit(); }
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
733d93256e55673a17d18543397ec462416acb13792Douglas Gregor  /// isActive - Determine whether this diagnostic is still active.
734d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool isActive() const { return DiagObj != 0; }
735d93256e55673a17d18543397ec462416acb13792Douglas Gregor
7369b623639378d53a675921ddfa7316034d571881eDouglas Gregor  /// \brief Retrieve the active diagnostic ID.
7379b623639378d53a675921ddfa7316034d571881eDouglas Gregor  ///
7389b623639378d53a675921ddfa7316034d571881eDouglas Gregor  /// \pre \c isActive()
7399b623639378d53a675921ddfa7316034d571881eDouglas Gregor  unsigned getDiagID() const {
740d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(isActive() && "DiagnosticsEngine is inactive");
7419b623639378d53a675921ddfa7316034d571881eDouglas Gregor    return DiagObj->CurDiagID;
7429b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
7439b623639378d53a675921ddfa7316034d571881eDouglas Gregor
7449b623639378d53a675921ddfa7316034d571881eDouglas Gregor  /// \brief Clear out the current diagnostic.
7459b623639378d53a675921ddfa7316034d571881eDouglas Gregor  void Clear() { DiagObj = 0; }
7469b623639378d53a675921ddfa7316034d571881eDouglas Gregor
7473cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// Operator bool: conversion of DiagnosticBuilder to bool always returns
7483cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// true.  This allows is to be used in boolean error contexts like:
7493cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// return Diag(...);
7503cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  operator bool() const { return true; }
7513cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
752686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void AddString(StringRef S) const {
753d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(NumArgs < DiagnosticsEngine::MaxArguments &&
7543cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
7555e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj) {
756d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie      DiagObj->DiagArgumentsKind[NumArgs] = DiagnosticsEngine::ak_std_string;
7575e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsStr[NumArgs++] = S;
7585e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    }
7593cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  }
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
761d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
762d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(NumArgs < DiagnosticsEngine::MaxArguments &&
7633cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
7645e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj) {
7655e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsKind[NumArgs] = Kind;
7665e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsVal[NumArgs++] = V;
7675e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    }
7680a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7700a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  void AddSourceRange(const CharSourceRange &R) const {
7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(NumRanges <
7723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) &&
7733cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
7745e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj)
77551c6d384551674facc19f745ecf6e289d28dc55fDouglas Gregor      DiagObj->DiagRanges[NumRanges++] = R;
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7774b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
778849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  void AddFixItHint(const FixItHint &Hint) const {
779d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(NumFixItHints < DiagnosticsEngine::MaxFixItHints &&
780849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor           "Too many fix-it hints!");
7817035f310f5cfedb35ee4422bcab98e9f18f4ce63Matt Beaumont-Gay    if (NumFixItHints >= DiagnosticsEngine::MaxFixItHints)
7827035f310f5cfedb35ee4422bcab98e9f18f4ce63Matt Beaumont-Gay      return;  // Don't crash in release builds
7835e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj)
784849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      DiagObj->FixItHints[NumFixItHints++] = Hint;
7854b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
7863cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner};
7873cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7883cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
789686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                           StringRef S) {
7903cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddString(S);
7913cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
7923cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
7933cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7943cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
7953cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const char *Str) {
7963cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str),
797d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                  DiagnosticsEngine::ak_c_string);
7983cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
7993cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8003cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
8013cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) {
802d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
8033cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
8043cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8053cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
8063bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidisinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,bool I) {
807d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
8083bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis  return DB;
8093bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis}
8103bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis
8113cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
8123cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           unsigned I) {
813d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DB.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
8143cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
8153cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8163cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
8173cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
8183cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const IdentifierInfo *II) {
8193cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(reinterpret_cast<intptr_t>(II),
820d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                  DiagnosticsEngine::ak_identifierinfo);
8213cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
8223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8243f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// Adds a DeclContext to the diagnostic. The enable_if template magic is here
8253f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// so that we only match those arguments that are (statically) DeclContexts;
8263f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// other arguments that derive from DeclContext (e.g., RecordDecls) will not
8273f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// match.
8283f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregortemplate<typename T>
8293f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregorinline
8303f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregortypename llvm::enable_if<llvm::is_same<T, DeclContext>,
8313f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor                         const DiagnosticBuilder &>::type
8323f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregoroperator<<(const DiagnosticBuilder &DB, T *DC) {
8333f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor  DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
834d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                  DiagnosticsEngine::ak_declcontext);
8353f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor  return DB;
8363f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor}
8373f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor
8383cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
8393cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const SourceRange &R) {
8400a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  DB.AddSourceRange(CharSourceRange::getTokenRange(R));
8413cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
8423cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8434b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
8444b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
8450a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner                                           const CharSourceRange &R) {
8460a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  DB.AddSourceRange(R);
8470a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  return DB;
8480a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner}
8490a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
8500a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
851849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor                                           const FixItHint &Hint) {
852849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  DB.AddFixItHint(Hint);
8534b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  return DB;
8544b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
855ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
8563cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// Report - Issue the message to the client.  DiagID is a member of the
8573cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// diag::kind enum.  This actually returns a new instance of DiagnosticBuilder
8583cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// which emits the diagnostics (through ProcessDiag) when it is destroyed.
859d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieinline DiagnosticBuilder DiagnosticsEngine::Report(SourceLocation Loc,
86033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                                            unsigned DiagID){
8613cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
8623cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  CurDiagLoc = Loc;
8633cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  CurDiagID = DiagID;
8643cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DiagnosticBuilder(this);
8653cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
866d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieinline DiagnosticBuilder DiagnosticsEngine::Report(unsigned DiagID) {
86733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Report(SourceLocation(), DiagID);
8680f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbar}
8693cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
8703cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
87140847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie// Diagnostic
8723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87440847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie/// Diagnostic - This is a little helper class (which is basically a smart
875d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie/// pointer that forward info from DiagnosticsEngine) that allows clients to
876d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie/// enquire about the currently in-flight diagnostic.
87740847cfb58acc3cac7d68727df9455ac45f2e118David Blaikieclass Diagnostic {
878d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine *DiagObj;
879686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef StoredDiagMessage;
8803cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerpublic:
88140847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  explicit Diagnostic(const DiagnosticsEngine *DO) : DiagObj(DO) {}
88240847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  Diagnostic(const DiagnosticsEngine *DO, StringRef storedDiagMessage)
883e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis    : DiagObj(DO), StoredDiagMessage(storedDiagMessage) {}
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
885d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine *getDiags() const { return DiagObj; }
886b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  unsigned getID() const { return DiagObj->CurDiagID; }
88733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const SourceLocation &getLocation() const { return DiagObj->CurDiagLoc; }
88833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool hasSourceManager() const { return DiagObj->hasSourceManager(); }
88933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const { return DiagObj->getSourceManager();}
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8910a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  unsigned getNumArgs() const { return DiagObj->NumDiagArgs; }
8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89373d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// getArgKind - Return the kind of the specified index.  Based on the kind
89473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// of argument, the accessors below can be used to get the value.
895d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine::ArgumentKind getArgKind(unsigned Idx) const {
8963cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(Idx < getNumArgs() && "Argument index out of range!");
897d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    return (DiagnosticsEngine::ArgumentKind)DiagObj->DiagArgumentsKind[Idx];
89873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  }
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90073d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// getArgStdStr - Return the provided argument string specified by Idx.
90173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  const std::string &getArgStdStr(unsigned Idx) const {
902d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_std_string &&
9033cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
90473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    return DiagObj->DiagArgumentsStr[Idx];
90573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  }
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90773d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// getArgCStr - Return the specified C string argument.
90873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  const char *getArgCStr(unsigned Idx) const {
909d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_c_string &&
9103cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
91173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]);
9120a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91430bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  /// getArgSInt - Return the specified signed integer argument.
91530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  int getArgSInt(unsigned Idx) const {
916d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_sint &&
9173cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
91830bc96544346bea42921cf6837e66cef80d664b4Chris Lattner    return (int)DiagObj->DiagArgumentsVal[Idx];
91930bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  }
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
92130bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  /// getArgUInt - Return the specified unsigned integer argument.
92230bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  unsigned getArgUInt(unsigned Idx) const {
923d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_uint &&
9243cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
92530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner    return (unsigned)DiagObj->DiagArgumentsVal[Idx];
92630bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  }
9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
92843b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  /// getArgIdentifier - Return the specified IdentifierInfo argument.
92943b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  const IdentifierInfo *getArgIdentifier(unsigned Idx) const {
930d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_identifierinfo &&
9313cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
9329634379265855f1628190e926d9aaf1fb4a5d90eChris Lattner    return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]);
93343b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  }
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93522caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  /// getRawArg - Return the specified non-string argument in an opaque form.
93622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  intptr_t getRawArg(unsigned Idx) const {
937d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) != DiagnosticsEngine::ak_std_string &&
93822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner           "invalid argument accessor!");
93922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    return DiagObj->DiagArgumentsVal[Idx];
94022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9430a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// getNumRanges - Return the number of source ranges associated with this
9440a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// diagnostic.
9450a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  unsigned getNumRanges() const {
9460a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    return DiagObj->NumDiagRanges;
9470a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
9481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9490a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  const CharSourceRange &getRange(unsigned Idx) const {
9500a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!");
95151c6d384551674facc19f745ecf6e289d28dc55fDouglas Gregor    return DiagObj->DiagRanges[Idx];
9520a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95428409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth  /// \brief Return an array reference for this diagnostic's ranges.
95528409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth  ArrayRef<CharSourceRange> getRanges() const {
95628409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth    return llvm::makeArrayRef(DiagObj->DiagRanges, DiagObj->NumDiagRanges);
95728409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth  }
95828409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth
959849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  unsigned getNumFixItHints() const {
960849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    return DiagObj->NumFixItHints;
9614b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
9624b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
963849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  const FixItHint &getFixItHint(unsigned Idx) const {
964849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    return DiagObj->FixItHints[Idx];
9654b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
9664b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
967849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  const FixItHint *getFixItHints() const {
968849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    return DiagObj->NumFixItHints?
969849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor             &DiagObj->FixItHints[0] : 0;
9704b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
9714b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
972f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  /// FormatDiagnostic - Format this diagnostic into a string, substituting the
973f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  /// formal arguments into the %0 slots.  The result is appended onto the Str
974f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  /// array.
975686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void FormatDiagnostic(SmallVectorImpl<char> &OutStr) const;
9769f28614bf1a8387000d8df57a713fcf69e198145John McCall
9779f28614bf1a8387000d8df57a713fcf69e198145John McCall  /// FormatDiagnostic - Format the given format-string into the
9789f28614bf1a8387000d8df57a713fcf69e198145John McCall  /// output buffer using the arguments stored in this diagnostic.
9799f28614bf1a8387000d8df57a713fcf69e198145John McCall  void FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
980686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                        SmallVectorImpl<char> &OutStr) const;
981a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor};
982a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
983a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor/**
984d3ab63e0f66429abf2a3e4cde889e420e41e8790Douglas Gregor * \brief Represents a diagnostic in a form that can be retained until its
985d3ab63e0f66429abf2a3e4cde889e420e41e8790Douglas Gregor * corresponding source manager is destroyed.
986a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor */
987a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregorclass StoredDiagnostic {
988aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  unsigned ID;
989d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine::Level Level;
990a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  FullSourceLoc Loc;
991a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  std::string Message;
9920a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  std::vector<CharSourceRange> Ranges;
993849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  std::vector<FixItHint> FixIts;
994a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
995a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregorpublic:
996a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  StoredDiagnostic();
99740847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  StoredDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info);
998d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
999686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                   StringRef Message);
1000d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1001686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                   StringRef Message, FullSourceLoc Loc,
10022d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                   ArrayRef<CharSourceRange> Ranges,
10032d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                   ArrayRef<FixItHint> Fixits);
1004a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  ~StoredDiagnostic();
1005a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1006a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief Evaluates true when this object stores a diagnostic.
1007a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  operator bool() const { return Message.size() > 0; }
1008a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1009aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  unsigned getID() const { return ID; }
1010d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine::Level getLevel() const { return Level; }
1011a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  const FullSourceLoc &getLocation() const { return Loc; }
1012686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMessage() const { return Message; }
1013c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
1014c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  void setLocation(FullSourceLoc Loc) { this->Loc = Loc; }
1015c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
10160a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  typedef std::vector<CharSourceRange>::const_iterator range_iterator;
1017a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  range_iterator range_begin() const { return Ranges.begin(); }
1018a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  range_iterator range_end() const { return Ranges.end(); }
1019a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  unsigned range_size() const { return Ranges.size(); }
1020a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1021849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  typedef std::vector<FixItHint>::const_iterator fixit_iterator;
1022a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  fixit_iterator fixit_begin() const { return FixIts.begin(); }
1023a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  fixit_iterator fixit_end() const { return FixIts.end(); }
1024a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  unsigned fixit_size() const { return FixIts.size(); }
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102778ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie/// DiagnosticConsumer - This is an abstract interface implemented by clients of
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the front-end, which formats and prints fully processed diagnostics.
102978ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikieclass DiagnosticConsumer {
1030ab41b97c3cddf1bcdd8bf82ab09ed3ceafcd05b1Argyrios Kyrtzidisprotected:
1031f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned NumWarnings;       // Number of warnings reported
1032f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned NumErrors;         // Number of errors reported
1033f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
103578ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer() : NumWarnings(0), NumErrors(0) { }
1036f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
1037f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned getNumErrors() const { return NumErrors; }
1038f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned getNumWarnings() const { return NumWarnings; }
1039f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
104078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  virtual ~DiagnosticConsumer();
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1042efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// BeginSourceFile - Callback to inform the diagnostic client that processing
1043efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// of a source file is beginning.
1044efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  ///
1045efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// Note that diagnostics may be emitted outside the processing of a source
1046efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// file, for example during the parsing of command line options. However,
1047efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// diagnostics with source range information are required to only be emitted
1048efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// in between BeginSourceFile() and EndSourceFile().
1049efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  ///
1050efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// \arg LO - The language options for the source file being processed.
1051efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// \arg PP - The preprocessor object being used for the source; this optional
1052efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// and may not be present, for example when processing AST source files.
10537d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  virtual void BeginSourceFile(const LangOptions &LangOpts,
10547d90199f109290e9d587479a481a2850d390b552Daniel Dunbar                               const Preprocessor *PP = 0) {}
1055efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar
1056efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// EndSourceFile - Callback to inform the diagnostic client that processing
1057efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// of a source file has ended. The diagnostic client should assume that any
1058efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// objects made available via \see BeginSourceFile() are inaccessible.
1059efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  virtual void EndSourceFile() {}
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1061cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek  /// IncludeInDiagnosticCounts - This method (whose default implementation
1062efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// returns true) indicates whether the diagnostics handled by this
106378ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  /// DiagnosticConsumer should be included in the number of diagnostics
106478ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  /// reported by DiagnosticsEngine.
1065cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek  virtual bool IncludeInDiagnosticCounts() const;
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// capturing it to a log as needed.
1069f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  ///
1070f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  /// Default implementation just keeps track of the total number of warnings
1071f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  /// and errors.
1072d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
107340847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie                                const Diagnostic &Info);
1074aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor
1075aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  /// \brief Clone the diagnostic consumer, producing an equivalent consumer
1076aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  /// that can be used in a different context.
1077aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const = 0;
1078aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor};
1079aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor
1080aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor/// IgnoringDiagConsumer - This is a diagnostic client that just ignores all
1081aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor/// diags.
1082aee526e77657afd1600276450e9c346953ad51d7Douglas Gregorclass IgnoringDiagConsumer : public DiagnosticConsumer {
1083aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
1084aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                        const Diagnostic &Info) {
1085aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor    // Just ignore it.
1086aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  }
1087aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
1088aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor    return new IgnoringDiagConsumer();
1089aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  }
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1095