diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp index 05cf891e71db4..6d7fd62016991 100644 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -125,6 +125,13 @@ TEST(HighlightsTest, All) { [Foo [[x]]:2 [[^y]]:4]; } )cpp", + R"cpp( // Label + int main() { + goto [[^theLabel]]; + [[theLabel]]: + return 1; + } + )cpp", }; for (const char *Test : Tests) { Annotations T(Test); diff --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp index e5f1764550ffa..e88f321f18a71 100644 --- a/clang/lib/Index/IndexBody.cpp +++ b/clang/lib/Index/IndexBody.cpp @@ -144,6 +144,17 @@ class BodyIndexer : public RecursiveASTVisitor { Parent, ParentDC, Roles, Relations, E); } + bool VisitGotoStmt(GotoStmt *S) { + return IndexCtx.handleReference(S->getLabel(), S->getLabelLoc(), Parent, + ParentDC); + } + + bool VisitLabelStmt(LabelStmt *S) { + if (IndexCtx.shouldIndexFunctionLocalSymbols()) + return IndexCtx.handleDecl(S->getDecl()); + return true; + } + bool VisitMemberExpr(MemberExpr *E) { SourceLocation Loc = E->getMemberLoc(); if (Loc.isInvalid()) diff --git a/clang/unittests/Index/IndexTests.cpp b/clang/unittests/Index/IndexTests.cpp index 690673a1072b0..4d19f47283c28 100644 --- a/clang/unittests/Index/IndexTests.cpp +++ b/clang/unittests/Index/IndexTests.cpp @@ -218,6 +218,28 @@ TEST(IndexTest, IndexParametersInDecls) { EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar")))); } +TEST(IndexTest, IndexLabels) { + std::string Code = R"cpp( + int main() { + goto theLabel; + theLabel: + return 1; + } + )cpp"; + auto Index = std::make_shared(); + IndexingOptions Opts; + Opts.IndexFunctionLocals = true; + tooling::runToolOnCode(std::make_unique(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Contains(AllOf(QName("theLabel"), WrittenAt(Position(3, 16)), + DeclAt(Position(4, 11))))); + + Opts.IndexFunctionLocals = false; + Index->Symbols.clear(); + tooling::runToolOnCode(std::make_unique(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, Not(Contains(QName("theLabel")))); +} + TEST(IndexTest, IndexExplicitTemplateInstantiation) { std::string Code = R"cpp( template