This is an archive of the discontinued LLVM Phabricator instance.

[clangd]Fix addUsing tweak doesn't traverse same-level anon namespaces
Needs ReviewPublic

Authored by chouzz on Jul 27 2023, 8:33 AM.

Details

Summary

This patch provide fixes about addUsing tweak doesn't traverse same-level anon namespaces.

Fixes: https://github.com/clangd/clangd/issues/1572

Diff Detail

Event Timeline

chouzz created this revision.Jul 27 2023, 8:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 8:33 AM
Herald added a subscriber: arphaman. · View Herald Transcript
chouzz requested review of this revision.Jul 27 2023, 8:33 AM
chouzz added a comment.EditedAug 2 2023, 7:10 PM

Hi, @kadircet Could you please take a look this patch?

This would be a great fix to have! However I don't think the specific changes to the RecursiveASTVisitor are correct.

I can see a couple of approaches:

  1. prevent the RecursiveASTVisitor from traversing into uninteresting contexts, and drop the Encloses check in VisitUsingDecl
  2. explicitly gather the list of interesting contexts

option 1 is most similar to this patch, I believe TraverseNamespaceDecl needs to call base::TraverseNamespaceDecl only if the NS either encloses the selection DC or is anonymous.

option 2 would mean walking up the declcontext chain and grabbing each entry and also getAnonymousNamespace() from TranslationUnitDecl and NamespaceDecl, and changing our Encloses() tests to check membership in that set.

clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
97

This doesn't look like it can be right:

  • there's no distinction on whether the namespace is anonymous or not
  • you're calling VisitUsingDecl, but also Base::TraverseNamespaceDecl which will visit the contained using decls again

this also suggests we need more testcases here: some negative ones verifying that we don't look at UsingDecls that are inside named namespaces.