This is an archive of the discontinued LLVM Phabricator instance.

[clangd] AddUsing: Used spelled text instead of type name.
ClosedPublic

Authored by adamcz on Nov 23 2020, 7:19 AM.

Details

Summary

This improves the behavior related to type aliases, as well as cases of
typo correction.

Diff Detail

Event Timeline

adamcz created this revision.Nov 23 2020, 7:19 AM
adamcz requested review of this revision.Nov 23 2020, 7:19 AM
kadircet added inline comments.Nov 23 2020, 9:33 AM
clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
268

what about saving the full range here, and using it's start and length - name.size() as removal range in apply?

similarly we can also use the range.text as text to insert. that way we can inline getNNSLAsString as it won't be needed elsewhere anymore.

274

it is not that important, but I suppose NameRef is going to be alive during apply, so I suppose we can keep Name as a llvm::StringRef.

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

Tests don't seem to be covering something like:

namespace foo { namespace bar { struct X {}; } }
using namespace foo;
bar::X^ x;
adamcz updated this revision to Diff 307347.Nov 24 2020, 7:14 AM
adamcz marked an inline comment as done.

addressed review comments

adamcz added inline comments.Nov 24 2020, 7:17 AM
clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
268

We only have a range in this branch of the code. The other one (for DeclRefExpr) is not using TokenBuffers, so no range right now.

We may change it in the future. Or would you prefer this changed now?

274

Right, it used to be NameRef, then in some random version of the code pre-review it had to be a string and I never reverted that. Good point, thanks.

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

So this has the unfortunate side-effect of triggering a variation of https://github.com/clangd/clangd/issues/585

That's something I'm fixing in a later change (although this case is still not handled). For now, I'm putting an extra namespace scope to make this test correct, while still testing your example.

kadircet accepted this revision.Nov 24 2020, 8:45 AM

thanks, lgtm!

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

We only have a range in this branch of the code. The other one (for DeclRefExpr) is not using TokenBuffers, so no range right now.

Ah right, I've forgot that one. My main concern was the extra getSpellingLoc in apply.

We may change it in the future. Or would you prefer this changed now?

No hurries.

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

sorry for being confusing, I was actually suggesting putting:

namespace foo { namespace bar { struct X {}; } }
using namespace foo;

into the header (to prevent that bug from triggering), and only having the usage in the CC file. but this works too.

This revision is now accepted and ready to land.Nov 24 2020, 8:45 AM
This revision was automatically updated to reflect the committed changes.