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