arphaman updated this revision to Diff 113533.
arphaman added a comment.

Rebase on top of trunk and https://reviews.llvm.org/D37210.

Ping.


Repository:
  rL LLVM

https://reviews.llvm.org/D36574

Files:
  include/clang/Tooling/Refactoring/RefactoringAction.h
  include/clang/Tooling/Refactoring/RefactoringActionRegistry.def
  include/clang/Tooling/Refactoring/RefactoringActionRule.h
  include/clang/Tooling/Refactoring/RefactoringActionRuleRequirementsInternal.h
  include/clang/Tooling/Refactoring/RefactoringActionRules.h
  include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
  include/clang/Tooling/Refactoring/RefactoringEngine.h
  include/clang/Tooling/Refactoring/RefactoringRuleContext.h
  include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h
  include/clang/Tooling/Refactoring/Rename/USRFindingAction.h
  include/clang/Tooling/Refactoring/SourceSelectionConstraints.h
  include/clang/module.modulemap
  lib/Tooling/Refactoring/CMakeLists.txt
  lib/Tooling/Refactoring/RefactoringEngine.cpp
  lib/Tooling/Refactoring/Rename/RenamingAction.cpp
  lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
  test/CMakeLists.txt
  test/Refactor/LocalRename/Field.cpp
  test/Refactor/tool-common-options.c
  test/Refactor/tool-test-support.c
  test/clang-rename/Field.cpp
  tools/CMakeLists.txt
  tools/clang-refactor/CMakeLists.txt
  tools/clang-refactor/ClangRefactor.cpp
  tools/clang-refactor/DumpRefactoringResult.cpp
  tools/clang-refactor/RefactoringResult.h
  tools/clang-refactor/TestSupport.cpp
  tools/clang-refactor/TestSupport.h

