Debian Codegraph @

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

raw
1 //===-- clang-doc/YAMLGeneratorTest.cpp
2 //------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 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 #include "ClangDocTest.h"
11 #include "Generators.h"
12 #include "Representation.h"
13 #include "gtest/gtest.h"
14
15 namespace clang {
16 namespace doc {
17
18 static std::unique_ptr<Generator> getYAMLGenerator() {
19 auto G = doc::findGeneratorByName("yaml");
20 if (!G)
21 return nullptr;
22 return std::move(G.get());
23 }
24
25 class YAMLGeneratorTest : public ClangDocContextTest {};
26
27 TEST_F(YAMLGeneratorTest, emitNamespaceYAML) {
28 NamespaceInfo I;
29 I.Name = "Namespace";
30 I.Path = "path/to/A";
31 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
32
33 I.Children.Namespaces.emplace_back(
34 EmptySID, "ChildNamespace", InfoType::IT_namespace,
35 "path::to::A::Namespace::ChildNamespace", "path/to/A/Namespace");
36 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record,
37 "path::to::A::Namespace::ChildStruct",
38 "path/to/A/Namespace");
39 I.Children.Functions.emplace_back();
40 I.Children.Functions.back().Name = "OneFunction";
41 I.Children.Functions.back().Access = AccessSpecifier::AS_none;
42 I.Children.Enums.emplace_back();
43 I.Children.Enums.back().Name = "OneEnum";
44
45 auto G = getYAMLGenerator();
46 assert(G);
47 std::string Buffer;
48 llvm::raw_string_ostream Actual(Buffer);
49 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
50 assert(!Err);
51 std::string Expected =
52 R"raw(---
53 USR: '0000000000000000000000000000000000000000'
54 Name: 'Namespace'
55 Path: 'path/to/A'
56 Namespace:
57 - Type: Namespace
58 Name: 'A'
59 QualName: 'A'
60 ChildNamespaces:
61 - Type: Namespace
62 Name: 'ChildNamespace'
63 QualName: 'path::to::A::Namespace::ChildNamespace'
64 Path: 'path/to/A/Namespace'
65 ChildRecords:
66 - Type: Record
67 Name: 'ChildStruct'
68 QualName: 'path::to::A::Namespace::ChildStruct'
69 Path: 'path/to/A/Namespace'
70 ChildFunctions:
71 - USR: '0000000000000000000000000000000000000000'
72 Name: 'OneFunction'
73 ReturnType: {}
74 ChildEnums:
75 - USR: '0000000000000000000000000000000000000000'
76 Name: 'OneEnum'
77 ...
78 )raw";
79 EXPECT_EQ(Expected, Actual.str());
80 }
81
82 TEST_F(YAMLGeneratorTest, emitRecordYAML) {
83 RecordInfo I;
84 I.Name = "r";
85 I.Path = "path/to/A";
86 I.IsTypeDef = true;
87 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
88
89 I.DefLoc = Location(10, 10, "test.cpp");
90 I.Loc.emplace_back(12, 12, "test.cpp");
91
92 I.Members.emplace_back(TypeInfo("int"), "X", AccessSpecifier::AS_private);
93
94 // Member documentation.
95 CommentInfo TopComment;
96 TopComment.Kind = CommentKind::CK_FullComment;
97 TopComment.Children.emplace_back(std::make_unique<CommentInfo>());
98 CommentInfo *Brief = TopComment.Children.back().get();
99 Brief->Kind = CommentKind::CK_ParagraphComment;
100 Brief->Children.emplace_back(std::make_unique<CommentInfo>());
101 Brief->Children.back()->Kind = CommentKind::CK_TextComment;
102 Brief->Children.back()->Name = "ParagraphComment";
103 Brief->Children.back()->Text = "Value of the thing.";
104 I.Members.back().Description.push_back(std::move(TopComment));
105
106 I.TagType = TagTypeKind::Class;
107 I.Bases.emplace_back(EmptySID, "F", "path/to/F", true,
108 AccessSpecifier::AS_public, true);
109 I.Bases.back().Children.Functions.emplace_back();
110 I.Bases.back().Children.Functions.back().Name = "InheritedFunctionOne";
111 I.Bases.back().Members.emplace_back(TypeInfo("int"), "N",
112 AccessSpecifier::AS_private);
113 // F is in the global namespace
114 I.Parents.emplace_back(EmptySID, "F", InfoType::IT_record, "");
115 I.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record,
116 "path::to::G::G", "path/to/G");
117
118 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record,
119 "path::to::A::r::ChildStruct", "path/to/A/r");
120 I.Children.Functions.emplace_back();
121 I.Children.Functions.back().Name = "OneFunction";
122 I.Children.Enums.emplace_back();
123 I.Children.Enums.back().Name = "OneEnum";
124
125 auto G = getYAMLGenerator();
126 assert(G);
127 std::string Buffer;
128 llvm::raw_string_ostream Actual(Buffer);
129 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
130 assert(!Err);
131 std::string Expected =
132 R"raw(---
133 USR: '0000000000000000000000000000000000000000'
134 Name: 'r'
135 Path: 'path/to/A'
136 Namespace:
137 - Type: Namespace
138 Name: 'A'
139 QualName: 'A'
140 DefLocation:
141 LineNumber: 10
142 Filename: 'test.cpp'
143 Location:
144 - LineNumber: 12
145 Filename: 'test.cpp'
146 TagType: Class
147 IsTypeDef: true
148 Members:
149 - Type:
150 Name: 'int'
151 QualName: 'int'
152 Name: 'X'
153 Access: Private
154 Description:
155 - Kind: FullComment
156 Children:
157 - Kind: ParagraphComment
158 Children:
159 - Kind: TextComment
160 Text: 'Value of the thing.'
161 Name: 'ParagraphComment'
162 Bases:
163 - USR: '0000000000000000000000000000000000000000'
164 Name: 'F'
165 Path: 'path/to/F'
166 TagType: Struct
167 Members:
168 - Type:
169 Name: 'int'
170 QualName: 'int'
171 Name: 'N'
172 Access: Private
173 ChildFunctions:
174 - USR: '0000000000000000000000000000000000000000'
175 Name: 'InheritedFunctionOne'
176 ReturnType: {}
177 Access: Public
178 IsVirtual: true
179 Access: Public
180 IsParent: true
181 Parents:
182 - Type: Record
183 Name: 'F'
184 VirtualParents:
185 - Type: Record
186 Name: 'G'
187 QualName: 'path::to::G::G'
188 Path: 'path/to/G'
189 ChildRecords:
190 - Type: Record
191 Name: 'ChildStruct'
192 QualName: 'path::to::A::r::ChildStruct'
193 Path: 'path/to/A/r'
194 ChildFunctions:
195 - USR: '0000000000000000000000000000000000000000'
196 Name: 'OneFunction'
197 ReturnType: {}
198 Access: Public
199 ChildEnums:
200 - USR: '0000000000000000000000000000000000000000'
201 Name: 'OneEnum'
202 ...
203 )raw";
204 EXPECT_EQ(Expected, Actual.str());
205 }
206
207 TEST_F(YAMLGeneratorTest, emitFunctionYAML) {
208 FunctionInfo I;
209 I.Name = "f";
210 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
211
212 I.DefLoc = Location(10, 10, "test.cpp");
213 I.Loc.emplace_back(12, 12, "test.cpp");
214
215 I.Access = AccessSpecifier::AS_none;
216
217 I.ReturnType = TypeInfo(Reference(EmptySID, "void", InfoType::IT_default));
218 I.Params.emplace_back(TypeInfo("int"), "P");
219 I.Params.emplace_back(TypeInfo("double"), "D");
220 I.Params.back().DefaultValue = "2.0 * M_PI";
221 I.IsMethod = true;
222 I.Parent = Reference(EmptySID, "Parent", InfoType::IT_record);
223
224 auto G = getYAMLGenerator();
225 assert(G);
226 std::string Buffer;
227 llvm::raw_string_ostream Actual(Buffer);
228 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
229 assert(!Err);
230 std::string Expected =
231 R"raw(---
232 USR: '0000000000000000000000000000000000000000'
233 Name: 'f'
234 Namespace:
235 - Type: Namespace
236 Name: 'A'
237 QualName: 'A'
238 DefLocation:
239 LineNumber: 10
240 Filename: 'test.cpp'
241 Location:
242 - LineNumber: 12
243 Filename: 'test.cpp'
244 IsMethod: true
245 Parent:
246 Type: Record
247 Name: 'Parent'
248 QualName: 'Parent'
249 Params:
250 - Type:
251 Name: 'int'
252 QualName: 'int'
253 Name: 'P'
254 - Type:
255 Name: 'double'
256 QualName: 'double'
257 Name: 'D'
258 DefaultValue: '2.0 * M_PI'
259 ReturnType:
260 Type:
261 Name: 'void'
262 QualName: 'void'
263 ...
264 )raw";
265 EXPECT_EQ(Expected, Actual.str());
266 }
267
268 // Tests the equivalent of:
269 // namespace A {
270 // enum e { X };
271 // }
272 TEST_F(YAMLGeneratorTest, emitSimpleEnumYAML) {
273 EnumInfo I;
274 I.Name = "e";
275 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
276
277 I.DefLoc = Location(10, 10, "test.cpp");
278 I.Loc.emplace_back(12, 12, "test.cpp");
279
280 I.Members.emplace_back("X");
281 I.Scoped = false;
282
283 auto G = getYAMLGenerator();
284 assert(G);
285 std::string Buffer;
286 llvm::raw_string_ostream Actual(Buffer);
287 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
288 assert(!Err);
289 std::string Expected =
290 R"raw(---
291 USR: '0000000000000000000000000000000000000000'
292 Name: 'e'
293 Namespace:
294 - Type: Namespace
295 Name: 'A'
296 QualName: 'A'
297 DefLocation:
298 LineNumber: 10
299 Filename: 'test.cpp'
300 Location:
301 - LineNumber: 12
302 Filename: 'test.cpp'
303 Members:
304 - Name: 'X'
305 Value: '0'
306 ...
307 )raw";
308 EXPECT_EQ(Expected, Actual.str());
309 }
310
311 // Tests the equivalent of:
312 // enum class e : short { X = FOO_BAR + 2 };
313 TEST_F(YAMLGeneratorTest, enumTypedScopedEnumYAML) {
314 EnumInfo I;
315 I.Name = "e";
316
317 I.Members.emplace_back("X", "-9876", "FOO_BAR + 2");
318 I.Scoped = true;
319 I.BaseType = TypeInfo("short");
320
321 auto G = getYAMLGenerator();
322 assert(G);
323 std::string Buffer;
324 llvm::raw_string_ostream Actual(Buffer);
325 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
326 assert(!Err);
327 std::string Expected =
328 R"raw(---
329 USR: '0000000000000000000000000000000000000000'
330 Name: 'e'
331 Scoped: true
332 BaseType:
333 Type:
334 Name: 'short'
335 QualName: 'short'
336 Members:
337 - Name: 'X'
338 Value: '-9876'
339 Expr: 'FOO_BAR + 2'
340 ...
341 )raw";
342 EXPECT_EQ(Expected, Actual.str());
343 }
344
345 TEST_F(YAMLGeneratorTest, enumTypedefYAML) {
346 TypedefInfo I;
347 I.Name = "MyUsing";
348 I.Underlying = TypeInfo("int");
349 I.IsUsing = true;
350
351 auto G = getYAMLGenerator();
352 assert(G);
353 std::string Buffer;
354 llvm::raw_string_ostream Actual(Buffer);
355 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
356 assert(!Err);
357 std::string Expected =
358 R"raw(---
359 USR: '0000000000000000000000000000000000000000'
360 Name: 'MyUsing'
361 Underlying:
362 Name: 'int'
363 QualName: 'int'
364 IsUsing: true
365 ...
366 )raw";
367 EXPECT_EQ(Expected, Actual.str());
368 }
369
370 TEST_F(YAMLGeneratorTest, emitCommentYAML) {
371 FunctionInfo I;
372 I.Name = "f";
373 I.DefLoc = Location(10, 10, "test.cpp");
374 I.ReturnType = TypeInfo("void");
375 I.Params.emplace_back(TypeInfo("int"), "I");
376 I.Params.emplace_back(TypeInfo("int"), "J");
377 I.Access = AccessSpecifier::AS_none;
378
379 CommentInfo Top;
380 Top.Kind = CommentKind::CK_FullComment;
381
382 Top.Children.emplace_back(std::make_unique<CommentInfo>());
383 CommentInfo *BlankLine = Top.Children.back().get();
384 BlankLine->Kind = CommentKind::CK_ParagraphComment;
385 BlankLine->Children.emplace_back(std::make_unique<CommentInfo>());
386 BlankLine->Children.back()->Kind = CommentKind::CK_TextComment;
387
388 Top.Children.emplace_back(std::make_unique<CommentInfo>());
389 CommentInfo *Brief = Top.Children.back().get();
390 Brief->Kind = CommentKind::CK_ParagraphComment;
391 Brief->Children.emplace_back(std::make_unique<CommentInfo>());
392 Brief->Children.back()->Kind = CommentKind::CK_TextComment;
393 Brief->Children.back()->Name = "ParagraphComment";
394 Brief->Children.back()->Text = " Brief description.";
395
396 Top.Children.emplace_back(std::make_unique<CommentInfo>());
397 CommentInfo *Extended = Top.Children.back().get();
398 Extended->Kind = CommentKind::CK_ParagraphComment;
399 Extended->Children.emplace_back(std::make_unique<CommentInfo>());
400 Extended->Children.back()->Kind = CommentKind::CK_TextComment;
401 Extended->Children.back()->Text = " Extended description that";
402 Extended->Children.emplace_back(std::make_unique<CommentInfo>());
403 Extended->Children.back()->Kind = CommentKind::CK_TextComment;
404 Extended->Children.back()->Text = " continues onto the next line.";
405
406 Top.Children.emplace_back(std::make_unique<CommentInfo>());
407 CommentInfo *HTML = Top.Children.back().get();
408 HTML->Kind = CommentKind::CK_ParagraphComment;
409 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
410 HTML->Children.back()->Kind = CommentKind::CK_TextComment;
411 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
412 HTML->Children.back()->Kind = CommentKind::CK_HTMLStartTagComment;
413 HTML->Children.back()->Name = "ul";
414 HTML->Children.back()->AttrKeys.emplace_back("class");
415 HTML->Children.back()->AttrValues.emplace_back("test");
416 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
417 HTML->Children.back()->Kind = CommentKind::CK_HTMLStartTagComment;
418 HTML->Children.back()->Name = "li";
419 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
420 HTML->Children.back()->Kind = CommentKind::CK_TextComment;
421 HTML->Children.back()->Text = " Testing.";
422 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
423 HTML->Children.back()->Kind = CommentKind::CK_HTMLEndTagComment;
424 HTML->Children.back()->Name = "ul";
425 HTML->Children.back()->SelfClosing = true;
426
427 Top.Children.emplace_back(std::make_unique<CommentInfo>());
428 CommentInfo *Verbatim = Top.Children.back().get();
429 Verbatim->Kind = CommentKind::CK_VerbatimBlockComment;
430 Verbatim->Name = "verbatim";
431 Verbatim->CloseName = "endverbatim";
432 Verbatim->Children.emplace_back(std::make_unique<CommentInfo>());
433 Verbatim->Children.back()->Kind = CommentKind::CK_VerbatimBlockLineComment;
434 Verbatim->Children.back()->Text = " The description continues.";
435
436 Top.Children.emplace_back(std::make_unique<CommentInfo>());
437 CommentInfo *ParamOut = Top.Children.back().get();
438 ParamOut->Kind = CommentKind::CK_ParamCommandComment;
439 ParamOut->Direction = "[out]";
440 ParamOut->ParamName = "I";
441 ParamOut->Explicit = true;
442 ParamOut->Children.emplace_back(std::make_unique<CommentInfo>());
443 ParamOut->Children.back()->Kind = CommentKind::CK_ParagraphComment;
444 ParamOut->Children.back()->Children.emplace_back(
445 std::make_unique<CommentInfo>());
446 ParamOut->Children.back()->Children.back()->Kind =
447 CommentKind::CK_TextComment;
448 ParamOut->Children.back()->Children.emplace_back(
449 std::make_unique<CommentInfo>());
450 ParamOut->Children.back()->Children.back()->Kind =
451 CommentKind::CK_TextComment;
452 ParamOut->Children.back()->Children.back()->Text = " is a parameter.";
453
454 Top.Children.emplace_back(std::make_unique<CommentInfo>());
455 CommentInfo *ParamIn = Top.Children.back().get();
456 ParamIn->Kind = CommentKind::CK_ParamCommandComment;
457 ParamIn->Direction = "[in]";
458 ParamIn->ParamName = "J";
459 ParamIn->Children.emplace_back(std::make_unique<CommentInfo>());
460 ParamIn->Children.back()->Kind = CommentKind::CK_ParagraphComment;
461 ParamIn->Children.back()->Children.emplace_back(
462 std::make_unique<CommentInfo>());
463 ParamIn->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
464 ParamIn->Children.back()->Children.back()->Text = " is a parameter.";
465 ParamIn->Children.back()->Children.emplace_back(
466 std::make_unique<CommentInfo>());
467 ParamIn->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
468
469 Top.Children.emplace_back(std::make_unique<CommentInfo>());
470 CommentInfo *Return = Top.Children.back().get();
471 Return->Kind = CommentKind::CK_BlockCommandComment;
472 Return->Name = "return";
473 Return->Explicit = true;
474 Return->Children.emplace_back(std::make_unique<CommentInfo>());
475 Return->Children.back()->Kind = CommentKind::CK_ParagraphComment;
476 Return->Children.back()->Children.emplace_back(
477 std::make_unique<CommentInfo>());
478 Return->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
479 Return->Children.back()->Children.back()->Text = "void";
480
481 I.Description.emplace_back(std::move(Top));
482
483 auto G = getYAMLGenerator();
484 assert(G);
485 std::string Buffer;
486 llvm::raw_string_ostream Actual(Buffer);
487 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
488 assert(!Err);
489 std::string Expected =
490 R"raw(---
491 USR: '0000000000000000000000000000000000000000'
492 Name: 'f'
493 Description:
494 - Kind: FullComment
495 Children:
496 - Kind: ParagraphComment
497 Children:
498 - Kind: TextComment
499 - Kind: ParagraphComment
500 Children:
501 - Kind: TextComment
502 Text: ' Brief description.'
503 Name: 'ParagraphComment'
504 - Kind: ParagraphComment
505 Children:
506 - Kind: TextComment
507 Text: ' Extended description that'
508 - Kind: TextComment
509 Text: ' continues onto the next line.'
510 - Kind: ParagraphComment
511 Children:
512 - Kind: TextComment
513 - Kind: HTMLStartTagComment
514 Name: 'ul'
515 AttrKeys:
516 - 'class'
517 AttrValues:
518 - 'test'
519 - Kind: HTMLStartTagComment
520 Name: 'li'
521 - Kind: TextComment
522 Text: ' Testing.'
523 - Kind: HTMLEndTagComment
524 Name: 'ul'
525 SelfClosing: true
526 - Kind: VerbatimBlockComment
527 Name: 'verbatim'
528 CloseName: 'endverbatim'
529 Children:
530 - Kind: VerbatimBlockLineComment
531 Text: ' The description continues.'
532 - Kind: ParamCommandComment
533 Direction: '[out]'
534 ParamName: 'I'
535 Explicit: true
536 Children:
537 - Kind: ParagraphComment
538 Children:
539 - Kind: TextComment
540 - Kind: TextComment
541 Text: ' is a parameter.'
542 - Kind: ParamCommandComment
543 Direction: '[in]'
544 ParamName: 'J'
545 Children:
546 - Kind: ParagraphComment
547 Children:
548 - Kind: TextComment
549 Text: ' is a parameter.'
550 - Kind: TextComment
551 - Kind: BlockCommandComment
552 Name: 'return'
553 Explicit: true
554 Children:
555 - Kind: ParagraphComment
556 Children:
557 - Kind: TextComment
558 Text: 'void'
559 DefLocation:
560 LineNumber: 10
561 Filename: 'test.cpp'
562 Params:
563 - Type:
564 Name: 'int'
565 QualName: 'int'
566 Name: 'I'
567 - Type:
568 Name: 'int'
569 QualName: 'int'
570 Name: 'J'
571 ReturnType:
572 Type:
573 Name: 'void'
574 QualName: 'void'
575 ...
576 )raw";
577
578 EXPECT_EQ(Expected, Actual.str());
579 }
580
581 } // namespace doc
582 } // namespace clang