1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
|
//===-- TypeHierarchyTests.cpp ---------------------------*- C++ -*-------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "Compiler.h"
#include "Matchers.h"
#include "ParsedAST.h"
#include "SyncAPI.h"
#include "TestFS.h"
#include "TestTU.h"
#include "XRefs.h"
#include "index/FileIndex.h"
#include "index/SymbolCollector.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Index/IndexingAction.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/ScopedPrinter.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Matcher;
using ::testing::UnorderedElementsAre;
// GMock helpers for matching TypeHierarchyItem.
MATCHER_P(WithName, N, "") { return arg.name == N; }
MATCHER_P(WithKind, Kind, "") { return arg.kind == Kind; }
MATCHER_P(SelectionRangeIs, R, "") { return arg.selectionRange == R; }
template <class... ParentMatchers>
::testing::Matcher<TypeHierarchyItem> Parents(ParentMatchers... ParentsM) {
return Field(&TypeHierarchyItem::parents,
HasValue(UnorderedElementsAre(ParentsM...)));
}
template <class... ChildMatchers>
::testing::Matcher<TypeHierarchyItem> Children(ChildMatchers... ChildrenM) {
return Field(&TypeHierarchyItem::children,
HasValue(UnorderedElementsAre(ChildrenM...)));
}
// Note: "not resolved" is different from "resolved but empty"!
MATCHER(ParentsNotResolved, "") { return !arg.parents; }
MATCHER(ChildrenNotResolved, "") { return !arg.children; }
TEST(FindRecordTypeAt, TypeOrVariable) {
Annotations Source(R"cpp(
struct Ch^ild2 {
int c;
};
using A^lias = Child2;
int main() {
Ch^ild2 ch^ild2;
ch^ild2.c = 1;
}
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
for (Position Pt : Source.points()) {
const CXXRecordDecl *RD = findRecordTypeAt(AST, Pt);
EXPECT_EQ(&findDecl(AST, "Child2"), static_cast<const NamedDecl *>(RD));
}
}
TEST(FindRecordTypeAt, Method) {
Annotations Source(R"cpp(
struct Child2 {
void met^hod ();
void met^hod (int x);
};
int main() {
Child2 child2;
child2.met^hod(5);
}
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
for (Position Pt : Source.points()) {
const CXXRecordDecl *RD = findRecordTypeAt(AST, Pt);
EXPECT_EQ(&findDecl(AST, "Child2"), static_cast<const NamedDecl *>(RD));
}
}
TEST(FindRecordTypeAt, Field) {
Annotations Source(R"cpp(
struct Child2 {
int fi^eld;
};
int main() {
Child2 child2;
child2.fi^eld = 5;
}
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
for (Position Pt : Source.points()) {
const CXXRecordDecl *RD = findRecordTypeAt(AST, Pt);
// A field does not unambiguously specify a record type
// (possible associated reocrd types could be the field's type,
// or the type of the record that the field is a member of).
EXPECT_EQ(nullptr, RD);
}
}
TEST(TypeParents, SimpleInheritance) {
Annotations Source(R"cpp(
struct Parent {
int a;
};
struct Child1 : Parent {
int b;
};
struct Child2 : Child1 {
int c;
};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Parent =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Parent"));
const CXXRecordDecl *Child1 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Child1"));
const CXXRecordDecl *Child2 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Child2"));
EXPECT_THAT(typeParents(Parent), ElementsAre());
EXPECT_THAT(typeParents(Child1), ElementsAre(Parent));
EXPECT_THAT(typeParents(Child2), ElementsAre(Child1));
}
TEST(TypeParents, MultipleInheritance) {
Annotations Source(R"cpp(
struct Parent1 {
int a;
};
struct Parent2 {
int b;
};
struct Parent3 : Parent2 {
int c;
};
struct Child : Parent1, Parent3 {
int d;
};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Parent1 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Parent1"));
const CXXRecordDecl *Parent2 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Parent2"));
const CXXRecordDecl *Parent3 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Parent3"));
const CXXRecordDecl *Child = dyn_cast<CXXRecordDecl>(&findDecl(AST, "Child"));
EXPECT_THAT(typeParents(Parent1), ElementsAre());
EXPECT_THAT(typeParents(Parent2), ElementsAre());
EXPECT_THAT(typeParents(Parent3), ElementsAre(Parent2));
EXPECT_THAT(typeParents(Child), ElementsAre(Parent1, Parent3));
}
TEST(TypeParents, ClassTemplate) {
Annotations Source(R"cpp(
struct Parent {};
template <typename T>
struct Child : Parent {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Parent =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Parent"));
const CXXRecordDecl *Child =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Child"))->getTemplatedDecl();
EXPECT_THAT(typeParents(Child), ElementsAre(Parent));
}
MATCHER_P(ImplicitSpecOf, ClassTemplate, "") {
const ClassTemplateSpecializationDecl *CTS =
dyn_cast<ClassTemplateSpecializationDecl>(arg);
return CTS &&
CTS->getSpecializedTemplate()->getTemplatedDecl() == ClassTemplate &&
CTS->getSpecializationKind() == TSK_ImplicitInstantiation;
}
// This is similar to findDecl(AST, QName), but supports using
// a template-id as a query.
const NamedDecl &findDeclWithTemplateArgs(ParsedAST &AST,
llvm::StringRef Query) {
return findDecl(AST, [&Query](const NamedDecl &ND) {
std::string QName;
llvm::raw_string_ostream OS(QName);
PrintingPolicy Policy(ND.getASTContext().getLangOpts());
// Use getNameForDiagnostic() which includes the template
// arguments in the printed name.
ND.getNameForDiagnostic(OS, Policy, /*Qualified=*/true);
OS.flush();
return QName == Query;
});
}
TEST(TypeParents, TemplateSpec1) {
Annotations Source(R"cpp(
template <typename T>
struct Parent {};
template <>
struct Parent<int> {};
struct Child1 : Parent<float> {};
struct Child2 : Parent<int> {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Parent =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Parent"))->getTemplatedDecl();
const CXXRecordDecl *ParentSpec =
dyn_cast<CXXRecordDecl>(&findDeclWithTemplateArgs(AST, "Parent<int>"));
const CXXRecordDecl *Child1 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Child1"));
const CXXRecordDecl *Child2 =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Child2"));
EXPECT_THAT(typeParents(Child1), ElementsAre(ImplicitSpecOf(Parent)));
EXPECT_THAT(typeParents(Child2), ElementsAre(ParentSpec));
}
TEST(TypeParents, TemplateSpec2) {
Annotations Source(R"cpp(
struct Parent {};
template <typename T>
struct Child {};
template <>
struct Child<int> : Parent {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Parent =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Parent"));
const CXXRecordDecl *Child =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Child"))->getTemplatedDecl();
const CXXRecordDecl *ChildSpec =
dyn_cast<CXXRecordDecl>(&findDeclWithTemplateArgs(AST, "Child<int>"));
EXPECT_THAT(typeParents(Child), ElementsAre());
EXPECT_THAT(typeParents(ChildSpec), ElementsAre(Parent));
}
TEST(TypeParents, DependentBase) {
Annotations Source(R"cpp(
template <typename T>
struct Parent {};
template <typename T>
struct Child1 : Parent<T> {};
template <typename T>
struct Child2 : Parent<T>::Type {};
template <typename T>
struct Child3 : T {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Parent =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Parent"))->getTemplatedDecl();
const CXXRecordDecl *Child1 =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Child1"))->getTemplatedDecl();
const CXXRecordDecl *Child2 =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Child2"))->getTemplatedDecl();
const CXXRecordDecl *Child3 =
dyn_cast<ClassTemplateDecl>(&findDecl(AST, "Child3"))->getTemplatedDecl();
// For "Parent<T>", use the primary template as a best-effort guess.
EXPECT_THAT(typeParents(Child1), ElementsAre(Parent));
// For "Parent<T>::Type", there is nothing we can do.
EXPECT_THAT(typeParents(Child2), ElementsAre());
// Likewise for "T".
EXPECT_THAT(typeParents(Child3), ElementsAre());
}
TEST(TypeParents, IncompleteClass) {
Annotations Source(R"cpp(
class Incomplete;
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Incomplete =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Incomplete"));
EXPECT_THAT(typeParents(Incomplete), IsEmpty());
}
// Parts of getTypeHierarchy() are tested in more detail by the
// FindRecordTypeAt.* and TypeParents.* tests above. This test exercises the
// entire operation.
TEST(TypeHierarchy, Parents) {
Annotations Source(R"cpp(
struct $Parent1Def[[Parent1]] {
int a;
};
struct $Parent2Def[[Parent2]] {
int b;
};
struct $Parent3Def[[Parent3]] : Parent2 {
int c;
};
struct Ch^ild : Parent1, Parent3 {
int d;
};
int main() {
Ch^ild ch^ild;
ch^ild.a = 1;
}
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
for (Position Pt : Source.points()) {
// Set ResolveLevels to 0 because it's only used for Children;
// for Parents, getTypeHierarchy() always returns all levels.
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Pt, /*ResolveLevels=*/0, TypeHierarchyDirection::Parents);
ASSERT_TRUE(bool(Result));
EXPECT_THAT(
*Result,
AllOf(
WithName("Child"), WithKind(SymbolKind::Struct),
Parents(AllOf(WithName("Parent1"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("Parent1Def")),
Parents()),
AllOf(WithName("Parent3"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("Parent3Def")),
Parents(AllOf(
WithName("Parent2"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("Parent2Def")),
Parents()))))));
}
}
TEST(TypeHierarchy, RecursiveHierarchyUnbounded) {
Annotations Source(R"cpp(
template <int N>
struct $SDef[[S]] : S<N + 1> {};
S^<0> s; // error-ok
)cpp");
TestTU TU = TestTU::withCode(Source.code());
TU.ExtraArgs.push_back("-ftemplate-depth=10");
auto AST = TU.build();
// The compiler should produce a diagnostic for hitting the
// template instantiation depth.
ASSERT_TRUE(!AST.getDiagnostics()->empty());
// Make sure getTypeHierarchy() doesn't get into an infinite recursion.
// The parent is reported as "S" because "S<0>" is an invalid instantiation.
// We then iterate once more and find "S" again before detecting the
// recursion.
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Source.points()[0], 0, TypeHierarchyDirection::Parents);
ASSERT_TRUE(bool(Result));
EXPECT_THAT(
*Result,
AllOf(WithName("S<0>"), WithKind(SymbolKind::Struct),
Parents(
AllOf(WithName("S"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("SDef")),
Parents(AllOf(WithName("S"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("SDef")),
Parents()))))));
}
TEST(TypeHierarchy, RecursiveHierarchyBounded) {
Annotations Source(R"cpp(
template <int N>
struct $SDef[[S]] : S<N - 1> {};
template <>
struct S<0>{};
S$SRefConcrete^<2> s;
template <int N>
struct Foo {
S$SRefDependent^<N> s;
};)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
// Make sure getTypeHierarchy() doesn't get into an infinite recursion
// for either a concrete starting point or a dependent starting point.
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Source.point("SRefConcrete"), 0, TypeHierarchyDirection::Parents);
ASSERT_TRUE(bool(Result));
EXPECT_THAT(
*Result,
AllOf(WithName("S<2>"), WithKind(SymbolKind::Struct),
Parents(AllOf(
WithName("S<1>"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("SDef")),
Parents(AllOf(WithName("S<0>"), WithKind(SymbolKind::Struct),
Parents()))))));
Result = getTypeHierarchy(AST, Source.point("SRefDependent"), 0,
TypeHierarchyDirection::Parents);
ASSERT_TRUE(bool(Result));
EXPECT_THAT(
*Result,
AllOf(WithName("S"), WithKind(SymbolKind::Struct),
Parents(AllOf(WithName("S"), WithKind(SymbolKind::Struct),
SelectionRangeIs(Source.range("SDef")), Parents()))));
}
TEST(TypeHierarchy, DeriveFromImplicitSpec) {
Annotations Source(R"cpp(
template <typename T>
struct Parent {};
struct Child1 : Parent<int> {};
struct Child2 : Parent<char> {};
Parent<int> Fo^o;
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
auto Index = TU.index();
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Source.points()[0], 2, TypeHierarchyDirection::Children, Index.get(),
testPath(TU.Filename));
ASSERT_TRUE(bool(Result));
EXPECT_THAT(*Result,
AllOf(WithName("Parent"), WithKind(SymbolKind::Struct),
Children(AllOf(WithName("Child1"),
WithKind(SymbolKind::Struct), Children()),
AllOf(WithName("Child2"),
WithKind(SymbolKind::Struct), Children()))));
}
TEST(TypeHierarchy, DeriveFromPartialSpec) {
Annotations Source(R"cpp(
template <typename T> struct Parent {};
template <typename T> struct Parent<T*> {};
struct Child : Parent<int*> {};
Parent<int> Fo^o;
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
auto Index = TU.index();
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Source.points()[0], 2, TypeHierarchyDirection::Children, Index.get(),
testPath(TU.Filename));
ASSERT_TRUE(bool(Result));
EXPECT_THAT(*Result, AllOf(WithName("Parent"), WithKind(SymbolKind::Struct),
Children()));
}
TEST(TypeHierarchy, DeriveFromTemplate) {
Annotations Source(R"cpp(
template <typename T>
struct Parent {};
template <typename T>
struct Child : Parent<T> {};
Parent<int> Fo^o;
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
auto Index = TU.index();
// FIXME: We'd like this to show the implicit specializations Parent<int>
// and Child<int>, but currently libIndex does not expose relationships
// between implicit specializations.
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Source.points()[0], 2, TypeHierarchyDirection::Children, Index.get(),
testPath(TU.Filename));
ASSERT_TRUE(bool(Result));
EXPECT_THAT(*Result,
AllOf(WithName("Parent"), WithKind(SymbolKind::Struct),
Children(AllOf(WithName("Child"),
WithKind(SymbolKind::Struct), Children()))));
}
TEST(TypeHierarchy, Preamble) {
Annotations SourceAnnotations(R"cpp(
struct Ch^ild : Parent {
int b;
};)cpp");
Annotations HeaderInPreambleAnnotations(R"cpp(
struct [[Parent]] {
int a;
};)cpp");
TestTU TU = TestTU::withCode(SourceAnnotations.code());
TU.HeaderCode = HeaderInPreambleAnnotations.code().str();
auto AST = TU.build();
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, SourceAnnotations.point(), 1, TypeHierarchyDirection::Parents);
ASSERT_TRUE(Result);
EXPECT_THAT(
*Result,
AllOf(WithName("Child"),
Parents(AllOf(WithName("Parent"),
SelectionRangeIs(HeaderInPreambleAnnotations.range()),
Parents()))));
}
SymbolID findSymbolIDByName(SymbolIndex *Index, llvm::StringRef Name,
llvm::StringRef TemplateArgs = "") {
SymbolID Result;
FuzzyFindRequest Request;
Request.Query = std::string(Name);
Request.AnyScope = true;
bool GotResult = false;
Index->fuzzyFind(Request, [&](const Symbol &S) {
if (TemplateArgs == S.TemplateSpecializationArgs) {
EXPECT_FALSE(GotResult);
Result = S.ID;
GotResult = true;
}
});
EXPECT_TRUE(GotResult);
return Result;
}
std::vector<SymbolID> collectSubtypes(SymbolID Subject, SymbolIndex *Index) {
std::vector<SymbolID> Result;
RelationsRequest Req;
Req.Subjects.insert(Subject);
Req.Predicate = RelationKind::BaseOf;
Index->relations(Req,
[&Result](const SymbolID &Subject, const Symbol &Object) {
Result.push_back(Object.ID);
});
return Result;
}
TEST(Subtypes, SimpleInheritance) {
Annotations Source(R"cpp(
struct Parent {};
struct Child1a : Parent {};
struct Child1b : Parent {};
struct Child2 : Child1a {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto Index = TU.index();
SymbolID Parent = findSymbolIDByName(Index.get(), "Parent");
SymbolID Child1a = findSymbolIDByName(Index.get(), "Child1a");
SymbolID Child1b = findSymbolIDByName(Index.get(), "Child1b");
SymbolID Child2 = findSymbolIDByName(Index.get(), "Child2");
EXPECT_THAT(collectSubtypes(Parent, Index.get()),
UnorderedElementsAre(Child1a, Child1b));
EXPECT_THAT(collectSubtypes(Child1a, Index.get()), ElementsAre(Child2));
}
TEST(Subtypes, MultipleInheritance) {
Annotations Source(R"cpp(
struct Parent1 {};
struct Parent2 {};
struct Parent3 : Parent2 {};
struct Child : Parent1, Parent3 {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto Index = TU.index();
SymbolID Parent1 = findSymbolIDByName(Index.get(), "Parent1");
SymbolID Parent2 = findSymbolIDByName(Index.get(), "Parent2");
SymbolID Parent3 = findSymbolIDByName(Index.get(), "Parent3");
SymbolID Child = findSymbolIDByName(Index.get(), "Child");
EXPECT_THAT(collectSubtypes(Parent1, Index.get()), ElementsAre(Child));
EXPECT_THAT(collectSubtypes(Parent2, Index.get()), ElementsAre(Parent3));
EXPECT_THAT(collectSubtypes(Parent3, Index.get()), ElementsAre(Child));
}
TEST(Subtypes, ClassTemplate) {
Annotations Source(R"cpp(
struct Parent {};
template <typename T>
struct Child : Parent {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto Index = TU.index();
SymbolID Parent = findSymbolIDByName(Index.get(), "Parent");
SymbolID Child = findSymbolIDByName(Index.get(), "Child");
EXPECT_THAT(collectSubtypes(Parent, Index.get()), ElementsAre(Child));
}
TEST(Subtypes, TemplateSpec1) {
Annotations Source(R"cpp(
template <typename T>
struct Parent {};
template <>
struct Parent<int> {};
struct Child1 : Parent<float> {};
struct Child2 : Parent<int> {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto Index = TU.index();
SymbolID Parent = findSymbolIDByName(Index.get(), "Parent");
SymbolID ParentSpec = findSymbolIDByName(Index.get(), "Parent", "<int>");
SymbolID Child1 = findSymbolIDByName(Index.get(), "Child1");
SymbolID Child2 = findSymbolIDByName(Index.get(), "Child2");
EXPECT_THAT(collectSubtypes(Parent, Index.get()), ElementsAre(Child1));
EXPECT_THAT(collectSubtypes(ParentSpec, Index.get()), ElementsAre(Child2));
}
TEST(Subtypes, TemplateSpec2) {
Annotations Source(R"cpp(
struct Parent {};
template <typename T>
struct Child {};
template <>
struct Child<int> : Parent {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto Index = TU.index();
SymbolID Parent = findSymbolIDByName(Index.get(), "Parent");
SymbolID ChildSpec = findSymbolIDByName(Index.get(), "Child", "<int>");
EXPECT_THAT(collectSubtypes(Parent, Index.get()), ElementsAre(ChildSpec));
}
TEST(Subtypes, DependentBase) {
Annotations Source(R"cpp(
template <typename T>
struct Parent {};
template <typename T>
struct Child : Parent<T> {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto Index = TU.index();
SymbolID Parent = findSymbolIDByName(Index.get(), "Parent");
SymbolID Child = findSymbolIDByName(Index.get(), "Child");
EXPECT_THAT(collectSubtypes(Parent, Index.get()), ElementsAre(Child));
}
TEST(Subtypes, LazyResolution) {
Annotations Source(R"cpp(
struct P^arent {};
struct Child1 : Parent {};
struct Child2a : Child1 {};
struct Child2b : Child1 {};
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
auto Index = TU.index();
llvm::Optional<TypeHierarchyItem> Result = getTypeHierarchy(
AST, Source.point(), /*ResolveLevels=*/1,
TypeHierarchyDirection::Children, Index.get(), testPath(TU.Filename));
ASSERT_TRUE(bool(Result));
EXPECT_THAT(
*Result,
AllOf(WithName("Parent"), WithKind(SymbolKind::Struct),
ParentsNotResolved(),
Children(AllOf(WithName("Child1"), WithKind(SymbolKind::Struct),
ParentsNotResolved(), ChildrenNotResolved()))));
resolveTypeHierarchy((*Result->children)[0], /*ResolveLevels=*/1,
TypeHierarchyDirection::Children, Index.get());
EXPECT_THAT(
(*Result->children)[0],
AllOf(WithName("Child1"), WithKind(SymbolKind::Struct),
ParentsNotResolved(),
Children(AllOf(WithName("Child2a"), WithKind(SymbolKind::Struct),
ParentsNotResolved(), ChildrenNotResolved()),
AllOf(WithName("Child2b"), WithKind(SymbolKind::Struct),
ParentsNotResolved(), ChildrenNotResolved()))));
}
} // namespace
} // namespace clangd
} // namespace clang
|