[clang-tools-extra] af8b0cd - [clang][Index] Visit the default parameter arguements in libindex.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 17 07:47:45 PST 2020
Author: Haojian Wu
Date: 2020-02-17T16:45:04+01:00
New Revision: af8b0cd58dbda773a0cbbb2a9c67559fc6a59d1d
URL: https://github.com/llvm/llvm-project/commit/af8b0cd58dbda773a0cbbb2a9c67559fc6a59d1d
DIFF: https://github.com/llvm/llvm-project/commit/af8b0cd58dbda773a0cbbb2a9c67559fc6a59d1d.diff
LOG: [clang][Index] Visit the default parameter arguements in libindex.
Summary:
We are missing the default parmeter arguments when IndexFunctionLocals
is true.
Fixes https://github.com/clangd/clangd/issues/285.
Reviewers: kadircet
Subscribers: kristof.beyls, ilya-biryukov, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74610
Added:
Modified:
clang-tools-extra/clangd/unittests/XRefsTests.cpp
clang/lib/Index/IndexDecl.cpp
clang/unittests/Index/IndexTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 226213979b8b..b75c218d143f 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -943,6 +943,11 @@ TEST(FindReferences, WithinAST) {
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
index c59b1372e399..2002c695a9b1 100644
--- a/clang/lib/Index/IndexDecl.cpp
+++ b/clang/lib/Index/IndexDecl.cpp
@@ -90,6 +90,12 @@ class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
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<ParmVarDecl>(D)) {
auto *DC = Parm->getDeclContext();
@@ -106,7 +112,8 @@ class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(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 +123,7 @@ class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
if (const auto *FD = dyn_cast<FunctionDecl>(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
index 068b30ebfa8a..52744e101f92 100644
--- a/clang/unittests/Index/IndexTests.cpp
+++ b/clang/unittests/Index/IndexTests.cpp
@@ -319,6 +319,21 @@ TEST(IndexTest, InjecatedNameClass) {
WrittenAt(Position(4, 14)))));
}
+TEST(IndexTest, VisitDefaultArgs) {
+ std::string Code = R"cpp(
+ int var = 0;
+ void f(int s = var) {}
+ )cpp";
+ auto Index = std::make_shared<Indexer>();
+ IndexingOptions Opts;
+ Opts.IndexFunctionLocals = true;
+ Opts.IndexParametersInDeclarations = true;
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
+ EXPECT_THAT(Index->Symbols,
+ Contains(AllOf(QName("var"), HasRole(SymbolRole::Reference),
+ WrittenAt(Position(3, 20)))));
+}
+
} // namespace
} // namespace index
} // namespace clang
More information about the cfe-commits
mailing list