This is an archive of the discontinued LLVM Phabricator instance.

[index][clangd] Consider labels when indexing function bodies
ClosedPublic

Authored by ckandeler on May 8 2023, 8:45 AM.

Details

Summary

It's valuable to have document highlights for labels and be able to find
references to them.

Diff Detail

Event Timeline

ckandeler created this revision.May 8 2023, 8:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 8 2023, 8:45 AM
ckandeler requested review of this revision.May 8 2023, 8:45 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 8 2023, 8:45 AM

Thanks! This looks like a useful addition.

Could you add a test into libIndex for this too? Just for the sake of having better coverage, some folks probably don't run clang-tools-extra tests.
Also, could we add a test that a label does not get indexed if local function indexing is off?

clang/lib/Index/IndexBody.cpp
148

IndexingContext::handleReference already has this check, we should call handleReference unconditionally.

158

We should call handleDecl, indexing output can differentiate between a declarations and a references.
LabelStmt is exactly the node that declares a LabelDecl.

In clangd we also have findExplicitReferences and targetDecl functions that seem to handle the GoToStmt.
In fact, I believe they are used in findDocumentHighlights, so I'm not sure why your test did not work before this patch.

I have also not looked into libIndex for quite a long time, so looping in @kadircet for a second pair of eyes.

In clangd we also have findExplicitReferences and targetDecl functions that seem to handle the GoToStmt.

According to the commit message of the patch that added this, it was for "go to definition" functionality (which did indeed work already).

ckandeler updated this revision to Diff 521577.May 12 2023, 1:09 AM
ckandeler marked 2 inline comments as done.

Addressed review comments.

nridge added a subscriber: nridge.May 13 2023, 2:49 PM

In clangd we also have findExplicitReferences and targetDecl functions that seem to handle the GoToStmt.
In fact, I believe they are used in findDocumentHighlights, so I'm not sure why your test did not work before this patch.
findDocumentHighlights

I can explain this part: findDocumentHighlights uses targetDecl to associate the input cursor position with a symbol, but then it uses findRefs (which uses libIndex) to locate other usages of the symbol in the file.

clang/lib/Index/IndexBody.cpp
150

NameReference was introduced in https://github.com/llvm/llvm-project/commit/e7eb27a9a0edd859de49bcc9af7ca27dbb435886 to handle the somewhat unique situation with constructors and destructors where the constructor/destructor references the class by name but semantically denotes a separate entity.

Why is that applicable here?

Note that handleReference() will automatically add SymbolRole::Reference here.

clang/unittests/Index/IndexTests.cpp
233

I think the outer AllOf() here is a no-op, since it has only one argument. (AllOf(x, y, z) means "each of the matchers x, y, and z match").

ckandeler updated this revision to Diff 545054.Jul 28 2023, 1:26 AM

Addressed review comment.

ckandeler marked an inline comment as done.Jul 28 2023, 1:26 AM

sorry for missing this review. Nathan's explanation around targetDecl vs findRefs is absolutely right (btw, thanks a lot Nathan for taking good care of clangd, I don't think I say that enough). hopefully one day we can switch clangd's usage of libindex to our internal find target/explicitrefs implementation, but until then we actually need to address such things in both places to make sure functionality is actually consistent.

clang/lib/Index/IndexBody.cpp
150

+1 we shouldn't be setting namereference here.

ckandeler added inline comments.Jul 28 2023, 6:15 AM
clang/lib/Index/IndexBody.cpp
150

So set nothing at all here?

nridge added inline comments.Jul 28 2023, 7:34 PM
clang/lib/Index/IndexBody.cpp
150

Yes, I think we should pass a default constructed SymbolRoleSet() here, and handleReference() will add SymbolRole::Reference into it automatically.

btw, thanks a lot Nathan for taking good care of clangd, I don't think I say that enough

I'm happy to help!

ckandeler updated this revision to Diff 545551.Jul 31 2023, 2:27 AM

Addressed another review comment.

ckandeler marked an inline comment as done.Jul 31 2023, 2:27 AM
nridge accepted this revision.Jul 31 2023, 6:19 PM

LGTM

This revision is now accepted and ready to land.Jul 31 2023, 6:19 PM
This revision was landed with ongoing or failed builds.Aug 1 2023, 12:07 AM
This revision was automatically updated to reflect the committed changes.

Build failures seem unrelated.