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