Debian Codegraph @

clang-tools-extra/unittests/clang-doc/BitcodeTest.cpp

raw
1 //===-- clang-doc/BitcodeTest.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "BitcodeReader.h"
10 #include "BitcodeWriter.h"
11 #include "ClangDocTest.h"
12 #include "Representation.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/DiagnosticOptions.h"
15 #include "llvm/Bitstream/BitstreamReader.h"
16 #include "llvm/Bitstream/BitstreamWriter.h"
17 #include "gtest/gtest.h"
18
19 namespace clang {
20 namespace doc {
21
22 template <typename T>
23 static std::string writeInfo(T &I, DiagnosticsEngine &Diags) {
24 SmallString<2048> Buffer;
25 llvm::BitstreamWriter Stream(Buffer);
26 ClangDocBitcodeWriter Writer(Stream, Diags);
27 Writer.emitBlock(I);
28 return Buffer.str().str();
29 }
30
31 static std::string writeInfo(Info *I, DiagnosticsEngine &Diags) {
32 switch (I->IT) {
33 case InfoType::IT_namespace:
34 return writeInfo(*static_cast<NamespaceInfo *>(I), Diags);
35 case InfoType::IT_record:
36 return writeInfo(*static_cast<RecordInfo *>(I), Diags);
37 case InfoType::IT_enum:
38 return writeInfo(*static_cast<EnumInfo *>(I), Diags);
39 case InfoType::IT_function:
40 return writeInfo(*static_cast<FunctionInfo *>(I), Diags);
41 case InfoType::IT_typedef:
42 return writeInfo(*static_cast<TypedefInfo *>(I), Diags);
43 case InfoType::IT_concept:
44 return writeInfo(*static_cast<ConceptInfo *>(I), Diags);
45 case InfoType::IT_variable:
46 return writeInfo(*static_cast<VarInfo *>(I), Diags);
47 case InfoType::IT_friend:
48 return writeInfo(*static_cast<FriendInfo *>(I), Diags);
49 case InfoType::IT_default:
50 return "";
51 }
52 }
53
54 static std::vector<std::unique_ptr<Info>>
55 readInfo(StringRef Bitcode, size_t NumInfos, DiagnosticsEngine &Diags) {
56 llvm::BitstreamCursor Stream(Bitcode);
57 doc::ClangDocBitcodeReader Reader(Stream, Diags);
58 auto Infos = Reader.readBitcode();
59
60 // Check that there was no error in the read.
61 assert(Infos);
62 EXPECT_EQ(Infos.get().size(), NumInfos);
63 return std::move(Infos.get());
64 }
65
66 class BitcodeTest : public ClangDocContextTest {};
67
68 TEST_F(BitcodeTest, emitNamespaceInfoBitcode) {
69 NamespaceInfo I;
70 I.Name = "r";
71 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
72
73 I.Children.Namespaces.emplace_back(EmptySID, "ChildNamespace",
74 InfoType::IT_namespace);
75 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record);
76 I.Children.Functions.emplace_back();
77 I.Children.Enums.emplace_back();
78
79 std::string WriteResult = writeInfo(&I, this->Diags);
80 EXPECT_TRUE(WriteResult.size() > 0);
81 std::vector<std::unique_ptr<Info>> ReadResults =
82 readInfo(WriteResult, 1, this->Diags);
83
84 CheckNamespaceInfo(&I, InfoAsNamespace(ReadResults[0].get()));
85 }
86
87 TEST_F(BitcodeTest, emitRecordInfoBitcode) {
88 RecordInfo I;
89 I.Name = "r";
90 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
91
92 I.DefLoc = Location(10, 10, "test.cpp");
93 I.Loc.emplace_back(12, 12, "test.cpp");
94
95 I.Members.emplace_back(TypeInfo("int"), "X", AccessSpecifier::AS_private);
96 I.TagType = TagTypeKind::Class;
97 I.IsTypeDef = true;
98 I.Bases.emplace_back(EmptySID, "F", "path/to/F", true,
99 AccessSpecifier::AS_public, true);
100 I.Bases.back().Children.Functions.emplace_back();
101 I.Bases.back().Members.emplace_back(TypeInfo("int"), "X",
102 AccessSpecifier::AS_private);
103 I.Parents.emplace_back(EmptySID, "F", InfoType::IT_record);
104 I.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record);
105
106 // Documentation for the data member.
107 CommentInfo TopComment;
108 TopComment.Kind = CommentKind::CK_FullComment;
109 TopComment.Children.emplace_back(std::make_unique<CommentInfo>());
110 CommentInfo *Brief = TopComment.Children.back().get();
111 Brief->Kind = CommentKind::CK_ParagraphComment;
112 Brief->Children.emplace_back(std::make_unique<CommentInfo>());
113 Brief->Children.back()->Kind = CommentKind::CK_TextComment;
114 Brief->Children.back()->Name = "ParagraphComment";
115 Brief->Children.back()->Text = "Value of the thing.";
116 I.Bases.back().Members.back().Description.emplace_back(std::move(TopComment));
117
118 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record);
119 I.Children.Functions.emplace_back();
120 I.Children.Enums.emplace_back();
121
122 std::string WriteResult = writeInfo(&I, this->Diags);
123 EXPECT_TRUE(WriteResult.size() > 0);
124 std::vector<std::unique_ptr<Info>> ReadResults =
125 readInfo(WriteResult, 1, this->Diags);
126
127 CheckRecordInfo(&I, InfoAsRecord(ReadResults[0].get()));
128 }
129
130 TEST_F(BitcodeTest, emitFunctionInfoBitcode) {
131 FunctionInfo I;
132 I.Name = "f";
133 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
134
135 I.DefLoc = Location(10, 10, "test.cpp");
136 I.Loc.emplace_back(12, 12, "test.cpp");
137
138 I.ReturnType = TypeInfo("void");
139 I.Params.emplace_back(TypeInfo("int"), "P");
140
141 I.Access = AccessSpecifier::AS_none;
142
143 std::string WriteResult = writeInfo(&I, this->Diags);
144 EXPECT_TRUE(WriteResult.size() > 0);
145 std::vector<std::unique_ptr<Info>> ReadResults =
146 readInfo(WriteResult, 1, this->Diags);
147
148 CheckFunctionInfo(&I, InfoAsFunction(ReadResults[0].get()));
149 }
150
151 TEST_F(BitcodeTest, emitMethodInfoBitcode) {
152 FunctionInfo I;
153 I.Name = "f";
154 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
155
156 I.DefLoc = Location(10, 10, "test.cpp");
157 I.Loc.emplace_back(12, 12, "test.cpp");
158
159 I.ReturnType = TypeInfo("void");
160 I.Params.emplace_back(TypeInfo("int"), "P");
161 I.IsMethod = true;
162 I.Parent = Reference(EmptySID, "Parent", InfoType::IT_record);
163
164 I.Access = AccessSpecifier::AS_public;
165
166 std::string WriteResult = writeInfo(&I, this->Diags);
167 EXPECT_TRUE(WriteResult.size() > 0);
168 std::vector<std::unique_ptr<Info>> ReadResults =
169 readInfo(WriteResult, 1, this->Diags);
170
171 CheckFunctionInfo(&I, InfoAsFunction(ReadResults[0].get()));
172 }
173
174 TEST_F(BitcodeTest, emitEnumInfoBitcode) {
175 EnumInfo I;
176 I.Name = "e";
177 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
178
179 I.DefLoc = Location(10, 10, "test.cpp");
180 I.Loc.emplace_back(12, 12, "test.cpp");
181
182 I.Members.emplace_back("X");
183 I.Scoped = true;
184
185 std::string WriteResult = writeInfo(&I, this->Diags);
186 EXPECT_TRUE(WriteResult.size() > 0);
187 std::vector<std::unique_ptr<Info>> ReadResults =
188 readInfo(WriteResult, 1, this->Diags);
189
190 CheckEnumInfo(&I, InfoAsEnum(ReadResults[0].get()));
191 }
192
193 TEST_F(BitcodeTest, emitTypedefInfoBitcode) {
194 TypedefInfo I;
195 I.Name = "MyInt";
196 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
197
198 I.DefLoc = Location(10, 10, "test.cpp");
199 I.Underlying = TypeInfo("unsigned");
200 I.IsUsing = true;
201
202 CommentInfo Top;
203 Top.Kind = CommentKind::CK_FullComment;
204
205 Top.Children.emplace_back(std::make_unique<CommentInfo>());
206 CommentInfo *BlankLine = Top.Children.back().get();
207 BlankLine->Kind = CommentKind::CK_ParagraphComment;
208 BlankLine->Children.emplace_back(std::make_unique<CommentInfo>());
209 BlankLine->Children.back()->Kind = CommentKind::CK_TextComment;
210
211 I.Description.emplace_back(std::move(Top));
212
213 std::string WriteResult = writeInfo(&I, this->Diags);
214 EXPECT_TRUE(WriteResult.size() > 0);
215 std::vector<std::unique_ptr<Info>> ReadResults =
216 readInfo(WriteResult, 1, this->Diags);
217
218 CheckTypedefInfo(&I, InfoAsTypedef(ReadResults[0].get()));
219
220 // Check one with no IsUsing set, no description, and no definition location.
221 TypedefInfo I2;
222 I2.Name = "SomethingElse";
223 I2.IsUsing = false;
224 I2.Underlying = TypeInfo("int");
225
226 WriteResult = writeInfo(&I2, this->Diags);
227 EXPECT_TRUE(WriteResult.size() > 0);
228 ReadResults = readInfo(WriteResult, 1, this->Diags);
229 CheckTypedefInfo(&I2, InfoAsTypedef(ReadResults[0].get()));
230 }
231
232 TEST_F(BitcodeTest, emitInfoWithCommentBitcode) {
233 FunctionInfo F;
234 F.Name = "F";
235 F.ReturnType = TypeInfo("void");
236 F.DefLoc = Location(0, 0, "test.cpp");
237 F.Params.emplace_back(TypeInfo("int"), "I");
238
239 CommentInfo Top;
240 Top.Kind = CommentKind::CK_FullComment;
241
242 Top.Children.emplace_back(std::make_unique<CommentInfo>());
243 CommentInfo *BlankLine = Top.Children.back().get();
244 BlankLine->Kind = CommentKind::CK_ParagraphComment;
245 BlankLine->Children.emplace_back(std::make_unique<CommentInfo>());
246 BlankLine->Children.back()->Kind = CommentKind::CK_TextComment;
247
248 Top.Children.emplace_back(std::make_unique<CommentInfo>());
249 CommentInfo *Brief = Top.Children.back().get();
250 Brief->Kind = CommentKind::CK_ParagraphComment;
251 Brief->Children.emplace_back(std::make_unique<CommentInfo>());
252 Brief->Children.back()->Kind = CommentKind::CK_TextComment;
253 Brief->Children.back()->Name = "ParagraphComment";
254 Brief->Children.back()->Text = " Brief description.";
255
256 Top.Children.emplace_back(std::make_unique<CommentInfo>());
257 CommentInfo *Extended = Top.Children.back().get();
258 Extended->Kind = CommentKind::CK_ParagraphComment;
259 Extended->Children.emplace_back(std::make_unique<CommentInfo>());
260 Extended->Children.back()->Kind = CommentKind::CK_TextComment;
261 Extended->Children.back()->Text = " Extended description that";
262 Extended->Children.emplace_back(std::make_unique<CommentInfo>());
263 Extended->Children.back()->Kind = CommentKind::CK_TextComment;
264 Extended->Children.back()->Text = " continues onto the next line.";
265
266 Top.Children.emplace_back(std::make_unique<CommentInfo>());
267 CommentInfo *HTML = Top.Children.back().get();
268 HTML->Kind = CommentKind::CK_ParagraphComment;
269 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
270 HTML->Children.back()->Kind = CommentKind::CK_TextComment;
271 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
272 HTML->Children.back()->Kind = CommentKind::CK_HTMLStartTagComment;
273 HTML->Children.back()->Name = "ul";
274 HTML->Children.back()->AttrKeys.emplace_back("class");
275 HTML->Children.back()->AttrValues.emplace_back("test");
276 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
277 HTML->Children.back()->Kind = CommentKind::CK_HTMLStartTagComment;
278 HTML->Children.back()->Name = "li";
279 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
280 HTML->Children.back()->Kind = CommentKind::CK_TextComment;
281 HTML->Children.back()->Text = " Testing.";
282 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
283 HTML->Children.back()->Kind = CommentKind::CK_HTMLEndTagComment;
284 HTML->Children.back()->Name = "ul";
285 HTML->Children.back()->SelfClosing = true;
286
287 Top.Children.emplace_back(std::make_unique<CommentInfo>());
288 CommentInfo *Verbatim = Top.Children.back().get();
289 Verbatim->Kind = CommentKind::CK_VerbatimBlockComment;
290 Verbatim->Name = "verbatim";
291 Verbatim->CloseName = "endverbatim";
292 Verbatim->Children.emplace_back(std::make_unique<CommentInfo>());
293 Verbatim->Children.back()->Kind = CommentKind::CK_VerbatimBlockLineComment;
294 Verbatim->Children.back()->Text = " The description continues.";
295
296 Top.Children.emplace_back(std::make_unique<CommentInfo>());
297 CommentInfo *ParamOut = Top.Children.back().get();
298 ParamOut->Kind = CommentKind::CK_ParamCommandComment;
299 ParamOut->Direction = "[out]";
300 ParamOut->ParamName = "I";
301 ParamOut->Explicit = true;
302 ParamOut->Children.emplace_back(std::make_unique<CommentInfo>());
303 ParamOut->Children.back()->Kind = CommentKind::CK_ParagraphComment;
304 ParamOut->Children.back()->Children.emplace_back(
305 std::make_unique<CommentInfo>());
306 ParamOut->Children.back()->Children.back()->Kind =
307 CommentKind::CK_TextComment;
308 ParamOut->Children.back()->Children.emplace_back(
309 std::make_unique<CommentInfo>());
310 ParamOut->Children.back()->Children.back()->Kind =
311 CommentKind::CK_TextComment;
312 ParamOut->Children.back()->Children.back()->Text = " is a parameter.";
313
314 Top.Children.emplace_back(std::make_unique<CommentInfo>());
315 CommentInfo *ParamIn = Top.Children.back().get();
316 ParamIn->Kind = CommentKind::CK_ParamCommandComment;
317 ParamIn->Direction = "[in]";
318 ParamIn->ParamName = "J";
319 ParamIn->Children.emplace_back(std::make_unique<CommentInfo>());
320 ParamIn->Children.back()->Kind = CommentKind::CK_ParagraphComment;
321 ParamIn->Children.back()->Children.emplace_back(
322 std::make_unique<CommentInfo>());
323 ParamIn->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
324 ParamIn->Children.back()->Children.back()->Text = " is a parameter.";
325 ParamIn->Children.back()->Children.emplace_back(
326 std::make_unique<CommentInfo>());
327 ParamIn->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
328
329 Top.Children.emplace_back(std::make_unique<CommentInfo>());
330 CommentInfo *Return = Top.Children.back().get();
331 Return->Kind = CommentKind::CK_BlockCommandComment;
332 Return->Name = "return";
333 Return->Explicit = true;
334 Return->Children.emplace_back(std::make_unique<CommentInfo>());
335 Return->Children.back()->Kind = CommentKind::CK_ParagraphComment;
336 Return->Children.back()->Children.emplace_back(
337 std::make_unique<CommentInfo>());
338 Return->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
339 Return->Children.back()->Children.back()->Text = "void";
340
341 F.Description.emplace_back(std::move(Top));
342
343 std::string WriteResult = writeInfo(&F, this->Diags);
344 EXPECT_TRUE(WriteResult.size() > 0);
345 std::vector<std::unique_ptr<Info>> ReadResults =
346 readInfo(WriteResult, 1, this->Diags);
347
348 CheckFunctionInfo(&F, InfoAsFunction(ReadResults[0].get()));
349 }
350
351 } // namespace doc
352 } // namespace clang