This is an archive of the discontinued LLVM Phabricator instance.

[clangd] When finding refs for a renaming alias, do not return refs to underlying decls
ClosedPublic

Authored by nridge on Sep 6 2020, 11:29 PM.

Diff Detail

Event Timeline

nridge created this revision.Sep 6 2020, 11:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 6 2020, 11:29 PM
nridge requested review of this revision.Sep 6 2020, 11:29 PM
hokein added inline comments.Sep 8 2020, 12:40 AM
clang-tools-extra/clangd/XRefs.cpp
1142–1143

IIRC, clangd's index doesn't distinguish refs of template specifications/primary templates, all refs are treated as primary templates -- if we query refs for a template specification from the index, we'll get no results, so we will end up with refs from the main file (which are from the AST) only.

clang-tools-extra/clangd/unittests/XRefsTests.cpp
1574

hmm, the new behavior seems wired (at least in this case), I thought it is a bug at first glance, then I realized that this is xrefs for template specification. I'd expect to see the primary template in refs in xrefs result.

however if there is an explicit template specification, excluding the primary template maybe reasonable.

template <typename T>
class Foo {};
template <> class [[Foo]]<int> {};
void func([[Fo^o]]<int>);
nridge updated this revision to Diff 293047.Sep 20 2020, 6:12 PM

Take a different fix approach, as suggested in the issue discussion

nridge retitled this revision from [clangd] When finding refs for a template specialization, do not return refs to other specializations to [clangd] When finding refs for a renaming alias, do not return refs to underlying decls.Sep 20 2020, 6:13 PM
hokein added inline comments.Sep 25 2020, 6:42 AM
clang-tools-extra/clangd/XRefs.cpp
1150

I think it should not happen in practice (Decls just have 1 element in most cases), but the code feels hacky, we are throwing other decls if one of the Decls is a using decl.

I suppose if we're using the same workaround as locateASTReferent, then we should follow that way by only adjusting the UsingDecl/UnresolvedValueDecl results and keeping others.

In general, I think we probably need to remove this workaround (see the FIXME in locateASTReferent) by refining TargetDecl API. The current DeclRelation::Underlying enum is not enough to support our use case where we only want underlying decls for *non-renaming* alias. One rough idea to fix it is to split the Underlying to two RenameAliasUnderlying and RemainingUnderlying -- this would need some API design work, so no need to do it in this patch.

nridge added inline comments.Sep 25 2020, 8:24 AM
clang-tools-extra/clangd/XRefs.cpp
1150

I don't think we're actually throwing out the other results: by calling getDeclAtPosition() with Relations | Underlying, where Relations is the original flags, the call should find the other results again. If we only replaced the UsingDecl/UnresolvedValueDecl results, I think the other results would appear in duplicate.

nridge updated this revision to Diff 294580.Sep 27 2020, 5:40 PM

Address review comment

nridge marked 3 inline comments as done.Sep 27 2020, 5:41 PM
nridge added inline comments.
clang-tools-extra/clangd/XRefs.cpp
1150

Anyways, I've revised it as you suggested. I don't think it should make a difference in practice.

hokein accepted this revision.Sep 27 2020, 11:47 PM

thanks.

clang-tools-extra/clangd/XRefs.cpp
1149

nit: name it NonrenamingAliasUnderlyingDecls?

1157

I think the break is not necessarily needed. In practice we should just have one non-renaming alias at the current position. If not, that probably means we likely have a bug, break seems to just hide the bug.

This revision is now accepted and ready to land.Sep 27 2020, 11:47 PM
nridge updated this revision to Diff 294851.Sep 28 2020, 6:17 PM
nridge marked 2 inline comments as done.

Address final review comments

clang-tools-extra/clangd/XRefs.cpp
1157

Agreed, the break was left over from the previous approach and shouldn't be there any more.

This revision was landed with ongoing or failed builds.Sep 28 2020, 6:18 PM
This revision was automatically updated to reflect the committed changes.