Index: tools/clang-refactor/TestSupport.h
===================================================================
--- /dev/null
+++ tools/clang-refactor/TestSupport.h
@@ -0,0 +1,105 @@
+//===--- TestSupport.h - Clang-based refactoring tool -----------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief Declares datatypes and routines that are used by test-specific code
+/// in clang-refactor.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H
+#define LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H
+
+#include "RefactoringResult.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Error.h"
+#include <map>
+#include <string>
+
+namespace clang {
+
+class SourceManager;
+
+namespace clang_refactor {
+
+/// A source selection range that's specified in a test file using an inline
+/// command in the comment. These commands can take the following forms:
+///
+/// - /*range=*/ will create an empty selection range in the default group
+///   right after the comment.
+/// - /*range a=*/ will create an empty selection range in the 'a' group right
+///   after the comment.
+/// - /*range = +1*/ will create an empty selection range at a location that's
+///   right after the comment with one offset to the column.
+/// - /*range= -> +2:3*/ will create a selection range that starts at the
+///   location right after the comment, and ends at column 3 of the 2nd line
+///   after the line of the starting location.
+///
+/// Clang-refactor will expected all ranges in one test group to produce
+/// identical results.
+struct TestSelectionRange {
+  unsigned Begin, End;
+};
+
+/// A set of test selection ranges specified in one file.
+struct TestSelectionRangesInFile {
+  std::string Filename;
+  std::map<std::string, SmallVector<TestSelectionRange, 8>> GroupedRanges;
+
+  TestSelectionRangesInFile(TestSelectionRangesInFile &&) = default;
+  TestSelectionRangesInFile &operator=(TestSelectionRangesInFile &&) = default;
+
+  bool dispatch(
+      const SourceManager &SM,
+      llvm::function_ref<
+          Expected<std::unique_ptr<AbstractRefactoringResult>>(SourceRange)>
+          Producer) const;
+
+  void dump(llvm::raw_ostream &OS) const;
+};
+
+/// Extracts the grouped selection ranges from the file that's specified in
+/// the -selection=test:<filename> option.
+///
+/// The grouped ranges are specified in comments using the following syntax:
+/// "range" [ group-name ] "=" [ "+" starting-column-offset ] [ "->"
+///                              "+" ending-line-offset ":"
+///                                  ending-column-position ]
+///
+/// The selection range is then computed from this command by taking the ending
+/// location of the comment, and adding 'starting-column-offset' to the column
+/// for that location. That location in turns becomes the whole selection range,
+/// unless 'ending-line-offset' and 'ending-column-position' are specified. If
+/// they are specified, then the ending location of the selection range is
+/// the starting location's line + 'ending-line-offset' and the
+/// 'ending-column-position' column.
+///
+/// All selection ranges in one group are expected to produce the same
+/// refactoring result.
+///
+/// When testing, zero is returned from clang-refactor even when a group
+/// produces an initiation error, which is different from normal invocation
+/// that returns a non-zero value. This is done on purpose, to ensure that group
+/// consistency checks can return non-zero, but still dump the output of
+/// the group. So even if a test matches the output of group, it will still fail
+/// because clang-refactor should return zero on exit when the group results are
+/// consistent.
+///
+/// \returns None on failure (errors are emitted to stderr), or a set of
+/// grouped source ranges in the given file otherwise.
+Optional<TestSelectionRangesInFile>
+findTestSelectionRangesIn(StringRef Filename);
+
+} // end namespace clang_refactor
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H
Index: tools/clang-refactor/TestSupport.cpp
===================================================================
--- /dev/null
+++ tools/clang-refactor/TestSupport.cpp
@@ -0,0 +1,209 @@
+//===--- TestSupport.cpp - Clang-based refactoring tool -------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file implements routines that provide refactoring testing
+/// utilities.
+///
+//===----------------------------------------------------------------------===//
+
+#include "TestSupport.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorOr.h"
+#include "llvm/Support/Regex.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+using namespace clang;
+using namespace clang_refactor;
+
+void TestSelectionRangesInFile::dump(raw_ostream &OS) const {
+  for (const auto &Group : GroupedRanges) {
+    OS << "Test selection group '" << Group.first << "':\n";
+    for (const auto &Range : Group.second) {
+      OS << "  " << Range.Begin << "-" << Range.End << "\n";
+    }
+  }
+}
+
+bool TestSelectionRangesInFile::dispatch(
+    const SourceManager &SM,
+    llvm::function_ref<
+        Expected<std::unique_ptr<AbstractRefactoringResult>>(SourceRange)>
+        Producer) const {
+  const FileEntry *FE = SM.getFileManager().getFile(Filename);
+  FileID FID = FE ? SM.translateFile(FE) : FileID();
+  if (!FE || FID.isInvalid()) {
+    llvm::errs() << "error: -selection=test:" << Filename
+                 << " : given file is not in the target TU";
+    return true;
+  }
+  SourceLocation FileLoc = SM.getLocForStartOfFile(FID);
+  bool Failed = false;
+  for (const auto &Group : GroupedRanges) {
+    // All ranges in the group must produce the same result.
+    std::unique_ptr<AbstractRefactoringResult> CanonicalResult;
+    Optional<std::string> CanonicalErrorMessage;
+    for (const TestSelectionRange &Range : Group.second) {
+      // Translate the offset pair to a true source range.
+      SourceLocation Start =
+          SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Range.Begin));
+      SourceLocation End =
+          SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Range.End));
+      assert(Start.isValid() && End.isValid() && "unexpected invalid range");
+
+      Expected<std::unique_ptr<AbstractRefactoringResult>> Result =
+          Producer(SourceRange(Start, End));
+      std::string ErrorMessage;
+      bool HasResult = !!Result;
+      if (!HasResult) {
+        // FIXME: Handle diagnostic error as well.
+        handleAllErrors(Result.takeError(), [&](StringError &Err) {
+          ErrorMessage = Err.getMessage();
+        });
+      }
+      if (!CanonicalResult && !CanonicalErrorMessage) {
+        if (HasResult)
+          CanonicalResult = std::move(*Result);
+        else
+          CanonicalErrorMessage = std::move(ErrorMessage);
+        continue;
+      }
+
+      // Verify that this result corresponds to the canonical result.
+      if (CanonicalErrorMessage) {
+        // The error messages must match.
+        if (!HasResult && ErrorMessage == *CanonicalErrorMessage)
+          continue;
+      } else {
+        assert(CanonicalResult && "missing canonical result");
+        // The results must match.
+        if (HasResult && **Result == *CanonicalResult)
+          continue;
+      }
+      Failed = true;
+      // Report the mismatch.
+      llvm::errs()
+          << "error: unexpected refactoring result for range starting at "
+          << SM.getLineNumber(FID, Range.Begin) << ':'
+          << SM.getColumnNumber(FID, Range.Begin) << " in group '"
+          << Group.first << "':\n  ";
+      if (HasResult)
+        llvm::errs() << "valid result";
+      else
+        llvm::errs() << "error '" << ErrorMessage << "'";
+      llvm::errs() << " does not match initial ";
+      if (CanonicalErrorMessage)
+        llvm::errs() << "error '" << *CanonicalErrorMessage << "'\n";
+      else
+        llvm::errs() << "valid result\n";
+      if (HasResult && !CanonicalErrorMessage) {
+        llvm::errs() << "  Expected to Produce:\n";
+        CanonicalResult->dump(llvm::errs(), SM, /*Indent=*/4);
+        llvm::errs() << "  Produced:\n";
+        (*Result)->dump(llvm::errs(), SM, /*Indent=*/4);
+      }
+    }
+
+    // Dump the results:
+    if (!CanonicalResult) {
+      llvm::errs() << Group.second.size() << " '" << Group.first
+                   << "' results:\n";
+      llvm::errs() << *CanonicalErrorMessage << "\n";
+    } else {
+      llvm::outs() << Group.second.size() << " '" << Group.first
+                   << "' results:\n";
+      CanonicalResult->dump(llvm::outs(), SM);
+    }
+  }
+  return Failed;
+}
+
+/// Adds the \p ColumnOffset to file offset \p Offset, without going past a
+/// newline.
+static unsigned addColumnOffset(StringRef Source, unsigned Offset,
+                                unsigned ColumnOffset) {
+  if (!ColumnOffset)
+    return Offset;
+  StringRef Substr = Source.drop_front(Offset).take_front(ColumnOffset);
+  size_t NewlinePos = Substr.find_first_of("\r\n");
+  return Offset +
+         (NewlinePos == StringRef::npos ? ColumnOffset : (unsigned)NewlinePos);
+}
+
+Optional<TestSelectionRangesInFile>
+clang_refactor::findTestSelectionRangesIn(StringRef Filename) {
+  ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile =
+      MemoryBuffer::getFile(Filename);
+  if (!ErrOrFile) {
+    llvm::errs() << "error: -selection=test:" << Filename
+                 << " : could not open the given file";
+    return None;
+  }
+  StringRef Source = ErrOrFile.get()->getBuffer();
+
+  // FIXME (Alex L): 3rd capture groups for +line:column.
+  // See the doc comment for this function for the explanation of this
+  // syntax.
+  static Regex RangeRegex("range[[:blank:]]*([[:alpha:]_]*)?[[:blank:]]*=[[:"
+                          "blank:]]*(\\+[[:digit:]]+)?");
+
+  TestSelectionRangesInFile TestRanges = {Filename.str(), {}};
+
+  LangOptions LangOpts;
+  LangOpts.CPlusPlus = 1;
+  LangOpts.CPlusPlus11 = 1;
+  Lexer Lex(SourceLocation::getFromRawEncoding(0), LangOpts, Source.begin(),
+            Source.begin(), Source.end());
+  Lex.SetCommentRetentionState(true);
+  Token Tok;
+  for (Lex.LexFromRawLexer(Tok); Tok.isNot(tok::eof);
+       Lex.LexFromRawLexer(Tok)) {
+    if (Tok.isNot(tok::comment))
+      continue;
+    StringRef Comment =
+        Source.substr(Tok.getLocation().getRawEncoding(), Tok.getLength());
+    SmallVector<StringRef, 4> Matches;
+    if (!RangeRegex.match(Comment, &Matches)) {
+      // Try to detect mistyped 'range:' comments to ensure tests don't miss
+      // anything.
+      if (Comment.contains_lower("range") && !Comment.contains_lower("run")) {
+        llvm::errs() << "error: suspicious comment '" << Comment
+                     << "' that "
+                        "resembles the range command found\n";
+        llvm::errs() << "note: please reword if this isn't a range command\n";
+        return None;
+      }
+      continue;
+    }
+    unsigned Offset = Tok.getEndLoc().getRawEncoding();
+    unsigned ColumnOffset = 0;
+    if (!Matches[2].empty()) {
+      // Don't forget to drop the '+'!
+      if (Matches[2].drop_front().getAsInteger(10, ColumnOffset))
+        assert(false && "regex should have produced a number");
+    }
+    // FIXME (Alex L): Support true ranges.
+    Offset = addColumnOffset(Source, Offset, ColumnOffset);
+    TestSelectionRange Range = {Offset, Offset};
+    auto It = TestRanges.GroupedRanges.insert(std::make_pair(
+        Matches[1].str(), SmallVector<TestSelectionRange, 8>{Range}));
+    if (!It.second)
+      It.first->second.push_back(Range);
+  }
+  if (TestRanges.GroupedRanges.empty()) {
+    llvm::errs() << "error: -selection=test:" << Filename
+                 << ": no 'range' commands";
+    return None;
+  }
+  return std::move(TestRanges);
+}
Index: tools/clang-refactor/RefactoringResult.h
===================================================================
--- /dev/null
+++ tools/clang-refactor/RefactoringResult.h
@@ -0,0 +1,107 @@
+//===--- RefactoringResult.h - Clang-based refactoring tool -----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_CLANG_REFACTOR_REFACTORING_RESULT_H
+#define LLVM_CLANG_TOOLS_CLANG_REFACTOR_REFACTORING_RESULT_H
+
+#include "clang/Basic/LLVM.h"
+#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
+#include "llvm/Support/raw_ostream.h"
+#include <vector>
+
+namespace clang {
+
+class SourceManager;
+
+namespace clang_refactor {
+
+/// An abstract interface that defines common useful operations that can be
+/// performed on any refactoring result.
+class AbstractRefactoringResult {
+public:
+  virtual ~AbstractRefactoringResult() {}
+
+  virtual int getTypeId() const = 0;
+
+  virtual bool operator==(const AbstractRefactoringResult &Other) const = 0;
+  bool operator!=(const AbstractRefactoringResult &Other) const {
+    return !(*this == Other);
+  }
+
+  /// Refactoring results can be dumped by clang-refactor using the -dump
+  /// option.
+  virtual void dump(llvm::raw_ostream &OS, const SourceManager &SM,
+                    int Indent = 0) const = 0;
+};
+
+namespace internal {
+
+template <typename T> struct RefactoringResultTypeId {};
+template <> struct RefactoringResultTypeId<tooling::AtomicChanges> {
+  static constexpr int value = 0;
+};
+template <> struct RefactoringResultTypeId<tooling::SymbolOccurrences> {
+  static constexpr int value = 1;
+};
+
+template <typename T> bool isEqualTo(const T &LHS, const T &RHS) {
+  return LHS == RHS;
+}
+
+template <typename T>
+bool isEqualTo(const std::vector<T> &LHS, const std::vector<T> &RHS) {
+  for (const auto &I : llvm::zip(LHS, RHS)) {
+    if (!(std::get<0>(I) == std::get<1>(I)))
+      return false;
+  }
+  return true;
+}
+
+void dump(const tooling::SymbolOccurrence &Occurrences, llvm::raw_ostream &OS,
+          const SourceManager &SM, int Indent);
+
+template <typename T>
+void dump(const std::vector<T> &Values, llvm::raw_ostream &OS,
+          const SourceManager &SM, int Indent) {
+  for (const auto &I : Values)
+    dump(I, OS, SM, Indent);
+}
+
+} // end namespace internal
+
+/// Implements \c AbstractRefactoringResult for the specific refactoring result
+/// type.
+template <typename T>
+class RefactoringResultWrapper : public AbstractRefactoringResult {
+public:
+  RefactoringResultWrapper(T Value) : Value(std::move(Value)) {}
+
+  int getTypeId() const override {
+    return internal::RefactoringResultTypeId<T>::value;
+  }
+
+  bool operator==(const AbstractRefactoringResult &Other) const override {
+    if (getTypeId() != Other.getTypeId())
+      return false;
+    return internal::isEqualTo(
+        Value, static_cast<const RefactoringResultWrapper<T> &>(Other).Value);
+  }
+
+  void dump(llvm::raw_ostream &OS, const SourceManager &SM,
+            int Indent) const override {
+    internal::dump(Value, OS, SM, Indent);
+  }
+
+  T Value;
+};
+
+} // end namespace clang_refactor
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLS_CLANG_REFACTOR_REFACTORING_RESULT_H
Index: tools/clang-refactor/DumpRefactoringResult.cpp
===================================================================
--- /dev/null
+++ tools/clang-refactor/DumpRefactoringResult.cpp
@@ -0,0 +1,51 @@
+//===--- TestSupport.cpp - Clang-based refactoring tool -------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file implements routines that provide refactoring testing
+/// utilities.
+///
+//===----------------------------------------------------------------------===//
+
+#include "RefactoringResult.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
+
+using namespace llvm;
+using namespace clang;
+using namespace tooling;
+
+void clang_refactor::internal::dump(const SymbolOccurrence &Occurrence,
+                                    raw_ostream &OS, const SourceManager &SM,
+                                    int Indent) {
+  OS.indent(Indent);
+  switch (Occurrence.getKind()) {
+  case SymbolOccurrence::MatchingSymbol:
+    OS << "symbol ";
+    break;
+  }
+  for (const auto &I : llvm::enumerate(Occurrence.getNameRanges())) {
+    if (I.index())
+      OS << ", ";
+    SourceRange Range = I.value();
+    auto DumpLoc = [&](SourceLocation L) {
+      std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(L);
+      if (LocInfo.first.isInvalid()) {
+        OS << "<invalid>";
+        return;
+      }
+      OS << SM.getLineNumber(LocInfo.first, LocInfo.second) << ':'
+         << SM.getColumnNumber(LocInfo.first, LocInfo.second);
+    };
+    DumpLoc(Range.getBegin());
+    OS << " -> ";
+    DumpLoc(Range.getEnd());
+    OS << "\n";
+  }
+}
Index: tools/clang-refactor/ClangRefactor.cpp
===================================================================
--- /dev/null
+++ tools/clang-refactor/ClangRefactor.cpp
@@ -0,0 +1,337 @@
+//===--- ClangRefactor.cpp - Clang-based refactoring tool -----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file implements a clang-refactor tool that performs various
+/// source transformations.
+///
+//===----------------------------------------------------------------------===//
+
+#include "TestSupport.h"
+#include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/Refactoring.h"
+#include "clang/Tooling/Refactoring/RefactoringEngine.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/raw_ostream.h"
+#include <string>
+
+using namespace clang;
+using namespace tooling;
+using namespace clang_refactor;
+namespace cl = llvm::cl;
+
+namespace opts {
+
+static cl::OptionCategory CommonRefactorOptions("Common refactoring options");
+
+static cl::opt<bool> DumpResults(
+    "dump",
+    cl::desc("Dump the produced source replacements instead of applying them"),
+    cl::cat(CommonRefactorOptions), cl::sub(*cl::AllSubCommands));
+
+static cl::opt<bool>
+    NoDatabases("no-dbs",
+                cl::desc("Ignore external databases including Clang's "
+                         "compilation database and indexer stores"),
+                cl::cat(CommonRefactorOptions), cl::sub(*cl::AllSubCommands));
+
+static cl::opt<bool> Verbose("v", cl::desc("Use verbose output"),
+                             cl::cat(CommonRefactorOptions),
+                             cl::sub(*cl::AllSubCommands));
+} // end namespace opts
+
+namespace {
+
+/// A subcommand that corresponds to individual refactoring action.
+class RefactoringActionSubcommand : public cl::SubCommand {
+public:
+  RefactoringActionSubcommand(StringRef Name, StringRef Description,
+                              RefactoringAction &Action,
+                              cl::OptionCategory &Category, bool HasSelection)
+      : SubCommand(Name, Description), Action(&Action) {
+    Sources = llvm::make_unique<cl::list<std::string>>(
+        cl::Positional, cl::ZeroOrMore, cl::desc("<source0> [... <sourceN>]"),
+        cl::cat(Category), cl::sub(*this));
+    if (HasSelection) {
+      Selection = llvm::make_unique<cl::opt<std::string>>(
+          "selection", cl::desc("Source selection range"), cl::cat(Category),
+          cl::sub(*this));
+    }
+  }
+
+  ~RefactoringActionSubcommand() { unregisterSubCommand(); }
+
+  RefactoringAction &getAction() const { return *Action; }
+
+  StringRef getSelection() const {
+    assert(Selection && "selection not supported!");
+    return *Selection;
+  }
+
+  /// Returns a pointer to the parsed -selection=find(<filename>) option, or
+  /// null if the selection was given in another format.
+  const TestSelectionRangesInFile *getTestSelection() {
+    parseSelection();
+    return ParsedTestSelection ? ParsedTestSelection.getPointer() : nullptr;
+  }
+
+  ArrayRef<std::string> getSources() const { return *Sources; }
+
+private:
+  void parseSelection() {
+    assert(Selection && "selection not supported!");
+    if (IsSelectionParsed)
+      return;
+    IsSelectionParsed = true;
+    // FIXME: Support true selection ranges.
+    StringRef Value = *Selection;
+    if (Value.startswith("test:")) {
+      StringRef Filename = Value.drop_front(strlen("test:"));
+      ParsedTestSelection = findTestSelectionRangesIn(Filename);
+      if (!ParsedTestSelection) {
+        // A parsing error was already reported.
+        exit(1);
+      }
+    } else {
+      llvm::errs() << "error: '-selection' option must be specified using "
+                      "<file>:<line>:<column> or "
+                      "<file>:<line>:<column>-<line>:<column> format";
+      exit(1);
+    }
+  }
+
+  RefactoringAction *Action;
+  std::unique_ptr<cl::list<std::string>> Sources;
+  std::unique_ptr<cl::opt<std::string>> Selection;
+
+  bool IsSelectionParsed;
+  /// The parsed -selection=test:<filename> option.
+  Optional<TestSelectionRangesInFile> ParsedTestSelection;
+};
+
+/// Wraps the received refactoring results in a \c AbstractRefactoringResult.
+class TestRefactoringResultConsumer final : public RefactoringResultConsumer {
+public:
+  virtual void handleError(llvm::Error Err) override {
+    Result = std::move(Err);
+  }
+
+  virtual void handle(SymbolOccurrences Occurrences) override {
+    Result = llvm::make_unique<RefactoringResultWrapper<SymbolOccurrences>>(
+        std::move(Occurrences));
+  }
+
+  Optional<Expected<std::unique_ptr<AbstractRefactoringResult>>> Result;
+};
+
+class ToolRefactoringEngine final : public RefactoringEngine {
+public:
+  std::vector<std::unique_ptr<RefactoringActionSubcommand>> SubCommands;
+  PartialDiagnostic::StorageAllocator DiagnosticStorage;
+
+  void initialize() override {
+    RefactoringEngine::initialize();
+
+    // Create subcommands and command-line options.
+    for (const auto &Action : Actions) {
+      const ActionState &ActionInfo = State[Action.get()];
+      // Check if the selection option is supported.
+      bool HasSelection = false;
+      for (const auto &Rule : ActionInfo.Rules) {
+        if ((HasSelection = Rule->hasSelectionRequirement()))
+          break;
+      }
+
+      SubCommands.push_back(llvm::make_unique<RefactoringActionSubcommand>(
+          Action->getCommand(), Action->getDescription(), *Action,
+          opts::CommonRefactorOptions, HasSelection));
+    }
+  }
+
+  using TUCallbackType = llvm::function_ref<void(ASTContext &)>;
+
+  /// Parses the translation units that were given to the subcommand using
+  /// the 'sources' option and invokes the callback for each parsed
+  /// translation unit.
+  bool foreachTranslationUnit(RefactoringActionSubcommand &Subcommand,
+                              TUCallbackType Callback) {
+    std::unique_ptr<CompilationDatabase> Compilations;
+    if (opts::NoDatabases) {
+      // FIXME (Alex L): Support compilation options.
+      Compilations =
+          llvm::make_unique<clang::tooling::FixedCompilationDatabase>(
+              ".", std::vector<std::string>());
+    } else {
+      // FIXME (Alex L): Support compilation database.
+      llvm::errs() << "compilation databases are not supported yet!\n";
+      return true;
+    }
+
+    class ToolASTConsumer : public ASTConsumer {
+    public:
+      TUCallbackType Callback;
+      ToolASTConsumer(TUCallbackType Callback) : Callback(Callback) {}
+
+      void HandleTranslationUnit(ASTContext &Context) override {
+        Callback(Context);
+      }
+    };
+    class ActionWrapper {
+    public:
+      TUCallbackType Callback;
+      ActionWrapper(TUCallbackType Callback) : Callback(Callback) {}
+
+      std::unique_ptr<ASTConsumer> newASTConsumer() {
+        return llvm::make_unique<ToolASTConsumer>(std::move(Callback));
+      }
+    };
+
+    ClangTool Tool(*Compilations, Subcommand.getSources());
+    ActionWrapper ToolAction(std::move(Callback));
+    std::unique_ptr<tooling::FrontendActionFactory> Factory =
+        tooling::newFrontendActionFactory(&ToolAction);
+    return Tool.run(Factory.get());
+  }
+
+  /// Logs an individual refactoring action invocation to STDOUT.
+  void logInvocation(RefactoringActionSubcommand &Subcommand,
+                     const RefactoringRuleContext &Context) {
+    if (!opts::Verbose)
+      return;
+    llvm::outs() << "invoking action '" << Subcommand.getName() << "':\n";
+    if (Context.getSelectionRange().isValid()) {
+      SourceRange R = Context.getSelectionRange();
+      llvm::outs() << "  -selection=";
+      R.getBegin().print(llvm::outs(), Context.getSources());
+      llvm::outs() << " -> ";
+      R.getEnd().print(llvm::outs(), Context.getSources());
+      llvm::outs() << "\n";
+    }
+  }
+
+  /// Testing support: invokes the selection action for each selection range in
+  /// the test file.
+  bool
+  runSelectionTestInvocations(RefactoringActionSubcommand &Subcommand,
+                              RefactoringRuleContext &Context,
+                              const TestSelectionRangesInFile &TestSelections,
+                              ArrayRef<RefactoringActionRule *> MatchingRules) {
+    if (opts::Verbose)
+      TestSelections.dump(llvm::outs());
+    if (!opts::DumpResults) {
+      llvm::errs() << "error: test refactoring results can't be applied."
+                      " Did you forget -dump?\n";
+      return true;
+    }
+    return TestSelections.dispatch(
+        Context.getSources(),
+        [&](SourceRange R)
+            -> Expected<std::unique_ptr<AbstractRefactoringResult>> {
+          Context.setSelectionRange(R);
+          logInvocation(Subcommand, Context);
+          for (RefactoringActionRule *Rule : MatchingRules) {
+            if (!Rule->hasSelectionRequirement())
+              continue;
+            TestRefactoringResultConsumer Consumer;
+            Rule->invoke(Consumer, Context);
+            return std::move(*Consumer.Result);
+          }
+          llvm_unreachable("The action must have at least one selection rule");
+        });
+  }
+
+  bool invokeAction(RefactoringActionSubcommand &Subcommand) {
+    const RefactoringAction &Action = Subcommand.getAction();
+    const ActionState &ActionInfo = State[&Action];
+
+    // Find a set of matching rules.
+    SmallVector<RefactoringActionRule *, 4> MatchingRules;
+    llvm::StringSet<> MissingOptions;
+
+    bool HasSelection = false;
+    for (const auto &Rule : ActionInfo.Rules) {
+      if (Rule->hasSelectionRequirement()) {
+        HasSelection = true;
+        if (!Subcommand.getSelection().empty())
+          MatchingRules.push_back(Rule.get());
+        else
+          MissingOptions.insert("selection");
+      }
+      // FIXME (Alex L): Support custom options.
+    }
+    if (MatchingRules.empty()) {
+      llvm::errs() << "error: '" << Subcommand.getName()
+                   << "' can't be invoked with the given arguments:\n";
+      for (const auto &Opt : MissingOptions)
+        llvm::errs() << "  missing '-" << Opt.getKey() << "' option\n";
+      return true;
+    }
+
+    bool HasFailed = false;
+    if (foreachTranslationUnit(Subcommand, [&](ASTContext &AST) {
+          RefactoringRuleContext Context(AST.getSourceManager());
+          Context.setASTContext(AST);
+
+          if (HasSelection && Subcommand.getTestSelection()) {
+            // Use a slightly different invocation path for tests.
+            HasFailed = runSelectionTestInvocations(
+                Subcommand, Context, *Subcommand.getTestSelection(),
+                MatchingRules);
+            return;
+          }
+          // FIXME (Alex L): Implement non-test invocation path.
+          // FIXME (Alex L): If more than one initiation succeeded, then the
+          // rules are ambiguous.
+        }))
+      return true;
+    return HasFailed;
+  }
+};
+
+} // end anonymous namespace
+
+int main(int argc, const char **argv) {
+  ToolRefactoringEngine Engine;
+  Engine.initialize();
+
+  cl::HideUnrelatedOptions(opts::CommonRefactorOptions);
+  cl::ParseCommandLineOptions(
+      argc, argv, "Clang-based refactoring tool for C, C++ and Objective-C");
+  cl::PrintOptionValues();
+
+  // Figure out which action is given by the user.
+  auto It = llvm::find_if(
+      Engine.SubCommands,
+      [](const std::unique_ptr<RefactoringActionSubcommand> &SubCommand) {
+        return !!(*SubCommand);
+      });
+  if (It == Engine.SubCommands.end()) {
+    llvm::errs() << "error: no refactoring action given\n";
+    llvm::errs() << "note: the following actions are supported:\n";
+    for (const auto &Subcommand : Engine.SubCommands)
+      llvm::errs().indent(2) << Subcommand->getName() << "\n";
+    return 1;
+  }
+  RefactoringActionSubcommand &ActionCommand = **It;
+
+  ArrayRef<std::string> Sources = ActionCommand.getSources();
+  // When -no-dbs is used, at least one file (TU) must be given to any
+  // subcommand.
+  if (opts::NoDatabases && Sources.empty()) {
+    llvm::errs() << "error: must provide paths to the source files when "
+                    "'-no-dbs' is used\n";
+    return 1;
+  }
+  if (Engine.invokeAction(ActionCommand))
+    return 1;
+
+  return 0;
+}
Index: tools/clang-refactor/CMakeLists.txt
===================================================================
--- /dev/null
+++ tools/clang-refactor/CMakeLists.txt
@@ -0,0 +1,21 @@
+set(LLVM_LINK_COMPONENTS
+  Option
+  Support
+  )
+
+add_clang_executable(clang-refactor
+  ClangRefactor.cpp
+  DumpRefactoringResult.cpp
+  TestSupport.cpp
+  )
+
+target_link_libraries(clang-refactor
+  clangBasic
+  clangFrontend
+  clangRewrite
+  clangTooling
+  clangToolingCore
+  clangToolingRefactor
+  )
+
+install(TARGETS clang-refactor RUNTIME DESTINATION bin)
Index: tools/CMakeLists.txt
===================================================================
--- tools/CMakeLists.txt
+++ tools/CMakeLists.txt
@@ -12,6 +12,7 @@
 add_clang_subdirectory(c-index-test)
 
 add_clang_subdirectory(clang-rename)
+add_clang_subdirectory(clang-refactor)
 
 if(CLANG_ENABLE_ARCMT)
   add_clang_subdirectory(arcmt-test)
Index: test/clang-rename/Field.cpp
===================================================================
--- test/clang-rename/Field.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-class Baz {
-  int Foo; /* Test 1 */ // CHECK: int Bar;
-public:
-  Baz();
-};
-
-Baz::Baz() : Foo(0) /* Test 2 */ {}  // CHECK: Baz::Baz() : Bar(0)
-
-// Test 1.
-// RUN: clang-rename -offset=18 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
-// Test 2.
-// RUN: clang-rename -offset=89 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
-
-// To find offsets after modifying the file, use:
-//   grep -Ubo 'Foo.*' <file>
Index: test/Refactor/tool-test-support.c
===================================================================
--- /dev/null
+++ test/Refactor/tool-test-support.c
@@ -0,0 +1,34 @@
+// RUN: clang-refactor local-rename -selection=test:%s -no-dbs -dump -v %s 2>&1 | FileCheck %s
+
+/*range=*/int test;
+
+/*range named=*/int test2;
+
+/*range= +1*/int test3;
+
+/* range = +100 */int test4;
+
+/*range named =+0*/int test5;
+
+// CHECK: Test selection group '':
+// CHECK-NEXT:   106-106
+// CHECK-NEXT:   159-159
+// CHECK-NEXT:   198-198
+// CHECK-NEXT: Test selection group 'named':
+// CHECK-NEXT:   133-133
+// CHECK-NEXT:   219-219
+
+// CHECK: invoking action 'local-rename':
+// CHECK-NEXT: -selection={{.*}}tool-test-support.c:3:11
+
+// CHECK: invoking action 'local-rename':
+// CHECK-NEXT: -selection={{.*}}tool-test-support.c:7:15
+
+// CHECK: invoking action 'local-rename':
+// CHECK-NEXT: -selection={{.*}}tool-test-support.c:9:29
+
+// CHECK: invoking action 'local-rename':
+// CHECK-NEXT: -selection={{.*}}tool-test-support.c:5:17
+
+// CHECK: invoking action 'local-rename':
+// CHECK-NEXT: -selection={{.*}}tool-test-support.c:11:20
Index: test/Refactor/tool-common-options.c
===================================================================
--- /dev/null
+++ test/Refactor/tool-common-options.c
@@ -0,0 +1,6 @@
+// RUN: not clang-refactor 2>&1 | FileCheck --check-prefix=MISSING_ACTION %s
+// MISSING_ACTION: error: no refactoring action given
+// MISSING_ACTION-NEXT: note: the following actions are supported:
+
+// RUN: not clang-refactor local-rename -no-dbs 2>&1 | FileCheck --check-prefix=MISSING_SOURCES %s
+// MISSING_SOURCES: error: must provide paths to the source files when '-no-dbs' is used
Index: test/Refactor/LocalRename/Field.cpp
===================================================================
--- /dev/null
+++ test/Refactor/LocalRename/Field.cpp
@@ -0,0 +1,9 @@
+// RUN: clang-refactor local-rename -selection=test:%s -no-dbs -dump %s | FileCheck %s
+
+class Baz {
+  int /*range=*/Foo; // CHECK: symbol [[@LINE]]:17 -> [[@LINE]]:20
+public:
+  Baz();
+};
+
+Baz::Baz() : /*range=*/Foo(0) {}  // CHECK: symbol [[@LINE]]:24 -> [[@LINE]]:27
Index: test/CMakeLists.txt
===================================================================
--- test/CMakeLists.txt
+++ test/CMakeLists.txt
@@ -48,6 +48,7 @@
   clang-offload-bundler
   clang-import-test
   clang-rename
+  clang-refactor
   clang-diff
   )
   
