| 1 | /*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\ |
| 2 | |* *| |
| 3 | |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| |
| 4 | |* Exceptions. *| |
| 5 | |* See https://llvm.org/LICENSE.txt for license information. *| |
| 6 | |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| |
| 7 | |* *| |
| 8 | |*===----------------------------------------------------------------------===*| |
| 9 | |* *| |
| 10 | |* Implements the diagnostic functions of the Clang C interface. *| |
| 11 | |* *| |
| 12 | \*===----------------------------------------------------------------------===*/ |
| 13 | #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H |
| 14 | #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H |
| 15 | |
| 16 | #include "clang-c/Index.h" |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 | #include <assert.h> |
| 20 | |
| 21 | namespace clang { |
| 22 | |
| 23 | class LangOptions; |
| 24 | class StoredDiagnostic; |
| 25 | class CXDiagnosticImpl; |
| 26 | |
| 27 | class CXDiagnosticSetImpl { |
| 28 | std::vector<std::unique_ptr<CXDiagnosticImpl>> Diagnostics; |
| 29 | const bool IsExternallyManaged; |
| 30 | public: |
| 31 | CXDiagnosticSetImpl(bool isManaged = false) |
| 32 | : IsExternallyManaged(isManaged) {} |
| 33 | |
| 34 | virtual ~CXDiagnosticSetImpl(); |
| 35 | |
| 36 | size_t getNumDiagnostics() const { |
| 37 | return Diagnostics.size(); |
| 38 | } |
| 39 | |
| 40 | CXDiagnosticImpl *getDiagnostic(unsigned i) const { |
| 41 | assert(i < getNumDiagnostics()); |
| 42 | return Diagnostics[i].get(); |
| 43 | } |
| 44 | |
| 45 | void appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D); |
| 46 | |
| 47 | bool empty() const { |
| 48 | return Diagnostics.empty(); |
| 49 | } |
| 50 | |
| 51 | bool isExternallyManaged() const { return IsExternallyManaged; } |
| 52 | }; |
| 53 | |
| 54 | class CXDiagnosticImpl { |
| 55 | public: |
| 56 | enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind, |
| 57 | CustomNoteDiagnosticKind }; |
| 58 | |
| 59 | virtual ~CXDiagnosticImpl(); |
| 60 | |
| 61 | /// Return the severity of the diagnostic. |
| 62 | virtual CXDiagnosticSeverity getSeverity() const = 0; |
| 63 | |
| 64 | /// Return the location of the diagnostic. |
| 65 | virtual CXSourceLocation getLocation() const = 0; |
| 66 | |
| 67 | /// Return the spelling of the diagnostic. |
| 68 | virtual CXString getSpelling() const = 0; |
| 69 | |
| 70 | /// Return the text for the diagnostic option. |
| 71 | virtual CXString getDiagnosticOption(CXString *Disable) const = 0; |
| 72 | |
| 73 | /// Return the category of the diagnostic. |
| 74 | virtual unsigned getCategory() const = 0; |
| 75 | |
| 76 | /// Return the category string of the diagnostic. |
| 77 | virtual CXString getCategoryText() const = 0; |
| 78 | |
| 79 | /// Return the number of source ranges for the diagnostic. |
| 80 | virtual unsigned getNumRanges() const = 0; |
| 81 | |
| 82 | /// Return the source ranges for the diagnostic. |
| 83 | virtual CXSourceRange getRange(unsigned Range) const = 0; |
| 84 | |
| 85 | /// Return the number of FixIts. |
| 86 | virtual unsigned getNumFixIts() const = 0; |
| 87 | |
| 88 | /// 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; } |
| 93 | |
| 94 | CXDiagnosticSetImpl &getChildDiagnostics() { |
| 95 | return ChildDiags; |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | CXDiagnosticImpl(Kind k) : K(k) {} |
| 100 | CXDiagnosticSetImpl ChildDiags; |
| 101 | |
| 102 | void append(std::unique_ptr<CXDiagnosticImpl> D) { |
| 103 | ChildDiags.appendDiagnostic(D: std::move(D)); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | Kind K; |
| 108 | }; |
| 109 | |
| 110 | /// The storage behind a CXDiagnostic |
| 111 | struct CXStoredDiagnostic : public CXDiagnosticImpl { |
| 112 | const StoredDiagnostic &Diag; |
| 113 | const LangOptions &LangOpts; |
| 114 | |
| 115 | CXStoredDiagnostic(const StoredDiagnostic &Diag, |
| 116 | const LangOptions &LangOpts) |
| 117 | : CXDiagnosticImpl(StoredDiagnosticKind), |
| 118 | Diag(Diag), LangOpts(LangOpts) { } |
| 119 | |
| 120 | ~CXStoredDiagnostic() override {} |
| 121 | |
| 122 | /// Return the severity of the diagnostic. |
| 123 | CXDiagnosticSeverity getSeverity() const override; |
| 124 | |
| 125 | /// Return the location of the diagnostic. |
| 126 | CXSourceLocation getLocation() const override; |
| 127 | |
| 128 | /// Return the spelling of the diagnostic. |
| 129 | CXString getSpelling() const override; |
| 130 | |
| 131 | /// Return the text for the diagnostic option. |
| 132 | CXString getDiagnosticOption(CXString *Disable) const override; |
| 133 | |
| 134 | /// Return the category of the diagnostic. |
| 135 | unsigned getCategory() const override; |
| 136 | |
| 137 | /// Return the category string of the diagnostic. |
| 138 | CXString getCategoryText() const override; |
| 139 | |
| 140 | /// Return the number of source ranges for the diagnostic. |
| 141 | unsigned getNumRanges() const override; |
| 142 | |
| 143 | /// Return the source ranges for the diagnostic. |
| 144 | CXSourceRange getRange(unsigned Range) const override; |
| 145 | |
| 146 | /// Return the number of FixIts. |
| 147 | unsigned getNumFixIts() const override; |
| 148 | |
| 149 | /// Return the FixIt information (source range and inserted text). |
| 150 | CXString getFixIt(unsigned FixIt, |
| 151 | CXSourceRange *ReplacementRange) const override; |
| 152 | |
| 153 | static bool classof(const CXDiagnosticImpl *D) { |
| 154 | return D->getKind() == StoredDiagnosticKind; |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | namespace cxdiag { |
| 159 | CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU, |
| 160 | bool checkIfChanged = false); |
| 161 | } // end namespace cxdiag |
| 162 | |
| 163 | } // end namespace clang |
| 164 | |
| 165 | #endif |
| 166 | |