• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 namespace clang {
21 
22 class LangOptions;
23 class StoredDiagnostic;
24 class CXDiagnosticImpl;
25 
26 class CXDiagnosticSetImpl {
27   std::vector<CXDiagnosticImpl *> Diagnostics;
28   const bool IsExternallyManaged;
29 public:
30   CXDiagnosticSetImpl(bool isManaged = false)
IsExternallyManaged(isManaged)31     : IsExternallyManaged(isManaged) {}
32 
33   virtual ~CXDiagnosticSetImpl();
34 
getNumDiagnostics()35   size_t getNumDiagnostics() const {
36     return Diagnostics.size();
37   }
38 
getDiagnostic(unsigned i)39   CXDiagnosticImpl *getDiagnostic(unsigned i) const {
40     assert(i < getNumDiagnostics());
41     return Diagnostics[i];
42   }
43 
appendDiagnostic(CXDiagnosticImpl * D)44   void appendDiagnostic(CXDiagnosticImpl *D) {
45     Diagnostics.push_back(D);
46   }
47 
empty()48   bool empty() const {
49     return Diagnostics.empty();
50   }
51 
isExternallyManaged()52   bool isExternallyManaged() const { return IsExternallyManaged; }
53 };
54 
55 class CXDiagnosticImpl {
56 public:
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 category string of the diagnostic.
78   virtual CXString getCategoryText() const = 0;
79 
80   /// \brief Return the number of source ranges for the diagnostic.
81   virtual unsigned getNumRanges() const = 0;
82 
83   /// \brief Return the source ranges for the diagnostic.
84   virtual CXSourceRange getRange(unsigned Range) const = 0;
85 
86   /// \brief Return the number of FixIts.
87   virtual unsigned getNumFixIts() const = 0;
88 
89   /// \brief Return the FixIt information (source range and inserted text).
90   virtual CXString getFixIt(unsigned FixIt,
91                             CXSourceRange *ReplacementRange) const = 0;
92 
getKind()93   Kind getKind() const { return K; }
94 
getChildDiagnostics()95   CXDiagnosticSetImpl &getChildDiagnostics() {
96     return ChildDiags;
97   }
98 
99 protected:
CXDiagnosticImpl(Kind k)100   CXDiagnosticImpl(Kind k) : K(k) {}
101   CXDiagnosticSetImpl ChildDiags;
102 
append(CXDiagnosticImpl * D)103   void append(CXDiagnosticImpl *D) {
104     ChildDiags.appendDiagnostic(D);
105   }
106 
107 private:
108   Kind K;
109 };
110 
111 /// \brief The storage behind a CXDiagnostic
112 struct CXStoredDiagnostic : public CXDiagnosticImpl {
113   const StoredDiagnostic &Diag;
114   const LangOptions &LangOpts;
115 
CXStoredDiagnosticCXStoredDiagnostic116   CXStoredDiagnostic(const StoredDiagnostic &Diag,
117                      const LangOptions &LangOpts)
118     : CXDiagnosticImpl(StoredDiagnosticKind),
119       Diag(Diag), LangOpts(LangOpts) { }
120 
~CXStoredDiagnosticCXStoredDiagnostic121   virtual ~CXStoredDiagnostic() {}
122 
123   /// \brief Return the severity of the diagnostic.
124   virtual CXDiagnosticSeverity getSeverity() const;
125 
126   /// \brief Return the location of the diagnostic.
127   virtual CXSourceLocation getLocation() const;
128 
129   /// \brief Return the spelling of the diagnostic.
130   virtual CXString getSpelling() const;
131 
132   /// \brief Return the text for the diagnostic option.
133   virtual CXString getDiagnosticOption(CXString *Disable) const;
134 
135   /// \brief Return the category of the diagnostic.
136   virtual unsigned getCategory() const;
137 
138   /// \brief Return the category string of the diagnostic.
139   virtual CXString getCategoryText() const;
140 
141   /// \brief Return the number of source ranges for the diagnostic.
142   virtual unsigned getNumRanges() const;
143 
144   /// \brief Return the source ranges for the diagnostic.
145   virtual CXSourceRange getRange(unsigned Range) const;
146 
147   /// \brief Return the number of FixIts.
148   virtual unsigned getNumFixIts() const;
149 
150   /// \brief Return the FixIt information (source range and inserted text).
151   virtual CXString getFixIt(unsigned FixIt,
152                             CXSourceRange *ReplacementRange) const;
153 
classofCXStoredDiagnostic154   static bool classof(const CXDiagnosticImpl *D) {
155     return D->getKind() == StoredDiagnosticKind;
156   }
157 };
158 
159 namespace cxdiag {
160 CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU,
161                                      bool checkIfChanged = false);
162 } // end namespace cxdiag
163 
164 } // end namespace clang
165 
166 #endif // LLVM_CLANG_CINDEX_DIAGNOSTIC_H
167