summaryrefslogtreecommitdiffstats
path: root/clang/lib
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/lib
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/lib')
-rw-r--r--clang/lib/Tooling/Refactoring/CMakeLists.txt1
-rw-r--r--clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp90
-rw-r--r--clang/lib/Tooling/Refactoring/SourceSelectionConstraints.cpp23
3 files changed, 57 insertions, 57 deletions
diff --git a/clang/lib/Tooling/Refactoring/CMakeLists.txt b/clang/lib/Tooling/Refactoring/CMakeLists.txt
index ff9cd1ff9e2..43ea1d9c542 100644
--- a/clang/lib/Tooling/Refactoring/CMakeLists.txt
+++ b/clang/lib/Tooling/Refactoring/CMakeLists.txt
@@ -9,7 +9,6 @@ add_clang_library(clangToolingRefactor
Rename/USRFinder.cpp
Rename/USRFindingAction.cpp
Rename/USRLocFinder.cpp
- SourceSelectionConstraints.cpp
LINK_LIBS
clangAST
diff --git a/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp b/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp
index 384e4660154..fe3067f8258 100644
--- a/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp
+++ b/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp
@@ -23,7 +23,6 @@
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Refactoring/RefactoringAction.h"
-#include "clang/Tooling/Refactoring/RefactoringActionRules.h"
#include "clang/Tooling/Refactoring/Rename/USRFinder.h"
#include "clang/Tooling/Refactoring/Rename/USRFindingAction.h"
#include "clang/Tooling/Refactoring/Rename/USRLocFinder.h"
@@ -39,53 +38,78 @@ namespace tooling {
namespace {
-class LocalRename : public RefactoringAction {
+class SymbolSelectionRequirement : public SourceRangeSelectionRequirement {
public:
- StringRef getCommand() const override { return "local-rename"; }
-
- StringRef getDescription() const override {
- return "Finds and renames symbols in code with no indexer support";
+ Expected<const NamedDecl *> evaluate(RefactoringRuleContext &Context) const {
+ Expected<SourceRange> Selection =
+ SourceRangeSelectionRequirement::evaluate(Context);
+ if (!Selection)
+ return Selection.takeError();
+ const NamedDecl *ND =
+ getNamedDeclAt(Context.getASTContext(), Selection->getBegin());
+ if (!ND) {
+ // FIXME: Use a diagnostic.
+ return llvm::make_error<StringError>("no symbol selected",
+ llvm::inconvertibleErrorCode());
+ }
+ return getCanonicalSymbolDeclaration(ND);
}
+};
- /// Returns a set of refactoring actions rules that are defined by this
- /// action.
- RefactoringActionRules createActionRules() const override {
- using namespace refactoring_action_rules;
- RefactoringActionRules Rules;
- Rules.push_back(createRefactoringRule(
- renameOccurrences, requiredSelection(SymbolSelectionRequirement())));
- return Rules;
- }
+class OccurrenceFinder final : public FindSymbolOccurrencesRefactoringRule {
+public:
+ OccurrenceFinder(const NamedDecl *ND) : ND(ND) {}
-private:
- static Expected<AtomicChanges>
- renameOccurrences(const RefactoringRuleContext &Context,
- const NamedDecl *ND) {
+ Expected<SymbolOccurrences>
+ findSymbolOccurrences(RefactoringRuleContext &Context) override {
std::vector<std::string> USRs =
getUSRsForDeclaration(ND, Context.getASTContext());
std::string PrevName = ND->getNameAsString();
- auto Occurrences = getOccurrencesOfUSRs(
+ return getOccurrencesOfUSRs(
USRs, PrevName, Context.getASTContext().getTranslationUnitDecl());
+ }
+private:
+ const NamedDecl *ND;
+};
+
+class RenameOccurrences final : public SourceChangeRefactoringRule {
+public:
+ RenameOccurrences(const NamedDecl *ND) : Finder(ND) {}
+
+ Expected<AtomicChanges>
+ createSourceReplacements(RefactoringRuleContext &Context) {
+ Expected<SymbolOccurrences> Occurrences =
+ Finder.findSymbolOccurrences(Context);
+ if (!Occurrences)
+ return Occurrences.takeError();
// FIXME: This is a temporary workaround that's needed until the refactoring
// options are implemented.
StringRef NewName = "Bar";
return createRenameReplacements(
- Occurrences, Context.getASTContext().getSourceManager(), NewName);
+ *Occurrences, Context.getASTContext().getSourceManager(), NewName);
}
- class SymbolSelectionRequirement : public selection::Requirement {
- public:
- Expected<Optional<const NamedDecl *>>
- evaluateSelection(const RefactoringRuleContext &Context,
- selection::SourceSelectionRange Selection) const {
- const NamedDecl *ND = getNamedDeclAt(Context.getASTContext(),
- Selection.getRange().getBegin());
- if (!ND)
- return None;
- return getCanonicalSymbolDeclaration(ND);
- }
- };
+private:
+ OccurrenceFinder Finder;
+};
+
+class LocalRename final : public RefactoringAction {
+public:
+ StringRef getCommand() const override { return "local-rename"; }
+
+ StringRef getDescription() const override {
+ return "Finds and renames symbols in code with no indexer support";
+ }
+
+ /// Returns a set of refactoring actions rules that are defined by this
+ /// action.
+ RefactoringActionRules createActionRules() const override {
+ RefactoringActionRules Rules;
+ Rules.push_back(createRefactoringActionRule<RenameOccurrences>(
+ SymbolSelectionRequirement()));
+ return Rules;
+ }
};
} // end anonymous namespace
diff --git a/clang/lib/Tooling/Refactoring/SourceSelectionConstraints.cpp b/clang/lib/Tooling/Refactoring/SourceSelectionConstraints.cpp
deleted file mode 100644
index 5fbe2946a9d..00000000000
--- a/clang/lib/Tooling/Refactoring/SourceSelectionConstraints.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-//===--- SourceSelectionConstraints.cpp - Evaluate selection constraints --===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/Tooling/Refactoring/SourceSelectionConstraints.h"
-#include "clang/Tooling/Refactoring/RefactoringRuleContext.h"
-
-using namespace clang;
-using namespace tooling;
-using namespace selection;
-
-Optional<SourceSelectionRange>
-SourceSelectionRange::evaluate(RefactoringRuleContext &Context) {
- SourceRange R = Context.getSelectionRange();
- if (R.isInvalid())
- return None;
- return SourceSelectionRange(Context.getSources(), R);
-}
OpenPOWER on IntegriCloud