This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Fix AddUsing tweak for out-of-line functions.
ClosedPublic

Authored by adamcz on May 6 2020, 8:35 AM.

Details

Summary

We used getEnclosingNamespaceContext(), which calls getParent() rather
than getLexicalParent(), so we would end up adding the "using" line in
places that do not affect the cursor location, or just return an error
when declaration was in another file.

Diff Detail

Event Timeline

adamcz created this revision.May 6 2020, 8:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2020, 8:35 AM
hokein accepted this revision.May 6 2020, 12:01 PM

thanks.

clang-tools-extra/clangd/unittests/TweakTests.cpp
2673

IIUC, before this patch, the using was added inside the above namespace foo, the code was still valid after applying the tweak.

There is no enclosing namespace for foo::fun(), so we will insert the using at the first top-level decl. It seems that we will break the code if the one::two::ff decl is defined in the main file (not the test.hpp), e.g.

// using one::two::ff will be inserted here, which is not valid
namespace one {
namespace two {
void ff();
}
}

namespace foo { void fun(); }
void foo::fun() {...}

this case was working before the patch, and now is broken after this patch. It is not a common case, probably ok for now.

This revision is now accepted and ready to land.May 6 2020, 12:01 PM
adamcz marked an inline comment as done.May 7 2020, 3:19 AM
adamcz added inline comments.
clang-tools-extra/clangd/unittests/TweakTests.cpp
2673

You are correct that this will not work. It is not a regression, however, since it was broken before too. Prior to this patch, the using would be inserted inside the namespace foo { }, so while the using statement would compile, it wouldn't affect the out-of-line definition of the function, meaning removing the qualifier would not work.

The insertion point logic will have to change a little when we add the feature to remove all other full qualifiers for that name. I think it might be best to address these corner cases at that point, but I can also do it here if you prefer.

hokein added inline comments.May 7 2020, 3:54 AM
clang-tools-extra/clangd/unittests/TweakTests.cpp
2673

You are correct that this will not work. It is not a regression, however, since it was broken before too. Prior to this patch, the using would be inserted inside the namespace foo { }, so while the using statement would compile, it wouldn't affect the out-of-line definition of the function, meaning removing the qualifier would not work.

hmm, that's interesting, the following code is compilable.

namespace one {
namespace two {
void ff();
}
}

namespace foo {
using one::two::ff;
void fun();
} // namespace foo
void foo::fun() { ff(); }

The insertion point logic will have to change a little when we add the feature to remove all other full qualifiers for that name. I think it might be best to address these corner cases at that point, but I can also do it here if you prefer.

agree to defer it, this is something we should keep in mind.

This revision was automatically updated to reflect the committed changes.