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