Author: arphaman Date: Tue Sep 12 05:48:37 2017 New Revision: 313025 URL: http://llvm.org/viewvc/llvm-project?rev=313025&view=rev Log: [refactor] add a refactoring action rule that returns symbol occurrences
Differential Revision: https://reviews.llvm.org/D36574 Modified: cfe/trunk/include/clang/Tooling/Refactoring/RefactoringResultConsumer.h cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp Modified: cfe/trunk/include/clang/Tooling/Refactoring/RefactoringResultConsumer.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/RefactoringResultConsumer.h?rev=313025&r1=313024&r2=313025&view=diff ============================================================================== --- cfe/trunk/include/clang/Tooling/Refactoring/RefactoringResultConsumer.h (original) +++ cfe/trunk/include/clang/Tooling/Refactoring/RefactoringResultConsumer.h Tue Sep 12 05:48:37 2017 @@ -12,6 +12,7 @@ #include "clang/Basic/LLVM.h" #include "clang/Tooling/Refactoring/AtomicChange.h" +#include "clang/Tooling/Refactoring/Rename/SymbolOccurrences.h" #include "llvm/Support/Error.h" namespace clang { @@ -34,6 +35,10 @@ public: defaultResultHandler(); } + /// Handles the symbol occurrences that are found by an interactive + /// refactoring action. + virtual void handle(SymbolOccurrences Occurrences) { defaultResultHandler(); } + private: void defaultResultHandler() { handleError(llvm::make_error<llvm::StringError>( Modified: cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp?rev=313025&r1=313024&r2=313025&view=diff ============================================================================== --- cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp (original) +++ cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp Tue Sep 12 05:48:37 2017 @@ -11,6 +11,7 @@ #include "RewriterTestContext.h" #include "clang/Tooling/Refactoring.h" #include "clang/Tooling/Refactoring/RefactoringActionRules.h" +#include "clang/Tooling/Refactoring/Rename/SymbolName.h" #include "clang/Tooling/Tooling.h" #include "llvm/Support/Errc.h" #include "gtest/gtest.h" @@ -175,4 +176,49 @@ TEST_F(RefactoringActionRulesTest, Retur EXPECT_EQ(Message, "bad selection"); } +Optional<SymbolOccurrences> findOccurrences(RefactoringActionRule &Rule, + RefactoringRuleContext &Context) { + class Consumer final : public RefactoringResultConsumer { + void handleError(llvm::Error) override {} + void handle(SymbolOccurrences Occurrences) override { + Result = std::move(Occurrences); + } + + public: + Optional<SymbolOccurrences> Result; + }; + + Consumer C; + Rule.invoke(C, Context); + return std::move(C.Result); +} + +TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) { + auto Rule = createRefactoringRule( + [](selection::SourceSelectionRange Selection) + -> Expected<SymbolOccurrences> { + SymbolOccurrences Occurrences; + Occurrences.push_back(SymbolOccurrence( + SymbolName("test"), SymbolOccurrence::MatchingSymbol, + Selection.getRange().getBegin())); + return Occurrences; + }, + requiredSelection( + selection::identity<selection::SourceSelectionRange>())); + + RefactoringRuleContext RefContext(Context.Sources); + SourceLocation Cursor = + Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID()); + RefContext.setSelectionRange({Cursor, Cursor}); + Optional<SymbolOccurrences> Result = findOccurrences(*Rule, RefContext); + + ASSERT_FALSE(!Result); + SymbolOccurrences Occurrences = std::move(*Result); + EXPECT_EQ(Occurrences.size(), 1u); + EXPECT_EQ(Occurrences[0].getKind(), SymbolOccurrence::MatchingSymbol); + EXPECT_EQ(Occurrences[0].getNameRanges().size(), 1u); + EXPECT_EQ(Occurrences[0].getNameRanges()[0], + SourceRange(Cursor, Cursor.getLocWithOffset(strlen("test")))); +} + } // end anonymous namespace _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits