[PATCH] D77717: [clang][index] index the missing LabelDecl in libindex.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 8 04:49:44 PDT 2020


hokein created this revision.
hokein added a reviewer: kadircet.
Herald added a subscriber: arphaman.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77717

Files:
  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())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77717.255958.patch
Type: text/x-patch
Size: 2106 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200408/be9b15dd/attachment.bin>


More information about the cfe-commits mailing list