blob: b1c3978e0ae8a7535b4eb15fd924d6e662a788ec [file] [log] [blame]
Douglas Gregord93256e2010-01-28 06:00:51 +00001/*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\
Douglas Gregor5352ac02010-01-28 00:27:43 +00002|* *|
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
Ted Kremenek1edabbc2011-10-31 21:40:19 +000016#include "clang-c/Index.h"
Ted Kremenek15322172011-11-10 08:43:12 +000017#include <vector>
18#include <assert.h>
Ted Kremenek1edabbc2011-10-31 21:40:19 +000019
Douglas Gregor5352ac02010-01-28 00:27:43 +000020namespace clang {
21
Daniel Dunbar35b84402010-01-30 23:31:40 +000022class LangOptions;
Benjamin Kramerb846deb2010-04-12 19:45:50 +000023class StoredDiagnostic;
Ted Kremenek15322172011-11-10 08:43:12 +000024class 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};
Daniel Dunbar49146122010-01-30 23:31:49 +000054
Ted Kremenek1edabbc2011-10-31 21:40:19 +000055class CXDiagnosticImpl {
56public:
Ted Kremenek7473b1c2012-02-14 02:46:03 +000057 enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind,
58 CustomNoteDiagnosticKind };
Ted Kremenek1edabbc2011-10-31 21:40:19 +000059
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;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000076
77 /// \brief Return the category string of the diagnostic.
78 virtual CXString getCategoryText() const = 0;
79
Ted Kremenek1edabbc2011-10-31 21:40:19 +000080 /// \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
93 Kind getKind() const { return K; }
Ted Kremenek15322172011-11-10 08:43:12 +000094
95 CXDiagnosticSetImpl &getChildDiagnostics() {
96 return ChildDiags;
97 }
98
Ted Kremenek1edabbc2011-10-31 21:40:19 +000099protected:
100 CXDiagnosticImpl(Kind k) : K(k) {}
Ted Kremenek15322172011-11-10 08:43:12 +0000101 CXDiagnosticSetImpl ChildDiags;
102
103 void append(CXDiagnosticImpl *D) {
104 ChildDiags.appendDiagnostic(D);
105 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000106
107private:
108 Kind K;
109};
110
Douglas Gregora88084b2010-02-18 18:08:43 +0000111/// \brief The storage behind a CXDiagnostic
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000112struct CXStoredDiagnostic : public CXDiagnosticImpl {
Douglas Gregora88084b2010-02-18 18:08:43 +0000113 const StoredDiagnostic &Diag;
114 const LangOptions &LangOpts;
115
116 CXStoredDiagnostic(const StoredDiagnostic &Diag,
117 const LangOptions &LangOpts)
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000118 : CXDiagnosticImpl(StoredDiagnosticKind),
119 Diag(Diag), LangOpts(LangOpts) { }
120
121 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
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000138 /// \brief Return the category string of the diagnostic.
139 virtual CXString getCategoryText() const;
140
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000141 /// \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
154 static bool classof(const CXDiagnosticImpl *D) {
155 return D->getKind() == StoredDiagnosticKind;
156 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000157};
Douglas Gregord93256e2010-01-28 06:00:51 +0000158
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +0000159namespace cxdiag {
160CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU,
161 bool checkIfChanged = false);
162} // end namespace cxdiag
163
Douglas Gregor5352ac02010-01-28 00:27:43 +0000164} // end namespace clang
165
166#endif // LLVM_CLANG_CINDEX_DIAGNOSTIC_H