Diagnostic.h revision add80bb3957dba1d9389304453c0a4c975768bd7
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"
190827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include "llvm/ADT/DenseMap.h"
2028019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
21bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor#include "llvm/ADT/OwningPtr.h"
223f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor#include "llvm/Support/type_traits.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis#include <vector>
250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include <list>
26f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
28b2153946fff330e54abfdc740c0a83aedd485b33Anders Carlsson  class DiagnosticClient;
2933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DiagnosticBuilder;
30d42ffbd22fc7eb61321f6a88173ee424991f01c6Ted Kremenek  class IdentifierInfo;
3133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DeclContext;
3240469651a3f8379dc0f32df69e9bade06a2aad36Chris Lattner  class LangOptions;
337d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  class Preprocessor;
349c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  class DiagnosticErrorTrap;
35e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  class StoredDiagnostic;
361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
374b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Annotates a diagnostic with some code that should be
384b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// inserted, removed, or replaced to fix the problem.
394b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
404b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// This kind of hint should be used when we are certain that the
414b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// introduction, removal, or modification of a particular (small!)
424b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// amount of code will correct a compilation error. The compiler
434b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// should also provide full recovery from such errors, such that
44b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor/// suppressing the diagnostic output can still result in successful
454b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// compilation.
46849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregorclass FixItHint {
474b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorpublic:
48783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// \brief Code that should be replaced to correct the error. Empty for an
49783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// insertion hint.
500a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  CharSourceRange RemoveRange;
514b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
524b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief The actual code to insert at the insertion location, as a
534b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// string.
544b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  std::string CodeToInsert;
554b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
564b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Empty code modification hint, indicating that no code
574b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// modification is known.
58783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  FixItHint() : RemoveRange() { }
594b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
602f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  bool isNull() const {
61783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    return !RemoveRange.isValid();
622f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  }
632f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson
644b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that inserts the given
654b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// code string at a specific location.
66849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  static FixItHint CreateInsertion(SourceLocation InsertionLoc,
67849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor                                   llvm::StringRef Code) {
68849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
69783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    Hint.RemoveRange =
70783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor      CharSourceRange(SourceRange(InsertionLoc, InsertionLoc), false);
71b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
72b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
73b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
744b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
754b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that removes the given
764b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range.
770a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(CharSourceRange RemoveRange) {
78849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
79b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
80b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
81b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
820a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(SourceRange RemoveRange) {
830a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateRemoval(CharSourceRange::getTokenRange(RemoveRange));
840a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
850a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
864b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that replaces the given
874b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range with the given code string.
880a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(CharSourceRange RemoveRange,
89849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor                                     llvm::StringRef Code) {
90849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
91b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
92b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
93b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
94b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
950a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
960a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(SourceRange RemoveRange,
970a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner                                     llvm::StringRef Code) {
980a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateReplacement(CharSourceRange::getTokenRange(RemoveRange), Code);
990a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
1004b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor};
1014b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diagnostic - This concrete class is used by the front-end to report
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// problems and issues.  It massages the diagnostics (e.g. handling things like
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// "report warnings as errors" and passes them off to the DiagnosticClient for
10533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis/// reporting to the user. Diagnostic is tied to one translation unit and
10633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis/// one SourceManager.
10728019772db70d4547be05a042eb950bc910f134fDouglas Gregorclass Diagnostic : public llvm::RefCountedBase<Diagnostic> {
108182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattnerpublic:
109182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// Level - The level of the diagnostic, after it has been through mapping.
110182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  enum Level {
11133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Ignored = DiagnosticIDs::Ignored,
11233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Note = DiagnosticIDs::Note,
11333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Warning = DiagnosticIDs::Warning,
11433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Error = DiagnosticIDs::Error,
11533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Fatal = DiagnosticIDs::Fatal
116182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  };
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// ExtensionHandling - How do we handle otherwise-unmapped extension?  This
119b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// is controlled by -pedantic and -pedantic-errors.
120b54b276a920246c595a0498da281821eb9d22996Chris Lattner  enum ExtensionHandling {
121b54b276a920246c595a0498da281821eb9d22996Chris Lattner    Ext_Ignore, Ext_Warn, Ext_Error
122b54b276a920246c595a0498da281821eb9d22996Chris Lattner  };
1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1243fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  enum ArgumentKind {
12547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_std_string,      // std::string
12647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_c_string,        // const char *
12747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_sint,            // int
12847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_uint,            // unsigned
12947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_identifierinfo,  // IdentifierInfo
13047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_qualtype,        // QualType
13147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    ak_declarationname, // DeclarationName
132dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    ak_nameddecl,       // NamedDecl *
1333f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor    ak_nestednamespec,  // NestedNameSpecifier *
1343f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor    ak_declcontext      // DeclContext *
1353fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  };
1365edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
1375edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// Specifies which overload candidates to display when overload resolution
1385edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// fails.
1395edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  enum OverloadsShown {
1405edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    Ovl_All,  ///< Show all overloads.
1415edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    Ovl_Best  ///< Show just the "best" overload candidates.
1425edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  };
1435edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
144b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ArgumentValue - This typedef represents on argument value, which is a
145b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// union discriminated by ArgumentKind, with a value.
146b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  typedef std::pair<ArgumentKind, intptr_t> ArgumentValue;
147ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpprivate:
14927ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  unsigned char AllExtensionsSilenced; // Used by __extension__
150b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool IgnoreAllWarnings;        // Ignore all warnings: -w
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool WarningsAsErrors;         // Treat warnings like errors:
152e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool ErrorsAsFatal;            // Treat errors like fatal errors.
153b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool SuppressSystemWarnings;   // Suppress warnings in system headers.
15481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool SuppressAllDiagnostics;   // Suppress all diagnostics.
1555edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown ShowOverloads;  // Which overload candidates to show.
156c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  unsigned ErrorLimit;           // Cap of # errors emitted, 0 -> no limit.
157575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned TemplateBacktraceLimit; // Cap on depth of template backtrace stack,
158575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor                                   // 0 -> no limit.
159b54b276a920246c595a0498da281821eb9d22996Chris Lattner  ExtensionHandling ExtBehavior; // Map extensions onto warnings or errors?
16033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  llvm::IntrusiveRefCntPtr<DiagnosticIDs> Diags;
16133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticClient *Client;
16233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool OwnsDiagClient;
16333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager *SourceMgr;
1640827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1650827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Mapping information for diagnostics.  Mapping info is
166691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// packed into four bits per diagnostic.  The low three bits are the mapping
167691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// (an instance of diag::Mapping), or zero if unset.  The high bit is set
168691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// when the mapping was established as a user mapping.  If the high bit is
169691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// clear, then the low bits are set to the default value, and should be
170691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// mapped with -pedantic, -Werror, etc.
1710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
1723efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// A new DiagState is created and kept around when diagnostic pragmas modify
1733efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// the state so that we know what is the diagnostic state at any given
1743efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// source location.
1750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  class DiagState {
1763efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    llvm::DenseMap<unsigned, unsigned> DiagMap;
1773c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
1783c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  public:
1793efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    typedef llvm::DenseMap<unsigned, unsigned>::const_iterator iterator;
1803efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis
1810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    void setMapping(diag::kind Diag, unsigned Map) { DiagMap[Diag] = Map; }
1823c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
1830827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    diag::Mapping getMapping(diag::kind Diag) const {
1843efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      iterator I = DiagMap.find(Diag);
1853efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      if (I != DiagMap.end())
1863efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis        return (diag::Mapping)I->second;
1873efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      return diag::Mapping();
1883c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer    }
1893efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis
1903efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    iterator begin() const { return DiagMap.begin(); }
1913efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    iterator end() const { return DiagMap.end(); }
1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  };
1933c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
1940827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Keeps and automatically disposes all DiagStates that we create.
1950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  std::list<DiagState> DiagStates;
1960827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1970827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Represents a point in source where the diagnostic state was
1980827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// modified because of a pragma. 'Loc' can be null if the point represents
1990827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the diagnostic state modifications done through the command-line.
2000827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  struct DiagStatePoint {
2010827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagState *State;
2020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    FullSourceLoc Loc;
2030827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStatePoint(DiagState *State, FullSourceLoc Loc)
2040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      : State(State), Loc(Loc) { }
2050827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2060827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    bool operator<(const DiagStatePoint &RHS) const {
2070827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      // If Loc is invalid it means it came from <command-line>, in which case
2080827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      // we regard it as coming before any valid source location.
2090827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      if (RHS.Loc.isInvalid())
2100827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis        return false;
2110827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      if (Loc.isInvalid())
2120827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis        return true;
2130827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      return Loc.isBeforeInTranslationUnitThan(RHS.Loc);
2143c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer    }
2153c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  };
21604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
2170827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief A vector of all DiagStatePoints representing changes in diagnostic
2180827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// state due to diagnostic pragmas. The vector is always sorted according to
2190827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the SourceLocation of the DiagStatePoint.
2200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  typedef std::vector<DiagStatePoint> DiagStatePointsTy;
2210827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  mutable DiagStatePointsTy DiagStatePoints;
2220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Keeps the DiagState that was active during each diagnostic 'push'
2240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// so we can get back at it when we 'pop'.
2250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  std::vector<DiagState *> DiagStateOnPushStack;
2260827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2270827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagState *GetCurDiagState() const {
2280827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert(!DiagStatePoints.empty());
2290827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return DiagStatePoints.back().State;
2300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void PushDiagStatePoint(DiagState *State, SourceLocation L) {
2330827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    FullSourceLoc Loc(L, *SourceMgr);
2340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // Make sure that DiagStatePoints is always sorted according to Loc.
2350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert((Loc.isValid() || DiagStatePoints.empty()) &&
2360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis           "Adding invalid loc point after another point");
2370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert((Loc.isInvalid() || DiagStatePoints.empty() ||
2380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis            DiagStatePoints.back().Loc.isInvalid() ||
2390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis            DiagStatePoints.back().Loc.isBeforeInTranslationUnitThan(Loc)) &&
2400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis           "Previous point loc comes after or is the same as new one");
2410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStatePoints.push_back(DiagStatePoint(State,
2420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                             FullSourceLoc(Loc, *SourceMgr)));
2430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2450827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Finds the DiagStatePoint that contains the diagnostic state of
2460827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the given source location.
2470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator GetDiagStatePointForLoc(SourceLocation Loc) const;
24804ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
24915221422eda7bac679e38b07512feda49715ef66Chris Lattner  /// ErrorOccurred / FatalErrorOccurred - This is set to true when an error or
25015221422eda7bac679e38b07512feda49715ef66Chris Lattner  /// fatal error is emitted, and is sticky.
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool ErrorOccurred;
25215221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool FatalErrorOccurred;
2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// \brief Toggles for DiagnosticErrorTrap to check whether an error occurred
255add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// during a parsing section, e.g. during parsing a function.
256add80bb3957dba1d9389304453c0a4c975768bd7John McCall  bool TrapErrorOccurred;
257add80bb3957dba1d9389304453c0a4c975768bd7John McCall  bool TrapUnrecoverableErrorOccurred;
258add80bb3957dba1d9389304453c0a4c975768bd7John McCall
259f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// LastDiagLevel - This is the level of the last diagnostic emitted.  This is
260f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// used to emit continuation diagnostics with the same level as the
261f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// diagnostic that they follow.
26233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticIDs::Level LastDiagLevel;
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26453eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned NumWarnings;       // Number of warnings reported
26553eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned NumErrors;         // Number of errors reported
2661864f2eb59471d07db51adfc3e5b1a229eed631aDouglas Gregor  unsigned NumErrorsSuppressed; // Number of errors suppressed
2677bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
2683fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// ArgToStringFn - A function pointer that converts an opaque diagnostic
2693fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// argument to a strings.  This takes the modifiers and argument that was
2703fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// present in the diagnostic.
271b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
272b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// The PrevArgs array (whose length is NumPrevArgs) indicates the previous
273b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// arguments formatted for this diagnostic.  Implementations of this function
274b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// can use this information to avoid redundancy across arguments.
275b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
27622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  /// This is a hack to avoid a layering violation between libbasic and libsema.
2773fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  typedef void (*ArgToStringFnTy)(ArgumentKind Kind, intptr_t Val,
2783fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                                  const char *Modifier, unsigned ModifierLen,
2793fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                                  const char *Argument, unsigned ArgumentLen,
280b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                  const ArgumentValue *PrevArgs,
281b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                  unsigned NumPrevArgs,
28292dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner                                  llvm::SmallVectorImpl<char> &Output,
28392dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner                                  void *Cookie);
28492dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void *ArgToStringCookie;
2853fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFnTy ArgToStringFn;
28693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
28793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief ID of the "delayed" diagnostic, which is a (typically
28893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// fatal) diagnostic that had to be delayed because it was found
28993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// while emitting another diagnostic.
29093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  unsigned DelayedDiagID;
29193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
29293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief First string argument for the delayed diagnostic.
29393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg1;
29493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
29593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Second string argument for the delayed diagnostic.
29693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg2;
29793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
29933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  explicit Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &Diags,
30033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                      DiagnosticClient *client = 0,
30133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                      bool ShouldOwnClient = true);
302182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  ~Diagnostic();
3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &getDiagnosticIDs() const {
30533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags;
30633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
30733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
30833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticClient *getClient() { return Client; }
30933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const DiagnosticClient *getClient() const { return Client; }
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Return the current diagnostic client along with ownership of that
312bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// client.
31333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticClient *takeClient() {
31433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    OwnsDiagClient = false;
31533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Client;
31633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
31733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
31833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool hasSourceManager() const { return SourceMgr != 0; }
31933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const {
32033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    assert(SourceMgr && "SourceManager not set!");
32133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return *SourceMgr;
32233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
32333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  void setSourceManager(SourceManager *SrcMgr) { SourceMgr = SrcMgr; }
32433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
32533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
32633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //  Diagnostic characterization methods, used by a client to customize how
32733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //  diagnostics are emitted.
32833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //
32904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// pushMappings - Copies the current DiagMappings and pushes the new copy
33104ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// onto the top of the stack.
3320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void pushMappings(SourceLocation Loc);
33304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
33404ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// popMappings - Pops the current DiagMappings off the top of the stack
33504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// causing the new top of the stack to be the active mappings. Returns
33604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// true if the pop happens, false if there is only one DiagMapping on the
33704ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// stack.
3380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  bool popMappings(SourceLocation Loc);
33904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
340bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Set the diagnostic client associated with this diagnostic object.
341bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  ///
34233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// \param ShouldOwnClient true if the diagnostic object should take
34333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// ownership of \c client.
3444f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  void setClient(DiagnosticClient *client, bool ShouldOwnClient = true);
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
346c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  /// setErrorLimit - Specify a limit for the number of errors we should
347b205ac9fcd22b87b41697172d1983c5ae9dabaafChris Lattner  /// emit before giving up.  Zero disables the limit.
348c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  void setErrorLimit(unsigned Limit) { ErrorLimit = Limit; }
349b205ac9fcd22b87b41697172d1983c5ae9dabaafChris Lattner
350575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Specify the maximum number of template instantiation
351575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// notes to emit along with a given diagnostic.
352575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  void setTemplateBacktraceLimit(unsigned Limit) {
353575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    TemplateBacktraceLimit = Limit;
354575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
3556c1cb9916e9988dcdd65b9266dbe24afd173427aDouglas Gregor
356575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Retrieve the maximum number of template instantiation
357575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// nodes to emit along with a given diagnostic.
358575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned getTemplateBacktraceLimit() const {
359575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    return TemplateBacktraceLimit;
360575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
3616c1cb9916e9988dcdd65b9266dbe24afd173427aDouglas Gregor
3625b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  /// setIgnoreAllWarnings - When set to true, any unmapped warnings are
3635b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  /// ignored.  If this and WarningsAsErrors are both set, then this one wins.
3645b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }
3655b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; }
3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// setWarningsAsErrors - When set to true, any warnings reported are issued
3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// as errors.
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getWarningsAsErrors() const { return WarningsAsErrors; }
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  /// setErrorsAsFatal - When set to true, any error reported is made a
373e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  /// fatal error.
374e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  void setErrorsAsFatal(bool Val) { ErrorsAsFatal = Val; }
375e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool getErrorsAsFatal() const { return ErrorsAsFatal; }
376e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner
3772fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  /// setSuppressSystemWarnings - When set to true mask warnings that
3782fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  /// come from system headers.
3792fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
3802fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; }
3812fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar
38281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Suppress all diagnostics, to silence the front end when we
38381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// know that we don't want any more diagnostics to be passed along to the
38481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// client
38581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  void setSuppressAllDiagnostics(bool Val = true) {
38681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor    SuppressAllDiagnostics = Val;
38781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  }
38881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
38981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
3905edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// \brief Specify which overload candidates to show when overload resolution
3915edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// fails.  By default, we show all candidates.
3925edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  void setShowOverloads(OverloadsShown Val) {
3935edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    ShowOverloads = Val;
3945edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  }
3955edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown getShowOverloads() const { return ShowOverloads; }
3965edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
3970b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  /// \brief Pretend that the last diagnostic issued was ignored. This can
3980b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  /// be used by clients who suppress diagnostics themselves.
3990b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  void setLastDiagnosticIgnored() {
40033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    LastDiagLevel = DiagnosticIDs::Ignored;
4010b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  }
4020b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor
403b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// setExtensionHandlingBehavior - This controls whether otherwise-unmapped
404b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// extension diagnostics are mapped onto ignore/warning/error.  This
405b54b276a920246c595a0498da281821eb9d22996Chris Lattner  /// corresponds to the GCC -pedantic and -pedantic-errors option.
406b54b276a920246c595a0498da281821eb9d22996Chris Lattner  void setExtensionHandlingBehavior(ExtensionHandling H) {
407b54b276a920246c595a0498da281821eb9d22996Chris Lattner    ExtBehavior = H;
408b54b276a920246c595a0498da281821eb9d22996Chris Lattner  }
409c1b5fa41f09512c74030b9a2a0d1564535e22a76Peter Collingbourne  ExtensionHandling getExtensionHandlingBehavior() const { return ExtBehavior; }
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41127ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  /// AllExtensionsSilenced - This is a counter bumped when an __extension__
41227ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  /// block is encountered.  When non-zero, all extension diagnostics are
41327ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  /// entirely silenced, no matter how they are mapped.
41427ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
41527ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
41691a2886d558ea6ca7a0bed73ab5acea5ae78eac2Douglas Gregor  bool hasAllExtensionsSilenced() { return AllExtensionsSilenced != 0; }
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4180827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief This allows the client to specify that certain
419691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// warnings are ignored.  Notes can never be mapped, errors can only be
420691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
4210827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
4220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location that this change of diagnostic state should
4230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the latest state.
4240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
4250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                            SourceLocation Loc);
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4273bc172bc6a787842db6fea351cf6929539fca70dChris Lattner  /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
4283bc172bc6a787842db6fea351cf6929539fca70dChris Lattner  /// "unknown-pragmas" to have the specified mapping.  This returns true and
4293bc172bc6a787842db6fea351cf6929539fca70dChris Lattner  /// ignores the request if "Group" was unknown, false otherwise.
4300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
4310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// 'Loc' is the source location that this change of diagnostic state should
4320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the state from command-line.
433477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis  bool setDiagnosticGroupMapping(llvm::StringRef Group, diag::Mapping Map,
4340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                 SourceLocation Loc = SourceLocation()) {
4350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return Diags->setDiagnosticGroupMapping(Group, Map, Loc, *this);
43633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool hasErrorOccurred() const { return ErrorOccurred; }
43915221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
44153eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned getNumWarnings() const { return NumWarnings; }
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
443c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  void setNumWarnings(unsigned NumWarnings) {
444c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    this->NumWarnings = NumWarnings;
445c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  }
446c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
447182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// getCustomDiagID - Return an ID for a diagnostic with the specified message
448182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// and level.  If this is the first request for this diagnosic, it is
449182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  /// registered and created, otherwise the existing ID is returned.
45033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  unsigned getCustomDiagID(Level L, llvm::StringRef Message) {
45133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->getCustomDiagID((DiagnosticIDs::Level)L, Message);
45233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4543fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// ConvertArgToString - This method converts a diagnostic argument (as an
4553fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  /// intptr_t) into the string that represents it.
4563fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
4573fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          const char *Modifier, unsigned ModLen,
4583fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          const char *Argument, unsigned ArgLen,
459b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                          const ArgumentValue *PrevArgs, unsigned NumPrevArgs,
4603fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          llvm::SmallVectorImpl<char> &Output) const {
461b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen,
462b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                  PrevArgs, NumPrevArgs, Output, ArgToStringCookie);
46322caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46592dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {
4663fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner    ArgToStringFn = Fn;
46792dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner    ArgToStringCookie = Cookie;
46822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
470abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reset the state of the diagnostic object to its initial
471abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// configuration.
472abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void Reset();
473abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
4755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Diagnostic classification and reporting interfaces.
4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Based on the way the client configured the Diagnostic
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// object, classify the specified diagnostic ID into a Level, consumable by
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the DiagnosticClient.
4810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
4820827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location we are interested in finding out the
4830827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// diagnostic state. Can be null in order to query the latest state.
4847decebfc7b9dc841f228c93cc2e41e3e62911ff8Ted Kremenek  Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
4857decebfc7b9dc841f228c93cc2e41e3e62911ff8Ted Kremenek                           diag::Mapping *mapping = 0) const {
4867decebfc7b9dc841f228c93cc2e41e3e62911ff8Ted Kremenek    return (Level)Diags->getDiagnosticLevel(DiagID, Loc, *this, mapping);
48733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
489509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// Report - Issue the message to the client.  @c DiagID is a member of the
490509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// @c diag::kind enum.  This actually returns aninstance of DiagnosticBuilder
491509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// which emits the diagnostics (through @c ProcessDiag) when it is destroyed.
492509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// @c Pos represents the source location associated with the diagnostic,
493509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// which can be an invalid location if no position information is available.
49433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  inline DiagnosticBuilder Report(SourceLocation Pos, unsigned DiagID);
4950f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbar  inline DiagnosticBuilder Report(unsigned DiagID);
496de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor
497e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  void Report(const StoredDiagnostic &storedDiag);
498e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
49993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Determine whethere there is already a diagnostic in flight.
5005800f7ea9cf5621280089d690f677bd28064d6b5Ted Kremenek  bool isDiagnosticInFlight() const { return CurDiagID != ~0U; }
50193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
50293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Set the "delayed" diagnostic that will be emitted once
50393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// the current diagnostic completes.
50493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
50593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  If a diagnostic is already in-flight but the front end must
50693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  report a problem (e.g., with an inconsistent file system
50793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  state), this routine sets a "delayed" diagnostic that will be
50893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  emitted after the current diagnostic completes. This should
50993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  only be used for fatal errors detected at inconvenient
51093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  times. If emitting a delayed diagnostic causes a second delayed
51193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  diagnostic to be introduced, that second delayed diagnostic
51293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  will be ignored.
51393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
51493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param DiagID The ID of the diagnostic being delayed.
51593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
51693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param Arg1 A string argument that will be provided to the
51793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// diagnostic. A copy of this string will be stored in the
51893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// Diagnostic object itself.
51993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
52093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param Arg2 A string argument that will be provided to the
52193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// diagnostic. A copy of this string will be stored in the
52293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// Diagnostic object itself.
52393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  void SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1 = "",
52493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor                            llvm::StringRef Arg2 = "");
52593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
526de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor  /// \brief Clear out the current diagnostic.
527de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor  void Clear() { CurDiagID = ~0U; }
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5290a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattnerprivate:
53093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Report the delayed diagnostic.
53193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  void ReportDelayed();
53293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
53393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
534691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// getDiagnosticMappingInfo - Return the mapping info currently set for the
535691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// specified builtin diagnostic.  This returns the high bit encoding, or zero
536691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  /// if the field is completely uninitialized.
5370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  diag::Mapping getDiagnosticMappingInfo(diag::kind Diag,
5380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                         DiagState *State) const {
5390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return State->getMapping(Diag);
540691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  }
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
542691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner  void setDiagnosticMappingInternal(unsigned DiagId, unsigned Map,
5433efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis                                    DiagState *State,
5443efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis                                    bool isUser, bool isPragma) const {
545691f1ae7164ba78c3dd7ac20c487399935b9544bChris Lattner    if (isUser) Map |= 8;  // Set the high bit for user mappings.
5463efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    if (isPragma) Map |= 0x10;  // Set the bit for diagnostic pragma mappings.
5470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    State->setMapping((diag::kind)DiagId, Map);
5489faf50e62ddcd2535b472abc8daafd7042120caaChris Lattner  }
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5503cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  // This is private state used by DiagnosticBuilder.  We put it here instead of
5513cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight
5520a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // object.  This implementation choice means that we can only have one
5530a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // diagnostic "in flight" at a time, but this seems to be a reasonable
5540a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // tradeoff to keep these objects small.  Assertions verify that only one
5550a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // diagnostic is in flight at a time.
55633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  friend class DiagnosticIDs;
5573cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  friend class DiagnosticBuilder;
5580a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  friend class DiagnosticInfo;
5599b623639378d53a675921ddfa7316034d571881eDouglas Gregor  friend class PartialDiagnostic;
5609c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  friend class DiagnosticErrorTrap;
5619b623639378d53a675921ddfa7316034d571881eDouglas Gregor
562b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  /// CurDiagLoc - This is the location of the current diagnostic that is in
563b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  /// flight.
56433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceLocation CurDiagLoc;
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
566b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  /// CurDiagID - This is the ID of the current diagnostic that is in flight.
5673cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// This is set to ~0U when there is no diagnostic in flight.
568b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  unsigned CurDiagID;
569b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner
57073d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  enum {
57173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    /// MaxArguments - The maximum number of arguments we can hold. We currently
57273d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    /// only support up to 10 arguments (%0-%9).  A single diagnostic with more
57373d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    /// than that almost certainly has to be simplified anyway.
57473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    MaxArguments = 10
57573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  };
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5773cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// NumDiagArgs - This contains the number of entries in Arguments.
578730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  signed char NumDiagArgs;
579730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  /// NumRanges - This is the number of ranges in the DiagRanges array.
580730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  unsigned char NumDiagRanges;
5814b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief The number of code modifications hints in the
582849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  /// FixItHints array.
583849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  unsigned char NumFixItHints;
584ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
58573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
58673d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// values, with one for each argument.  This specifies whether the argument
58773d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// is in DiagArgumentsStr or in DiagArguments.
58873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  unsigned char DiagArgumentsKind[MaxArguments];
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59073d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// DiagArgumentsStr - This holds the values of each string argument for the
59173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// current diagnostic.  This value is only used when the corresponding
59273d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// ArgumentKind is ak_std_string.
59373d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  std::string DiagArgumentsStr[MaxArguments];
59473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner
59573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// DiagArgumentsVal - The values for the various substitution positions. This
59673d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// is used when the argument is not an std::string.  The specific value is
597fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  /// mangled into an intptr_t and the interpretation depends on exactly what
59873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// sort of argument kind it is.
59973d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  intptr_t DiagArgumentsVal[MaxArguments];
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6010a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// DiagRanges - The list of ranges added to this diagnostic.  It currently
6020a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// only support 10 ranges, could easily be extended if needed.
6030a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  CharSourceRange DiagRanges[10];
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
605849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  enum { MaxFixItHints = 3 };
6064b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
607849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  /// FixItHints - If valid, provides a hint with some code
6084b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// to insert, remove, or modify at a particular position.
609849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  FixItHint FixItHints[MaxFixItHints];
6104b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
6110a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// ProcessDiag - This is the method used to report a diagnostic that is
6120a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// finally fully formed.
6135e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  ///
6145e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \returns true if the diagnostic was emitted, false if it was
6155e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// suppressed.
61633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool ProcessDiag() {
61733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->ProcessDiag(*this);
61833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
619f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis
620f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  friend class ASTReader;
621f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  friend class ASTWriter;
6223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner};
6233cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
6249c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// \brief RAII class that determines when any errors have occurred
6259c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// between the time the instance was created and the time it was
6269c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// queried.
6279c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidisclass DiagnosticErrorTrap {
6289c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  Diagnostic &Diag;
6299c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6309c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidispublic:
6319c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  explicit DiagnosticErrorTrap(Diagnostic &Diag)
632add80bb3957dba1d9389304453c0a4c975768bd7John McCall    : Diag(Diag) { reset(); }
6339c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6349c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  /// \brief Determine whether any errors have occurred since this
6359c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  /// object instance was created.
6369c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  bool hasErrorOccurred() const {
637add80bb3957dba1d9389304453c0a4c975768bd7John McCall    return Diag.TrapErrorOccurred;
638add80bb3957dba1d9389304453c0a4c975768bd7John McCall  }
639add80bb3957dba1d9389304453c0a4c975768bd7John McCall
640add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// \brief Determine whether any unrecoverable errors have occurred since this
641add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// object instance was created.
642add80bb3957dba1d9389304453c0a4c975768bd7John McCall  bool hasUnrecoverableErrorOccurred() const {
643add80bb3957dba1d9389304453c0a4c975768bd7John McCall    return Diag.TrapUnrecoverableErrorOccurred;
6449c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  }
6459c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6469c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  // Set to initial state of "no errors occurred".
647add80bb3957dba1d9389304453c0a4c975768bd7John McCall  void reset() {
648add80bb3957dba1d9389304453c0a4c975768bd7John McCall    Diag.TrapErrorOccurred = false;
649add80bb3957dba1d9389304453c0a4c975768bd7John McCall    Diag.TrapUnrecoverableErrorOccurred = false;
650add80bb3957dba1d9389304453c0a4c975768bd7John McCall  }
6519c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis};
6529c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
6533cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
6543cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner// DiagnosticBuilder
6553cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
6563cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
6573cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// DiagnosticBuilder - This is a little helper class used to produce
6583cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// diagnostics.  This is constructed by the Diagnostic::Report method, and
6593cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// allows insertion of extra information (arguments and source ranges) into the
6603cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// currently "in flight" diagnostic.  When the temporary for the builder is
6613cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// destroyed, the diagnostic is issued.
6623cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner///
6633cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// Note that many of these will be created as temporary objects (many call
6643cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// sites), so we want them to be small and we never want their address taken.
6653cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// This ensures that compilers with somewhat reasonable optimizers will promote
6663cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// the common fields to registers, eliminating increments of the NumArgs field,
6673cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// for example.
6683cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerclass DiagnosticBuilder {
6693cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  mutable Diagnostic *DiagObj;
670849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  mutable unsigned NumArgs, NumRanges, NumFixItHints;
6711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT
6733cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  friend class Diagnostic;
6743cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  explicit DiagnosticBuilder(Diagnostic *diagObj)
675849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    : DiagObj(diagObj), NumArgs(0), NumRanges(0), NumFixItHints(0) {}
676ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
6779b623639378d53a675921ddfa7316034d571881eDouglas Gregor  friend class PartialDiagnostic;
678b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
679b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregorprotected:
680b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  void FlushCounts();
6819b623639378d53a675921ddfa7316034d571881eDouglas Gregor
6821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
6830a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// Copy constructor.  When copied, this "takes" the diagnostic info from the
6840a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// input and neuters it.
6853cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DiagnosticBuilder(const DiagnosticBuilder &D) {
6860a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    DiagObj = D.DiagObj;
6870a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    D.DiagObj = 0;
68825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    NumArgs = D.NumArgs;
68925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    NumRanges = D.NumRanges;
690849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    NumFixItHints = D.NumFixItHints;
6910a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
69225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor
6935e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \brief Simple enumeration value used to give a name to the
6945e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// suppress-diagnostic constructor.
6955e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  enum SuppressKind { Suppress };
6965e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor
6975e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \brief Create an empty DiagnosticBuilder object that represents
6985e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// no actual diagnostic.
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  explicit DiagnosticBuilder(SuppressKind)
700849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    : DiagObj(0), NumArgs(0), NumRanges(0), NumFixItHints(0) { }
7015e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor
70225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// \brief Force the diagnostic builder to emit the diagnostic now.
70325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  ///
70425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// Once this function has been called, the DiagnosticBuilder object
70525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// should not be used again before it is destroyed.
7065e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  ///
7075e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// \returns true if a diagnostic was emitted, false if the
7085e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  /// diagnostic was suppressed.
70993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  bool Emit();
71025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor
71125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// Destructor - The dtor emits the diagnostic if it hasn't already
71225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  /// been emitted.
71325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  ~DiagnosticBuilder() { Emit(); }
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
715d93256e55673a17d18543397ec462416acb13792Douglas Gregor  /// isActive - Determine whether this diagnostic is still active.
716d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool isActive() const { return DiagObj != 0; }
717d93256e55673a17d18543397ec462416acb13792Douglas Gregor
7189b623639378d53a675921ddfa7316034d571881eDouglas Gregor  /// \brief Retrieve the active diagnostic ID.
7199b623639378d53a675921ddfa7316034d571881eDouglas Gregor  ///
7209b623639378d53a675921ddfa7316034d571881eDouglas Gregor  /// \pre \c isActive()
7219b623639378d53a675921ddfa7316034d571881eDouglas Gregor  unsigned getDiagID() const {
7229b623639378d53a675921ddfa7316034d571881eDouglas Gregor    assert(isActive() && "Diagnostic is inactive");
7239b623639378d53a675921ddfa7316034d571881eDouglas Gregor    return DiagObj->CurDiagID;
7249b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
7259b623639378d53a675921ddfa7316034d571881eDouglas Gregor
7269b623639378d53a675921ddfa7316034d571881eDouglas Gregor  /// \brief Clear out the current diagnostic.
7279b623639378d53a675921ddfa7316034d571881eDouglas Gregor  void Clear() { DiagObj = 0; }
7289b623639378d53a675921ddfa7316034d571881eDouglas Gregor
7293cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// Operator bool: conversion of DiagnosticBuilder to bool always returns
7303cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// true.  This allows is to be used in boolean error contexts like:
7313cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// return Diag(...);
7323cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  operator bool() const { return true; }
7333cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
734eaea925e70e3ee85809e572aa8474b8dfc491af7Daniel Dunbar  void AddString(llvm::StringRef S) const {
7353cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(NumArgs < Diagnostic::MaxArguments &&
7363cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
7375e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj) {
7385e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string;
7395e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsStr[NumArgs++] = S;
7405e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    }
7413cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  }
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7433cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const {
7443cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(NumArgs < Diagnostic::MaxArguments &&
7453cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
7465e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj) {
7475e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsKind[NumArgs] = Kind;
7485e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor      DiagObj->DiagArgumentsVal[NumArgs++] = V;
7495e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    }
7500a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7520a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  void AddSourceRange(const CharSourceRange &R) const {
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(NumRanges <
7543cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) &&
7553cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
7565e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj)
75751c6d384551674facc19f745ecf6e289d28dc55fDouglas Gregor      DiagObj->DiagRanges[NumRanges++] = R;
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7594b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
760849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  void AddFixItHint(const FixItHint &Hint) const {
761849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    assert(NumFixItHints < Diagnostic::MaxFixItHints &&
762849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor           "Too many fix-it hints!");
7635e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    if (DiagObj)
764849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      DiagObj->FixItHints[NumFixItHints++] = Hint;
7654b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
7663cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner};
7673cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7683cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
769eaea925e70e3ee85809e572aa8474b8dfc491af7Daniel Dunbar                                           llvm::StringRef S) {
7703cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddString(S);
7713cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
7723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
7733cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7743cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
7753cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const char *Str) {
7763cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str),
7773cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                  Diagnostic::ak_c_string);
7783cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
7793cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
7803cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7813cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) {
7823cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(I, Diagnostic::ak_sint);
7833cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
7843cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
7853cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7863bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidisinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,bool I) {
7873bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis  DB.AddTaggedVal(I, Diagnostic::ak_sint);
7883bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis  return DB;
7893bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis}
7903bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis
7913cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
7923cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           unsigned I) {
7933cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(I, Diagnostic::ak_uint);
7943cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
7953cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
7963cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7973cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
7983cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const IdentifierInfo *II) {
7993cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(reinterpret_cast<intptr_t>(II),
8003cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                  Diagnostic::ak_identifierinfo);
8013cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
8023cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8043f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// Adds a DeclContext to the diagnostic. The enable_if template magic is here
8053f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// so that we only match those arguments that are (statically) DeclContexts;
8063f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// other arguments that derive from DeclContext (e.g., RecordDecls) will not
8073f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// match.
8083f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregortemplate<typename T>
8093f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregorinline
8103f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregortypename llvm::enable_if<llvm::is_same<T, DeclContext>,
8113f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor                         const DiagnosticBuilder &>::type
8123f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregoroperator<<(const DiagnosticBuilder &DB, T *DC) {
8133f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor  DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
8143f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor                  Diagnostic::ak_declcontext);
8153f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor  return DB;
8163f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor}
8173f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor
8183cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
8193cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const SourceRange &R) {
8200a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  DB.AddSourceRange(CharSourceRange::getTokenRange(R));
8213cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
8223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8234b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
8244b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
8250a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner                                           const CharSourceRange &R) {
8260a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  DB.AddSourceRange(R);
8270a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  return DB;
8280a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner}
8290a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
8300a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
831849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor                                           const FixItHint &Hint) {
832849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  DB.AddFixItHint(Hint);
8334b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  return DB;
8344b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
835ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
8363cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// Report - Issue the message to the client.  DiagID is a member of the
8373cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// diag::kind enum.  This actually returns a new instance of DiagnosticBuilder
8383cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// which emits the diagnostics (through ProcessDiag) when it is destroyed.
83933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidisinline DiagnosticBuilder Diagnostic::Report(SourceLocation Loc,
84033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                                            unsigned DiagID){
8413cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
8423cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  CurDiagLoc = Loc;
8433cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  CurDiagID = DiagID;
8443cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DiagnosticBuilder(this);
8453cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
8460f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbarinline DiagnosticBuilder Diagnostic::Report(unsigned DiagID) {
84733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Report(SourceLocation(), DiagID);
8480f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbar}
8493cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
8503cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
8513cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner// DiagnosticInfo
8523cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
8531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8543cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// DiagnosticInfo - This is a little helper class (which is basically a smart
8554b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// pointer that forward info from Diagnostic) that allows clients to enquire
8563cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// about the currently in-flight diagnostic.
8573cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerclass DiagnosticInfo {
8583cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  const Diagnostic *DiagObj;
859e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  llvm::StringRef StoredDiagMessage;
8603cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerpublic:
8613cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  explicit DiagnosticInfo(const Diagnostic *DO) : DiagObj(DO) {}
862e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  DiagnosticInfo(const Diagnostic *DO, llvm::StringRef storedDiagMessage)
863e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis    : DiagObj(DO), StoredDiagMessage(storedDiagMessage) {}
8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8650a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  const Diagnostic *getDiags() const { return DiagObj; }
866b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  unsigned getID() const { return DiagObj->CurDiagID; }
86733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const SourceLocation &getLocation() const { return DiagObj->CurDiagLoc; }
86833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool hasSourceManager() const { return DiagObj->hasSourceManager(); }
86933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const { return DiagObj->getSourceManager();}
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8710a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  unsigned getNumArgs() const { return DiagObj->NumDiagArgs; }
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87373d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// getArgKind - Return the kind of the specified index.  Based on the kind
87473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// of argument, the accessors below can be used to get the value.
8753cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  Diagnostic::ArgumentKind getArgKind(unsigned Idx) const {
8763cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(Idx < getNumArgs() && "Argument index out of range!");
8773cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    return (Diagnostic::ArgumentKind)DiagObj->DiagArgumentsKind[Idx];
87873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  }
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88073d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// getArgStdStr - Return the provided argument string specified by Idx.
88173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  const std::string &getArgStdStr(unsigned Idx) const {
8823cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(getArgKind(Idx) == Diagnostic::ak_std_string &&
8833cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
88473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    return DiagObj->DiagArgumentsStr[Idx];
88573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  }
8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88773d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  /// getArgCStr - Return the specified C string argument.
88873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  const char *getArgCStr(unsigned Idx) const {
8893cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(getArgKind(Idx) == Diagnostic::ak_c_string &&
8903cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
89173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]);
8920a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89430bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  /// getArgSInt - Return the specified signed integer argument.
89530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  int getArgSInt(unsigned Idx) const {
8963cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(getArgKind(Idx) == Diagnostic::ak_sint &&
8973cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
89830bc96544346bea42921cf6837e66cef80d664b4Chris Lattner    return (int)DiagObj->DiagArgumentsVal[Idx];
89930bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  }
9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90130bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  /// getArgUInt - Return the specified unsigned integer argument.
90230bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  unsigned getArgUInt(unsigned Idx) const {
9033cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(getArgKind(Idx) == Diagnostic::ak_uint &&
9043cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
90530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner    return (unsigned)DiagObj->DiagArgumentsVal[Idx];
90630bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  }
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90843b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  /// getArgIdentifier - Return the specified IdentifierInfo argument.
90943b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  const IdentifierInfo *getArgIdentifier(unsigned Idx) const {
9103cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(getArgKind(Idx) == Diagnostic::ak_identifierinfo &&
9113cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
9129634379265855f1628190e926d9aaf1fb4a5d90eChris Lattner    return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]);
91343b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  }
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91522caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  /// getRawArg - Return the specified non-string argument in an opaque form.
91622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  intptr_t getRawArg(unsigned Idx) const {
91722caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    assert(getArgKind(Idx) != Diagnostic::ak_std_string &&
91822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner           "invalid argument accessor!");
91922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    return DiagObj->DiagArgumentsVal[Idx];
92022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9230a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// getNumRanges - Return the number of source ranges associated with this
9240a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// diagnostic.
9250a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  unsigned getNumRanges() const {
9260a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    return DiagObj->NumDiagRanges;
9270a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9290a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  const CharSourceRange &getRange(unsigned Idx) const {
9300a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!");
93151c6d384551674facc19f745ecf6e289d28dc55fDouglas Gregor    return DiagObj->DiagRanges[Idx];
9320a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
934849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  unsigned getNumFixItHints() const {
935849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    return DiagObj->NumFixItHints;
9364b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
9374b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
938849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  const FixItHint &getFixItHint(unsigned Idx) const {
939849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    return DiagObj->FixItHints[Idx];
9404b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
9414b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
942849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  const FixItHint *getFixItHints() const {
943849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    return DiagObj->NumFixItHints?
944849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor             &DiagObj->FixItHints[0] : 0;
9454b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
9464b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
947f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  /// FormatDiagnostic - Format this diagnostic into a string, substituting the
948f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  /// formal arguments into the %0 slots.  The result is appended onto the Str
949f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  /// array.
950f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  void FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const;
9519f28614bf1a8387000d8df57a713fcf69e198145John McCall
9529f28614bf1a8387000d8df57a713fcf69e198145John McCall  /// FormatDiagnostic - Format the given format-string into the
9539f28614bf1a8387000d8df57a713fcf69e198145John McCall  /// output buffer using the arguments stored in this diagnostic.
9549f28614bf1a8387000d8df57a713fcf69e198145John McCall  void FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
9559f28614bf1a8387000d8df57a713fcf69e198145John McCall                        llvm::SmallVectorImpl<char> &OutStr) const;
956a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor};
957a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
958a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor/**
959d3ab63e0f66429abf2a3e4cde889e420e41e8790Douglas Gregor * \brief Represents a diagnostic in a form that can be retained until its
960d3ab63e0f66429abf2a3e4cde889e420e41e8790Douglas Gregor * corresponding source manager is destroyed.
961a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor */
962a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregorclass StoredDiagnostic {
963aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  unsigned ID;
964a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  Diagnostic::Level Level;
965a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  FullSourceLoc Loc;
966a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  std::string Message;
9670a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  std::vector<CharSourceRange> Ranges;
968849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  std::vector<FixItHint> FixIts;
969a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
970a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregorpublic:
971a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  StoredDiagnostic();
972a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  StoredDiagnostic(Diagnostic::Level Level, const DiagnosticInfo &Info);
973aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
974aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor                   llvm::StringRef Message);
975a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  ~StoredDiagnostic();
976a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
977a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief Evaluates true when this object stores a diagnostic.
978a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  operator bool() const { return Message.size() > 0; }
979a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
980aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  unsigned getID() const { return ID; }
981a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  Diagnostic::Level getLevel() const { return Level; }
982a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  const FullSourceLoc &getLocation() const { return Loc; }
983a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  llvm::StringRef getMessage() const { return Message; }
984c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
985c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  void setLocation(FullSourceLoc Loc) { this->Loc = Loc; }
986c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
9870a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  typedef std::vector<CharSourceRange>::const_iterator range_iterator;
988a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  range_iterator range_begin() const { return Ranges.begin(); }
989a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  range_iterator range_end() const { return Ranges.end(); }
990a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  unsigned range_size() const { return Ranges.size(); }
991a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
992849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  typedef std::vector<FixItHint>::const_iterator fixit_iterator;
993a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  fixit_iterator fixit_begin() const { return FixIts.begin(); }
994a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  fixit_iterator fixit_end() const { return FixIts.end(); }
995a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  unsigned fixit_size() const { return FixIts.size(); }
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DiagnosticClient - This is an abstract interface implemented by clients of
9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the front-end, which formats and prints fully processed diagnostics.
10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass DiagnosticClient {
1001ab41b97c3cddf1bcdd8bf82ab09ed3ceafcd05b1Argyrios Kyrtzidisprotected:
1002f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned NumWarnings;       // Number of warnings reported
1003f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned NumErrors;         // Number of errors reported
1004f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1006f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  DiagnosticClient() : NumWarnings(0), NumErrors(0) { }
1007f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
1008f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned getNumErrors() const { return NumErrors; }
1009f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned getNumWarnings() const { return NumWarnings; }
1010f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  virtual ~DiagnosticClient();
10121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1013efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// BeginSourceFile - Callback to inform the diagnostic client that processing
1014efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// of a source file is beginning.
1015efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  ///
1016efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// Note that diagnostics may be emitted outside the processing of a source
1017efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// file, for example during the parsing of command line options. However,
1018efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// diagnostics with source range information are required to only be emitted
1019efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// in between BeginSourceFile() and EndSourceFile().
1020efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  ///
1021efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// \arg LO - The language options for the source file being processed.
1022efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// \arg PP - The preprocessor object being used for the source; this optional
1023efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// and may not be present, for example when processing AST source files.
10247d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  virtual void BeginSourceFile(const LangOptions &LangOpts,
10257d90199f109290e9d587479a481a2850d390b552Daniel Dunbar                               const Preprocessor *PP = 0) {}
1026efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar
1027efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// EndSourceFile - Callback to inform the diagnostic client that processing
1028efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// of a source file has ended. The diagnostic client should assume that any
1029efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// objects made available via \see BeginSourceFile() are inaccessible.
1030efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  virtual void EndSourceFile() {}
10311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1032cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek  /// IncludeInDiagnosticCounts - This method (whose default implementation
1033efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// returns true) indicates whether the diagnostics handled by this
1034efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// DiagnosticClient should be included in the number of diagnostics reported
1035efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// by Diagnostic.
1036cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek  virtual bool IncludeInDiagnosticCounts() const;
10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// capturing it to a log as needed.
1040f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  ///
1041f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  /// Default implementation just keeps track of the total number of warnings
1042f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  /// and errors.
10430a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
1044f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis                                const DiagnosticInfo &Info);
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1050