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"
246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/ADT/iterator_range.h"
250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include <list>
2630a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <vector>
27f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  class DeclContext;
3033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  class DiagnosticBuilder;
316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  class DiagnosticConsumer;
326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  class DiagnosticErrorTrap;
3302c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  class DiagnosticOptions;
34d42ffbd22fc7eb61321f6a88173ee424991f01c6Ted Kremenek  class IdentifierInfo;
3540469651a3f8379dc0f32df69e9bade06a2aad36Chris Lattner  class LangOptions;
367d90199f109290e9d587479a481a2850d390b552Daniel Dunbar  class Preprocessor;
37e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  class StoredDiagnostic;
38651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  namespace tok {
39651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  enum TokenKind : unsigned short;
40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Annotates a diagnostic with some code that should be
434b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// inserted, removed, or replaced to fix the problem.
444b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
454b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// This kind of hint should be used when we are certain that the
464b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// introduction, removal, or modification of a particular (small!)
474b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// amount of code will correct a compilation error. The compiler
484b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// should also provide full recovery from such errors, such that
49b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor/// suppressing the diagnostic output can still result in successful
504b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// compilation.
51849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregorclass FixItHint {
524b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorpublic:
53783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// \brief Code that should be replaced to correct the error. Empty for an
54783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor  /// insertion hint.
550a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  CharSourceRange RemoveRange;
564b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
5730660a898545416f0fea2d717f16f75640001e38Ted Kremenek  /// \brief Code in the specific range that should be inserted in the insertion
5830660a898545416f0fea2d717f16f75640001e38Ted Kremenek  /// location.
5930660a898545416f0fea2d717f16f75640001e38Ted Kremenek  CharSourceRange InsertFromRange;
6030660a898545416f0fea2d717f16f75640001e38Ted Kremenek
614b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief The actual code to insert at the insertion location, as a
624b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// string.
634b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  std::string CodeToInsert;
644b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
6530660a898545416f0fea2d717f16f75640001e38Ted Kremenek  bool BeforePreviousInsertions;
6630660a898545416f0fea2d717f16f75640001e38Ted Kremenek
674b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Empty code modification hint, indicating that no code
684b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// modification is known.
6930660a898545416f0fea2d717f16f75640001e38Ted Kremenek  FixItHint() : BeforePreviousInsertions(false) { }
704b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
712f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  bool isNull() const {
72783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    return !RemoveRange.isValid();
732f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson  }
742f019aadc96f8fa3185c684c7150bd596d045064Anders Carlsson
754b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that inserts the given
764b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// code string at a specific location.
77849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  static FixItHint CreateInsertion(SourceLocation InsertionLoc,
7830660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                   StringRef Code,
7930660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                   bool BeforePreviousInsertions = false) {
80849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
81783c56f47745f719590b17afd7ed937bf2407b53Douglas Gregor    Hint.RemoveRange =
824a2ef80d896c1bac38ce113ec19a8d509a619288Nico Weber      CharSourceRange::getCharRange(InsertionLoc, InsertionLoc);
83b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
8430660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.BeforePreviousInsertions = BeforePreviousInsertions;
8530660a898545416f0fea2d717f16f75640001e38Ted Kremenek    return Hint;
8630660a898545416f0fea2d717f16f75640001e38Ted Kremenek  }
8730660a898545416f0fea2d717f16f75640001e38Ted Kremenek
8830660a898545416f0fea2d717f16f75640001e38Ted Kremenek  /// \brief Create a code modification hint that inserts the given
89af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// code from \p FromRange at a specific location.
9030660a898545416f0fea2d717f16f75640001e38Ted Kremenek  static FixItHint CreateInsertionFromRange(SourceLocation InsertionLoc,
9130660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                            CharSourceRange FromRange,
9230660a898545416f0fea2d717f16f75640001e38Ted Kremenek                                        bool BeforePreviousInsertions = false) {
9330660a898545416f0fea2d717f16f75640001e38Ted Kremenek    FixItHint Hint;
9430660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.RemoveRange =
954a2ef80d896c1bac38ce113ec19a8d509a619288Nico Weber      CharSourceRange::getCharRange(InsertionLoc, InsertionLoc);
9630660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.InsertFromRange = FromRange;
9730660a898545416f0fea2d717f16f75640001e38Ted Kremenek    Hint.BeforePreviousInsertions = BeforePreviousInsertions;
98b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
99b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
1004b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1014b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that removes the given
1024b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range.
1030a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(CharSourceRange RemoveRange) {
104849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
105b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
106b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
107b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
1080a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateRemoval(SourceRange RemoveRange) {
1090a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateRemoval(CharSourceRange::getTokenRange(RemoveRange));
1100a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
1110a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
1124b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// \brief Create a code modification hint that replaces the given
1134b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  /// source range with the given code string.
1140a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(CharSourceRange RemoveRange,
115686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                     StringRef Code) {
116849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixItHint Hint;
117b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.RemoveRange = RemoveRange;
118b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    Hint.CodeToInsert = Code;
119b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor    return Hint;
120b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  }
1210a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner
1220a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  static FixItHint CreateReplacement(SourceRange RemoveRange,
123686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                     StringRef Code) {
1240a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner    return CreateReplacement(CharSourceRange::getTokenRange(RemoveRange), Code);
1250a76aae8c03cb7dd7bdbe683485560afaf695959Chris Lattner  }
1264b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor};
1274b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
128af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// \brief Concrete class used by the front-end to report problems and issues.
129af50aab0c317462129d73ae8000c6394c718598dJames Dennett///
130af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// This massages the diagnostics (e.g. handling things like "report warnings
131af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// as errors" and passes them off to the DiagnosticConsumer for reporting to
132af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// the user. DiagnosticsEngine is tied to one translation unit and one
133af50aab0c317462129d73ae8000c6394c718598dJames Dennett/// SourceManager.
134c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
1356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DiagnosticsEngine(const DiagnosticsEngine &) LLVM_DELETED_FUNCTION;
1366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void operator=(const DiagnosticsEngine &) LLVM_DELETED_FUNCTION;
1376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
138182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattnerpublic:
139af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief The level of the diagnostic, after it has been through mapping.
140182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  enum Level {
14133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Ignored = DiagnosticIDs::Ignored,
14233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Note = DiagnosticIDs::Note,
143651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Remark = DiagnosticIDs::Remark,
14433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Warning = DiagnosticIDs::Warning,
14533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Error = DiagnosticIDs::Error,
14633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Fatal = DiagnosticIDs::Fatal
147182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner  };
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1493fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  enum ArgumentKind {
150af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_std_string,      ///< std::string
151af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_c_string,        ///< const char *
152af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_sint,            ///< int
153af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ak_uint,            ///< unsigned
154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ak_tokenkind,       ///< enum TokenKind : 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 *
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ak_qualtype_pair,   ///< pair<QualType, QualType>
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ak_attr             ///< Attr *
1633fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  };
1645edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
165af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Represents on argument value, which is a union discriminated
166af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// by ArgumentKind, with a value.
167b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  typedef std::pair<ArgumentKind, intptr_t> ArgumentValue;
168ee1828a6b5ae1bc4ea300e48f3840ac1ec5be295Douglas Gregor
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpprivate:
17027ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  unsigned char AllExtensionsSilenced; // Used by __extension__
171b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool IgnoreAllWarnings;        // Ignore all warnings: -w
1721e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool WarningsAsErrors;         // Treat warnings like errors.
1731e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  bool EnableAllWarnings;        // Enable all warnings.
174e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool ErrorsAsFatal;            // Treat errors like fatal errors.
175b54b276a920246c595a0498da281821eb9d22996Chris Lattner  bool SuppressSystemWarnings;   // Suppress warnings in system headers.
17681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool SuppressAllDiagnostics;   // Suppress all diagnostics.
177246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool ElideType;                // Elide common types of templates.
178246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool PrintTemplateTree;        // Print a tree when comparing templates.
179246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool ShowColors;               // Color printing is enabled.
1805edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown ShowOverloads;  // Which overload candidates to show.
181c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  unsigned ErrorLimit;           // Cap of # errors emitted, 0 -> no limit.
182575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned TemplateBacktraceLimit; // Cap on depth of template backtrace stack,
183575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor                                   // 0 -> no limit.
18408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ConstexprBacktraceLimit; // Cap on depth of constexpr evaluation
18508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                                    // backtrace stack, 0 -> no limit.
186ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  diag::Severity ExtBehavior;       // Map extensions to warnings or errors?
187c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<DiagnosticIDs> Diags;
18802c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
18978ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *Client;
19033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  bool OwnsDiagClient;
19133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager *SourceMgr;
1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
193af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Mapping information for diagnostics.
194af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
195af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Mapping info is packed into four bits per diagnostic.  The low three
196ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// bits are the mapping (an instance of diag::Severity), or zero if unset.
197af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// The high bit is set when the mapping was established as a user mapping.
198af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If the high bit is clear, then the low bits are set to the default
199af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// value, and should be mapped with -pedantic, -Werror, etc.
2000827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
2013efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// A new DiagState is created and kept around when diagnostic pragmas modify
2023efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// the state so that we know what is the diagnostic state at any given
2033efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  /// source location.
2040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  class DiagState {
205ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    llvm::DenseMap<unsigned, DiagnosticMapping> DiagMap;
2063c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
2073c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer  public:
208ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    typedef llvm::DenseMap<unsigned, DiagnosticMapping>::iterator iterator;
209ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    typedef llvm::DenseMap<unsigned, DiagnosticMapping>::const_iterator
210ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    const_iterator;
2113efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis
212ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    void setMapping(diag::kind Diag, DiagnosticMapping Info) {
213b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar      DiagMap[Diag] = Info;
214b1c99c679adb53be8f478d08be83af6c9c22ae4bDaniel Dunbar    }
2153c2d3016adec79c81c4efff64e208fd3ecdd92aeBenjamin Kramer
216ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    DiagnosticMapping &getOrAddMapping(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
28212f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  /// \brief Sticky flag set to \c true when an "uncompilable error" occurs.
28312f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  /// I.e. an error that was not upgraded from a warning by -Werror.
28412f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  bool UncompilableErrorOccurred;
28512f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins
286af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Sticky flag set to \c true when a fatal error is emitted.
28715221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool FatalErrorOccurred;
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28985bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  /// \brief Indicates that an unrecoverable error has occurred.
29085bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  bool UnrecoverableErrorOccurred;
29185bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
292c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  /// \brief Counts for DiagnosticErrorTrap to check whether an error occurred
293add80bb3957dba1d9389304453c0a4c975768bd7John McCall  /// during a parsing section, e.g. during parsing a function.
294c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned TrapNumErrorsOccurred;
295c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  unsigned TrapNumUnrecoverableErrorsOccurred;
296add80bb3957dba1d9389304453c0a4c975768bd7John McCall
297af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief The level of the last diagnostic emitted.
298af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
299af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This is used to emit continuation diagnostics with the same level as the
300f5d2328fc849288c3a62e43d065685f516d57091Chris Lattner  /// diagnostic that they follow.
30133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticIDs::Level LastDiagLevel;
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
303af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumWarnings;         ///< Number of warnings reported
304af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumErrors;           ///< Number of errors reported
305af50aab0c317462129d73ae8000c6394c718598dJames Dennett  unsigned NumErrorsSuppressed; ///< Number of errors suppressed
3067bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
307af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief A function pointer that converts an opaque diagnostic
308af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// argument to a strings.
309af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
310af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This takes the modifiers and argument that was present in the diagnostic.
311b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
312ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// The PrevArgs array indicates the previous arguments formatted for this
313ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// diagnostic.  Implementations of this function can use this information to
314ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// avoid redundancy across arguments.
315b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  ///
31622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  /// This is a hack to avoid a layering violation between libbasic and libsema.
3170673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth  typedef void (*ArgToStringFnTy)(
3180673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      ArgumentKind Kind, intptr_t Val,
319ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      StringRef Modifier, StringRef Argument,
320ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      ArrayRef<ArgumentValue> PrevArgs,
321686775deca8b8685eb90801495880e3abdd844c2Chris Lattner      SmallVectorImpl<char> &Output,
3220673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      void *Cookie,
323341785ec52f87c0803ba52dc88faac4e136f8593Bill Wendling      ArrayRef<intptr_t> QualTypeVals);
32492dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  void *ArgToStringCookie;
3253fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFnTy ArgToStringFn;
32693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
32793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief ID of the "delayed" diagnostic, which is a (typically
32893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// fatal) diagnostic that had to be delayed because it was found
32993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// while emitting another diagnostic.
33093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  unsigned DelayedDiagID;
33193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
33293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief First string argument for the delayed diagnostic.
33393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg1;
33493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
33593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  /// \brief Second string argument for the delayed diagnostic.
33693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  std::string DelayedDiagArg2;
33793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
338ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// \brief Optional flag value.
3396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ///
340ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// Some flags accept values, for instance: -Wframe-larger-than=<value> and
341ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// -Rpass=<value>. The content of this string is emitted after the flag name
342ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  /// and '='.
343ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  std::string FlagValue;
3446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
346d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  explicit DiagnosticsEngine(
347c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                      const IntrusiveRefCntPtr<DiagnosticIDs> &Diags,
34802c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor                      DiagnosticOptions *DiagOpts,
3496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                      DiagnosticConsumer *client = nullptr,
35033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                      bool ShouldOwnClient = true);
351d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  ~DiagnosticsEngine();
3521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
353c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  const IntrusiveRefCntPtr<DiagnosticIDs> &getDiagnosticIDs() const {
35433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags;
35533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
35633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
35702c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  /// \brief Retrieve the diagnostic options.
35802c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  DiagnosticOptions &getDiagnosticOptions() const { return *DiagOpts; }
35902c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor
3606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef llvm::iterator_range<DiagState::const_iterator> diag_mapping_range;
3616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// \brief Get the current set of diagnostic mappings.
3636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  diag_mapping_range getDiagnosticMappings() const {
3646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const DiagState &DS = *GetCurDiagState();
3656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return diag_mapping_range(DS.begin(), DS.end());
3666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
3676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
36878ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *getClient() { return Client; }
36978ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  const DiagnosticConsumer *getClient() const { return Client; }
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
371d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief Determine whether this \c DiagnosticsEngine object own its client.
37278243658c533168d51fd076fba328437932ba6f1Douglas Gregor  bool ownsClient() const { return OwnsDiagClient; }
37378243658c533168d51fd076fba328437932ba6f1Douglas Gregor
374bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Return the current diagnostic client along with ownership of that
375bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// client.
37678ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *takeClient() {
37733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    OwnsDiagClient = false;
37833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Client;
37933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
38033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
3816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool hasSourceManager() const { return SourceMgr != nullptr; }
38233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  SourceManager &getSourceManager() const {
38333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    assert(SourceMgr && "SourceManager not set!");
38433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return *SourceMgr;
38533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  }
38633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  void setSourceManager(SourceManager *SrcMgr) { SourceMgr = SrcMgr; }
38733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis
38833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
389d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  //  DiagnosticsEngine characterization methods, used by a client to customize
390d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  //  how diagnostics are emitted.
39133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  //
39204ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
393af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Copies the current DiagMappings and pushes the new copy
39404ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  /// onto the top of the stack.
3950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void pushMappings(SourceLocation Loc);
39604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
397af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Pops the current DiagMappings off the top of the stack,
398af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// causing the new top of the stack to be the active mappings.
399af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
400af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \returns \c true if the pop happens, \c false if there is only one
401af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// DiagMapping on the stack.
4020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  bool popMappings(SourceLocation Loc);
40304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
404bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  /// \brief Set the diagnostic client associated with this diagnostic object.
405bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor  ///
40633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// \param ShouldOwnClient true if the diagnostic object should take
40733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  /// ownership of \c client.
40878ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  void setClient(DiagnosticConsumer *client, bool ShouldOwnClient = true);
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
410af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Specify a limit for the number of errors we should
411af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// emit before giving up.
412af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
413af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Zero disables the limit.
414c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner  void setErrorLimit(unsigned Limit) { ErrorLimit = Limit; }
415b205ac9fcd22b87b41697172d1983c5ae9dabaafChris Lattner
416575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Specify the maximum number of template instantiation
417575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// notes to emit along with a given diagnostic.
418575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  void setTemplateBacktraceLimit(unsigned Limit) {
419575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    TemplateBacktraceLimit = Limit;
420575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
42108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
422575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  /// \brief Retrieve the maximum number of template instantiation
42308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// notes to emit along with a given diagnostic.
424575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  unsigned getTemplateBacktraceLimit() const {
425575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor    return TemplateBacktraceLimit;
426575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor  }
42708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
42808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// \brief Specify the maximum number of constexpr evaluation
42908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// notes to emit along with a given diagnostic.
43008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  void setConstexprBacktraceLimit(unsigned Limit) {
43108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    ConstexprBacktraceLimit = Limit;
43208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
43308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
43408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// \brief Retrieve the maximum number of constexpr evaluation
43508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  /// notes to emit along with a given diagnostic.
43608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned getConstexprBacktraceLimit() const {
43708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    return ConstexprBacktraceLimit;
43808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
43908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
440af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any unmapped warnings are ignored.
441af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
442af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If this and WarningsAsErrors are both set, then this one wins.
4435b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }
4445b4681c8ef65808ec4d72ab6081efd24d53d4969Chris Lattner  bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; }
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any unmapped ignored warnings are no longer
447af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// ignored.
448af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
449af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If this and IgnoreAllWarnings are both set, then that one wins.
4501e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  void setEnableAllWarnings(bool Val) { EnableAllWarnings = Val; }
45104863a86d15cb8de3361f8c77cedfd025819f174Benjamin Kramer  bool getEnableAllWarnings() const { return EnableAllWarnings; }
45204863a86d15cb8de3361f8c77cedfd025819f174Benjamin Kramer
453af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any warnings reported are issued as errors.
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getWarningsAsErrors() const { return WarningsAsErrors; }
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
457af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true, any error reported is made a fatal error.
458e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  void setErrorsAsFatal(bool Val) { ErrorsAsFatal = Val; }
459e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner  bool getErrorsAsFatal() const { return ErrorsAsFatal; }
460e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner
461af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief When set to true mask warnings that come from system headers.
4622fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
4632fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar  bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; }
4642fe0997427d92388e66e7573f4b043e7ba285ef0Daniel Dunbar
46581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Suppress all diagnostics, to silence the front end when we
46681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// know that we don't want any more diagnostics to be passed along to the
46781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// client
46881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  void setSuppressAllDiagnostics(bool Val = true) {
46981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor    SuppressAllDiagnostics = Val;
47081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  }
47181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
472246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
473246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// \brief Set type eliding, to skip outputting same types occurring in
474246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// template types.
475246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  void setElideType(bool Val = true) { ElideType = Val; }
476246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool getElideType() { return ElideType; }
477246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
478246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// \brief Set tree printing, to outputting the template difference in a
479246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// tree format.
480246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  void setPrintTemplateTree(bool Val = false) { PrintTemplateTree = Val; }
481246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool getPrintTemplateTree() { return PrintTemplateTree; }
482246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
483246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// \brief Set color printing, so the type diffing will inject color markers
484246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  /// into the output.
485246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  void setShowColors(bool Val = false) { ShowColors = Val; }
486246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  bool getShowColors() { return ShowColors; }
487246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
4885edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  /// \brief Specify which overload candidates to show when overload resolution
489af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// fails.
490af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
491af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// By default, we show all candidates.
4925edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  void setShowOverloads(OverloadsShown Val) {
4935edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin    ShowOverloads = Val;
4945edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  }
4955edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin  OverloadsShown getShowOverloads() const { return ShowOverloads; }
4965edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin
4975d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith  /// \brief Pretend that the last diagnostic issued was ignored, so any
4985d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith  /// subsequent notes will be suppressed.
499af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
500af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This can be used by clients who suppress diagnostics themselves.
5010b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  void setLastDiagnosticIgnored() {
5025d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith    if (LastDiagLevel == DiagnosticIDs::Fatal)
5035d99a252c63a7745bcd71231ca5240d2a65e4f1dRichard Smith      FatalErrorOccurred = true;
50433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    LastDiagLevel = DiagnosticIDs::Ignored;
5050b60d9e0097e2d6a1a5e62396967e207c4a772f2Douglas Gregor  }
506cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith
507cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith  /// \brief Determine whether the previous diagnostic was ignored. This can
508cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith  /// be used by clients that want to determine whether notes attached to a
509cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith  /// diagnostic will be suppressed.
510cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith  bool isLastDiagnosticIgnored() const {
511cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith    return LastDiagLevel == DiagnosticIDs::Ignored;
512cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith  }
513cbc820af7193e3ad9c71fbe6e3c8bd3c1166e540Richard Smith
514af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Controls whether otherwise-unmapped extension diagnostics are
515af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// mapped onto ignore/warning/error.
516af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
517af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This corresponds to the GCC -pedantic and -pedantic-errors option.
518ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  void setExtensionHandlingBehavior(diag::Severity H) { ExtBehavior = H; }
519ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  diag::Severity getExtensionHandlingBehavior() const { return ExtBehavior; }
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Counter bumped when an __extension__  block is/ encountered.
522af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
523af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// When non-zero, all extension diagnostics are entirely silenced, no
524af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// matter how they are mapped.
52527ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
52627ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner  void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
52791a2886d558ea6ca7a0bed73ab5acea5ae78eac2Douglas Gregor  bool hasAllExtensionsSilenced() { return AllExtensionsSilenced != 0; }
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief This allows the client to specify that certain warnings are
530697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// ignored.
531697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
532697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// Notes can never be mapped, errors can only be mapped to fatal, and
533697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// WARNINGs and EXTENSIONs can be mapped arbitrarily.
5340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
5350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \param Loc The source location that this change of diagnostic state should
5360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the latest state.
537ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  void setSeverity(diag::kind Diag, diag::Severity Map, SourceLocation Loc);
5381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
539697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Change an entire diagnostic group (e.g. "unknown-pragmas") to
540697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// have the specified mapping.
541697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
542697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \returns true (and ignores the request) if "Group" was unknown, false
543697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// otherwise.
5440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  ///
545697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \param Loc The source location that this change of diagnostic state should
5460827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// take affect. It can be null if we are setting the state from command-line.
547ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  bool setSeverityForGroup(StringRef Group, diag::Severity Map,
548ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                           SourceLocation Loc = SourceLocation());
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
550697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Set the warning-as-error flag for the given diagnostic group.
551697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
552697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This function always only operates on the current diagnostic state.
5534aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  ///
5544aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  /// \returns True if the given group is unknown, false otherwise.
5554aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled);
5564aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
557697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Set the error-as-fatal flag for the given diagnostic group.
558697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
559697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// This function always only operates on the current diagnostic state.
5604aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  ///
5614aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  /// \returns True if the given group is unknown, false otherwise.
5624aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar  bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled);
5634aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
564697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// \brief Add the specified mapping to all diagnostics.
565697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  ///
566697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// Mainly to be used by -Wno-everything to disable all warnings but allow
567697c7a8af6d090bff2b572553da0957de1724e1eJames Dennett  /// subsequent -W options to enable specific warnings.
568ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  void setSeverityForAll(diag::Severity Map,
569ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                         SourceLocation Loc = SourceLocation());
57011583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool hasErrorOccurred() const { return ErrorOccurred; }
57212f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins
57312f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  /// \brief Errors that actually prevent compilation, not those that are
57412f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  /// upgraded from a warning by -Werror.
57512f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  bool hasUncompilableErrorOccurred() const {
57612f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins    return UncompilableErrorOccurred;
57712f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  }
57815221422eda7bac679e38b07512feda49715ef66Chris Lattner  bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
57985bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
58085bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  /// \brief Determine whether any kind of unrecoverable error has occurred.
58185bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  bool hasUnrecoverableErrorOccurred() const {
58285bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor    return FatalErrorOccurred || UnrecoverableErrorOccurred;
58385bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  }
58485bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
58553eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  unsigned getNumWarnings() const { return NumWarnings; }
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
587c0659ec614c428c7d15746fcad15d50a2703751d