diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -943,6 +943,11 @@ int x = [[MACRO]]([[MACRO]](1)); } )cpp", + + R"cpp( + int [[v^ar]] = 0; + void foo(int s = [[var]]); + )cpp", }; for (const char *Test : Tests) { Annotations T(Test); diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp --- a/clang/lib/Index/IndexDecl.cpp +++ b/clang/lib/Index/IndexDecl.cpp @@ -90,9 +90,16 @@ Parent->getLexicalDeclContext(), /*isBase=*/false, isIBType); IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent); + auto IndexDefaultParmeterArgument = [&](const ParmVarDecl *Parm, + const NamedDecl *Parent) { + if (Parm->hasDefaultArg() && !Parm->hasUninstantiatedDefaultArg() && + !Parm->hasUnparsedDefaultArg()) + IndexCtx.indexBody(Parm->getDefaultArg(), Parent); + }; if (IndexCtx.shouldIndexFunctionLocalSymbols()) { if (const ParmVarDecl *Parm = dyn_cast(D)) { auto *DC = Parm->getDeclContext(); + IndexDefaultParmeterArgument(Parm, Parent); if (auto *FD = dyn_cast(DC)) { if (IndexCtx.shouldIndexParametersInDeclarations() || FD->isThisDeclarationADefinition()) @@ -106,7 +113,8 @@ } else if (const FunctionDecl *FD = dyn_cast(D)) { if (IndexCtx.shouldIndexParametersInDeclarations() || FD->isThisDeclarationADefinition()) { - for (auto PI : FD->parameters()) { + for (const auto *PI : FD->parameters()) { + IndexDefaultParmeterArgument(PI, D); IndexCtx.handleDecl(PI); } } @@ -116,9 +124,7 @@ if (const auto *FD = dyn_cast(D)) { if (FD->isThisDeclarationADefinition()) { for (const auto *PV : FD->parameters()) { - if (PV->hasDefaultArg() && !PV->hasUninstantiatedDefaultArg() && - !PV->hasUnparsedDefaultArg()) - IndexCtx.indexBody(PV->getDefaultArg(), D); + IndexDefaultParmeterArgument(PV, D); } } } diff --git a/clang/unittests/Index/IndexTests.cpp b/clang/unittests/Index/IndexTests.cpp --- a/clang/unittests/Index/IndexTests.cpp +++ b/clang/unittests/Index/IndexTests.cpp @@ -319,6 +319,21 @@ WrittenAt(Position(4, 14))))); } +TEST(IndexTest, VisitDefaultTemplateArgs) { + std::string Code = R"cpp( + int var = 0; + void f(int s = var) {} + )cpp"; + auto Index = std::make_shared(); + IndexingOptions Opts; + Opts.IndexFunctionLocals = true; + Opts.IndexParametersInDeclarations = true; + tooling::runToolOnCode(std::make_unique(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Contains(AllOf(QName("var"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 20))))); +} + } // namespace } // namespace index } // namespace clang