Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1 | /*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\ |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2 | |* *| |
| 3 | |* The LLVM Compiler Infrastructure *| |
| 4 | |* *| |
| 5 | |* This file is distributed under the University of Illinois Open Source *| |
| 6 | |* License. See LICENSE.TXT for details. *| |
| 7 | |* *| |
| 8 | |*===----------------------------------------------------------------------===*| |
| 9 | |* *| |
| 10 | |* Implements the diagnostic functions of the Clang C interface. *| |
| 11 | |* *| |
| 12 | \*===----------------------------------------------------------------------===*/ |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 13 | #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H |
| 14 | #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 15 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 16 | #include "clang-c/Index.h" |
David Blaikie | 759548b | 2014-08-29 18:43:24 +0000 | [diff] [blame] | 17 | #include <memory> |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | #include <assert.h> |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 20 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | |
Daniel Dunbar | 854d36b | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 23 | class LangOptions; |
Benjamin Kramer | 06441453 | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 24 | class StoredDiagnostic; |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 25 | class CXDiagnosticImpl; |
| 26 | |
| 27 | class CXDiagnosticSetImpl { |
David Blaikie | 759548b | 2014-08-29 18:43:24 +0000 | [diff] [blame] | 28 | std::vector<std::unique_ptr<CXDiagnosticImpl>> Diagnostics; |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 29 | const bool IsExternallyManaged; |
| 30 | public: |
| 31 | CXDiagnosticSetImpl(bool isManaged = false) |
| 32 | : IsExternallyManaged(isManaged) {} |
| 33 | |
| 34 | virtual ~CXDiagnosticSetImpl(); |
David Blaikie | 759548b | 2014-08-29 18:43:24 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 36 | size_t getNumDiagnostics() const { |
| 37 | return Diagnostics.size(); |
| 38 | } |
| 39 | |
| 40 | CXDiagnosticImpl *getDiagnostic(unsigned i) const { |
| 41 | assert(i < getNumDiagnostics()); |
David Blaikie | 759548b | 2014-08-29 18:43:24 +0000 | [diff] [blame] | 42 | return Diagnostics[i].get(); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 43 | } |
David Blaikie | 759548b | 2014-08-29 18:43:24 +0000 | [diff] [blame] | 44 | |
| 45 | void appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D); |
| 46 | |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 47 | bool empty() const { |
| 48 | return Diagnostics.empty(); |
| 49 | } |
| 50 | |
| 51 | bool isExternallyManaged() const { return IsExternallyManaged; } |
| 52 | }; |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 54 | class CXDiagnosticImpl { |
| 55 | public: |
Ted Kremenek | 914c7e6 | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 56 | enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind, |
| 57 | CustomNoteDiagnosticKind }; |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 58 | |
| 59 | virtual ~CXDiagnosticImpl(); |
| 60 | |
| 61 | /// \brief Return the severity of the diagnostic. |
| 62 | virtual CXDiagnosticSeverity getSeverity() const = 0; |
| 63 | |
| 64 | /// \brief Return the location of the diagnostic. |
| 65 | virtual CXSourceLocation getLocation() const = 0; |
| 66 | |
| 67 | /// \brief Return the spelling of the diagnostic. |
| 68 | virtual CXString getSpelling() const = 0; |
| 69 | |
| 70 | /// \brief Return the text for the diagnostic option. |
| 71 | virtual CXString getDiagnosticOption(CXString *Disable) const = 0; |
| 72 | |
| 73 | /// \brief Return the category of the diagnostic. |
| 74 | virtual unsigned getCategory() const = 0; |
Ted Kremenek | 26a6d49 | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 75 | |
| 76 | /// \brief Return the category string of the diagnostic. |
| 77 | virtual CXString getCategoryText() const = 0; |
| 78 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 79 | /// \brief Return the number of source ranges for the diagnostic. |
| 80 | virtual unsigned getNumRanges() const = 0; |
| 81 | |
| 82 | /// \brief Return the source ranges for the diagnostic. |
| 83 | virtual CXSourceRange getRange(unsigned Range) const = 0; |
| 84 | |
| 85 | /// \brief Return the number of FixIts. |
| 86 | virtual unsigned getNumFixIts() const = 0; |
| 87 | |
| 88 | /// \brief Return the FixIt information (source range and inserted text). |
| 89 | virtual CXString getFixIt(unsigned FixIt, |
| 90 | CXSourceRange *ReplacementRange) const = 0; |
| 91 | |
| 92 | Kind getKind() const { return K; } |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 93 | |
| 94 | CXDiagnosticSetImpl &getChildDiagnostics() { |
| 95 | return ChildDiags; |
| 96 | } |
| 97 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 98 | protected: |
| 99 | CXDiagnosticImpl(Kind k) : K(k) {} |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 100 | CXDiagnosticSetImpl ChildDiags; |
David Blaikie | 759548b | 2014-08-29 18:43:24 +0000 | [diff] [blame] | 101 | |
| 102 | void append(std::unique_ptr<CXDiagnosticImpl> D) { |
| 103 | ChildDiags.appendDiagnostic(std::move(D)); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 104 | } |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | Kind K; |
| 108 | }; |
| 109 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 110 | /// \brief The storage behind a CXDiagnostic |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 111 | struct CXStoredDiagnostic : public CXDiagnosticImpl { |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 112 | const StoredDiagnostic &Diag; |
| 113 | const LangOptions &LangOpts; |
| 114 | |
| 115 | CXStoredDiagnostic(const StoredDiagnostic &Diag, |
| 116 | const LangOptions &LangOpts) |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 117 | : CXDiagnosticImpl(StoredDiagnosticKind), |
| 118 | Diag(Diag), LangOpts(LangOpts) { } |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 119 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 120 | ~CXStoredDiagnostic() override {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 122 | /// \brief Return the severity of the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 123 | CXDiagnosticSeverity getSeverity() const override; |
| 124 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 125 | /// \brief Return the location of the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 126 | CXSourceLocation getLocation() const override; |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 127 | |
| 128 | /// \brief Return the spelling of the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 129 | CXString getSpelling() const override; |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 130 | |
| 131 | /// \brief Return the text for the diagnostic option. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 132 | CXString getDiagnosticOption(CXString *Disable) const override; |
| 133 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 134 | /// \brief Return the category of the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 135 | unsigned getCategory() const override; |
| 136 | |
Ted Kremenek | 26a6d49 | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 137 | /// \brief Return the category string of the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 138 | CXString getCategoryText() const override; |
Ted Kremenek | 26a6d49 | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 140 | /// \brief Return the number of source ranges for the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 141 | unsigned getNumRanges() const override; |
| 142 | |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 143 | /// \brief Return the source ranges for the diagnostic. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 144 | CXSourceRange getRange(unsigned Range) const override; |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 145 | |
| 146 | /// \brief Return the number of FixIts. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 147 | unsigned getNumFixIts() const override; |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 148 | |
| 149 | /// \brief Return the FixIt information (source range and inserted text). |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 150 | CXString getFixIt(unsigned FixIt, |
| 151 | CXSourceRange *ReplacementRange) const override; |
Ted Kremenek | bb2c710 | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 152 | |
| 153 | static bool classof(const CXDiagnosticImpl *D) { |
| 154 | return D->getKind() == StoredDiagnosticKind; |
| 155 | } |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 156 | }; |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 157 | |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 158 | namespace cxdiag { |
| 159 | CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU, |
| 160 | bool checkIfChanged = false); |
| 161 | } // end namespace cxdiag |
| 162 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 163 | } // end namespace clang |
| 164 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 165 | #endif |