blob: 25589bb57474ac2c2242501a6225722851770d78 [file] [log] [blame]
Douglas Gregorac0605e2010-01-28 06:00:51 +00001/*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\
Douglas Gregor4f9c3762010-01-28 00:27:43 +00002|* *|
Chandler Carruth2946cd72019-01-19 08:50:56 +00003|* 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 *|
Douglas Gregor4f9c3762010-01-28 00:27:43 +00007|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* Implements the diagnostic functions of the Clang C interface. *|
11|* *|
12\*===----------------------------------------------------------------------===*/
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H
14#define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H
Douglas Gregor4f9c3762010-01-28 00:27:43 +000015
Ted Kremenekbb2c7102011-10-31 21:40:19 +000016#include "clang-c/Index.h"
David Blaikie759548b2014-08-29 18:43:24 +000017#include <memory>
Ted Kremenekd010ba42011-11-10 08:43:12 +000018#include <vector>
19#include <assert.h>
Ted Kremenekbb2c7102011-10-31 21:40:19 +000020
Douglas Gregor4f9c3762010-01-28 00:27:43 +000021namespace clang {
22
Daniel Dunbar854d36b2010-01-30 23:31:40 +000023class LangOptions;
Benjamin Kramer064414532010-04-12 19:45:50 +000024class StoredDiagnostic;
Ted Kremenekd010ba42011-11-10 08:43:12 +000025class CXDiagnosticImpl;
26
27class CXDiagnosticSetImpl {
David Blaikie759548b2014-08-29 18:43:24 +000028 std::vector<std::unique_ptr<CXDiagnosticImpl>> Diagnostics;
Ted Kremenekd010ba42011-11-10 08:43:12 +000029 const bool IsExternallyManaged;
30public:
31 CXDiagnosticSetImpl(bool isManaged = false)
32 : IsExternallyManaged(isManaged) {}
33
34 virtual ~CXDiagnosticSetImpl();
David Blaikie759548b2014-08-29 18:43:24 +000035
Ted Kremenekd010ba42011-11-10 08:43:12 +000036 size_t getNumDiagnostics() const {
37 return Diagnostics.size();
38 }
39
40 CXDiagnosticImpl *getDiagnostic(unsigned i) const {
41 assert(i < getNumDiagnostics());
David Blaikie759548b2014-08-29 18:43:24 +000042 return Diagnostics[i].get();
Ted Kremenekd010ba42011-11-10 08:43:12 +000043 }
David Blaikie759548b2014-08-29 18:43:24 +000044
45 void appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D);
46
Ted Kremenekd010ba42011-11-10 08:43:12 +000047 bool empty() const {
48 return Diagnostics.empty();
49 }
50
51 bool isExternallyManaged() const { return IsExternallyManaged; }
52};
Daniel Dunbar9ee3a922010-01-30 23:31:49 +000053
Ted Kremenekbb2c7102011-10-31 21:40:19 +000054class CXDiagnosticImpl {
55public:
Ted Kremenek914c7e62012-02-14 02:46:03 +000056 enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind,
57 CustomNoteDiagnosticKind };
Ted Kremenekbb2c7102011-10-31 21:40:19 +000058
59 virtual ~CXDiagnosticImpl();
60
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000061 /// Return the severity of the diagnostic.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000062 virtual CXDiagnosticSeverity getSeverity() const = 0;
63
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000064 /// Return the location of the diagnostic.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000065 virtual CXSourceLocation getLocation() const = 0;
66
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000067 /// Return the spelling of the diagnostic.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000068 virtual CXString getSpelling() const = 0;
69
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000070 /// Return the text for the diagnostic option.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000071 virtual CXString getDiagnosticOption(CXString *Disable) const = 0;
72
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000073 /// Return the category of the diagnostic.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000074 virtual unsigned getCategory() const = 0;
Ted Kremenek26a6d492012-04-12 00:03:31 +000075
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000076 /// Return the category string of the diagnostic.
Ted Kremenek26a6d492012-04-12 00:03:31 +000077 virtual CXString getCategoryText() const = 0;
78
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000079 /// Return the number of source ranges for the diagnostic.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000080 virtual unsigned getNumRanges() const = 0;
81
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000082 /// Return the source ranges for the diagnostic.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000083 virtual CXSourceRange getRange(unsigned Range) const = 0;
84
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000085 /// Return the number of FixIts.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000086 virtual unsigned getNumFixIts() const = 0;
87
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000088 /// Return the FixIt information (source range and inserted text).
Ted Kremenekbb2c7102011-10-31 21:40:19 +000089 virtual CXString getFixIt(unsigned FixIt,
90 CXSourceRange *ReplacementRange) const = 0;
91
92 Kind getKind() const { return K; }
Ted Kremenekd010ba42011-11-10 08:43:12 +000093
94 CXDiagnosticSetImpl &getChildDiagnostics() {
95 return ChildDiags;
96 }
97
Ted Kremenekbb2c7102011-10-31 21:40:19 +000098protected:
99 CXDiagnosticImpl(Kind k) : K(k) {}
Ted Kremenekd010ba42011-11-10 08:43:12 +0000100 CXDiagnosticSetImpl ChildDiags;
David Blaikie759548b2014-08-29 18:43:24 +0000101
102 void append(std::unique_ptr<CXDiagnosticImpl> D) {
103 ChildDiags.appendDiagnostic(std::move(D));
Ted Kremenekd010ba42011-11-10 08:43:12 +0000104 }
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000105
106private:
107 Kind K;
108};
109
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000110/// The storage behind a CXDiagnostic
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000111struct CXStoredDiagnostic : public CXDiagnosticImpl {
Douglas Gregor33cdd812010-02-18 18:08:43 +0000112 const StoredDiagnostic &Diag;
113 const LangOptions &LangOpts;
114
115 CXStoredDiagnostic(const StoredDiagnostic &Diag,
116 const LangOptions &LangOpts)
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000117 : CXDiagnosticImpl(StoredDiagnosticKind),
118 Diag(Diag), LangOpts(LangOpts) { }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000119
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000120 ~CXStoredDiagnostic() override {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000122 /// Return the severity of the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000123 CXDiagnosticSeverity getSeverity() const override;
124
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000125 /// Return the location of the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000126 CXSourceLocation getLocation() const override;
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000127
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000128 /// Return the spelling of the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000129 CXString getSpelling() const override;
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000130
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000131 /// Return the text for the diagnostic option.
Craig Topper36835562014-03-15 07:47:46 +0000132 CXString getDiagnosticOption(CXString *Disable) const override;
133
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000134 /// Return the category of the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000135 unsigned getCategory() const override;
136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137 /// Return the category string of the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000138 CXString getCategoryText() const override;
Ted Kremenek26a6d492012-04-12 00:03:31 +0000139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000140 /// Return the number of source ranges for the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000141 unsigned getNumRanges() const override;
142
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000143 /// Return the source ranges for the diagnostic.
Craig Topper36835562014-03-15 07:47:46 +0000144 CXSourceRange getRange(unsigned Range) const override;
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000145
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000146 /// Return the number of FixIts.
Craig Topper36835562014-03-15 07:47:46 +0000147 unsigned getNumFixIts() const override;
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000148
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000149 /// Return the FixIt information (source range and inserted text).
Craig Topper36835562014-03-15 07:47:46 +0000150 CXString getFixIt(unsigned FixIt,
151 CXSourceRange *ReplacementRange) const override;
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000152
153 static bool classof(const CXDiagnosticImpl *D) {
154 return D->getKind() == StoredDiagnosticKind;
155 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000156};
Douglas Gregorac0605e2010-01-28 06:00:51 +0000157
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +0000158namespace cxdiag {
159CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU,
160 bool checkIfChanged = false);
161} // end namespace cxdiag
162
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000163} // end namespace clang
164
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000165#endif