summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Tooling/RefactoringActionRulesTest.cpp
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2017-10-02 18:42:43 +0000
committerAlex Lorenz <arphaman@gmail.com>2017-10-02 18:42:43 +0000
commit1c0a26bd54d18b2a11c6dbc0ade44509c437ca93 (patch)
tree9e2c888fac554e95de63243947df7c987ef77045 /clang/unittests/Tooling/RefactoringActionRulesTest.cpp
parenta02d0e2c50cb1240875f0ad9b2b190874c22e238 (diff)
downloadbcm5719-llvm-1c0a26bd54d18b2a11c6dbc0ade44509c437ca93.tar.gz
bcm5719-llvm-1c0a26bd54d18b2a11c6dbc0ade44509c437ca93.zip
[refactor] Simplify the refactoring interface
This commit simplifies the interface for the refactoring action rules and the refactoring requirements. It merges the selection constraints and the selection requirements into one class. The refactoring actions rules must now be implemented using subclassing instead of raw function / lambda pointers. This change also removes a bunch of template-based traits and other template definitions that are now redundant. Differential Revision: https://reviews.llvm.org/D37681 llvm-svn: 314704
Diffstat (limited to 'clang/unittests/Tooling/RefactoringActionRulesTest.cpp')
-rw-r--r--clang/unittests/Tooling/RefactoringActionRulesTest.cpp129
1 files changed, 57 insertions, 72 deletions
diff --git a/clang/unittests/Tooling/RefactoringActionRulesTest.cpp b/clang/unittests/Tooling/RefactoringActionRulesTest.cpp
index cf91b5fc8f4..afab34ef101 100644
--- a/clang/unittests/Tooling/RefactoringActionRulesTest.cpp
+++ b/clang/unittests/Tooling/RefactoringActionRulesTest.cpp
@@ -18,7 +18,6 @@
using namespace clang;
using namespace tooling;
-using namespace refactoring_action_rules;
namespace {
@@ -56,29 +55,39 @@ createReplacements(const std::unique_ptr<RefactoringActionRule> &Rule,
}
TEST_F(RefactoringActionRulesTest, MyFirstRefactoringRule) {
- auto ReplaceAWithB =
- [](const RefactoringRuleContext &,
- std::pair<selection::SourceSelectionRange, int> Selection)
- -> Expected<AtomicChanges> {
- const SourceManager &SM = Selection.first.getSources();
- SourceLocation Loc = Selection.first.getRange().getBegin().getLocWithOffset(
- Selection.second);
- AtomicChange Change(SM, Loc);
- llvm::Error E = Change.replace(SM, Loc, 1, "b");
- if (E)
- return std::move(E);
- return AtomicChanges{Change};
+ class ReplaceAWithB : public SourceChangeRefactoringRule {
+ std::pair<SourceRange, int> Selection;
+
+ public:
+ ReplaceAWithB(std::pair<SourceRange, int> Selection)
+ : Selection(Selection) {}
+
+ Expected<AtomicChanges>
+ createSourceReplacements(RefactoringRuleContext &Context) {
+ const SourceManager &SM = Context.getSources();
+ SourceLocation Loc =
+ Selection.first.getBegin().getLocWithOffset(Selection.second);
+ AtomicChange Change(SM, Loc);
+ llvm::Error E = Change.replace(SM, Loc, 1, "b");
+ if (E)
+ return std::move(E);
+ return AtomicChanges{Change};
+ }
};
- class SelectionRequirement : public selection::Requirement {
+
+ class SelectionRequirement : public SourceRangeSelectionRequirement {
public:
- std::pair<selection::SourceSelectionRange, int>
- evaluateSelection(const RefactoringRuleContext &,
- selection::SourceSelectionRange Selection) const {
- return std::make_pair(Selection, 20);
+ Expected<std::pair<SourceRange, int>>
+ evaluate(RefactoringRuleContext &Context) const {
+ Expected<SourceRange> R =
+ SourceRangeSelectionRequirement::evaluate(Context);
+ if (!R)
+ return R.takeError();
+ return std::make_pair(*R, 20);
}
};
- auto Rule = createRefactoringRule(ReplaceAWithB,
- requiredSelection(SelectionRequirement()));
+ auto Rule =
+ createRefactoringActionRule<ReplaceAWithB>(SelectionRequirement());
// When the requirements are satisifed, the rule's function must be invoked.
{
@@ -123,54 +132,24 @@ TEST_F(RefactoringActionRulesTest, MyFirstRefactoringRule) {
llvm::handleAllErrors(
ErrorOrResult.takeError(),
[&](llvm::StringError &Error) { Message = Error.getMessage(); });
- EXPECT_EQ(Message, "refactoring action can't be initiated with the "
- "specified selection range");
+ EXPECT_EQ(Message,
+ "refactoring action can't be initiated without a selection");
}
}
TEST_F(RefactoringActionRulesTest, ReturnError) {
- Expected<AtomicChanges> (*Func)(const RefactoringRuleContext &,
- selection::SourceSelectionRange) =
- [](const RefactoringRuleContext &,
- selection::SourceSelectionRange) -> Expected<AtomicChanges> {
- return llvm::make_error<llvm::StringError>(
- "Error", llvm::make_error_code(llvm::errc::invalid_argument));
- };
- auto Rule = createRefactoringRule(
- Func, requiredSelection(
- selection::identity<selection::SourceSelectionRange>()));
-
- RefactoringRuleContext RefContext(Context.Sources);
- SourceLocation Cursor =
- Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID());
- RefContext.setSelectionRange({Cursor, Cursor});
- Expected<AtomicChanges> Result = createReplacements(Rule, RefContext);
-
- ASSERT_TRUE(!Result);
- std::string Message;
- llvm::handleAllErrors(Result.takeError(), [&](llvm::StringError &Error) {
- Message = Error.getMessage();
- });
- EXPECT_EQ(Message, "Error");
-}
-
-TEST_F(RefactoringActionRulesTest, ReturnInitiationDiagnostic) {
- RefactoringRuleContext RefContext(Context.Sources);
- class SelectionRequirement : public selection::Requirement {
+ class ErrorRule : public SourceChangeRefactoringRule {
public:
- Expected<Optional<int>>
- evaluateSelection(const RefactoringRuleContext &,
- selection::SourceSelectionRange Selection) const {
+ ErrorRule(SourceRange R) {}
+ Expected<AtomicChanges> createSourceReplacements(RefactoringRuleContext &) {
return llvm::make_error<llvm::StringError>(
- "bad selection", llvm::make_error_code(llvm::errc::invalid_argument));
+ "Error", llvm::make_error_code(llvm::errc::invalid_argument));
}
};
- auto Rule = createRefactoringRule(
- [](const RefactoringRuleContext &, int) -> Expected<AtomicChanges> {
- llvm::report_fatal_error("Should not run!");
- },
- requiredSelection(SelectionRequirement()));
+ auto Rule =
+ createRefactoringActionRule<ErrorRule>(SourceRangeSelectionRequirement());
+ RefactoringRuleContext RefContext(Context.Sources);
SourceLocation Cursor =
Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID());
RefContext.setSelectionRange({Cursor, Cursor});
@@ -181,7 +160,7 @@ TEST_F(RefactoringActionRulesTest, ReturnInitiationDiagnostic) {
llvm::handleAllErrors(Result.takeError(), [&](llvm::StringError &Error) {
Message = Error.getMessage();
});
- EXPECT_EQ(Message, "bad selection");
+ EXPECT_EQ(Message, "Error");
}
Optional<SymbolOccurrences> findOccurrences(RefactoringActionRule &Rule,
@@ -205,18 +184,24 @@ Optional<SymbolOccurrences> findOccurrences(RefactoringActionRule &Rule,
}
TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) {
- auto Rule = createRefactoringRule(
- [](const RefactoringRuleContext &,
- selection::SourceSelectionRange Selection)
- -> Expected<SymbolOccurrences> {
- SymbolOccurrences Occurrences;
- Occurrences.push_back(SymbolOccurrence(
- SymbolName("test"), SymbolOccurrence::MatchingSymbol,
- Selection.getRange().getBegin()));
- return std::move(Occurrences);
- },
- requiredSelection(
- selection::identity<selection::SourceSelectionRange>()));
+ class FindOccurrences : public FindSymbolOccurrencesRefactoringRule {
+ SourceRange Selection;
+
+ public:
+ FindOccurrences(SourceRange Selection) : Selection(Selection) {}
+
+ Expected<SymbolOccurrences>
+ findSymbolOccurrences(RefactoringRuleContext &) override {
+ SymbolOccurrences Occurrences;
+ Occurrences.push_back(SymbolOccurrence(SymbolName("test"),
+ SymbolOccurrence::MatchingSymbol,
+ Selection.getBegin()));
+ return Occurrences;
+ }
+ };
+
+ auto Rule = createRefactoringActionRule<FindOccurrences>(
+ SourceRangeSelectionRequirement());
RefactoringRuleContext RefContext(Context.Sources);
SourceLocation Cursor =
OpenPOWER on IntegriCloud