1- // ===-- FindSymbolsTests .cpp -------------------------*- C++ -*------------===//
1+ // ===-- FindTargetTests .cpp - -------------------------*- C++ -*------------===//
22//
33// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44// See https://llvm.org/LICENSE.txt for license information.
@@ -553,8 +553,8 @@ class FindExplicitReferencesTest : public ::testing::Test {
553553 std::string DumpedReferences;
554554 };
555555
556- // / Parses \p Code, finds function '::foo' and annotates its body with results
557- // / of findExplicitReferecnces.
556+ // / Parses \p Code, finds function or namespace '::foo' and annotates its body
557+ // / with results of findExplicitReferecnces.
558558 // / See actual tests for examples of annotation format.
559559 AllRefs annotateReferencesInFoo (llvm::StringRef Code) {
560560 TestTU TU;
@@ -574,12 +574,21 @@ class FindExplicitReferencesTest : public ::testing::Test {
574574 auto *TestDecl = &findDecl (AST, " foo" );
575575 if (auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
576576 TestDecl = T->getTemplatedDecl ();
577- auto &Func = llvm::cast<FunctionDecl>(*TestDecl);
578577
579578 std::vector<ReferenceLoc> Refs;
580- findExplicitReferences (Func.getBody (), [&Refs](ReferenceLoc R) {
581- Refs.push_back (std::move (R));
582- });
579+ if (const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
580+ findExplicitReferences (Func->getBody (), [&Refs](ReferenceLoc R) {
581+ Refs.push_back (std::move (R));
582+ });
583+ else if (const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
584+ findExplicitReferences (NS, [&Refs, &NS](ReferenceLoc R) {
585+ // Avoid adding the namespace foo decl to the results.
586+ if (R.Targets .size () == 1 && R.Targets .front () == NS)
587+ return ;
588+ Refs.push_back (std::move (R));
589+ });
590+ else
591+ ADD_FAILURE () << " Failed to find ::foo decl for test" ;
583592
584593 auto &SM = AST.getSourceManager ();
585594 llvm::sort (Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) {
@@ -720,6 +729,25 @@ TEST_F(FindExplicitReferencesTest, All) {
720729 " 1: targets = {vi}, decl\n "
721730 " 2: targets = {valias}\n "
722731 " 3: targets = {vb}, decl\n " },
732+ // Injected class name.
733+ {R"cpp(
734+ namespace foo {
735+ template <typename $0^T>
736+ class $1^$2^Bar {
737+ ~$3^Bar();
738+ void $4^f($5^Bar);
739+ };
740+ }
741+ )cpp" ,
742+ " 0: targets = {foo::Bar::T}, decl\n "
743+ // FIXME: avoid the 2 duplicated foo::Bar references below, the first
744+ // one comes from ClassTemplateDecl; the second comes from the
745+ // underlying CXXRecordDecl.
746+ " 1: targets = {foo::Bar}, decl\n "
747+ " 2: targets = {foo::Bar}, decl\n "
748+ " 3: targets = {foo::Bar}\n "
749+ " 4: targets = {foo::Bar::f}, decl\n "
750+ " 5: targets = {foo::Bar}\n " },
723751 // MemberExpr should know their using declaration.
724752 {R"cpp(
725753 struct X { void func(int); };
0 commit comments