This is an archive of the discontinued LLVM Phabricator instance.

[refactor] selection: new CodeRangeASTSelection represents a set of selected consecutive statements
ClosedPublic

Authored by arphaman on Oct 11 2017, 10:26 PM.

Details

Summary

This patch adds a CodeRangeASTSelection value to the refactoring library. This value represents a set of selected statements in one body of code.

The refactoring rules will be able to use this value with the CodeRangeSelectionRequirement that will be introduced in follow up patches. This selection will be used to implement the "extract" refactoring.

Diff Detail

Repository
rL LLVM

Event Timeline

arphaman created this revision.Oct 11 2017, 10:26 PM
arphaman retitled this revision from [refactor] selection: new CodeRangeASTSelection represents a set of selected consecu to [refactor] selection: new CodeRangeASTSelection represents a set of selected consecutive statements.
arphaman edited the summary of this revision. (Show Details)
hokein added inline comments.Oct 12 2017, 4:42 AM
include/clang/Tooling/Refactoring/ASTSelection.h
74 ↗(On Diff #118738)

I see all your tests are for function body, does it support other body, i.e. "global namespace", "compound statement"?

if yes, how about adding more test cases to cover it.

// variable in global namespace
int a; // select int.
void f() {
   {
       int a;  // select a.
   }
}
95 ↗(On Diff #118738)

nit: instead of a structure, this feels more like a class.

lib/Tooling/Refactoring/ASTSelection.cpp
247 ↗(On Diff #118738)

nit: remove the empty line.

unittests/Tooling/ASTSelectionTest.cpp
685 ↗(On Diff #118738)

I think "No selection range" is more precise -- you are passing None parameter to the function.

For empty range, it should be something like FileRange{{2, 2}, {2, 2}}, we can also add a test for it.

688 ↗(On Diff #118738)

I'm a bit confused here, if the selection range is none/empty, shouldn't SelectedASTNode be empty?

If this behavior is intended, I'd suggest documenting it in findSelectedASTNodesWithRange.

718 ↗(On Diff #118738)

nit: would be clearer to add a comment indicating the corresponding code, the same below.

like

EXPECT_TRUE(...); // function f definition
EXPECT_TRUE(...); // function body of function f
arphaman updated this revision to Diff 119197.Oct 16 2017, 12:47 PM
arphaman marked 6 inline comments as done.

Address review comments

include/clang/Tooling/Refactoring/ASTSelection.h
74 ↗(On Diff #118738)

Yes, I added a couple that you suggested.

unittests/Tooling/ASTSelectionTest.cpp
688 ↗(On Diff #118738)

No, the AST selection will work even with a cursor location, hence it won't be empty.

However, the CodeRangeASTSelection requires a non-empty selection range, so it won't work with just cursors.

hokein accepted this revision.Oct 17 2017, 6:55 AM

LGTM.

unittests/Tooling/ASTSelectionTest.cpp
688 ↗(On Diff #118738)

I see, thanks.

the AST selection will work even with a cursor location, hence it won't be empty.

Is this documented somewhere in the code? I couldn't find any comments in the definition of findSelectedASTNodesWithRange or findSelectedASTNodes in this file. Would be nice to document it although this is a test API, so that it won't confuse future code readers.

This revision is now accepted and ready to land.Oct 17 2017, 6:55 AM
arphaman added inline comments.Oct 18 2017, 11:29 AM
unittests/Tooling/ASTSelectionTest.cpp
688 ↗(On Diff #118738)

Not quite, I'll add a comment in a follow-up commit.

This revision was automatically updated to reflect the committed changes.