diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -695,6 +695,13 @@ DeclRelation::Alias)}; } + void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { + Ref = ReferenceLoc{NestedNameSpecifierLoc(), + TL.getNameLoc(), + /*IsDecl=*/false, + {TL.getDecl()}}; + } + void VisitDependentTemplateSpecializationTypeLoc( DependentTemplateSpecializationTypeLoc L) { Ref = ReferenceLoc{ diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -1,4 +1,4 @@ -//===-- FindSymbolsTests.cpp -------------------------*- C++ -*------------===// +//===-- FindTargetTests.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. @@ -553,8 +553,8 @@ std::string DumpedReferences; }; - /// Parses \p Code, finds function '::foo' and annotates its body with results - /// of findExplicitReferecnces. + /// Parses \p Code, finds function or namespace '::foo' and annotates its body + /// with results of findExplicitReferecnces. /// See actual tests for examples of annotation format. AllRefs annotateReferencesInFoo(llvm::StringRef Code) { TestTU TU; @@ -574,12 +574,21 @@ auto *TestDecl = &findDecl(AST, "foo"); if (auto *T = llvm::dyn_cast(TestDecl)) TestDecl = T->getTemplatedDecl(); - auto &Func = llvm::cast(*TestDecl); std::vector Refs; - findExplicitReferences(Func.getBody(), [&Refs](ReferenceLoc R) { - Refs.push_back(std::move(R)); - }); + if (const auto *Func = llvm::dyn_cast(TestDecl)) + findExplicitReferences(Func->getBody(), [&Refs](ReferenceLoc R) { + Refs.push_back(std::move(R)); + }); + else if (const auto *NS = llvm::dyn_cast(TestDecl)) + findExplicitReferences(NS, [&Refs, &NS](ReferenceLoc R) { + // Avoid adding the namespace foo decl to the results. + if (R.Targets.size() == 1 && R.Targets.front() == NS) + return; + Refs.push_back(std::move(R)); + }); + else + ADD_FAILURE() << "Failed to find ::foo decl for test"; auto &SM = AST.getSourceManager(); llvm::sort(Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) { @@ -720,6 +729,22 @@ "1: targets = {vi}, decl\n" "2: targets = {valias}\n" "3: targets = {vb}, decl\n"}, + // Injected class name. + {R"cpp( + namespace foo { + template + class $1^$2^Bar { + ~$3^Bar(); + void $4^f($5^Bar); + }; + } + )cpp", + "0: targets = {foo::Bar::T}, decl\n" + "1: targets = {foo::Bar}, decl\n" + "2: targets = {foo::Bar}, decl\n" + "3: targets = {foo::Bar}\n" + "4: targets = {foo::Bar::f}, decl\n" + "5: targets = {foo::Bar}\n"}, // MemberExpr should know their using declaration. {R"cpp( struct X { void func(int); }; diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -525,7 +525,7 @@ using $Typedef[[LVReference]] = $TemplateParameter[[T]] &; using $Typedef[[RVReference]] = $TemplateParameter[[T]]&&; using $Typedef[[Array]] = $TemplateParameter[[T]]*[3]; - using $Typedef[[MemberPointer]] = int (A::*)(int); + using $Typedef[[MemberPointer]] = int ($Class[[A]]::*)(int); // Use various previously defined typedefs in a function type. void $Method[[func]](