Index: lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
===================================================================
--- lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
+++ lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
@@ -154,6 +154,12 @@
 };
 } // namespace
 
+std::vector<std::string> getUSRsForDeclaration(const NamedDecl *ND,
+                                               ASTContext &Context) {
+  AdditionalUSRFinder Finder(ND, Context);
+  return Finder.Find();
+}
+
 class NamedDeclFindingConsumer : public ASTConsumer {
 public:
   NamedDeclFindingConsumer(ArrayRef<unsigned> SymbolOffsets,
Index: lib/Tooling/Refactoring/Rename/RenamingAction.cpp
===================================================================
--- lib/Tooling/Refactoring/Rename/RenamingAction.cpp
+++ lib/Tooling/Refactoring/Rename/RenamingAction.cpp
@@ -22,17 +22,72 @@
 #include "clang/Lex/Preprocessor.h"
 #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"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/STLExtras.h"
 #include <string>
 #include <vector>
 
 using namespace llvm;
+using namespace clang;
+using namespace tooling;
+
+namespace {
+
+class LocalRename : 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";
+  }
+
+  static Expected<SymbolOccurrences>
+  findOccurrences(const ASTRefactoringContext &Context, const NamedDecl *ND) {
+    std::vector<std::string> USRs =
+        getUSRsForDeclaration(ND, Context.getASTContext());
+    std::string PrevName = ND->getNameAsString();
+    return getOccurrencesOfUSRs(
+        USRs, PrevName, Context.getASTContext().getTranslationUnitDecl());
+  }
+
+  class SymbolSelectionRequirement : public selection::Requirement {
+  public:
+    Expected<Optional<const NamedDecl *>>
+    evaluateSelection(const ASTRefactoringContext &Context,
+                      selection::SourceSelectionRange Selection) const {
+      const NamedDecl *ND = getNamedDeclAt(Context.getASTContext(),
+                                           Selection.getRange().getBegin());
+      if (!ND)
+        return None;
+      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(
+        findOccurrences, requiredSelection(SymbolSelectionRequirement())));
+    return Rules;
+  }
+};
+
+} // end anonymous namespace
 
 namespace clang {
 namespace tooling {
 
+std::unique_ptr<RefactoringAction> createLocalRenameAction() {
+  return llvm::make_unique<LocalRename>();
+}
+
 Expected<std::vector<AtomicChange>>
 createRenameReplacements(const SymbolOccurrences &Occurrences,
                          const SourceManager &SM,
Index: lib/Tooling/Refactoring/RefactoringEngine.cpp
===================================================================
--- /dev/null
+++ lib/Tooling/Refactoring/RefactoringEngine.cpp
@@ -0,0 +1,39 @@
+//===--- RefactoringEngine.cpp - Common refactoring engine code -----------===//
+//
+//                     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/RefactoringEngine.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/ADT/STLExtras.h"
+
+namespace clang {
+namespace tooling {
+
+// Forward declare the individual create*Action functions.
+#define REFACTORING_ACTION(Name)                                               \
+  std::unique_ptr<RefactoringAction> create##Name##Action();
+#include "clang/Tooling/Refactoring/RefactoringActionRegistry.def"
+
+} // end namespace tooling
+} // end namespace clang
+
+using namespace clang;
+using namespace tooling;
+
+void RefactoringEngine::initialize() {
+// Create the refactoring action.
+#define REFACTORING_ACTION(Name) Actions.push_back(create##Name##Action());
+#include "clang/Tooling/Refactoring/RefactoringActionRegistry.def"
+
+  // Create the refactoring action rules.
+  for (const auto &Action : Actions) {
+    RefactoringActionRules Rules = Action->createActionRules();
+    // FIXME (Alex L): Drop unsupported rules.
+    State[Action.get()].Rules = std::move(Rules);
+  }
+}
Index: lib/Tooling/Refactoring/CMakeLists.txt
===================================================================
--- lib/Tooling/Refactoring/CMakeLists.txt
+++ lib/Tooling/Refactoring/CMakeLists.txt
@@ -3,6 +3,7 @@
 add_clang_library(clangToolingRefactor
   ASTSelection.cpp
   AtomicChange.cpp
+  RefactoringEngine.cpp
   Rename/RenamingAction.cpp
   Rename/SymbolOccurrences.cpp
   Rename/USRFinder.cpp
Index: include/clang/module.modulemap
===================================================================
--- include/clang/module.modulemap
+++ include/clang/module.modulemap
@@ -138,6 +138,8 @@
   // importing the AST matchers library gives a link dependency on the AST
   // matchers (and thus the AST), which clang-format should not have.
   exclude header "Tooling/RefactoringCallbacks.h"
+
+  textual header "Tooling/Refactoring/RefactoringActionRegistry.def"
 }
 
 module Clang_ToolingCore {
Index: include/clang/Tooling/Refactoring/SourceSelectionConstraints.h
===================================================================
--- include/clang/Tooling/Refactoring/SourceSelectionConstraints.h
+++ include/clang/Tooling/Refactoring/SourceSelectionConstraints.h
@@ -12,6 +12,7 @@
 
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Tooling/Refactoring/RefactoringRuleContext.h"
 #include <type_traits>
 
 namespace clang {
@@ -41,8 +42,9 @@
 /// A custom selection requirement.
 class Requirement {
   /// Subclasses must implement 'T evaluateSelection(SelectionConstraint) const'
-  /// member function. \c T is used to determine the return type that is
-  /// passed to the refactoring rule's function.
+  /// or 'T evaluateSelection(const ASTRefactoringContext &,
+  /// SelectionConstraint) const' member function. \c T is used to determine
+  /// the return type that is passed to the refactoring rule's function.
   /// If T is \c DiagnosticOr<S> , then \c S is passed to the rule's function
   /// using move semantics.
   /// Otherwise, T is passed to the function directly using move semantics.
@@ -67,6 +69,24 @@
 struct EvaluateSelectionChecker<R (T::*)(A) const> : std::true_type {
   using ReturnType = R;
   using ArgType = A;
+
+  static R evaluateSelection(const T &Requirement, RefactoringRuleContext &,
+                             A Input) {
+    return Requirement.evaluateSelection(Input);
+  }
+};
+
+template <typename T, typename R, typename A>
+struct EvaluateSelectionChecker<R (T::*)(const ASTRefactoringContext &, A)
+                                    const> : std::true_type {
+  using ReturnType = R;
+  using ArgType = A;
+
+  static R evaluateSelection(const T &Requirement,
+                             RefactoringRuleContext &Context, A Input) {
+    return Requirement.evaluateSelection(
+        Context.getSpecificRuleContext<ASTRefactoringContext>(), Input);
+  }
 };
 
 template <typename T> class Identity : public Requirement {
@@ -101,6 +121,15 @@
                   &T::evaluateSelection)>::ArgType>::value,
           std::true_type, std::false_type>::type {};
 
+/// Declares an \c evaluateSelection static function that calls the corrept
+/// \c evaluateSelection method in the requirement.
+template <typename T>
+struct EvaluteRequirement
+    : internal::EvaluateSelectionChecker<decltype(&T::evaluateSelection)> {
+  using internal::EvaluateSelectionChecker<decltype(
+      &T::evaluateSelection)>::evaluateSelection;
+};
+
 } // end namespace traits
 } // end namespace selection
 } // end namespace tooling
Index: include/clang/Tooling/Refactoring/Rename/USRFindingAction.h
===================================================================
--- include/clang/Tooling/Refactoring/Rename/USRFindingAction.h
+++ include/clang/Tooling/Refactoring/Rename/USRFindingAction.h
@@ -23,6 +23,7 @@
 
 namespace clang {
 class ASTConsumer;
+class ASTContext;
 class CompilerInstance;
 class NamedDecl;
 
@@ -37,6 +38,10 @@
 /// - A destructor is canonicalized to its class.
 const NamedDecl *getCanonicalSymbolDeclaration(const NamedDecl *FoundDecl);
 
+/// Returns the set of USRs that correspond to the given declaration.
+std::vector<std::string> getUSRsForDeclaration(const NamedDecl *ND,
+                                               ASTContext &Context);
+
 struct USRFindingAction {
   USRFindingAction(ArrayRef<unsigned> SymbolOffsets,
                    ArrayRef<std::string> QualifiedNames, bool Force)
Index: include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h
===================================================================
--- include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h
+++ include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h
@@ -77,6 +77,18 @@
     return RangeOrNumRanges;
   }
 
+  bool operator==(const SymbolOccurrence &Other) const {
+    if (Kind != Other.Kind)
+      return false;
+    if (getNameRanges().size() != Other.getNameRanges().size())
+      return false;
+    for (size_t I = 0, E = getNameRanges().size(); I != E; ++I) {
+      if (getNameRanges()[I] != Other.getNameRanges()[I])
+        return false;
+    }
+    return true;
+  }
+
 private:
   OccurrenceKind Kind;
   std::unique_ptr<SourceRange[]> MultipleRanges;
Index: include/clang/Tooling/Refactoring/RefactoringRuleContext.h
===================================================================
--- include/clang/Tooling/Refactoring/RefactoringRuleContext.h
+++ include/clang/Tooling/Refactoring/RefactoringRuleContext.h
@@ -13,8 +13,23 @@
 #include "clang/Basic/SourceManager.h"
 
 namespace clang {
+
+class ASTContext;
+
 namespace tooling {
 
+/// A refactoring rule context that's available to the refactoring rules that
+/// require the AST.
+class ASTRefactoringContext {
+public:
+  ASTRefactoringContext(ASTContext &Context) : Context(Context) {}
+
+  ASTContext &getASTContext() const { return Context; }
+
+private:
+  ASTContext &Context;
+};
+
 /// The refactoring rule context stores all of the inputs that might be needed
 /// by a refactoring action rule. It can create the specialized
 /// \c ASTRefactoringOperation or \c PreprocessorRefactoringOperation values
@@ -38,15 +53,31 @@
 
   void setSelectionRange(SourceRange R) { SelectionRange = R; }
 
+  ASTContext *getASTContext() const { return AST; }
+
+  void setASTContext(ASTContext &Context) { AST = &Context; }
+
+  /// Returns a specific refactoring rule context like \c ASTRefactoringContext.
+  template <typename T> T getSpecificRuleContext();
+
 private:
   /// The source manager for the translation unit / file on which a refactoring
   /// action might operate on.
   const SourceManager &SM;
   /// An optional source selection range that's commonly used to represent
   /// a selection in an editor.
   SourceRange SelectionRange;
+  /// An optional AST for the translation unit on which a refactoring action
+  /// might operate on.
+  ASTContext *AST = nullptr;
 };
 
+template <>
+inline ASTRefactoringContext RefactoringRuleContext::getSpecificRuleContext() {
+  assert(AST && "can't create an ASTRefactoringContext without AST");
+  return ASTRefactoringContext(*AST);
+}
+
 } // end namespace tooling
 } // end namespace clang
 
Index: include/clang/Tooling/Refactoring/RefactoringEngine.h
===================================================================
--- /dev/null
+++ include/clang/Tooling/Refactoring/RefactoringEngine.h
@@ -0,0 +1,42 @@
+//===--- RefactoringEngine.h - Clang refactoring library ------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ENGINE_H
+#define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ENGINE_H
+
+#include "clang/Tooling/Refactoring/RefactoringAction.h"
+#include "clang/Tooling/Refactoring/RefactoringActionRules.h"
+#include "llvm/ADT/DenseMap.h"
+#include <vector>
+
+namespace clang {
+namespace tooling {
+
+/// A base class for an editor or a tool refactoring engine.
+///
+/// Contains the list of supported actions and refactoring action rules.
+class RefactoringEngine {
+public:
+  virtual ~RefactoringEngine() {}
+
+  virtual void initialize();
+
+protected:
+  struct ActionState {
+    RefactoringActionRules Rules;
+  };
+
+  std::vector<std::unique_ptr<RefactoringAction>> Actions;
+  llvm::DenseMap<const RefactoringAction *, ActionState> State;
+};
+
+} // end namespace tooling
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ENGINE_H
Index: include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
===================================================================
--- include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
+++ include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
@@ -26,7 +26,8 @@
 /// A specialized refactoring action rule that calls the stored function once
 /// all the of the requirements are fullfilled. The values produced during the
 /// evaluation of requirements are passed to the stored function.
-template <typename FunctionType, typename... RequirementTypes>
+template <typename FunctionType, typename SpecificContextType,
+          typename... RequirementTypes>
 class PlainFunctionRule final : public RefactoringActionRule {
 public:
   PlainFunctionRule(FunctionType Function,
@@ -39,6 +40,10 @@
                       llvm::index_sequence_for<RequirementTypes...>());
   }
 
+  bool hasSelectionRequirement() override {
+    return traits::HasSelectionRequirement<RequirementTypes...>::value;
+  }
+
 private:
   /// Returns \c T when given \c Expected<Optional<T>>, or \c T otherwise.
   template <typename T>
@@ -76,10 +81,38 @@
     return None;
   }
 
+  template <typename ContextType, typename ValueType, size_t... Is>
+  void invokeImplDispatch(
+      RefactoringResultConsumer &Consumer, RefactoringRuleContext &Context,
+      llvm::index_sequence<Is...>, ValueType &Values,
+      typename std::enable_if<
+          std::is_same<ContextType, ASTRefactoringContext>::value>::type * =
+          nullptr) {
+    auto Result =
+        Function(Context.getSpecificRuleContext<ContextType>(),
+                 unwrapRequirementResult(std::move(std::get<Is>(Values)))...);
+    if (!Result)
+      return Consumer.handleError(Result.takeError());
+    Consumer.handle(std::move(*Result));
+  }
+
+  template <typename ContextType, typename ValueType, size_t... Is>
+  void invokeImplDispatch(
+      RefactoringResultConsumer &Consumer, RefactoringRuleContext &,
+      llvm::index_sequence<Is...>, ValueType &Values,
+      typename std::enable_if<std::is_same<ContextType, void>::value>::type * =
+          nullptr) {
+    auto Result =
+        Function(unwrapRequirementResult(std::move(std::get<Is>(Values)))...);
+    if (!Result)
+      return Consumer.handleError(Result.takeError());
+    Consumer.handle(std::move(*Result));
+  }
+
   template <size_t... Is>
   void invokeImpl(RefactoringResultConsumer &Consumer,
                   RefactoringRuleContext &Context,
-                  llvm::index_sequence<Is...>) {
+                  llvm::index_sequence<Is...> Seq) {
     // Initiate the operation.
     auto Values =
         std::make_tuple(std::get<Is>(Requirements).evaluate(Context)...);
@@ -96,11 +129,8 @@
       return Consumer.handleError(std::move(Error));
     }
     // Perform the operation.
-    auto Result =
-        Function(unwrapRequirementResult(std::move(std::get<Is>(Values)))...);
-    if (!Result)
-      return Consumer.handleError(Result.takeError());
-    Consumer.handle(std::move(*Result));
+    invokeImplDispatch<SpecificContextType, decltype(Values), Is...>(
+        Consumer, Context, Seq, Values);
   }
 
   FunctionType Function;
@@ -113,6 +143,12 @@
 template <typename T, typename R, typename... Args>
 struct LambdaDeducer<R (T::*)(Args...) const> {
   using ReturnType = R;
+  using FunctionType = R (*)(Args...);
+};
+template <typename T, typename R, typename... Args>
+struct LambdaDeducer<R (T::*)(const ASTRefactoringContext &, Args...) const> {
+  using ReturnType = R;
+  using FunctionType = R (*)(const ASTRefactoringContext &, Args...);
 };
 
 } // end namespace internal
Index: include/clang/Tooling/Refactoring/RefactoringActionRules.h
===================================================================
--- include/clang/Tooling/Refactoring/RefactoringActionRules.h
+++ include/clang/Tooling/Refactoring/RefactoringActionRules.h
@@ -15,6 +15,9 @@
 
 namespace clang {
 namespace tooling {
+
+class ASTRefactoringContext;
+
 namespace refactoring_action_rules {
 
 /// Creates a new refactoring action rule that invokes the given function once
@@ -47,22 +50,38 @@
   static_assert(traits::IsRequirement<RequirementTypes...>::value,
                 "invalid refactoring action rule requirement");
   return llvm::make_unique<internal::PlainFunctionRule<
-      decltype(RefactoringFunction), RequirementTypes...>>(
+      decltype(RefactoringFunction), /*SpecificContextType=*/void,
+      RequirementTypes...>>(RefactoringFunction,
+                            std::make_tuple(Requirements...));
+}
+
+template <typename ResultType, typename... RequirementTypes>
+std::unique_ptr<RefactoringActionRule>
+createRefactoringRule(Expected<ResultType> (*RefactoringFunction)(
+                          const ASTRefactoringContext &,
+                          typename RequirementTypes::OutputType...),
+                      const RequirementTypes &... Requirements) {
+  static_assert(tooling::traits::IsValidRefactoringResult<ResultType>::value,
+                "invalid refactoring result type");
+  static_assert(traits::IsRequirement<RequirementTypes...>::value,
+                "invalid refactoring action rule requirement");
+  return llvm::make_unique<internal::PlainFunctionRule<
+      decltype(RefactoringFunction),
+      /*SpecificContextType=*/ASTRefactoringContext, RequirementTypes...>>(
       RefactoringFunction, std::make_tuple(Requirements...));
 }
 
 template <
     typename Callable, typename... RequirementTypes,
     typename Fn = decltype(&Callable::operator()),
     typename ResultType = typename internal::LambdaDeducer<Fn>::ReturnType,
     bool IsNonCapturingLambda = std::is_convertible<
-        Callable,
-        ResultType (*)(typename RequirementTypes::OutputType...)>::value,
+        Callable, typename internal::LambdaDeducer<Fn>::FunctionType>::value,
     typename = typename std::enable_if<IsNonCapturingLambda>::type>
 std::unique_ptr<RefactoringActionRule>
 createRefactoringRule(const Callable &C,
                       const RequirementTypes &... Requirements) {
-  ResultType (*Func)(typename RequirementTypes::OutputType...) = C;
+  typename internal::LambdaDeducer<Fn>::FunctionType Func = C;
   return createRefactoringRule(Func, Requirements...);
 }
 
Index: include/clang/Tooling/Refactoring/RefactoringActionRuleRequirementsInternal.h
===================================================================
--- include/clang/Tooling/Refactoring/RefactoringActionRuleRequirementsInternal.h
+++ include/clang/Tooling/Refactoring/RefactoringActionRuleRequirementsInternal.h
@@ -58,7 +58,9 @@
     Optional<InputT> Value = InputT::evaluate(Context);
     if (!Value)
       return None;
-    return std::move(Requirement.evaluateSelection(*Value));
+    return std::move(
+        selection::traits::EvaluteRequirement<RequirementT>::evaluateSelection(
+            Requirement, Context, *Value));
   }
 
 private:
@@ -82,6 +84,20 @@
     : std::conditional<std::is_base_of<internal::RequirementBase, T>::value,
                        std::true_type, std::false_type>::type {};
 
+/// A type trait that returns true when the given type has at least one source
+/// selection requirement.
+template <typename First, typename... Rest>
+struct HasSelectionRequirement
+    : std::conditional<HasSelectionRequirement<First>::value ||
+                           HasSelectionRequirement<Rest...>::value,
+                       std::true_type, std::false_type>::type {};
+
+template <typename I, typename O, typename R>
+struct HasSelectionRequirement<internal::SourceSelectionRequirement<I, O, R>>
+    : std::true_type {};
+
+template <typename T> struct HasSelectionRequirement<T> : std::false_type {};
+
 } // end namespace traits
 } // end namespace refactoring_action_rules
 } // end namespace tooling
