Debian Codegraph @

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

raw
1 //===-- clang-doc/SerializeTest.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 "Serialize.h"
10 #include "ClangDocTest.h"
11 #include "Representation.h"
12 #include "clang/AST/Comment.h"
13 #include "clang/AST/RecursiveASTVisitor.h"
14 #include "gtest/gtest.h"
15
16 namespace clang {
17 namespace doc {
18
19 class SerializeTest : public ClangDocContextTest {};
20
21 class ClangDocSerializeTestVisitor
22 : public RecursiveASTVisitor<ClangDocSerializeTestVisitor> {
23
24 EmittedInfoList &EmittedInfos;
25 bool Public;
26
27 comments::FullComment *getComment(const NamedDecl *D) const {
28 if (RawComment *Comment =
29 D->getASTContext().getRawCommentForDeclNoCache(D)) {
30 Comment->setAttached();
31 return Comment->parse(D->getASTContext(), nullptr, D);
32 }
33 return nullptr;
34 }
35
36 public:
37 ClangDocSerializeTestVisitor(EmittedInfoList &EmittedInfos, bool Public,
38 DiagnosticsEngine &Diags)
39 : EmittedInfos(EmittedInfos), Public(Public) {}
40
41 template <typename T> bool mapDecl(const T *D) {
42 Location Loc(0, 0, "test.cpp");
43 auto [Child, Parent] = serialize::emitInfo(D, getComment(D), Loc, Public);
44 if (Child)
45 EmittedInfos.emplace_back(std::move(Child));
46 if (Parent)
47 EmittedInfos.emplace_back(std::move(Parent));
48 return true;
49 }
50
51 bool VisitNamespaceDecl(const NamespaceDecl *D) { return mapDecl(D); }
52
53 bool VisitFunctionDecl(const FunctionDecl *D) {
54 // Don't visit CXXMethodDecls twice
55 if (isa<CXXMethodDecl>(D))
56 return true;
57 return mapDecl(D);
58 }
59
60 bool VisitCXXMethodDecl(const CXXMethodDecl *D) { return mapDecl(D); }
61
62 bool VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
63
64 bool VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
65
66 bool VisitTypedefDecl(const TypedefDecl *D) { return mapDecl(D); }
67
68 bool VisitTypeAliasDecl(const TypeAliasDecl *D) { return mapDecl(D); }
69 };
70
71 static void extractInfosFromCode(StringRef Code, size_t NumExpectedInfos,
72 bool Public, EmittedInfoList &EmittedInfos,
73 DiagnosticsEngine &Diags) {
74 auto ASTUnit = clang::tooling::buildASTFromCode(Code);
75 TranslationUnitDecl *TU = ASTUnit->getASTContext().getTranslationUnitDecl();
76 ClangDocSerializeTestVisitor Visitor(EmittedInfos, Public, Diags);
77 Visitor.TraverseTranslationUnitDecl(TU);
78 ASSERT_EQ(NumExpectedInfos, EmittedInfos.size());
79 }
80
81 static void extractInfosFromCodeWithArgs(StringRef Code,
82 size_t NumExpectedInfos, bool Public,
83 EmittedInfoList &EmittedInfos,
84 std::vector<std::string> &Args,
85 DiagnosticsEngine &Diags) {
86 auto ASTUnit = clang::tooling::buildASTFromCodeWithArgs(Code, Args);
87 TranslationUnitDecl *TU = ASTUnit->getASTContext().getTranslationUnitDecl();
88 ClangDocSerializeTestVisitor Visitor(EmittedInfos, Public, Diags);
89 Visitor.TraverseTranslationUnitDecl(TU);
90 ASSERT_EQ(NumExpectedInfos, EmittedInfos.size());
91 }
92
93 // Constructs a comment definition as the parser would for one comment line.
94 /* TODO uncomment this when the missing comment is fixed in emitRecordInfo and
95 the code that calls this is re-enabled.
96 CommentInfo MakeOneLineCommentInfo(const std::string &Text) {
97 CommentInfo TopComment;
98 TopComment.Kind = "FullComment";
99 TopComment.Children.emplace_back(std::make_unique<CommentInfo>());
100
101 CommentInfo *Brief = TopComment.Children.back().get();
102 Brief->Kind = "ParagraphComment";
103
104 Brief->Children.emplace_back(std::make_unique<CommentInfo>());
105 Brief->Children.back()->Kind = "TextComment";
106 Brief->Children.back()->Name = "ParagraphComment";
107 Brief->Children.back()->Text = Text;
108
109 return TopComment;
110 }
111 */
112
113 // Test serialization of namespace declarations.
114 TEST_F(SerializeTest, emitNamespaceInfo) {
115 EmittedInfoList Infos;
116 extractInfosFromCode("namespace A { namespace B { void f() {} } }", 5,
117 /*Public=*/false, Infos, this->Diags);
118
119 NamespaceInfo *A = InfoAsNamespace(Infos[0].get());
120 NamespaceInfo ExpectedA(EmptySID, "A");
121 CheckNamespaceInfo(&ExpectedA, A);
122
123 NamespaceInfo *B = InfoAsNamespace(Infos[2].get());
124 NamespaceInfo ExpectedB(EmptySID, /*Name=*/"B", /*Path=*/"A");
125 ExpectedB.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
126 CheckNamespaceInfo(&ExpectedB, B);
127
128 NamespaceInfo *BWithFunction = InfoAsNamespace(Infos[4].get());
129 NamespaceInfo ExpectedBWithFunction(EmptySID);
130 FunctionInfo F;
131 F.Name = "f";
132 F.ReturnType = TypeInfo("void");
133 F.DefLoc = Location(0, 0, "test.cpp");
134 F.Namespace.emplace_back(EmptySID, "B", InfoType::IT_namespace);
135 F.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
136 F.Access = AccessSpecifier::AS_none;
137 ExpectedBWithFunction.Children.Functions.emplace_back(std::move(F));
138 CheckNamespaceInfo(&ExpectedBWithFunction, BWithFunction);
139 }
140
141 TEST_F(SerializeTest, emitAnonymousNamespaceInfo) {
142 EmittedInfoList Infos;
143 extractInfosFromCode("namespace { }", 2, /*Public=*/false, Infos,
144 this->Diags);
145
146 NamespaceInfo *A = InfoAsNamespace(Infos[0].get());
147 NamespaceInfo ExpectedA(EmptySID);
148 ExpectedA.Name = "@nonymous_namespace";
149 CheckNamespaceInfo(&ExpectedA, A);
150 }
151
152 TEST_F(SerializeTest, emitRecordInfo) {
153 EmittedInfoList Infos;
154 extractInfosFromCode(R"raw(class E {
155 public:
156 E() {}
157
158 // Some docs.
159 int value;
160 protected:
161 void ProtectedMethod();
162 };
163 template <typename T>
164 struct F {
165 void TemplateMethod();
166 };
167 template <>
168 void F<int>::TemplateMethod();
169 typedef struct {} G;)raw",
170 10, /*Public=*/false, Infos, this->Diags);
171
172 RecordInfo *E = InfoAsRecord(Infos[0].get());
173 RecordInfo ExpectedE(EmptySID, /*Name=*/"E", /*Path=*/"GlobalNamespace");
174 ExpectedE.Namespace.emplace_back(EmptySID, "GlobalNamespace",
175 InfoType::IT_namespace);
176 ExpectedE.TagType = TagTypeKind::Class;
177 ExpectedE.DefLoc = Location(0, 0, "test.cpp");
178 ExpectedE.Members.emplace_back(TypeInfo("int"), "value",
179 AccessSpecifier::AS_public);
180 // TODO the data member should have the docstring on it:
181 //ExpectedE.Members.back().Description.push_back(MakeOneLineCommentInfo(" Some docs"));
182 CheckRecordInfo(&ExpectedE, E);
183
184 RecordInfo *RecordWithEConstructor = InfoAsRecord(Infos[2].get());
185 RecordInfo ExpectedRecordWithEConstructor(EmptySID);
186 FunctionInfo EConstructor;
187 EConstructor.Name = "E";
188 EConstructor.Parent = Reference(EmptySID, "E", InfoType::IT_record);
189 EConstructor.ReturnType = TypeInfo("void");
190 EConstructor.DefLoc = Location(0, 0, "test.cpp");
191 EConstructor.Namespace.emplace_back(EmptySID, "E", InfoType::IT_record);
192 EConstructor.Namespace.emplace_back(EmptySID, "GlobalNamespace",
193 InfoType::IT_namespace);
194 EConstructor.Access = AccessSpecifier::AS_public;
195 EConstructor.IsMethod = true;
196 ExpectedRecordWithEConstructor.Children.Functions.emplace_back(
197 std::move(EConstructor));
198 CheckRecordInfo(&ExpectedRecordWithEConstructor, RecordWithEConstructor);
199
200 RecordInfo *RecordWithMethod = InfoAsRecord(Infos[3].get());
201 RecordInfo ExpectedRecordWithMethod(EmptySID);
202 FunctionInfo Method;
203 Method.Name = "ProtectedMethod";
204 Method.Parent = Reference(EmptySID, "E", InfoType::IT_record);
205 Method.ReturnType = TypeInfo("void");
206 Method.Loc.emplace_back(0, 0, "test.cpp");
207 Method.Namespace.emplace_back(EmptySID, "E", InfoType::IT_record);
208 Method.Namespace.emplace_back(EmptySID, "GlobalNamespace",
209 InfoType::IT_namespace);
210 Method.Access = AccessSpecifier::AS_protected;
211 Method.IsMethod = true;
212 ExpectedRecordWithMethod.Children.Functions.emplace_back(std::move(Method));
213 CheckRecordInfo(&ExpectedRecordWithMethod, RecordWithMethod);
214
215 RecordInfo *F = InfoAsRecord(Infos[4].get());
216 RecordInfo ExpectedF(EmptySID, /*Name=*/"F", /*Path=*/"GlobalNamespace");
217 ExpectedF.Namespace.emplace_back(EmptySID, "GlobalNamespace",
218 InfoType::IT_namespace);
219 ExpectedF.TagType = TagTypeKind::Struct;
220 ExpectedF.DefLoc = Location(0, 0, "test.cpp");
221 CheckRecordInfo(&ExpectedF, F);
222
223 RecordInfo *RecordWithTemplateMethod = InfoAsRecord(Infos[6].get());
224 RecordInfo ExpectedRecordWithTemplateMethod(EmptySID);
225 FunctionInfo TemplateMethod;
226 TemplateMethod.Name = "TemplateMethod";
227 TemplateMethod.Parent = Reference(EmptySID, "F", InfoType::IT_record);
228 TemplateMethod.ReturnType = TypeInfo("void");
229 TemplateMethod.Loc.emplace_back(0, 0, "test.cpp");
230 TemplateMethod.Namespace.emplace_back(EmptySID, "F", InfoType::IT_record);
231 TemplateMethod.Namespace.emplace_back(EmptySID, "GlobalNamespace",
232 InfoType::IT_namespace);
233 TemplateMethod.Access = AccessSpecifier::AS_public;
234 TemplateMethod.IsMethod = true;
235 ExpectedRecordWithTemplateMethod.Children.Functions.emplace_back(
236 std::move(TemplateMethod));
237 CheckRecordInfo(&ExpectedRecordWithTemplateMethod, RecordWithTemplateMethod);
238
239 RecordInfo *TemplatedRecord = InfoAsRecord(Infos[7].get());
240 RecordInfo ExpectedTemplatedRecord(EmptySID);
241 FunctionInfo SpecializedTemplateMethod;
242 SpecializedTemplateMethod.Name = "TemplateMethod";
243 SpecializedTemplateMethod.Parent =
244 Reference(EmptySID, "F", InfoType::IT_record);
245 SpecializedTemplateMethod.ReturnType = TypeInfo("void");
246 SpecializedTemplateMethod.Loc.emplace_back(0, 0, "test.cpp");
247 SpecializedTemplateMethod.Namespace.emplace_back(EmptySID, "F",
248 InfoType::IT_record);
249 SpecializedTemplateMethod.Namespace.emplace_back(EmptySID, "GlobalNamespace",
250 InfoType::IT_namespace);
251 SpecializedTemplateMethod.Access = AccessSpecifier::AS_public;
252 SpecializedTemplateMethod.IsMethod = true;
253 ExpectedTemplatedRecord.Children.Functions.emplace_back(
254 std::move(SpecializedTemplateMethod));
255 CheckRecordInfo(&ExpectedTemplatedRecord, TemplatedRecord);
256
257 RecordInfo *G = InfoAsRecord(Infos[8].get());
258 RecordInfo ExpectedG(EmptySID, /*Name=*/"G", /*Path=*/"GlobalNamespace");
259 ExpectedG.Namespace.emplace_back(EmptySID, "GlobalNamespace",
260 InfoType::IT_namespace);
261 ExpectedG.TagType = TagTypeKind::Struct;
262 ExpectedG.DefLoc = Location(0, 0, "test.cpp");
263 ExpectedG.IsTypeDef = true;
264 CheckRecordInfo(&ExpectedG, G);
265 }
266
267 // Test serialization of enum declarations.
268 TEST_F(SerializeTest, emitEnumInfo) {
269 EmittedInfoList Infos;
270 extractInfosFromCode("enum E { X, Y }; enum class G { A, B };", 2,
271 /*Public=*/false, Infos, this->Diags);
272
273 NamespaceInfo *NamespaceWithEnum = InfoAsNamespace(Infos[0].get());
274 NamespaceInfo ExpectedNamespaceWithEnum(EmptySID);
275 EnumInfo E;
276 E.Name = "E";
277 E.DefLoc = Location(0, 0, "test.cpp");
278 E.Members.emplace_back("X", "0");
279 E.Members.emplace_back("Y", "1");
280 ExpectedNamespaceWithEnum.Children.Enums.emplace_back(std::move(E));
281 CheckNamespaceInfo(&ExpectedNamespaceWithEnum, NamespaceWithEnum);
282
283 NamespaceInfo *NamespaceWithScopedEnum = InfoAsNamespace(Infos[1].get());
284 NamespaceInfo ExpectedNamespaceWithScopedEnum(EmptySID);
285 EnumInfo G;
286 G.Name = "G";
287 G.Scoped = true;
288 G.DefLoc = Location(0, 0, "test.cpp");
289 G.Members.emplace_back("A", "0");
290 G.Members.emplace_back("B", "1");
291 ExpectedNamespaceWithScopedEnum.Children.Enums.emplace_back(std::move(G));
292 CheckNamespaceInfo(&ExpectedNamespaceWithScopedEnum, NamespaceWithScopedEnum);
293 }
294
295 TEST_F(SerializeTest, emitUndefinedRecordInfo) {
296 EmittedInfoList Infos;
297 extractInfosFromCode("class E;", 2, /*Public=*/false, Infos, this->Diags);
298
299 RecordInfo *E = InfoAsRecord(Infos[0].get());
300 RecordInfo ExpectedE(EmptySID, /*Name=*/"E", /*Path=*/"GlobalNamespace");
301 ExpectedE.Namespace.emplace_back(EmptySID, "GlobalNamespace",
302 InfoType::IT_namespace);
303 ExpectedE.TagType = TagTypeKind::Class;
304 ExpectedE.Loc.emplace_back(0, 0, "test.cpp");
305 CheckRecordInfo(&ExpectedE, E);
306 }
307
308 TEST_F(SerializeTest, emitRecordMemberInfo) {
309 EmittedInfoList Infos;
310 extractInfosFromCode("struct E { int I; };", 2, /*Public=*/false, Infos,
311 this->Diags);
312
313 RecordInfo *E = InfoAsRecord(Infos[0].get());
314 RecordInfo ExpectedE(EmptySID, /*Name=*/"E", /*Path=*/"GlobalNamespace");
315 ExpectedE.Namespace.emplace_back(EmptySID, "GlobalNamespace",
316 InfoType::IT_namespace);
317 ExpectedE.TagType = TagTypeKind::Struct;
318 ExpectedE.DefLoc = Location(0, 0, "test.cpp");
319 ExpectedE.Members.emplace_back(TypeInfo("int"), "I",
320 AccessSpecifier::AS_public);
321 CheckRecordInfo(&ExpectedE, E);
322 }
323
324 TEST_F(SerializeTest, emitInternalRecordInfo) {
325 EmittedInfoList Infos;
326 extractInfosFromCode("class E { class G {}; };", 4, /*Public=*/false, Infos,
327 this->Diags);
328
329 RecordInfo *E = InfoAsRecord(Infos[0].get());
330 RecordInfo ExpectedE(EmptySID, /*Name=*/"E", /*Path=*/"GlobalNamespace");
331 ExpectedE.Namespace.emplace_back(EmptySID, "GlobalNamespace",
332 InfoType::IT_namespace);
333 ExpectedE.DefLoc = Location(0, 0, "test.cpp");
334 ExpectedE.TagType = TagTypeKind::Class;
335 CheckRecordInfo(&ExpectedE, E);
336
337 RecordInfo *G = InfoAsRecord(Infos[2].get());
338 llvm::SmallString<128> ExpectedGPath("GlobalNamespace/E");
339 llvm::sys::path::native(ExpectedGPath);
340 RecordInfo ExpectedG(EmptySID, /*Name=*/"G", /*Path=*/ExpectedGPath);
341 ExpectedG.DefLoc = Location(0, 0, "test.cpp");
342 ExpectedG.TagType = TagTypeKind::Class;
343 ExpectedG.Namespace.emplace_back(EmptySID, "E", InfoType::IT_record);
344 ExpectedG.Namespace.emplace_back(EmptySID, "GlobalNamespace",
345 InfoType::IT_namespace);
346 CheckRecordInfo(&ExpectedG, G);
347 }
348
349 TEST_F(SerializeTest, emitPublicAnonymousNamespaceInfo) {
350 EmittedInfoList Infos;
351 extractInfosFromCode("namespace { class A; }", 0, /*Public=*/true, Infos,
352 this->Diags);
353 }
354
355 TEST_F(SerializeTest, emitPublicFunctionInternalInfo) {
356 EmittedInfoList Infos;
357 extractInfosFromCode("int F() { class G {}; return 0; };", 1, /*Public=*/true,
358 Infos, this->Diags);
359
360 NamespaceInfo *BWithFunction = InfoAsNamespace(Infos[0].get());
361 NamespaceInfo ExpectedBWithFunction(EmptySID);
362 FunctionInfo F;
363 F.Name = "F";
364 F.ReturnType = TypeInfo("int");
365 F.DefLoc = Location(0, 0, "test.cpp");
366 F.Access = AccessSpecifier::AS_none;
367 ExpectedBWithFunction.Children.Functions.emplace_back(std::move(F));
368 CheckNamespaceInfo(&ExpectedBWithFunction, BWithFunction);
369 }
370
371 TEST_F(SerializeTest, emitInlinedFunctionInfo) {
372 EmittedInfoList Infos;
373 extractInfosFromCode("inline void F(int I) { };", 1, /*Public=*/true, Infos,
374 this->Diags);
375
376 NamespaceInfo *BWithFunction = InfoAsNamespace(Infos[0].get());
377 NamespaceInfo ExpectedBWithFunction(EmptySID);
378 FunctionInfo F;
379 F.Name = "F";
380 F.ReturnType = TypeInfo("void");
381 F.DefLoc = Location(0, 0, "test.cpp");
382 F.Params.emplace_back(TypeInfo("int"), "I");
383 F.Access = AccessSpecifier::AS_none;
384 ExpectedBWithFunction.Children.Functions.emplace_back(std::move(F));
385 CheckNamespaceInfo(&ExpectedBWithFunction, BWithFunction);
386 }
387
388 TEST_F(SerializeTest, emitInheritedRecordInfo) {
389 EmittedInfoList Infos;
390 extractInfosFromCode(R"raw(class F { protected: void set(int N); };
391 class G { public: int get() { return 1; } protected: int I; };
392 class E : public F, virtual private G {};
393 class H : private E {};
394 template <typename T>
395 class I {} ;
396 class J : public I<int> {} ;)raw",
397 14, /*Public=*/false, Infos, this->Diags);
398
399 RecordInfo *F = InfoAsRecord(Infos[0].get());
400 RecordInfo ExpectedF(EmptySID, /*Name=*/"F", /*Path=*/"GlobalNamespace");
401 ExpectedF.Namespace.emplace_back(EmptySID, "GlobalNamespace",
402 InfoType::IT_namespace, "");
403 ExpectedF.TagType = TagTypeKind::Class;
404 ExpectedF.DefLoc = Location(0, 0, "test.cpp");
405 CheckRecordInfo(&ExpectedF, F);
406
407 RecordInfo *G = InfoAsRecord(Infos[3].get());
408 RecordInfo ExpectedG(EmptySID, /*Name=*/"G", /*Path=*/"GlobalNamespace");
409 ExpectedG.Namespace.emplace_back(EmptySID, "GlobalNamespace",
410 InfoType::IT_namespace);
411 ExpectedG.TagType = TagTypeKind::Class;
412 ExpectedG.DefLoc = Location(0, 0, "test.cpp");
413 ExpectedG.Members.emplace_back(TypeInfo("int"), "I",
414 AccessSpecifier::AS_protected);
415 CheckRecordInfo(&ExpectedG, G);
416
417 RecordInfo *E = InfoAsRecord(Infos[6].get());
418 RecordInfo ExpectedE(EmptySID, /*Name=*/"E", /*Path=*/"GlobalNamespace");
419 ExpectedE.Namespace.emplace_back(EmptySID, "GlobalNamespace",
420 InfoType::IT_namespace);
421 ExpectedE.Parents.emplace_back(EmptySID, /*Name=*/"F", InfoType::IT_record,
422 /*QualName=*/"", /*Path*=*/"GlobalNamespace");
423 ExpectedE.VirtualParents.emplace_back(EmptySID, /*Name=*/"G",
424 InfoType::IT_record, /*QualName=*/"G",
425 /*Path*=*/"GlobalNamespace");
426 ExpectedE.Bases.emplace_back(EmptySID, /*Name=*/"F",
427 /*Path=*/"GlobalNamespace", false,
428 AccessSpecifier::AS_public, true);
429 FunctionInfo FunctionSet;
430 FunctionSet.Name = "set";
431 FunctionSet.ReturnType = TypeInfo("void");
432 FunctionSet.Loc.emplace_back();
433 FunctionSet.Params.emplace_back(TypeInfo("int"), "N");
434 FunctionSet.Namespace.emplace_back(EmptySID, "F", InfoType::IT_record);
435 FunctionSet.Namespace.emplace_back(EmptySID, "GlobalNamespace",
436 InfoType::IT_namespace);
437 FunctionSet.Access = AccessSpecifier::AS_protected;
438 FunctionSet.IsMethod = true;
439 ExpectedE.Bases.back().Children.Functions.emplace_back(
440 std::move(FunctionSet));
441 ExpectedE.Bases.emplace_back(EmptySID, /*Name=*/"G",
442 /*Path=*/"GlobalNamespace", true,
443 AccessSpecifier::AS_private, true);
444 FunctionInfo FunctionGet;
445 FunctionGet.Name = "get";
446 FunctionGet.ReturnType = TypeInfo("int");
447 FunctionGet.DefLoc = Location();
448 FunctionGet.Namespace.emplace_back(EmptySID, "G", InfoType::IT_record);
449 FunctionGet.Namespace.emplace_back(EmptySID, "GlobalNamespace",
450 InfoType::IT_namespace);
451 FunctionGet.Access = AccessSpecifier::AS_private;
452 FunctionGet.IsMethod = true;
453 ExpectedE.Bases.back().Children.Functions.emplace_back(
454 std::move(FunctionGet));
455 ExpectedE.Bases.back().Members.emplace_back(TypeInfo("int"), "I",
456 AccessSpecifier::AS_private);
457 ExpectedE.DefLoc = Location(0, 0, "test.cpp");
458 ExpectedE.TagType = TagTypeKind::Class;
459 CheckRecordInfo(&ExpectedE, E);
460
461 RecordInfo *H = InfoAsRecord(Infos[8].get());
462 RecordInfo ExpectedH(EmptySID, /*Name=*/"H", /*Path=*/"GlobalNamespace");
463 ExpectedH.Namespace.emplace_back(EmptySID, "GlobalNamespace",
464 InfoType::IT_namespace);
465 ExpectedH.TagType = TagTypeKind::Class;
466 ExpectedH.DefLoc = Location(0, 0, "test.cpp");
467 ExpectedH.Parents.emplace_back(EmptySID, /*Name=*/"E", InfoType::IT_record,
468 /*QualName=*/"E", /*Path=*/"GlobalNamespace");
469 ExpectedH.VirtualParents.emplace_back(EmptySID, /*Name=*/"G",
470 InfoType::IT_record, /*QualName=*/"G",
471 /*Path=*/"GlobalNamespace");
472 ExpectedH.Bases.emplace_back(EmptySID, /*Name=*/"E",
473 /*Path=*/"GlobalNamespace", false,
474 AccessSpecifier::AS_private, true);
475 ExpectedH.Bases.emplace_back(EmptySID, /*Name=*/"F",
476 /*Path=*/"GlobalNamespace", false,
477 AccessSpecifier::AS_private, false);
478 FunctionInfo FunctionSetNew;
479 FunctionSetNew.Name = "set";
480 FunctionSetNew.ReturnType = TypeInfo("void");
481 FunctionSetNew.Loc.emplace_back();
482 FunctionSetNew.Params.emplace_back(TypeInfo("int"), "N");
483 FunctionSetNew.Namespace.emplace_back(EmptySID, "F", InfoType::IT_record);
484 FunctionSetNew.Namespace.emplace_back(EmptySID, "GlobalNamespace",
485 InfoType::IT_namespace);
486 FunctionSetNew.Access = AccessSpecifier::AS_private;
487 FunctionSetNew.IsMethod = true;
488 ExpectedH.Bases.back().Children.Functions.emplace_back(
489 std::move(FunctionSetNew));
490 ExpectedH.Bases.emplace_back(EmptySID, /*Name=*/"G",
491 /*Path=*/"GlobalNamespace", true,
492 AccessSpecifier::AS_private, false);
493 FunctionInfo FunctionGetNew;
494 FunctionGetNew.Name = "get";
495 FunctionGetNew.ReturnType = TypeInfo("int");
496 FunctionGetNew.DefLoc = Location();
497 FunctionGetNew.Namespace.emplace_back(EmptySID, "G", InfoType::IT_record);
498 FunctionGetNew.Namespace.emplace_back(EmptySID, "GlobalNamespace",
499 InfoType::IT_namespace);
500 FunctionGetNew.Access = AccessSpecifier::AS_private;
501 FunctionGetNew.IsMethod = true;
502 ExpectedH.Bases.back().Children.Functions.emplace_back(
503 std::move(FunctionGetNew));
504 ExpectedH.Bases.back().Members.emplace_back(TypeInfo("int"), "I",
505 AccessSpecifier::AS_private);
506 CheckRecordInfo(&ExpectedH, H);
507
508 RecordInfo *I = InfoAsRecord(Infos[10].get());
509 RecordInfo ExpectedI(EmptySID, /*Name=*/"I", /*Path=*/"GlobalNamespace");
510 ExpectedI.Namespace.emplace_back(EmptySID, "GlobalNamespace",
511 InfoType::IT_namespace);
512 ExpectedI.TagType = TagTypeKind::Class;
513 ExpectedI.DefLoc = Location(0, 0, "test.cpp");
514 CheckRecordInfo(&ExpectedI, I);
515
516 RecordInfo *J = InfoAsRecord(Infos[12].get());
517 RecordInfo ExpectedJ(EmptySID, /*Name=*/"J", /*Path=*/"GlobalNamespace");
518 ExpectedJ.Namespace.emplace_back(EmptySID, "GlobalNamespace",
519 InfoType::IT_namespace);
520 ExpectedJ.Parents.emplace_back(EmptySID, /*Name=*/"I<int>",
521 InfoType::IT_record);
522 ExpectedJ.Bases.emplace_back(EmptySID, /*Name=*/"I<int>",
523 /*Path=*/"GlobalNamespace", false,
524 AccessSpecifier::AS_public, true);
525 ExpectedJ.DefLoc = Location(0, 0, "test.cpp");
526 ExpectedJ.TagType = TagTypeKind::Class;
527 CheckRecordInfo(&ExpectedJ, J);
528 }
529
530 TEST_F(SerializeTest, emitModulePublicLFunctions) {
531 EmittedInfoList Infos;
532 std::vector<std::string> Args;
533 Args.push_back("-fmodules-ts");
534 extractInfosFromCodeWithArgs(R"raw(export module M;
535 int moduleFunction(int x, double d = 3.2 - 1.0);
536 static int staticModuleFunction(int x);
537 export double exportedModuleFunction(double y);)raw",
538 2, /*Public=*/true, Infos, Args, this->Diags);
539
540 NamespaceInfo *BWithFunction = InfoAsNamespace(Infos[0].get());
541 NamespaceInfo ExpectedBWithFunction(EmptySID);
542 FunctionInfo F;
543 F.Name = "moduleFunction";
544 F.ReturnType = TypeInfo("int");
545 F.Loc.emplace_back(0, 0, "test.cpp");
546 F.Params.emplace_back(TypeInfo("int"), "x");
547 F.Params.emplace_back(TypeInfo("double"), "d");
548 F.Params.back().DefaultValue = "3.2 - 1.0";
549 F.Access = AccessSpecifier::AS_none;
550 ExpectedBWithFunction.Children.Functions.emplace_back(std::move(F));
551 CheckNamespaceInfo(&ExpectedBWithFunction, BWithFunction);
552
553 NamespaceInfo *BWithExportedFunction = InfoAsNamespace(Infos[1].get());
554 NamespaceInfo ExpectedBWithExportedFunction(EmptySID);
555 FunctionInfo ExportedF;
556 ExportedF.Name = "exportedModuleFunction";
557 ExportedF.ReturnType =
558 TypeInfo(Reference(EmptySID, "double", InfoType::IT_default));
559 ExportedF.Loc.emplace_back(0, 0, "test.cpp");
560 ExportedF.Params.emplace_back(TypeInfo("double"), "y");
561 ExportedF.Access = AccessSpecifier::AS_none;
562 ExpectedBWithExportedFunction.Children.Functions.emplace_back(
563 std::move(ExportedF));
564 CheckNamespaceInfo(&ExpectedBWithExportedFunction, BWithExportedFunction);
565 }
566
567 // Test serialization of child records in namespaces and other records
568 TEST_F(SerializeTest, emitChildRecords) {
569 EmittedInfoList Infos;
570 extractInfosFromCode("class A { class B {}; }; namespace { class C {}; } ", 8,
571 /*Public=*/false, Infos, this->Diags);
572
573 NamespaceInfo *ParentA = InfoAsNamespace(Infos[1].get());
574 NamespaceInfo ExpectedParentA(EmptySID);
575 ExpectedParentA.Children.Records.emplace_back(
576 EmptySID, "A", InfoType::IT_record, "A", "GlobalNamespace");
577 CheckNamespaceInfo(&ExpectedParentA, ParentA);
578
579 RecordInfo *ParentB = InfoAsRecord(Infos[3].get());
580 RecordInfo ExpectedParentB(EmptySID);
581 llvm::SmallString<128> ExpectedParentBPath("GlobalNamespace/A");
582 llvm::sys::path::native(ExpectedParentBPath);
583 ExpectedParentB.Children.Records.emplace_back(
584 EmptySID, "B", InfoType::IT_record, "A::B", ExpectedParentBPath);
585 CheckRecordInfo(&ExpectedParentB, ParentB);
586
587 NamespaceInfo *ParentC = InfoAsNamespace(Infos[7].get());
588 NamespaceInfo ExpectedParentC(EmptySID);
589 ExpectedParentC.Children.Records.emplace_back(
590 EmptySID, "C", InfoType::IT_record, "C", "@nonymous_namespace");
591 CheckNamespaceInfo(&ExpectedParentC, ParentC);
592 }
593
594 // Test serialization of child namespaces
595 TEST_F(SerializeTest, emitChildNamespaces) {
596 EmittedInfoList Infos;
597 extractInfosFromCode("namespace A { namespace B { } }", 4, /*Public=*/false,
598 Infos, this->Diags);
599
600 NamespaceInfo *ParentA = InfoAsNamespace(Infos[1].get());
601 NamespaceInfo ExpectedParentA(EmptySID);
602 ExpectedParentA.Children.Namespaces.emplace_back(EmptySID, "A",
603 InfoType::IT_namespace);
604 CheckNamespaceInfo(&ExpectedParentA, ParentA);
605
606 NamespaceInfo *ParentB = InfoAsNamespace(Infos[3].get());
607 NamespaceInfo ExpectedParentB(EmptySID);
608 ExpectedParentB.Children.Namespaces.emplace_back(
609 EmptySID, "B", InfoType::IT_namespace, "A::B", "A");
610 CheckNamespaceInfo(&ExpectedParentB, ParentB);
611 }
612
613 TEST_F(SerializeTest, emitTypedefs) {
614 EmittedInfoList Infos;
615 extractInfosFromCode("typedef int MyInt; using MyDouble = double;", 2,
616 /*Public=*/false, Infos, this->Diags);
617
618 // First info will be the global namespace with the typedef in it.
619 NamespaceInfo *GlobalNS1 = InfoAsNamespace(Infos[0].get());
620 ASSERT_EQ(1u, GlobalNS1->Children.Typedefs.size());
621
622 const TypedefInfo &FirstTD = GlobalNS1->Children.Typedefs[0];
623 EXPECT_EQ("MyInt", FirstTD.Name);
624 EXPECT_FALSE(FirstTD.IsUsing);
625 EXPECT_EQ("int", FirstTD.Underlying.Type.Name);
626
627 // The second will be another global namespace with the using in it (the
628 // global namespace is duplicated because the items haven't been merged at the
629 // serialization phase of processing).
630 NamespaceInfo *GlobalNS2 = InfoAsNamespace(Infos[1].get());
631 ASSERT_EQ(1u, GlobalNS2->Children.Typedefs.size());
632
633 // Second is the "using" typedef.
634 const TypedefInfo &SecondTD = GlobalNS2->Children.Typedefs[0];
635 EXPECT_EQ("MyDouble", SecondTD.Name);
636 EXPECT_TRUE(SecondTD.IsUsing);
637 EXPECT_EQ("double", SecondTD.Underlying.Type.Name);
638 }
639
640 TEST_F(SerializeTest, emitFunctionTemplate) {
641 EmittedInfoList Infos;
642 // A template and a specialization.
643 extractInfosFromCode("template<typename T = int> bool GetFoo(T);\n"
644 "template<> bool GetFoo<bool>(bool);",
645 2,
646 /*Public=*/false, Infos, this->Diags);
647
648 // First info will be the global namespace.
649 NamespaceInfo *GlobalNS1 = InfoAsNamespace(Infos[0].get());
650 ASSERT_EQ(1u, GlobalNS1->Children.Functions.size());
651
652 const FunctionInfo &Func1 = GlobalNS1->Children.Functions[0];
653 EXPECT_EQ("GetFoo", Func1.Name);
654 ASSERT_TRUE(Func1.Template);
655 EXPECT_FALSE(Func1.Template->Specialization); // Not a specialization.
656
657 // Template parameter.
658 ASSERT_EQ(1u, Func1.Template->Params.size());
659 EXPECT_EQ("typename T = int", Func1.Template->Params[0].Contents);
660
661 // The second will be another global namespace with the function in it (the
662 // global namespace is duplicated because the items haven't been merged at the
663 // serialization phase of processing).
664 NamespaceInfo *GlobalNS2 = InfoAsNamespace(Infos[1].get());
665 ASSERT_EQ(1u, GlobalNS2->Children.Functions.size());
666
667 // This one is a template specialization.
668 const FunctionInfo &Func2 = GlobalNS2->Children.Functions[0];
669 EXPECT_EQ("GetFoo", Func2.Name);
670 ASSERT_TRUE(Func2.Template);
671 EXPECT_TRUE(Func2.Template->Params.empty()); // No template params.
672 ASSERT_TRUE(Func2.Template->Specialization);
673
674 // Specialization values.
675 ASSERT_EQ(1u, Func2.Template->Specialization->Params.size());
676 EXPECT_EQ("bool", Func2.Template->Specialization->Params[0].Contents);
677 EXPECT_EQ(Func1.USR, Func2.Template->Specialization->SpecializationOf);
678
679 EXPECT_EQ("bool", Func2.ReturnType.Type.Name);
680 }
681
682 TEST_F(SerializeTest, emitClassTemplate) {
683 EmittedInfoList Infos;
684 // This will generate 2x the number of infos: each Record will be followed by
685 // a copy of the global namespace containing it (this test checks the data
686 // pre-merge).
687 extractInfosFromCode(
688 "template<int I> class MyTemplate { int i[I]; };\n"
689 "template<> class MyTemplate<0> {};\n"
690 "template<typename T, int U = 1> class OtherTemplate {};\n"
691 "template<int U> class OtherTemplate<MyTemplate<0>, U> {};",
692 8,
693 /*Public=*/false, Infos, this->Diags);
694
695 // First record.
696 const RecordInfo *Rec1 = InfoAsRecord(Infos[0].get());
697 EXPECT_EQ("MyTemplate", Rec1->Name);
698 ASSERT_TRUE(Rec1->Template);
699 EXPECT_FALSE(Rec1->Template->Specialization); // Not a specialization.
700
701 // First record template parameter.
702 ASSERT_EQ(1u, Rec1->Template->Params.size());
703 EXPECT_EQ("int I", Rec1->Template->Params[0].Contents);
704
705 // Second record.
706 const RecordInfo *Rec2 = InfoAsRecord(Infos[2].get());
707 EXPECT_EQ("MyTemplate", Rec2->Name);
708 ASSERT_TRUE(Rec2->Template);
709 EXPECT_TRUE(Rec2->Template->Params.empty()); // No template params.
710 ASSERT_TRUE(Rec2->Template->Specialization);
711
712 // Second record specialization values.
713 ASSERT_EQ(1u, Rec2->Template->Specialization->Params.size());
714 EXPECT_EQ("0", Rec2->Template->Specialization->Params[0].Contents);
715 EXPECT_EQ(Rec1->USR, Rec2->Template->Specialization->SpecializationOf);
716
717 // Third record.
718 const RecordInfo *Rec3 = InfoAsRecord(Infos[4].get());
719 EXPECT_EQ("OtherTemplate", Rec3->Name);
720 ASSERT_TRUE(Rec3->Template);
721
722 // Third record template parameters.
723 ASSERT_EQ(2u, Rec3->Template->Params.size());
724 EXPECT_EQ("typename T", Rec3->Template->Params[0].Contents);
725 EXPECT_EQ("int U = 1", Rec3->Template->Params[1].Contents);
726
727 // Fourth record.
728 const RecordInfo *Rec4 = InfoAsRecord(Infos[6].get());
729 EXPECT_EQ("OtherTemplate", Rec3->Name);
730 ASSERT_TRUE(Rec4->Template);
731 ASSERT_TRUE(Rec4->Template->Specialization);
732
733 // Fourth record template + specialization parameters.
734 ASSERT_EQ(1u, Rec4->Template->Params.size());
735 EXPECT_EQ("int U", Rec4->Template->Params[0].Contents);
736 ASSERT_EQ(2u, Rec4->Template->Specialization->Params.size());
737 EXPECT_EQ("MyTemplate<0>",
738 Rec4->Template->Specialization->Params[0].Contents);
739 EXPECT_EQ("U", Rec4->Template->Specialization->Params[1].Contents);
740 }
741
742 } // namespace doc
743 } // end namespace clang