From 38cd6e2c89099bb19cc48f1663f1f54942fdbd81 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Tue, 14 Nov 2017 22:06:55 +0000 Subject: [PATCH] [refactor][selection] canonicalize member expr callee to the full member call expression We would like to extract the full call when just the callee is selected. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318205 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Tooling/Refactoring/ASTSelection.cpp | 13 +++++- unittests/Tooling/ASTSelectionTest.cpp | 52 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/Tooling/Refactoring/ASTSelection.cpp b/lib/Tooling/Refactoring/ASTSelection.cpp index 4f1168becf..ae8a3cb7c5 100644 --- a/lib/Tooling/Refactoring/ASTSelection.cpp +++ b/lib/Tooling/Refactoring/ASTSelection.cpp @@ -115,6 +115,11 @@ public: return true; if (auto *Opaque = dyn_cast(S)) return TraverseOpaqueValueExpr(Opaque); + // Avoid selecting implicit 'this' expressions. + if (auto *TE = dyn_cast(S)) { + if (TE->isImplicit()) + return true; + } // FIXME (Alex Lorenz): Improve handling for macro locations. SourceSelectionKind SelectionKind = selectionKindFor(CharSourceRange::getTokenRange(S->getSourceRange())); @@ -268,9 +273,15 @@ void SelectedNodeWithParents::canonicalize() { // ~~~~~~ ~~~~~~~ if (isa(S) && isa(Parent)) Node = Parents.pop_back_val(); + // The entire call should be selected when just the member expression + // that refers to the method is selected. + // f.call(args) becomes f.call(args) + // ~~~~ ~~~~~~~~~~~~ + else if (isa(S) && isa(Parent) && + cast(Parent)->getCallee() == S) + Node = Parents.pop_back_val(); // FIXME: Syntactic form -> Entire pseudo-object expr. // FIXME: Callee -> Call. - // FIXME: Callee member expr -> Call. } /// Finds the set of bottom-most selected AST nodes that are in the selection diff --git a/unittests/Tooling/ASTSelectionTest.cpp b/unittests/Tooling/ASTSelectionTest.cpp index f10d899a0a..fc5186ea20 100644 --- a/unittests/Tooling/ASTSelectionTest.cpp +++ b/unittests/Tooling/ASTSelectionTest.cpp @@ -1004,4 +1004,56 @@ void foo() { SelectionFinderVisitor::Lang_OBJC); } +TEST(ASTSelectionFinder, CanonicalizeMemberCalleeToCall) { + StringRef Source = R"( +class AClass { public: + void method(); + int afield; + void selectWholeCallWhenJustMethodSelected(int &i) { + method(); + } +}; +void selectWholeCallWhenJustMethodSelected() { + AClass a; + a.method(); +} +void dontSelectArgument(AClass &a) { + a.selectWholeCallWhenJustMethodSelected(a.afield); +} + )"; + // Just 'method' with implicit 'this': + findSelectedASTNodesWithRange( + Source, {6, 5}, FileRange{{6, 5}, {6, 11}}, + [](SourceRange SelectionRange, Optional Node) { + EXPECT_TRUE(Node); + Optional SelectedCode = + CodeRangeASTSelection::create(SelectionRange, std::move(*Node)); + EXPECT_TRUE(SelectedCode); + EXPECT_EQ(SelectedCode->size(), 1u); + EXPECT_TRUE(isa((*SelectedCode)[0])); + }); + // Just 'method': + findSelectedASTNodesWithRange( + Source, {11, 5}, FileRange{{11, 5}, {11, 11}}, + [](SourceRange SelectionRange, Optional Node) { + EXPECT_TRUE(Node); + Optional SelectedCode = + CodeRangeASTSelection::create(SelectionRange, std::move(*Node)); + EXPECT_TRUE(SelectedCode); + EXPECT_EQ(SelectedCode->size(), 1u); + EXPECT_TRUE(isa((*SelectedCode)[0])); + }); + // Just 'afield', which should not select the call. + findSelectedASTNodesWithRange( + Source, {14, 5}, FileRange{{14, 45}, {14, 51}}, + [](SourceRange SelectionRange, Optional Node) { + EXPECT_TRUE(Node); + Optional SelectedCode = + CodeRangeASTSelection::create(SelectionRange, std::move(*Node)); + EXPECT_TRUE(SelectedCode); + EXPECT_EQ(SelectedCode->size(), 1u); + EXPECT_FALSE(isa((*SelectedCode)[0])); + }); +} + } // end anonymous namespace -- 2.50.1