30
30
#include " llvm/Support/Casting.h"
31
31
#include < memory>
32
32
33
+ namespace {
34
+
35
+ // / \brief A custom deleter used for ``std::unique_ptr`` to APIRecords stored
36
+ // / in the BumpPtrAllocator.
37
+ // /
38
+ // / \tparam T the exact type of the APIRecord subclass.
39
+ template <typename T> struct UniquePtrBumpPtrAllocatorDeleter {
40
+ void operator ()(T *Instance) { Instance->~T (); }
41
+ };
42
+
43
+ // / A unique pointer to an APIRecord stored in the BumpPtrAllocator.
44
+ // /
45
+ // / \tparam T the exact type of the APIRecord subclass.
46
+ template <typename T>
47
+ using APIRecordUniquePtr =
48
+ std::unique_ptr<T, UniquePtrBumpPtrAllocatorDeleter<T>>;
49
+
50
+ } // anonymous namespace
51
+
33
52
namespace clang {
34
53
namespace extractapi {
35
54
@@ -73,6 +92,8 @@ struct APIRecord {
73
92
// / Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
74
93
enum RecordKind {
75
94
RK_Global,
95
+ RK_EnumConstant,
96
+ RK_Enum,
76
97
};
77
98
78
99
private:
@@ -125,6 +146,36 @@ struct GlobalRecord : APIRecord {
125
146
virtual void anchor ();
126
147
};
127
148
149
+ // / This holds information associated with enum constants.
150
+ struct EnumConstantRecord : APIRecord {
151
+ EnumConstantRecord (StringRef Name, StringRef USR, PresumedLoc Loc,
152
+ const AvailabilityInfo &Availability,
153
+ const DocComment &Comment,
154
+ DeclarationFragments Declaration,
155
+ DeclarationFragments SubHeading)
156
+ : APIRecord(RK_EnumConstant, Name, USR, Loc, Availability,
157
+ LinkageInfo::none (), Comment, Declaration, SubHeading) {}
158
+
159
+ static bool classof (const APIRecord *Record) {
160
+ return Record->getKind () == RK_EnumConstant;
161
+ }
162
+ };
163
+
164
+ // / This holds information associated with enums.
165
+ struct EnumRecord : APIRecord {
166
+ SmallVector<APIRecordUniquePtr<EnumConstantRecord>> Constants;
167
+
168
+ EnumRecord (StringRef Name, StringRef USR, PresumedLoc Loc,
169
+ const AvailabilityInfo &Availability, const DocComment &Comment,
170
+ DeclarationFragments Declaration, DeclarationFragments SubHeading)
171
+ : APIRecord(RK_Enum, Name, USR, Loc, Availability, LinkageInfo::none(),
172
+ Comment, Declaration, SubHeading) {}
173
+
174
+ static bool classof (const APIRecord *Record) {
175
+ return Record->getKind () == RK_Enum;
176
+ }
177
+ };
178
+
128
179
// / APISet holds the set of API records collected from given inputs.
129
180
class APISet {
130
181
public:
@@ -166,35 +217,49 @@ class APISet {
166
217
DeclarationFragments SubHeading,
167
218
FunctionSignature Signature);
168
219
169
- private:
170
- // / \brief A custom deleter used for ``std::unique_ptr`` to APIRecords stored
171
- // / in the BumpPtrAllocator.
220
+ // / Create and add an enum constant record into the API set.
172
221
// /
173
- // / \tparam T the exact type of the APIRecord subclass.
174
- template <typename T> struct UniquePtrBumpPtrAllocatorDeleter {
175
- void operator ()(T *Instance) { Instance->~T (); }
176
- };
177
-
178
- public:
179
- // / A unique pointer to an APIRecord stored in the BumpPtrAllocator.
222
+ // / Note: the caller is responsible for keeping the StringRef \p Name and
223
+ // / \p USR alive. APISet::copyString provides a way to copy strings into
224
+ // / APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
225
+ // / to generate the USR for \c D and keep it alive in APISet.
226
+ EnumConstantRecord *addEnumConstant (EnumRecord *Enum, StringRef Name,
227
+ StringRef USR, PresumedLoc Loc,
228
+ const AvailabilityInfo &Availability,
229
+ const DocComment &Comment,
230
+ DeclarationFragments Declaration,
231
+ DeclarationFragments SubHeading);
232
+
233
+ // / Create and add an enum record into the API set.
180
234
// /
181
- // / \tparam T the exact type of the APIRecord subclass.
182
- template <typename T>
183
- using APIRecordUniquePtr =
184
- std::unique_ptr<T, UniquePtrBumpPtrAllocatorDeleter<T>>;
235
+ // / Note: the caller is responsible for keeping the StringRef \p Name and
236
+ // / \p USR alive. APISet::copyString provides a way to copy strings into
237
+ // / APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
238
+ // / to generate the USR for \c D and keep it alive in APISet.
239
+ EnumRecord *addEnum (StringRef Name, StringRef USR, PresumedLoc Loc,
240
+ const AvailabilityInfo &Availability,
241
+ const DocComment &Comment,
242
+ DeclarationFragments Declaration,
243
+ DeclarationFragments SubHeading);
185
244
186
245
// / A map to store the set of GlobalRecord%s with the declaration name as the
187
246
// / key.
188
247
using GlobalRecordMap =
189
248
llvm::MapVector<StringRef, APIRecordUniquePtr<GlobalRecord>>;
190
249
250
+ // / A map to store the set of EnumRecord%s with the declaration name as the
251
+ // / key.
252
+ using EnumRecordMap =
253
+ llvm::MapVector<StringRef, APIRecordUniquePtr<EnumRecord>>;
254
+
191
255
// / Get the target triple for the ExtractAPI invocation.
192
256
const llvm::Triple &getTarget () const { return Target; }
193
257
194
258
// / Get the language options used to parse the APIs.
195
259
const LangOptions &getLangOpts () const { return LangOpts; }
196
260
197
261
const GlobalRecordMap &getGlobals () const { return Globals; }
262
+ const EnumRecordMap &getEnums () const { return Enums; }
198
263
199
264
// / Generate and store the USR of declaration \p D.
200
265
// /
@@ -219,6 +284,7 @@ class APISet {
219
284
const LangOptions LangOpts;
220
285
221
286
GlobalRecordMap Globals;
287
+ EnumRecordMap Enums;
222
288
};
223
289
224
290
} // namespace extractapi
0 commit comments