This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Helper for determining member insertion point.
ClosedPublic

Authored by sammccall on Jan 2 2022, 2:24 PM.

Details

Summary

To be used in D116490 and D116385, and an upcoming patch to generate C++
constructors.

Diff Detail

Event Timeline

sammccall created this revision.Jan 2 2022, 2:24 PM
sammccall requested review of this revision.Jan 2 2022, 2:24 PM
sammccall updated this revision to Diff 396953.Jan 2 2022, 2:25 PM

Fix stale comment

sammccall updated this revision to Diff 396977.Jan 2 2022, 6:51 PM

[clangd] Add code action to generate a constructor for a C++ class.

sammccall updated this revision to Diff 396978.Jan 2 2022, 6:53 PM

Oops, revert wrong patch

We have some logic in AddUsing tweak to determine insertion point based on AST. i think it makes sense to migrate it to these helpers too. There's some more logic in extract variable/function too. Extract variable seems too elaborate as it actually looks at statements, rather than decls and extract function is quite simple already as we already figure out current function's range and whatnot, but having another usage might help.

(thinking out loud)
We've got some helpers in SourceCode.h to determine insertion point in the absence of ASTs. Concepts here and there around an "insertion point" seems to be quite different (it's just a sourcelocation here and a set of locations + a namespace in SourceCode.h).
I suppose those two are somewhat hard to merge and serve different purposes, so It's better to keep them separate.

kadircet added inline comments.Jan 3 2022, 1:21 AM
clang-tools-extra/clangd/refactor/InsertionPoint.cpp
52

nit: use a switch?

135

what if we had:

class Foo {
public:
  void foo();
};

and wanted to insert a private member/field?
I suppose we should check for the specifier of last decl in InClass instead.

sammccall updated this revision to Diff 397037.Jan 3 2022, 3:44 AM

Fix access protection bug, add more tests

sammccall marked 2 inline comments as done.Jan 3 2022, 3:54 AM

We have some logic in AddUsing tweak to determine insertion point based on AST. i think it makes sense to migrate it to these helpers too.

Thanks, I'd forgotten about those. I might tackle those next?

There's some more logic in extract variable/function too. Extract variable seems too elaborate as it actually looks at statements, rather than decls and extract function is quite simple already as we already figure out current function's range and whatnot, but having another usage might help.

Yeah I wasn't sure I could do a good job of generalizing these ones yet (and we haven't needed it yet).

We've got some helpers in SourceCode.h to determine insertion point in the absence of ASTs. Concepts here and there around an "insertion point" seems to be quite different (it's just a sourcelocation here and a set of locations + a namespace in SourceCode.h).
I suppose those two are somewhat hard to merge and serve different purposes, so It's better to keep them separate.

For sure ast-based and pseudoparsing-based cases are going to be different APIs, but I would like to move those into this header too.
The problem I hit was they share private infrastructure (in SourceCode.cpp) with other functionality (the namespace pseudoparsing for completion, I think). So it's a bit of work to extract.

clang-tools-extra/clangd/refactor/InsertionPoint.cpp
135

Oops, good catch.

In that particular case, you could make a case for inserting at the very top of the class (no access specifier needed). But some coding styles want the opposite.
I think using the presence/absence of decls in the chunk before the first access specifier is a reasonable hint, and it happens to be the easiest thing to implement :-)

kadircet accepted this revision.Jan 3 2022, 4:52 AM

Thanks, LGTM!

We have some logic in AddUsing tweak to determine insertion point based on AST. i think it makes sense to migrate it to these helpers too.

Thanks, I'd forgotten about those. I might tackle those next?

SGTM.

We've got some helpers in SourceCode.h to determine insertion point in the absence of ASTs. Concepts here and there around an "insertion point" seems to be quite different (it's just a sourcelocation here and a set of locations + a namespace in SourceCode.h).
I suppose those two are somewhat hard to merge and serve different purposes, so It's better to keep them separate.

For sure ast-based and pseudoparsing-based cases are going to be different APIs, but I would like to move those into this header too.

Makes sense.

The problem I hit was they share private infrastructure (in SourceCode.cpp) with other functionality (the namespace pseudoparsing for completion, I think). So it's a bit of work to extract.

Ah I see :/

This revision is now accepted and ready to land.Jan 3 2022, 4:52 AM
This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.