Diagnostic.h revision 30a2e16f6c27f888dd11eba6bbbae1e980078fcb
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//===----------------------------------------------------------------------===//
92f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett///
102f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// \file
112f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// \brief Defines the Diagnostic-related interfaces.
122f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett///
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_DIAGNOSTIC_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_DIAGNOSTIC_H
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis#include "clang/Basic/DiagnosticIDs.h"
19dc7b641574a733624489bd87fc7061771edf2113Douglas Gregor#include "clang/Basic/DiagnosticOptions.h"
202eefd8657c233bc7c9330acfe475fc270bbe7cabTed Kremenek#include "clang/Basic/SourceLocation.h"
21f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor#include "llvm/ADT/ArrayRef.h"
220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include "llvm/ADT/DenseMap.h"
2328019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
24bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor#include "llvm/ADT/OwningPtr.h"
253f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor#include "llvm/Support/type_traits.h"
260827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include <list>
2730a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <vector>
28f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
3078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  class DiagnosticConsumer;
3133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DiagnosticBuilder;
3202c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  class DiagnosticOptions;
33d42ffbd22fc7eb61321f6a88173ee424991f01c6Ted Kremenek  class IdentifierInfo;
3433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DeclContext;
3540469651a3f8379dc0f32df69e9bade06a2aad36Chris Lattner  class LangOptions;
367d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  class Preprocessor;
379c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  class DiagnosticErrorTrap;
38e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  class StoredDiagnostic;
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Annotates a diagnostic with some code that should be
414b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// inserted, removed, or replaced to fix the problem.
424b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
434b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// This kind of hint should be used when we are certain that the
444b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// introduction, removal, or modification of a particular (small!)
454b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// amount of code will correct a compilation error. The compiler
464b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// should also provide full recovery from such errors, such that
47b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor/// suppressing the diagnostic output can still result in successful
484b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// compilation.
49849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregorclass FixItHint {
504b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorpublic:
51783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// \brief Code that should be replaced to correct the error. Empty for an
52783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// insertion hint.
530a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  CharSourceRange RemoveRange;
544b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
5530660a898545416f0fea2d717f16f75640001e38Ted Kremenek  /// \brief Code in the specific range that should be inserted in the insertion
5630660a898545416f0fea2d717f16f75640001e38Ted Kremenek  /// location.
5730660a898545416f0fea2d717f16f75640001e38Ted Kremenek  CharSourceRange InsertFromRange;
5830660a898545416f0fea2d717f16f75640001e38Ted Kremenek
594b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief The actual code to insert at the insertion location, as a
604b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// string.
614b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  std::string CodeToInsert;
624b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
6330660a898545416f0fea2d717f16f75640001e38Ted Kremenek  bool BeforePreviousInsertions;
6430660a898545416f0fea2d717f16f75640001e38Ted Kremenek
654b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Empty code modification hint, indicating that no code
664b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// modification is known.
6730660a898545416f0fea2d717f16f75640001e38Ted Kremenek  FixItHint() : BeforePreviousInsertions(false) { }
684b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
692f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  bool isNull() const {
70783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    return !RemoveRange.isValid();
712f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  }
722f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson
734b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that inserts the given
744b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// code string at a specific location.
75849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  static FixItHint CreateInsertion(SourceLocation InsertionLoc,
7630660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                   StringRef Code,
7730660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                   bool BeforePreviousInsertions = false) {
78849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
79783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    Hint.RemoveRange =
80783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor      CharSourceRange(SourceRange(InsertionLoc, InsertionLoc), false);
81b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
8230660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.BeforePreviousInsertions = BeforePreviousInsertions;
8330660a898545416f0fea2d717f16f75640001e38Ted Kremenek    return Hint;
8430660a898545416f0fea2d717f16f75640001e38Ted Kremenek  }
8530660a898545416f0fea2d717f16f75640001e38Ted Kremenek
8630660a898545416f0fea2d717f16f75640001e38Ted Kremenek  /// \brief Create a code modification hint that inserts the given
87af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// code from \p FromRange at a specific location.
8830660a898545416f0fea2d717f16f75640001e38Ted Kremenek  static FixItHint CreateInsertionFromRange(SourceLocation InsertionLoc,
8930660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                            CharSourceRange FromRange,
9030660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                        bool BeforePreviousInsertions = false) {
9130660a898545416f0fea2d717f16f75640001e38Ted Kremenek    FixItHint Hint;
9230660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.RemoveRange =
9330660a898545416f0fea2d717f16f75640001e38Ted Kremenek      CharSourceRange(SourceRange(InsertionLoc, InsertionLoc), false);
9430660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.InsertFromRange = FromRange;
9530660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.BeforePreviousInsertions = BeforePreviousInsertions;
96b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
97b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
984b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
994b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that removes the given
1004b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range.
1010a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(CharSourceRange RemoveRange) {
102849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
103b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
104b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
105b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
1060a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(SourceRange RemoveRange) {
1070a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateRemoval(CharSourceRange::getTokenRange(RemoveRange));
1080a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
1090a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
1104b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that replaces the given
1114b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range with the given code string.
1120a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(CharSourceRange RemoveRange,
113686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                     StringRef Code) {
114849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
115b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
116b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
117b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
118b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
1190a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
1200a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(SourceRange RemoveRange,
121686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                     StringRef Code) {
1220a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateReplacement(CharSourceRange::getTokenRange(RemoveRange), Code);
1230a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
1244b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor};
1254b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
126af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// \brief Concrete class used by the front-end to report problems and issues.
127af50aab0c317462129d73ae8000c6394c718598dJames Dennett///
128af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// This massages the diagnostics (e.g. handling things like "report warnings
129af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// as errors" and passes them off to the DiagnosticConsumer for reporting to
130af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// the user. DiagnosticsEngine is tied to one translation unit and one
131af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// SourceManager.
132c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
133182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattnerpublic:
134af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief The level of the diagnostic, after it has been through mapping.
135182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  enum Level {
13633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Ignored = DiagnosticIDs::Ignored,
13733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Note = DiagnosticIDs::Note,
13833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Warning = DiagnosticIDs::Warning,
13933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Error = DiagnosticIDs::Error,
14033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Fatal = DiagnosticIDs::Fatal
141182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  };
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief How do we handle otherwise-unmapped extension?
144af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
145af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This is controlled by -pedantic and -pedantic-errors.
146b54b276a920246c595a0498da281821eb9d22996Chris Lattner  enum ExtensionHandling {
147b54b276a920246c595a0498da281821eb9d22996Chris Lattner    Ext_Ignore, Ext_Warn, Ext_Error
148b54b276a920246c595a0498da281821eb9d22996Chris Lattner  };
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1503fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  enum ArgumentKind {
151af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_std_string,      ///< std::string
152af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_c_string,        ///< const char *
153af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_sint,            ///< int
154af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_uint,            ///< unsigned
155af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_identifierinfo,  ///< IdentifierInfo
156af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_qualtype,        ///< QualType
157af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_declarationname, ///< DeclarationName
158af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_nameddecl,       ///< NamedDecl *
159af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_nestednamespec,  ///< NestedNameSpecifier *
160af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_declcontext,     ///< DeclContext *
161af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_qualtype_pair    ///< pair<QualType, QualType>
1623fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  };
1635edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
164af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Represents on argument value, which is a union discriminated
165af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// by ArgumentKind, with a value.
166b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  typedef std::pair<ArgumentKind, intptr_t> ArgumentValue;
167ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpprivate:
16927ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  unsigned char AllExtensionsSilenced; // Used by __extension__
170b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool IgnoreAllWarnings;        // Ignore all warnings: -w
1711e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool WarningsAsErrors;         // Treat warnings like errors.
1721e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool EnableAllWarnings;        // Enable all warnings.
173e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool ErrorsAsFatal;            // Treat errors like fatal errors.
174b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool SuppressSystemWarnings;   // Suppress warnings in system headers.
17581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool SuppressAllDiagnostics;   // Suppress all diagnostics.
176246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool ElideType;                // Elide common types of templates.
177246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool PrintTemplateTree;        // Print a tree when comparing templates.
178246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool ShowColors;               // Color printing is enabled.
1795edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown ShowOverloads;  // Which overload candidates to show.
180c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  unsigned ErrorLimit;           // Cap of # errors emitted, 0 -> no limit.
181575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned TemplateBacktraceLimit; // Cap on depth of template backtrace stack,
182575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor                                   // 0 -> no limit.
18308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ConstexprBacktraceLimit; // Cap on depth of constexpr evaluation
18408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                                    // backtrace stack, 0 -> no limit.
185b54b276a920246c595a0498da281821eb9d22996Chris Lattner  ExtensionHandling ExtBehavior; // Map extensions onto warnings or errors?
186c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<DiagnosticIDs> Diags;
18702c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
18878ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *Client;
18933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool OwnsDiagClient;
19033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager *SourceMgr;
1910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
192af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Mapping information for diagnostics.
193af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
194af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Mapping info is packed into four bits per diagnostic.  The low three
195af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// bits are the mapping (an instance of diag::Mapping), or zero if unset.
196af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The high bit is set when the mapping was established as a user mapping.
197af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If the high bit is clear, then the low bits are set to the default
198af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// value, and should be mapped with -pedantic, -Werror, etc.
1990827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
2003efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// A new DiagState is created and kept around when diagnostic pragmas modify
2013efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// the state so that we know what is the diagnostic state at any given
2023efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// source location.
2030827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  class DiagState {
204b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    llvm::DenseMap<unsigned, DiagnosticMappingInfo> DiagMap;
2053c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
2063c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  public:
207ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    typedef llvm::DenseMap<unsigned, DiagnosticMappingInfo>::iterator
208b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar      iterator;
209ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    typedef llvm::DenseMap<unsigned, DiagnosticMappingInfo>::const_iterator
210ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar      const_iterator;
2113efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis
212b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    void setMappingInfo(diag::kind Diag, DiagnosticMappingInfo Info) {
213b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar      DiagMap[Diag] = Info;
214b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    }
2153c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
216ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    DiagnosticMappingInfo &getOrAddMappingInfo(diag::kind Diag);
217ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar
218ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    const_iterator begin() const { return DiagMap.begin(); }
219ba494c64365d78b1cdb6baea4d1e79263389fda9Daniel Dunbar    const_iterator end() const { return DiagMap.end(); }
2200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  };
2213c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
2220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Keeps and automatically disposes all DiagStates that we create.
2230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  std::list<DiagState> DiagStates;
2240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Represents a point in source where the diagnostic state was
226af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// modified because of a pragma.
227af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
228af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// 'Loc' can be null if the point represents the diagnostic state
229af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// modifications done through the command-line.
2300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  struct DiagStatePoint {
2310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagState *State;
2320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    FullSourceLoc Loc;
2330827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStatePoint(DiagState *State, FullSourceLoc Loc)
2340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      : State(State), Loc(Loc) { }
2350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    bool operator<(const DiagStatePoint &RHS) const {
2370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      // If Loc is invalid it means it came from <command-line>, in which case
2380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      // we regard it as coming before any valid source location.
2390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      if (RHS.Loc.isInvalid())
2400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis        return false;
2410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      if (Loc.isInvalid())
2420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis        return true;
2430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      return Loc.isBeforeInTranslationUnitThan(RHS.Loc);
2443c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer    }
2453c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  };
24604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
247af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief A sorted vector of all DiagStatePoints representing changes in
248af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// diagnostic state due to diagnostic pragmas.
249af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
250af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The vector is always sorted according to the SourceLocation of the
251af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// DiagStatePoint.
2520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  typedef std::vector<DiagStatePoint> DiagStatePointsTy;
2530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  mutable DiagStatePointsTy DiagStatePoints;
2540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Keeps the DiagState that was active during each diagnostic 'push'
2560827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// so we can get back at it when we 'pop'.
2570827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  std::vector<DiagState *> DiagStateOnPushStack;
2580827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2590827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagState *GetCurDiagState() const {
2600827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    assert(!DiagStatePoints.empty());
2610827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return DiagStatePoints.back().State;
2620827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2630827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2640827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void PushDiagStatePoint(DiagState *State, SourceLocation L) {
26500aae5243d965aa7bcee81a39ba0900c7869be21Richard Smith    FullSourceLoc Loc(L, getSourceManager());
2660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // Make sure that DiagStatePoints is always sorted according to Loc.
26700aae5243d965aa7bcee81a39ba0900c7869be21Richard Smith    assert(Loc.isValid() && "Adding invalid loc point");
26800aae5243d965aa7bcee81a39ba0900c7869be21Richard Smith    assert(!DiagStatePoints.empty() &&
26900aae5243d965aa7bcee81a39ba0900c7869be21Richard Smith           (DiagStatePoints.back().Loc.isInvalid() ||
2700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis            DiagStatePoints.back().Loc.isBeforeInTranslationUnitThan(Loc)) &&
2710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis           "Previous point loc comes after or is the same as new one");
27200aae5243d965aa7bcee81a39ba0900c7869be21Richard Smith    DiagStatePoints.push_back(DiagStatePoint(State, Loc));
2730827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Finds the DiagStatePoint that contains the diagnostic state of
2760827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the given source location.
2770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator GetDiagStatePointForLoc(SourceLocation Loc) const;
27804ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
279af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Sticky flag set to \c true when an error is emitted.
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool ErrorOccurred;
281af50aab0c317462129d73ae8000c6394c718598dJames Dennett
282af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Sticky flag set to \c true when a fatal error is emitted.
28315221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool FatalErrorOccurred;
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28585bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  /// \brief Indicates that an unrecoverable error has occurred.
28685bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  bool UnrecoverableErrorOccurred;
28785bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
288c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  /// \brief Counts for DiagnosticErrorTrap to check whether an error occurred
289add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// during a parsing section, e.g. during parsing a function.
290c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned TrapNumErrorsOccurred;
291c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned TrapNumUnrecoverableErrorsOccurred;
292add80bb3957dba1d9389304453c0a4c975768bd7John McCall
293af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief The level of the last diagnostic emitted.
294af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
295af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This is used to emit continuation diagnostics with the same level as the
296f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// diagnostic that they follow.
29733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticIDs::Level LastDiagLevel;
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
299af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumWarnings;         ///< Number of warnings reported
300af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumErrors;           ///< Number of errors reported
301af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumErrorsSuppressed; ///< Number of errors suppressed
3027bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
303af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief A function pointer that converts an opaque diagnostic
304af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// argument to a strings.
305af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
306af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This takes the modifiers and argument that was present in the diagnostic.
307b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
308b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// The PrevArgs array (whose length is NumPrevArgs) indicates the previous
309b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// arguments formatted for this diagnostic.  Implementations of this function
310b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// can use this information to avoid redundancy across arguments.
311b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
31222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  /// This is a hack to avoid a layering violation between libbasic and libsema.
3130673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth  typedef void (*ArgToStringFnTy)(
3140673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      ArgumentKind Kind, intptr_t Val,
3150673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      const char *Modifier, unsigned ModifierLen,
3160673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      const char *Argument, unsigned ArgumentLen,
3170673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      const ArgumentValue *PrevArgs,
3180673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      unsigned NumPrevArgs,
319686775deca8b8685eb90801495880e3abdd844c2Chris Lattner      SmallVectorImpl<char> &Output,
3200673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      void *Cookie,
321341785ec52f87c0803ba52dc88faac4e136f8593Bill Wendling      ArrayRef<intptr_t> QualTypeVals);
32292dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void *ArgToStringCookie;
3233fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFnTy ArgToStringFn;
32493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
32593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief ID of the "delayed" diagnostic, which is a (typically
32693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// fatal) diagnostic that had to be delayed because it was found
32793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// while emitting another diagnostic.
32893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  unsigned DelayedDiagID;
32993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
33093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief First string argument for the delayed diagnostic.
33193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg1;
33293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
33393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Second string argument for the delayed diagnostic.
33493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg2;
33593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
337d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticsEngine(
338c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                      const IntrusiveRefCntPtr<DiagnosticIDs> &Diags,
33902c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor                      DiagnosticOptions *DiagOpts,
34078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie                      DiagnosticConsumer *client = 0,
34133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                      bool ShouldOwnClient = true);
342d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  ~DiagnosticsEngine();
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
344c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  const IntrusiveRefCntPtr<DiagnosticIDs> &getDiagnosticIDs() const {
34533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags;
34633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
34733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
34802c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  /// \brief Retrieve the diagnostic options.
34902c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  DiagnosticOptions &getDiagnosticOptions() const { return *DiagOpts; }
35002c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor
35178ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *getClient() { return Client; }
35278ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  const DiagnosticConsumer *getClient() const { return Client; }
3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
354d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief Determine whether this \c DiagnosticsEngine object own its client.
35578243658c533168d51fd076fba328437932ba6f1Douglas Gregor  bool ownsClient() const { return OwnsDiagClient; }
35678243658c533168d51fd076fba328437932ba6f1Douglas Gregor
357bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Return the current diagnostic client along with ownership of that
358bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// client.
35978ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *takeClient() {
36033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    OwnsDiagClient = false;
36133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Client;
36233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
36333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
36433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool hasSourceManager() const { return SourceMgr != 0; }
36533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const {
36633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    assert(SourceMgr && "SourceManager not set!");
36733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return *SourceMgr;
36833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
36933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  void setSourceManager(SourceManager *SrcMgr) { SourceMgr = SrcMgr; }
37033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
37133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
372d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  //  DiagnosticsEngine characterization methods, used by a client to customize
373d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  //  how diagnostics are emitted.
37433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //
37504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
376af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Copies the current DiagMappings and pushes the new copy
37704ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// onto the top of the stack.
3780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void pushMappings(SourceLocation Loc);
37904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
380af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Pops the current DiagMappings off the top of the stack,
381af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// causing the new top of the stack to be the active mappings.
382af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
383af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \returns \c true if the pop happens, \c false if there is only one
384af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// DiagMapping on the stack.
3850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  bool popMappings(SourceLocation Loc);
38604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
387bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Set the diagnostic client associated with this diagnostic object.
388bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  ///
38933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// \param ShouldOwnClient true if the diagnostic object should take
39033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// ownership of \c client.
39178ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  void setClient(DiagnosticConsumer *client, bool ShouldOwnClient = true);
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
393af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Specify a limit for the number of errors we should
394af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// emit before giving up.
395af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
396af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Zero disables the limit.
397c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  void setErrorLimit(unsigned Limit) { ErrorLimit = Limit; }
398b205ac9fcd22b87b41697172d1983c5ae9dabaafChris Lattner
399575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Specify the maximum number of template instantiation
400575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// notes to emit along with a given diagnostic.
401575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  void setTemplateBacktraceLimit(unsigned Limit) {
402575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    TemplateBacktraceLimit = Limit;
403575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
40408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
405575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Retrieve the maximum number of template instantiation
40608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// notes to emit along with a given diagnostic.
407575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned getTemplateBacktraceLimit() const {
408575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    return TemplateBacktraceLimit;
409575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
41008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
41108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// \brief Specify the maximum number of constexpr evaluation
41208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// notes to emit along with a given diagnostic.
41308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  void setConstexprBacktraceLimit(unsigned Limit) {
41408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    ConstexprBacktraceLimit = Limit;
41508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
41608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
41708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// \brief Retrieve the maximum number of constexpr evaluation
41808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// notes to emit along with a given diagnostic.
41908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned getConstexprBacktraceLimit() const {
42008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    return ConstexprBacktraceLimit;
42108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
42208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
423af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any unmapped warnings are ignored.
424af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
425af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If this and WarningsAsErrors are both set, then this one wins.
4265b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }
4275b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; }
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
429af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any unmapped ignored warnings are no longer
430af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// ignored.
431af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
432af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If this and IgnoreAllWarnings are both set, then that one wins.
4331e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  void setEnableAllWarnings(bool Val) { EnableAllWarnings = Val; }
4341e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool getEnableAllWarnngs() const { return EnableAllWarnings; }
4351e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek
436af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any warnings reported are issued as errors.
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getWarningsAsErrors() const { return WarningsAsErrors; }
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
440af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any error reported is made a fatal error.
441e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  void setErrorsAsFatal(bool Val) { ErrorsAsFatal = Val; }
442e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool getErrorsAsFatal() const { return ErrorsAsFatal; }
443e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner
444af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true mask warnings that come from system headers.
4452fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
4462fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; }
4472fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar
44881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Suppress all diagnostics, to silence the front end when we
44981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// know that we don't want any more diagnostics to be passed along to the
45081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// client
45181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  void setSuppressAllDiagnostics(bool Val = true) {
45281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor    SuppressAllDiagnostics = Val;
45381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  }
45481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
455246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
456246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// \brief Set type eliding, to skip outputting same types occurring in
457246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// template types.
458246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  void setElideType(bool Val = true) { ElideType = Val; }
459246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool getElideType() { return ElideType; }
460246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
461246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// \brief Set tree printing, to outputting the template difference in a
462246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// tree format.
463246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  void setPrintTemplateTree(bool Val = false) { PrintTemplateTree = Val; }
464246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool getPrintTemplateTree() { return PrintTemplateTree; }
465246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
466246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// \brief Set color printing, so the type diffing will inject color markers
467246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// into the output.
468246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  void setShowColors(bool Val = false) { ShowColors = Val; }
469246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool getShowColors() { return ShowColors; }
470246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
4715edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// \brief Specify which overload candidates to show when overload resolution
472af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// fails.
473af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
474af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// By default, we show all candidates.
4755edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  void setShowOverloads(OverloadsShown Val) {
4765edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    ShowOverloads = Val;
4775edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  }
4785edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown getShowOverloads() const { return ShowOverloads; }
4795edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
4805d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith  /// \brief Pretend that the last diagnostic issued was ignored, so any
4815d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith  /// subsequent notes will be suppressed.
482af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
483af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This can be used by clients who suppress diagnostics themselves.
4840b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  void setLastDiagnosticIgnored() {
4855d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith    if (LastDiagLevel == DiagnosticIDs::Fatal)
4865d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith      FatalErrorOccurred = true;
48733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    LastDiagLevel = DiagnosticIDs::Ignored;
4880b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  }
4890b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor
490af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Controls whether otherwise-unmapped extension diagnostics are
491af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// mapped onto ignore/warning/error.
492af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
493af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This corresponds to the GCC -pedantic and -pedantic-errors option.
494b54b276a920246c595a0498da281821eb9d22996Chris Lattner  void setExtensionHandlingBehavior(ExtensionHandling H) {
495b54b276a920246c595a0498da281821eb9d22996Chris Lattner    ExtBehavior = H;
496b54b276a920246c595a0498da281821eb9d22996Chris Lattner  }
497c1b5fa41f09512c74030b9a2a0d1564535e22a76Peter Collingbourne  ExtensionHandling getExtensionHandlingBehavior() const { return ExtBehavior; }
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
499af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Counter bumped when an __extension__  block is/ encountered.
500af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
501af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// When non-zero, all extension diagnostics are entirely silenced, no
502af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// matter how they are mapped.
50327ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
50427ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
50591a2886d558ea6ca7a0bed73ab5acea5ae78eac2Douglas Gregor  bool hasAllExtensionsSilenced() { return AllExtensionsSilenced != 0; }
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
507697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief This allows the client to specify that certain warnings are
508697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// ignored.
509697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
510697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// Notes can never be mapped, errors can only be mapped to fatal, and
511697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// WARNINGs and EXTENSIONs can be mapped arbitrarily.
5120827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
5130827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location that this change of diagnostic state should
5140827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the latest state.
5150827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
5160827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                            SourceLocation Loc);
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
518697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Change an entire diagnostic group (e.g. "unknown-pragmas") to
519697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// have the specified mapping.
520697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
521697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \returns true (and ignores the request) if "Group" was unknown, false
522697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// otherwise.
5230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
524697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \param Loc The source location that this change of diagnostic state should
5250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the state from command-line.
526686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  bool setDiagnosticGroupMapping(StringRef Group, diag::Mapping Map,
5273f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar                                 SourceLocation Loc = SourceLocation());
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
529697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Set the warning-as-error flag for the given diagnostic.
530697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
531697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This function always only operates on the current diagnostic state.
5323f22509ebf91aa96658ca6e5c5c0b926d8d62e34Chad Rosier  void setDiagnosticWarningAsError(diag::kind Diag, bool Enabled);
5333f22509ebf91aa96658ca6e5c5c0b926d8d62e34Chad Rosier
534697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Set the warning-as-error flag for the given diagnostic group.
535697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
536697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This function always only operates on the current diagnostic state.
5374aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  ///
5384aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  /// \returns True if the given group is unknown, false otherwise.
5394aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled);
5404aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
541697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Set the error-as-fatal flag for the given diagnostic.
542697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
543697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This function always only operates on the current diagnostic state.
5443f22509ebf91aa96658ca6e5c5c0b926d8d62e34Chad Rosier  void setDiagnosticErrorAsFatal(diag::kind Diag, bool Enabled);
5453f22509ebf91aa96658ca6e5c5c0b926d8d62e34Chad Rosier
546697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Set the error-as-fatal flag for the given diagnostic group.
547697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
548697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This function always only operates on the current diagnostic state.
5494aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  ///
5504aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  /// \returns True if the given group is unknown, false otherwise.
5514aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled);
5524aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
553697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Add the specified mapping to all diagnostics.
554697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
555697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// Mainly to be used by -Wno-everything to disable all warnings but allow
556697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// subsequent -W options to enable specific warnings.
55782e6411d004064a29f03a3ea8b8919f297bfa843Argyrios Kyrtzidis  void setMappingToAllDiagnostics(diag::Mapping Map,
55811583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis                                  SourceLocation Loc = SourceLocation());
55911583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool hasErrorOccurred() const { return ErrorOccurred; }
56115221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
56285bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
56385bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  /// \brief Determine whether any kind of unrecoverable error has occurred.
56485bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  bool hasUnrecoverableErrorOccurred() const {
56585bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor    return FatalErrorOccurred || UnrecoverableErrorOccurred;
56685bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  }
56785bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
56853eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned getNumWarnings() const { return NumWarnings; }
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
570c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  void setNumWarnings(unsigned NumWarnings) {
571c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    this->NumWarnings = NumWarnings;
572c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  }
573c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
574697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Return an ID for a diagnostic with the specified message and level.
575697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
576697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// If this is the first request for this diagnosic, it is registered and
577697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// created, otherwise the existing ID is returned.
578686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getCustomDiagID(Level L, StringRef Message) {
57933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->getCustomDiagID((DiagnosticIDs::Level)L, Message);
58033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
582697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Converts a diagnostic argument (as an intptr_t) into the string
583697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// that represents it.
5843fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
5853fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          const char *Modifier, unsigned ModLen,
5863fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                          const char *Argument, unsigned ArgLen,
587b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                          const ArgumentValue *PrevArgs, unsigned NumPrevArgs,
588686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                          SmallVectorImpl<char> &Output,
5894def70d3040e73707c738f7c366737a986135edfRichard Smith                          ArrayRef<intptr_t> QualTypeVals) const {
590b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen,
5910673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth                  PrevArgs, NumPrevArgs, Output, ArgToStringCookie,
5920673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth                  QualTypeVals);
59322caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59592dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {
5963fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner    ArgToStringFn = Fn;
59792dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner    ArgToStringCookie = Cookie;
59822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
600abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reset the state of the diagnostic object to its initial
601abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// configuration.
602abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void Reset();
603abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
605d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  // DiagnosticsEngine classification and reporting interfaces.
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
608d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief Based on the way the client configured the DiagnosticsEngine
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// object, classify the specified diagnostic ID into a Level, consumable by
61078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  /// the DiagnosticConsumer.
6110827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
6120827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location we are interested in finding out the
6130827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// diagnostic state. Can be null in order to query the latest state.
6141656aaec223949245fdbebf306b719cba0b687caDaniel Dunbar  Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc) const {
6151656aaec223949245fdbebf306b719cba0b687caDaniel Dunbar    return (Level)Diags->getDiagnosticLevel(DiagID, Loc, *this);
61633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
618697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Issue the message to the client.
619697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
620697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This actually returns an instance of DiagnosticBuilder which emits the
621697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// diagnostics (through @c ProcessDiag) when it is destroyed.
622697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
623697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \param DiagID A member of the @c diag::kind enum.
624efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett  /// \param Loc Represents the source location associated with the diagnostic,
625509355e982d15da4f8f3939493516379665f6275Ted Kremenek  /// which can be an invalid location if no position information is available.
626efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett  inline DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID);
6270f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbar  inline DiagnosticBuilder Report(unsigned DiagID);
628de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor
629e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  void Report(const StoredDiagnostic &storedDiag);
630e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
63193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Determine whethere there is already a diagnostic in flight.
6325800f7ea9cf5621280089d690f677bd28064d6b5Ted Kremenek  bool isDiagnosticInFlight() const { return CurDiagID != ~0U; }
63393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
63493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Set the "delayed" diagnostic that will be emitted once
63593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// the current diagnostic completes.
63693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
63793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  If a diagnostic is already in-flight but the front end must
63893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  report a problem (e.g., with an inconsistent file system
63993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  state), this routine sets a "delayed" diagnostic that will be
64093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  emitted after the current diagnostic completes. This should
64193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  only be used for fatal errors detected at inconvenient
64293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  times. If emitting a delayed diagnostic causes a second delayed
64393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  diagnostic to be introduced, that second delayed diagnostic
64493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///  will be ignored.
64593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
64693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param DiagID The ID of the diagnostic being delayed.
64793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
64893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param Arg1 A string argument that will be provided to the
64993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// diagnostic. A copy of this string will be stored in the
650d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// DiagnosticsEngine object itself.
65193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  ///
65293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \param Arg2 A string argument that will be provided to the
65393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// diagnostic. A copy of this string will be stored in the
654d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// DiagnosticsEngine object itself.
655686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1 = "",
656686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            StringRef Arg2 = "");
65793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
658de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor  /// \brief Clear out the current diagnostic.
659de4bf6a63219c5b9d3bce1fed3dfe075568098a0Douglas Gregor  void Clear() { CurDiagID = ~0U; }
6601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6610a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattnerprivate:
66293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Report the delayed diagnostic.
66393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  void ReportDelayed();
66493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
6653cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  // This is private state used by DiagnosticBuilder.  We put it here instead of
6663cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight
6670a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // object.  This implementation choice means that we can only have one
6680a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // diagnostic "in flight" at a time, but this seems to be a reasonable
6690a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // tradeoff to keep these objects small.  Assertions verify that only one
6700a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  // diagnostic is in flight at a time.
67133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  friend class DiagnosticIDs;
6723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  friend class DiagnosticBuilder;
67340847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  friend class Diagnostic;
6749b623639378d53a675921ddfa7316034d571881eDouglas Gregor  friend class PartialDiagnostic;
6759c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  friend class DiagnosticErrorTrap;
6769b623639378d53a675921ddfa7316034d571881eDouglas Gregor
677697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The location of the current diagnostic that is in flight.
67833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceLocation CurDiagLoc;
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
680697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The ID of the current diagnostic that is in flight.
681697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
6823cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// This is set to ~0U when there is no diagnostic in flight.
683b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  unsigned CurDiagID;
684b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner
68573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  enum {
686697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett    /// \brief The maximum number of arguments we can hold.
687697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett    ///
688af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// We currently only support up to 10 arguments (%0-%9).  A single
689af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// diagnostic with more than that almost certainly has to be simplified
690af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// anyway.
691981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    MaxArguments = 10,
692981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar
693697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett    /// \brief The maximum number of ranges we can hold.
694981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    MaxRanges = 10,
695981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar
696697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett    /// \brief The maximum number of ranges we can hold.
697981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    MaxFixItHints = 10
69873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  };
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
700697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The number of entries in Arguments.
701730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  signed char NumDiagArgs;
702697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The number of ranges in the DiagRanges array.
703730e0fdbbf9b315ef834074054ac55af3a4fcc0cChris Lattner  unsigned char NumDiagRanges;
704697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The number of hints in the DiagFixItHints array.
705981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar  unsigned char NumDiagFixItHints;
706ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
707af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Specifies whether an argument is in DiagArgumentsStr or
708af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// in DiagArguments.
709af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
710af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This is an array of ArgumentKind::ArgumentKind enum values, one for each
711af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// argument.
71273d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  unsigned char DiagArgumentsKind[MaxArguments];
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Holds the values of each string argument for the current
715697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// diagnostic.
716697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
717697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This is only used when the corresponding ArgumentKind is ak_std_string.
71873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  std::string DiagArgumentsStr[MaxArguments];
71973d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner
720697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The values for the various substitution positions.
721697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
722697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This is used when the argument is not an std::string.  The specific
723697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// value is mangled into an intptr_t and the interpretation depends on
724697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// exactly what sort of argument kind it is.
72573d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  intptr_t DiagArgumentsVal[MaxArguments];
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
727697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief The list of ranges added to this diagnostic.
728981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar  CharSourceRange DiagRanges[MaxRanges];
7291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
730697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief If valid, provides a hint with some code to insert, remove,
731981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar  /// or modify at a particular position.
732981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar  FixItHint DiagFixItHints[MaxFixItHints];
7334b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
73487429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis  DiagnosticMappingInfo makeMappingInfo(diag::Mapping Map, SourceLocation L) {
73587429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    bool isPragma = L.isValid();
73687429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    DiagnosticMappingInfo MappingInfo = DiagnosticMappingInfo::Make(
73787429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis      Map, /*IsUser=*/true, isPragma);
73887429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis
73987429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    // If this is a pragma mapping, then set the diagnostic mapping flags so
74087429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    // that we override command line options.
74187429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    if (isPragma) {
74287429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis      MappingInfo.setNoWarningAsError(true);
74387429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis      MappingInfo.setNoErrorAsFatal(true);
74487429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    }
74587429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis
74687429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis    return MappingInfo;
74787429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis  }
74887429a05ff66fe6460eaa4855fd0da574e26bf7bArgyrios Kyrtzidis
749697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Used to report a diagnostic that is finally fully formed.
7505e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  ///
751697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \returns true if the diagnostic was emitted, false if it was suppressed.
75233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool ProcessDiag() {
75333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->ProcessDiag(*this);
75433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
755f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis
756393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  /// @name Diagnostic Emission
757393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  /// @{
758393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbarprotected:
759393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // Sema requires access to the following functions because the current design
760393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // of SFINAE requires it to use its own SemaDiagnosticBuilder, which needs to
761393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // access us directly to ensure we minimize the emitted code for the common
762393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // Sema::Diag() patterns.
763393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  friend class Sema;
764393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar
7653054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  /// \brief Emit the current diagnostic and clear the diagnostic state.
766c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  ///
767c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  /// \param Force Emit the diagnostic regardless of suppression settings.
768c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  bool EmitCurrentDiagnostic(bool Force = false);
7693054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar
770393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  unsigned getCurrentDiagID() const { return CurDiagID; }
771393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar
772393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  SourceLocation getCurrentDiagLoc() const { return CurDiagLoc; }
773393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar
774393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  /// @}
775393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar
776f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  friend class ASTReader;
777f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  friend class ASTWriter;
7783cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner};
7793cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
7809c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// \brief RAII class that determines when any errors have occurred
7819c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// between the time the instance was created and the time it was
7829c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis/// queried.
7839c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidisclass DiagnosticErrorTrap {
784d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &Diag;
785c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned NumErrors;
786c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned NumUnrecoverableErrors;
7879c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
7889c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidispublic:
789d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticErrorTrap(DiagnosticsEngine &Diag)
790add80bb3957dba1d9389304453c0a4c975768bd7John McCall    : Diag(Diag) { reset(); }
7919c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
7929c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  /// \brief Determine whether any errors have occurred since this
7939c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  /// object instance was created.
7949c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  bool hasErrorOccurred() const {
795c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    return Diag.TrapNumErrorsOccurred > NumErrors;
796add80bb3957dba1d9389304453c0a4c975768bd7John McCall  }
797add80bb3957dba1d9389304453c0a4c975768bd7John McCall
798add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// \brief Determine whether any unrecoverable errors have occurred since this
799add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// object instance was created.
800add80bb3957dba1d9389304453c0a4c975768bd7John McCall  bool hasUnrecoverableErrorOccurred() const {
801c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    return Diag.TrapNumUnrecoverableErrorsOccurred > NumUnrecoverableErrors;
8029c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis  }
8039c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
804af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Set to initial state of "no errors occurred".
805add80bb3957dba1d9389304453c0a4c975768bd7John McCall  void reset() {
806c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    NumErrors = Diag.TrapNumErrorsOccurred;
807c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis    NumUnrecoverableErrors = Diag.TrapNumUnrecoverableErrorsOccurred;
808add80bb3957dba1d9389304453c0a4c975768bd7John McCall  }
8099c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis};
8109c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis
8113cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
8123cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner// DiagnosticBuilder
8133cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
8143cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
815697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// \brief A little helper class used to produce diagnostics.
816697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett///
817697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// This is constructed by the DiagnosticsEngine::Report method, and
818697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// allows insertion of extra information (arguments and source ranges) into
819697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// the currently "in flight" diagnostic.  When the temporary for the builder
820697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// is destroyed, the diagnostic is issued.
8213cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner///
8223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// Note that many of these will be created as temporary objects (many call
8233cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// sites), so we want them to be small and we never want their address taken.
8243cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// This ensures that compilers with somewhat reasonable optimizers will promote
8253cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// the common fields to registers, eliminating increments of the NumArgs field,
8263cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner/// for example.
8273cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerclass DiagnosticBuilder {
828d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  mutable DiagnosticsEngine *DiagObj;
829981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar  mutable unsigned NumArgs, NumRanges, NumFixits;
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8317059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  /// \brief Status variable indicating if this diagnostic is still active.
8327059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  ///
833f3477c13eeaf11b32a41f181398fb5deffd0dd73Sylvestre Ledru  // NOTE: This field is redundant with DiagObj (IsActive iff (DiagObj == 0)),
8347059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  // but LLVM is not currently smart enough to eliminate the null check that
8357059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  // Emit() would end up with if we used that as our status variable.
8367059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  mutable bool IsActive;
8377059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar
838c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  /// \brief Flag indicating that this diagnostic is being emitted via a
839c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  /// call to ForceEmit.
840c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  mutable bool IsForceEmit;
841c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose
842f56faa01936b9cf909623d7f06e3c2569ca4a78eDmitri Gribenko  void operator=(const DiagnosticBuilder &) LLVM_DELETED_FUNCTION;
843d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  friend class DiagnosticsEngine;
844ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
845ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  DiagnosticBuilder()
846c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    : DiagObj(0), NumArgs(0), NumRanges(0), NumFixits(0), IsActive(false),
847c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose      IsForceEmit(false) { }
848ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
849d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticBuilder(DiagnosticsEngine *diagObj)
850c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    : DiagObj(diagObj), NumArgs(0), NumRanges(0), NumFixits(0), IsActive(true),
851c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose      IsForceEmit(false) {
852981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    assert(diagObj && "DiagnosticBuilder requires a valid DiagnosticsEngine!");
853cbf46a0b2bb2b47d6e13437e0d52cc583c5ce539Argyrios Kyrtzidis  }
854ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
8559b623639378d53a675921ddfa7316034d571881eDouglas Gregor  friend class PartialDiagnostic;
856ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
857b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregorprotected:
8583054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  void FlushCounts() {
8593054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    DiagObj->NumDiagArgs = NumArgs;
8603054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    DiagObj->NumDiagRanges = NumRanges;
8613054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    DiagObj->NumDiagFixItHints = NumFixits;
8623054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  }
86306265bd82073c06294a02a0bd891951fafb484a5Daniel Dunbar
864cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  /// \brief Clear out the current diagnostic.
8657059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  void Clear() const {
8667059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    DiagObj = 0;
8677059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    IsActive = false;
868c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    IsForceEmit = false;
8697059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  }
870cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar
871697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Determine whether this diagnostic is still active.
8727059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar  bool isActive() const { return IsActive; }
87306265bd82073c06294a02a0bd891951fafb484a5Daniel Dunbar
874cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  /// \brief Force the diagnostic builder to emit the diagnostic now.
875cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  ///
876cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  /// Once this function has been called, the DiagnosticBuilder object
877cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  /// should not be used again before it is destroyed.
878cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  ///
879cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  /// \returns true if a diagnostic was emitted, false if the
880cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar  /// diagnostic was suppressed.
8813054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  bool Emit() {
8827059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    // If this diagnostic is inactive, then its soul was stolen by the copy ctor
8837059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    // (or by a subclass, as in SemaDiagnosticBuilder).
8847059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    if (!isActive()) return false;
8853054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar
8863054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    // When emitting diagnostics, we set the final argument count into
8873054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    // the DiagnosticsEngine object.
8883054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    FlushCounts();
8893054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar
8903054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    // Process the diagnostic.
891c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    bool Result = DiagObj->EmitCurrentDiagnostic(IsForceEmit);
8923054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar
8933054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    // This diagnostic is dead.
8947059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    Clear();
8953054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar
8963054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    return Result;
8973054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  }
8989b623639378d53a675921ddfa7316034d571881eDouglas Gregor
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
9000a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// Copy constructor.  When copied, this "takes" the diagnostic info from the
9010a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  /// input and neuters it.
9023cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DiagnosticBuilder(const DiagnosticBuilder &D) {
9030a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    DiagObj = D.DiagObj;
9047059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    IsActive = D.IsActive;
905c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    IsForceEmit = D.IsForceEmit;
9067059a1cbc29444bec19380059a62db085eb92964Daniel Dunbar    D.Clear();
90725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    NumArgs = D.NumArgs;
90825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    NumRanges = D.NumRanges;
909981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    NumFixits = D.NumFixits;
9100a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
91125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor
912ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  /// \brief Retrieve an empty diagnostic builder.
913ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  static DiagnosticBuilder getEmpty() {
914ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    return DiagnosticBuilder();
915ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  }
916ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
917697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Emits the diagnostic.
91853c83b32550b3f29db2419160fa2e3684e420556Daniel Dunbar  ~DiagnosticBuilder() {
9193054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    Emit();
92053c83b32550b3f29db2419160fa2e3684e420556Daniel Dunbar  }
9219b623639378d53a675921ddfa7316034d571881eDouglas Gregor
922c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  /// \brief Forces the diagnostic to be emitted.
923c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  const DiagnosticBuilder &setForceEmit() const {
924c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    IsForceEmit = true;
925c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    return *this;
926c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  }
927c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose
928af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Conversion of DiagnosticBuilder to bool always returns \c true.
929af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
930af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This allows is to be used in boolean error contexts (where \c true is
931af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// used to indicate that an error has occurred), like:
932af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \code
9333cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  /// return Diag(...);
934af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \endcode
9353cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  operator bool() const { return true; }
9363cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
937686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void AddString(StringRef S) const {
938cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    assert(isActive() && "Clients must not add to cleared diagnostic!");
939d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(NumArgs < DiagnosticsEngine::MaxArguments &&
9403cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
941cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    DiagObj->DiagArgumentsKind[NumArgs] = DiagnosticsEngine::ak_std_string;
942cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    DiagObj->DiagArgumentsStr[NumArgs++] = S;
9433cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  }
9441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
945d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
946cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    assert(isActive() && "Clients must not add to cleared diagnostic!");
947d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(NumArgs < DiagnosticsEngine::MaxArguments &&
9483cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
949cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    DiagObj->DiagArgumentsKind[NumArgs] = Kind;
950cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    DiagObj->DiagArgumentsVal[NumArgs++] = V;
9510a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9530a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  void AddSourceRange(const CharSourceRange &R) const {
954cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    assert(isActive() && "Clients must not add to cleared diagnostic!");
955981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    assert(NumRanges < DiagnosticsEngine::MaxRanges &&
9563cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "Too many arguments to diagnostic!");
957cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    DiagObj->DiagRanges[NumRanges++] = R;
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9594b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
960849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  void AddFixItHint(const FixItHint &Hint) const {
961cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    assert(isActive() && "Clients must not add to cleared diagnostic!");
962981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    assert(NumFixits < DiagnosticsEngine::MaxFixItHints &&
963981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar           "Too many arguments to diagnostic!");
964cefcbef6f3b1e34f31ce0e47214c8b90fedfb9acDaniel Dunbar    DiagObj->DiagFixItHints[NumFixits++] = Hint;
9654b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
96682c6dc72adc30e785ce5bc6e8b43ae92070d2e08David Blaikie
96782c6dc72adc30e785ce5bc6e8b43ae92070d2e08David Blaikie  bool hasMaxRanges() const {
96882c6dc72adc30e785ce5bc6e8b43ae92070d2e08David Blaikie    return NumRanges == DiagnosticsEngine::MaxRanges;
96982c6dc72adc30e785ce5bc6e8b43ae92070d2e08David Blaikie  }
9703cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner};
9713cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
9723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
973686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                           StringRef S) {
9743cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddString(S);
9753cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
9763cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
9773cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
9783cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
9793cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const char *Str) {
9803cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str),
981d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                  DiagnosticsEngine::ak_c_string);
9823cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
9833cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
9843cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
9853cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) {
986d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
9873cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
9883cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
9893cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
9903bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidisinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,bool I) {
991d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
9923bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis  return DB;
9933bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis}
9943bf4a79712fb30dc27692ef2d4214ee7fa6681beArgyrios Kyrtzidis
9953cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
9963cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           unsigned I) {
997d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DB.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
9983cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
9993cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
10003cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
10013cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
10023cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const IdentifierInfo *II) {
10033cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DB.AddTaggedVal(reinterpret_cast<intptr_t>(II),
1004d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                  DiagnosticsEngine::ak_identifierinfo);
10053cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
10063cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
10071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10083f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// Adds a DeclContext to the diagnostic. The enable_if template magic is here
10093f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// so that we only match those arguments that are (statically) DeclContexts;
10103f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// other arguments that derive from DeclContext (e.g., RecordDecls) will not
10113f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor// match.
10123f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregortemplate<typename T>
10133f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregorinline
10143f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregortypename llvm::enable_if<llvm::is_same<T, DeclContext>,
10153f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor                         const DiagnosticBuilder &>::type
10163f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregoroperator<<(const DiagnosticBuilder &DB, T *DC) {
10173f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor  DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
1018d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                  DiagnosticsEngine::ak_declcontext);
10193f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor  return DB;
10203f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor}
10213f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor
10223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
10233cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner                                           const SourceRange &R) {
10240a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  DB.AddSourceRange(CharSourceRange::getTokenRange(R));
10253cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DB;
10263cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
10274b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
10284b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
10290a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner                                           const CharSourceRange &R) {
10300a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  DB.AddSourceRange(R);
10310a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  return DB;
10320a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner}
10330a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
10340a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattnerinline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1035849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor                                           const FixItHint &Hint) {
10363739b32a01b9778176757425e62252c288b85383Ted Kremenek  if (!Hint.isNull())
10373739b32a01b9778176757425e62252c288b85383Ted Kremenek    DB.AddFixItHint(Hint);
10384b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  return DB;
10394b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
1040ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
1041d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieinline DiagnosticBuilder DiagnosticsEngine::Report(SourceLocation Loc,
104233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                                            unsigned DiagID){
10433cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
10443cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  CurDiagLoc = Loc;
10453cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  CurDiagID = DiagID;
10463cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  return DiagnosticBuilder(this);
10473cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner}
1048d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieinline DiagnosticBuilder DiagnosticsEngine::Report(unsigned DiagID) {
104933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Report(SourceLocation(), DiagID);
10500f9fed70cea107b3f79df554e38bd8e98d48fe47Daniel Dunbar}
10513cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner
10523cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
105340847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie// Diagnostic
10543cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner//===----------------------------------------------------------------------===//
10551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1056697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// A little helper class (which is basically a smart pointer that forwards
1057697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// info from DiagnosticsEngine) that allows clients to enquire about the
1058697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett/// currently in-flight diagnostic.
105940847cfb58acc3cac7d68727df9455ac45f2e118David Blaikieclass Diagnostic {
1060d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine *DiagObj;
1061686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef StoredDiagMessage;
10623cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerpublic:
106340847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  explicit Diagnostic(const DiagnosticsEngine *DO) : DiagObj(DO) {}
106440847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  Diagnostic(const DiagnosticsEngine *DO, StringRef storedDiagMessage)
1065e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis    : DiagObj(DO), StoredDiagMessage(storedDiagMessage) {}
10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1067d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine *getDiags() const { return DiagObj; }
1068b7fc3b87d065041a10eaa0603d738df21ff7af3aChris Lattner  unsigned getID() const { return DiagObj->CurDiagID; }
106933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const SourceLocation &getLocation() const { return DiagObj->CurDiagLoc; }
107033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool hasSourceManager() const { return DiagObj->hasSourceManager(); }
107133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const { return DiagObj->getSourceManager();}
10721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10730a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  unsigned getNumArgs() const { return DiagObj->NumDiagArgs; }
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1075af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the kind of the specified index.
1076af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1077af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Based on the kind of argument, the accessors below can be used to get
1078af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// the value.
1079af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1080af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre Idx < getNumArgs()
1081d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine::ArgumentKind getArgKind(unsigned Idx) const {
10823cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    assert(Idx < getNumArgs() && "Argument index out of range!");
1083d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    return (DiagnosticsEngine::ArgumentKind)DiagObj->DiagArgumentsKind[Idx];
108473d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  }
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1086af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the provided argument string specified by \p Idx.
1087af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_std_string
108873d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  const std::string &getArgStdStr(unsigned Idx) const {
1089d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_std_string &&
10903cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
109173d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    return DiagObj->DiagArgumentsStr[Idx];
109273d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  }
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1094af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the specified C string argument.
1095af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_c_string
109673d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner  const char *getArgCStr(unsigned Idx) const {
1097d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_c_string &&
10983cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
109973d2a1b05bb04ab0136af374ddaa5d4602d4c939Chris Lattner    return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]);
11000a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1102af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the specified signed integer argument.
1103af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_sint
110430bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  int getArgSInt(unsigned Idx) const {
1105d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_sint &&
11063cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
110730bc96544346bea42921cf6837e66cef80d664b4Chris Lattner    return (int)DiagObj->DiagArgumentsVal[Idx];
110830bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  }
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1110af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the specified unsigned integer argument.
1111af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_uint
111230bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  unsigned getArgUInt(unsigned Idx) const {
1113d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_uint &&
11143cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
111530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner    return (unsigned)DiagObj->DiagArgumentsVal[Idx];
111630bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  }
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1118af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the specified IdentifierInfo argument.
1119af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre getArgKind(Idx) == DiagnosticsEngine::ak_identifierinfo
112043b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  const IdentifierInfo *getArgIdentifier(unsigned Idx) const {
1121d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) == DiagnosticsEngine::ak_identifierinfo &&
11223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner           "invalid argument accessor!");
11239634379265855f1628190e926d9aaf1fb4a5d90eChris Lattner    return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]);
112443b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner  }
11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1126af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the specified non-string argument in an opaque form.
1127af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre getArgKind(Idx) != DiagnosticsEngine::ak_std_string
112822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  intptr_t getRawArg(unsigned Idx) const {
1129d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    assert(getArgKind(Idx) != DiagnosticsEngine::ak_std_string &&
113022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner           "invalid argument accessor!");
113122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    return DiagObj->DiagArgumentsVal[Idx];
113222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  }
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1134af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return the number of source ranges associated with this diagnostic.
11350a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  unsigned getNumRanges() const {
11360a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    return DiagObj->NumDiagRanges;
11370a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1139af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \pre Idx < getNumRanges()
11400a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  const CharSourceRange &getRange(unsigned Idx) const {
11410a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!");
114251c6d384551674facc19f745ecf6e289d28dc55fDouglas Gregor    return DiagObj->DiagRanges[Idx];
11430a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  }
11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114528409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth  /// \brief Return an array reference for this diagnostic's ranges.
114628409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth  ArrayRef<CharSourceRange> getRanges() const {
114728409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth    return llvm::makeArrayRef(DiagObj->DiagRanges, DiagObj->NumDiagRanges);
114828409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth  }
114928409289193afca1a3178a9796f8e8aebcb76896Chandler Carruth
1150849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  unsigned getNumFixItHints() const {
1151981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    return DiagObj->NumDiagFixItHints;
11524b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
11534b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1154849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  const FixItHint &getFixItHint(unsigned Idx) const {
1155981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    assert(Idx < getNumFixItHints() && "Invalid index!");
1156981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    return DiagObj->DiagFixItHints[Idx];
11574b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
11584b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1159849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  const FixItHint *getFixItHints() const {
1160981e279b95f3a71e2268124b1abaae050ec200dcDaniel Dunbar    return getNumFixItHints()? DiagObj->DiagFixItHints : 0;
11614b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
11624b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1163af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Format this diagnostic into a string, substituting the
1164af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// formal arguments into the %0 slots.
1165af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1166af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The result is appended onto the \p OutStr array.
1167686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void FormatDiagnostic(SmallVectorImpl<char> &OutStr) const;
11689f28614bf1a8387000d8df57a713fcf69e198145John McCall
1169af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Format the given format-string into the output buffer using the
1170af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// arguments stored in this diagnostic.
11719f28614bf1a8387000d8df57a713fcf69e198145John McCall  void FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
1172686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                        SmallVectorImpl<char> &OutStr) const;
1173a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor};
1174a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1175a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor/**
1176d3ab63e0f66429abf2a3e4cde889e420e41e8790Douglas Gregor * \brief Represents a diagnostic in a form that can be retained until its
1177d3ab63e0f66429abf2a3e4cde889e420e41e8790Douglas Gregor * corresponding source manager is destroyed.
1178a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor */
1179a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregorclass StoredDiagnostic {
1180aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  unsigned ID;
1181d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine::Level Level;
1182a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  FullSourceLoc Loc;
1183a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  std::string Message;
11840a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  std::vector<CharSourceRange> Ranges;
1185849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  std::vector<FixItHint> FixIts;
1186a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1187a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregorpublic:
1188a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  StoredDiagnostic();
118940847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  StoredDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info);
1190d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1191686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                   StringRef Message);
1192d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1193686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                   StringRef Message, FullSourceLoc Loc,
11942d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                   ArrayRef<CharSourceRange> Ranges,
11952d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                   ArrayRef<FixItHint> Fixits);
1196a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  ~StoredDiagnostic();
1197a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1198a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief Evaluates true when this object stores a diagnostic.
1199a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  operator bool() const { return Message.size() > 0; }
1200a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1201aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  unsigned getID() const { return ID; }
1202d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine::Level getLevel() const { return Level; }
1203a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  const FullSourceLoc &getLocation() const { return Loc; }
1204686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMessage() const { return Message; }
1205c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
1206c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  void setLocation(FullSourceLoc Loc) { this->Loc = Loc; }
1207c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
12080a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  typedef std::vector<CharSourceRange>::const_iterator range_iterator;
1209a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  range_iterator range_begin() const { return Ranges.begin(); }
1210a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  range_iterator range_end() const { return Ranges.end(); }
1211a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  unsigned range_size() const { return Ranges.size(); }
12126700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek
12136700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek  ArrayRef<CharSourceRange> getRanges() const {
12146700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek    return llvm::makeArrayRef(Ranges);
12156700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek  }
12166700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek
1217a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1218849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  typedef std::vector<FixItHint>::const_iterator fixit_iterator;
1219a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  fixit_iterator fixit_begin() const { return FixIts.begin(); }
1220a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  fixit_iterator fixit_end() const { return FixIts.end(); }
1221a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  unsigned fixit_size() const { return FixIts.size(); }
12226700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek
12236700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek  ArrayRef<FixItHint> getFixIts() const {
12246700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek    return llvm::makeArrayRef(FixIts);
12256700d67925abd674ecddfc56948967f49b25bf39Ted Kremenek  }
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1228af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// \brief Abstract interface, implemented by clients of the front-end, which
1229af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// formats and prints fully processed diagnostics.
123078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikieclass DiagnosticConsumer {
1231ab41b97c3cddf1bcdd8bf82ab09ed3ceafcd05b1Argyrios Kyrtzidisprotected:
1232af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumWarnings;       ///< Number of warnings reported
1233af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumErrors;         ///< Number of errors reported
1234f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
123678ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer() : NumWarnings(0), NumErrors(0) { }
1237f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
1238f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned getNumErrors() const { return NumErrors; }
1239f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  unsigned getNumWarnings() const { return NumWarnings; }
124061d679ab2831b161c857cf3f974312fbd4ef1efdArgyrios Kyrtzidis  virtual void clear() { NumWarnings = NumErrors = 0; }
1241f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
124278ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  virtual ~DiagnosticConsumer();
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1244af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Callback to inform the diagnostic client that processing
1245efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// of a source file is beginning.
1246efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  ///
1247efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// Note that diagnostics may be emitted outside the processing of a source
1248efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// file, for example during the parsing of command line options. However,
1249efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// diagnostics with source range information are required to only be emitted
1250efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  /// in between BeginSourceFile() and EndSourceFile().
1251efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  ///
1252af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \param LangOpts The language options for the source file being processed.
1253af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \param PP The preprocessor object being used for the source; this is
1254af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// optional, e.g., it may not be present when processing AST source files.
12557d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  virtual void BeginSourceFile(const LangOptions &LangOpts,
12567d90199f109290e9d587479a481a2850d390b552Daniel Dunbar                               const Preprocessor *PP = 0) {}
1257efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar
1258af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Callback to inform the diagnostic client that processing
1259af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// of a source file has ended.
1260af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1261af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The diagnostic client should assume that any objects made available via
1262af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// BeginSourceFile() are inaccessible.
1263efcbe9475348ecab6b85153baa21d0e894e39607Daniel Dunbar  virtual void EndSourceFile() {}
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
126529f2787b6da552018a7716f18f5bd8f67bd6edb5Argyrios Kyrtzidis  /// \brief Callback to inform the diagnostic client that processing of all
126629f2787b6da552018a7716f18f5bd8f67bd6edb5Argyrios Kyrtzidis  /// source files has ended.
126729f2787b6da552018a7716f18f5bd8f67bd6edb5Argyrios Kyrtzidis  virtual void finish() {}
126829f2787b6da552018a7716f18f5bd8f67bd6edb5Argyrios Kyrtzidis
1269af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Indicates whether the diagnostics handled by this
127078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  /// DiagnosticConsumer should be included in the number of diagnostics
127178ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  /// reported by DiagnosticsEngine.
1272af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1273af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The default implementation returns true.
1274cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek  virtual bool IncludeInDiagnosticCounts() const;
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1276af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Handle this diagnostic, reporting it to the user or
12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// capturing it to a log as needed.
1278f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  ///
1279af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The default implementation just keeps track of the total number of
1280af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// warnings and errors.
1281d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
128240847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie                                const Diagnostic &Info);
1283aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor
1284aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  /// \brief Clone the diagnostic consumer, producing an equivalent consumer
1285aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  /// that can be used in a different context.
1286aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const = 0;
1287aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor};
1288aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor
1289af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// \brief A diagnostic client that ignores all diagnostics.
1290aee526e77657afd1600276450e9c346953ad51d7Douglas Gregorclass IgnoringDiagConsumer : public DiagnosticConsumer {
129199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
1292aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
1293aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                        const Diagnostic &Info) {
1294aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor    // Just ignore it.
1295aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  }
1296aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
1297aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor    return new IgnoringDiagConsumer();
1298aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  }
12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1301246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu// Struct used for sending info about how a type should be printed.
1302246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieustruct TemplateDiffTypes {
1303246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  intptr_t FromType;
1304246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  intptr_t ToType;
1305246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  unsigned PrintTree : 1;
1306246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  unsigned PrintFromType : 1;
1307246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  unsigned ElideType : 1;
1308246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  unsigned ShowColors : 1;
13095409d28b6167032696f4915bb765a6f7db579f3fRichard Trieu  // The printer sets this variable to true if the template diff was used.
13105409d28b6167032696f4915bb765a6f7db579f3fRichard Trieu  unsigned TemplateDiffUsed : 1;
1311246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu};
1312246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
1313246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu/// Special character that the diagnostic printer will use to toggle the bold
1314246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu/// attribute.  The character itself will be not be printed.
1315246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieuconst char ToggleHighlight = 127;
1316246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1320