Index: include/clang/Tooling/Refactoring/RefactoringActionRule.h
===================================================================
--- include/clang/Tooling/Refactoring/RefactoringActionRule.h
+++ include/clang/Tooling/Refactoring/RefactoringActionRule.h
@@ -30,6 +30,10 @@
   /// consumer to propagate the result of the refactoring action.
   virtual void invoke(RefactoringResultConsumer &Consumer,
                       RefactoringRuleContext &Context) = 0;
+
+  /// Returns true when the rule has a source selection requirement that has
+  /// to be fullfilled before refactoring can be performed.
+  virtual bool hasSelectionRequirement() = 0;
 };
 
 /// A set of refactoring action rules that should have unique initiation
Index: include/clang/Tooling/Refactoring/RefactoringActionRegistry.def
===================================================================
--- /dev/null
+++ include/clang/Tooling/Refactoring/RefactoringActionRegistry.def
@@ -0,0 +1,7 @@
+#ifndef REFACTORING_ACTION
+#define REFACTORING_ACTION(Name)
+#endif
+
+REFACTORING_ACTION(LocalRename)
+
+#undef REFACTORING_ACTION
Index: include/clang/Tooling/Refactoring/RefactoringAction.h
===================================================================
--- /dev/null
+++ include/clang/Tooling/Refactoring/RefactoringAction.h
@@ -0,0 +1,38 @@
+//===--- RefactoringAction.h - Clang refactoring library ------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_H
+#define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_H
+
+#include "clang/Basic/LLVM.h"
+#include "clang/Tooling/Refactoring/RefactoringActionRules.h"
+#include <vector>
+
+namespace clang {
+namespace tooling {
+
+class RefactoringAction {
+public:
+  /// Returns the name of the subcommand that's used by clang-refactor for this
+  /// action.
+  virtual StringRef getCommand() const = 0;
+
+  virtual StringRef getDescription() const = 0;
+
+  /// Returns a set of refactoring actions rules that are defined by this
+  /// action.
+  virtual RefactoringActionRules createActionRules() const = 0;
+
+  virtual ~RefactoringAction() {}
+};
+
+} // end namespace tooling
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_H
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to