hokein updated this revision to Diff 256001. hokein added a comment. Herald added subscribers: usaxena95, jkorous.
add a clangd xref test. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D77717/new/ https://reviews.llvm.org/D77717 Files: clang-tools-extra/clangd/unittests/XRefsTests.cpp clang/lib/Index/IndexBody.cpp clang/unittests/Index/IndexTests.cpp Index: clang/unittests/Index/IndexTests.cpp =================================================================== --- clang/unittests/Index/IndexTests.cpp +++ clang/unittests/Index/IndexTests.cpp @@ -334,6 +334,28 @@ WrittenAt(Position(3, 20))))); } +TEST(IndexTest, LabelDecl) { + std::string Code = R"cpp( + void foo() { + label: + return; + } + )cpp"; + auto Index = std::make_shared<Indexer>(); + IndexingOptions Opts; + Opts.IndexFunctionLocals = true; + tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Contains(AllOf(QName("label"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 5))))); + Index->Symbols.clear(); + Opts.IndexFunctionLocals = false; + tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Not(Contains(AllOf(QName("label"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 5)))))); +} + } // namespace } // namespace index } // namespace clang Index: clang/lib/Index/IndexBody.cpp =================================================================== --- clang/lib/Index/IndexBody.cpp +++ clang/lib/Index/IndexBody.cpp @@ -140,6 +140,21 @@ Parent, ParentDC, Roles, Relations, E); } + bool VisitGotoStmt(GotoStmt *Goto) { + if (auto *LabelDecl = Goto->getLabel()) + IndexCtx.handleReference(LabelDecl, Goto->getLabelLoc(), Parent, ParentDC, + static_cast<unsigned>(SymbolRole::Reference)); + return true; + } + + bool VisitLabelStmt(LabelStmt *Label) { + if (auto *LabelDecl = Label->getDecl()) + IndexCtx.handleReference(LabelDecl, Label->getIdentLoc(), Parent, + ParentDC, + static_cast<unsigned>(SymbolRole::Reference)); + return true; + } + bool VisitMemberExpr(MemberExpr *E) { SourceLocation Loc = E->getMemberLoc(); if (Loc.isInvalid()) Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1083,6 +1083,14 @@ } )cpp", + R"cpp(// Goto Label. + void test() { + [[la^bel]]: + return; + goto [[label]]; + } + )cpp", + R"cpp( int [[v^ar]] = 0; void foo(int s = [[var]]);
Index: clang/unittests/Index/IndexTests.cpp =================================================================== --- clang/unittests/Index/IndexTests.cpp +++ clang/unittests/Index/IndexTests.cpp @@ -334,6 +334,28 @@ WrittenAt(Position(3, 20))))); } +TEST(IndexTest, LabelDecl) { + std::string Code = R"cpp( + void foo() { + label: + return; + } + )cpp"; + auto Index = std::make_shared<Indexer>(); + IndexingOptions Opts; + Opts.IndexFunctionLocals = true; + tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Contains(AllOf(QName("label"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 5))))); + Index->Symbols.clear(); + Opts.IndexFunctionLocals = false; + tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Not(Contains(AllOf(QName("label"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 5)))))); +} + } // namespace } // namespace index } // namespace clang Index: clang/lib/Index/IndexBody.cpp =================================================================== --- clang/lib/Index/IndexBody.cpp +++ clang/lib/Index/IndexBody.cpp @@ -140,6 +140,21 @@ Parent, ParentDC, Roles, Relations, E); } + bool VisitGotoStmt(GotoStmt *Goto) { + if (auto *LabelDecl = Goto->getLabel()) + IndexCtx.handleReference(LabelDecl, Goto->getLabelLoc(), Parent, ParentDC, + static_cast<unsigned>(SymbolRole::Reference)); + return true; + } + + bool VisitLabelStmt(LabelStmt *Label) { + if (auto *LabelDecl = Label->getDecl()) + IndexCtx.handleReference(LabelDecl, Label->getIdentLoc(), Parent, + ParentDC, + static_cast<unsigned>(SymbolRole::Reference)); + return true; + } + bool VisitMemberExpr(MemberExpr *E) { SourceLocation Loc = E->getMemberLoc(); if (Loc.isInvalid()) Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1083,6 +1083,14 @@ } )cpp", + R"cpp(// Goto Label. + void test() { + [[la^bel]]: + return; + goto [[label]]; + } + )cpp", + R"cpp( int [[v^ar]] = 0; void foo(int s = [[var]]);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits