[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-09-12 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman closed this revision.
arphaman added a comment.

Committed in r313025


Repository:
  rL LLVM

https://reviews.llvm.org/D37210



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-09-01 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman updated this revision to Diff 113521.
arphaman added a comment.

Rebase on ToT


Repository:
  rL LLVM

https://reviews.llvm.org/D37210

Files:
  include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
  unittests/Tooling/RefactoringActionRulesTest.cpp


Index: unittests/Tooling/RefactoringActionRulesTest.cpp
===
--- unittests/Tooling/RefactoringActionRulesTest.cpp
+++ unittests/Tooling/RefactoringActionRulesTest.cpp
@@ -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 @@
   EXPECT_EQ(Message, "bad selection");
 }
 
+Optional findOccurrences(RefactoringActionRule ,
+RefactoringRuleContext ) {
+  class Consumer final : public RefactoringResultConsumer {
+void handleError(llvm::Error) override {}
+void handle(SymbolOccurrences Occurrences) override {
+  Result = std::move(Occurrences);
+}
+
+  public:
+Optional Result;
+  };
+
+  Consumer C;
+  Rule.invoke(C, Context);
+  return std::move(C.Result);
+}
+
+TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) {
+  auto Rule = createRefactoringRule(
+  [](selection::SourceSelectionRange Selection)
+  -> Expected {
+SymbolOccurrences Occurrences;
+Occurrences.push_back(SymbolOccurrence(
+SymbolName("test"), SymbolOccurrence::MatchingSymbol,
+Selection.getRange().getBegin()));
+return Occurrences;
+  },
+  requiredSelection(
+  selection::identity()));
+
+  RefactoringRuleContext RefContext(Context.Sources);
+  SourceLocation Cursor =
+  Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID());
+  RefContext.setSelectionRange({Cursor, Cursor});
+  Optional 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
Index: include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
===
--- include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
+++ include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
@@ -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 @@
 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(


Index: unittests/Tooling/RefactoringActionRulesTest.cpp
===
--- unittests/Tooling/RefactoringActionRulesTest.cpp
+++ unittests/Tooling/RefactoringActionRulesTest.cpp
@@ -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 @@
   EXPECT_EQ(Message, "bad selection");
 }
 
+Optional findOccurrences(RefactoringActionRule ,
+RefactoringRuleContext ) {
+  class Consumer final : public RefactoringResultConsumer {
+void handleError(llvm::Error) override {}
+void handle(SymbolOccurrences Occurrences) override {
+  Result = std::move(Occurrences);
+}
+
+  public:
+Optional Result;
+  };
+
+  Consumer C;
+  Rule.invoke(C, Context);
+  return std::move(C.Result);
+}
+
+TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) {
+  auto Rule = createRefactoringRule(
+  [](selection::SourceSelectionRange Selection)
+  -> Expected {
+SymbolOccurrences Occurrences;
+Occurrences.push_back(SymbolOccurrence(
+SymbolName("test"), SymbolOccurrence::MatchingSymbol,
+Selection.getRange().getBegin()));
+return Occurrences;
+  },
+  requiredSelection(
+  selection::identity()));
+
+  RefactoringRuleContext RefContext(Context.Sources);
+  SourceLocation Cursor =
+  

[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-08-30 Thread Jonas Toth via Phabricator via cfe-commits
JonasToth resigned from this revision.
JonasToth added a comment.
This revision is now accepted and ready to land.

sry. misconfigured herald :(


Repository:
  rL LLVM

https://reviews.llvm.org/D37210



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-08-30 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman updated this revision to Diff 113224.
arphaman marked an inline comment as done.
arphaman added a comment.
Herald added a reviewer: JonasToth.
This revision now requires review to proceed.

Rebase on top of https://reviews.llvm.org/D37291


Repository:
  rL LLVM

https://reviews.llvm.org/D37210

Files:
  include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
  unittests/Tooling/RefactoringActionRulesTest.cpp

Index: unittests/Tooling/RefactoringActionRulesTest.cpp
===
--- unittests/Tooling/RefactoringActionRulesTest.cpp
+++ unittests/Tooling/RefactoringActionRulesTest.cpp
@@ -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"
@@ -32,10 +33,19 @@
   std::string DefaultCode = std::string(100, 'a');
 };
 
+class DefaultRefactoringResultConsumer : public RefactoringResultConsumer {
+public:
+  void handleInitiationFailure() override {}
+  void handleInitiationError(llvm::Error) override {}
+  void handleInvocationError(llvm::Error) override {}
+  void handle(AtomicChanges) override {}
+  void handle(SymbolOccurrences) override {}
+};
+
 Expected
 createReplacements(const std::unique_ptr ,
RefactoringRuleContext ) {
-  class Consumer final : public RefactoringResultConsumer {
+  class Consumer final : public DefaultRefactoringResultConsumer {
 void handleInitiationFailure() {
   Result = Expected(None);
 }
@@ -181,4 +191,48 @@
   EXPECT_EQ(Message, "bad selection");
 }
 
+Optional findOccurrences(RefactoringActionRule ,
+RefactoringRuleContext ) {
+  class Consumer final : public DefaultRefactoringResultConsumer {
+void handle(SymbolOccurrences Occurrences) override {
+  Result = std::move(Occurrences);
+}
+
+  public:
+Optional Result;
+  };
+
+  Consumer C;
+  Rule.invoke(C, Context);
+  return std::move(C.Result);
+}
+
+TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) {
+  auto Rule = createRefactoringRule(
+  [](selection::SourceSelectionRange Selection)
+  -> Expected {
+SymbolOccurrences Occurrences;
+Occurrences.push_back(SymbolOccurrence(
+SymbolName("test"), SymbolOccurrence::MatchingSymbol,
+Selection.getRange().getBegin()));
+return Occurrences;
+  },
+  requiredSelection(
+  selection::identity()));
+
+  RefactoringRuleContext RefContext(Context.Sources);
+  SourceLocation Cursor =
+  Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID());
+  RefContext.setSelectionRange({Cursor, Cursor});
+  Optional 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
Index: include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
===
--- include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
+++ include/clang/Tooling/Refactoring/RefactoringResultConsumer.h
@@ -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 {
@@ -37,6 +38,10 @@
 
   /// Handles the source replacements that are produced by a refactoring action.
   virtual void handle(AtomicChanges SourceReplacements) = 0;
+
+  /// Handles the symbol occurrences that are found by an interactive
+  /// refactoring action.
+  virtual void handle(SymbolOccurrences Occurrences) = 0;
 };
 
 namespace traits {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-08-30 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman added inline comments.



Comment at: include/clang/Tooling/Refactoring/RefactoringActionRule.h:29
+SourceChangeRefactoringRuleKind,
+FindSymbolOccurrencesRefactoringRuleKind
+  };

hokein wrote:
> I might miss some context here. As per your comment in 
> https://reviews.llvm.org/D36075#inline-323769, you'll try to remove this tag, 
> so I think we will hold off this patch until that is done?
Yeah, that would be better. I will remove the tag first.


Repository:
  rL LLVM

https://reviews.llvm.org/D37210



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-08-30 Thread Haojian Wu via Phabricator via cfe-commits
hokein added inline comments.



Comment at: include/clang/Tooling/Refactoring/RefactoringActionRule.h:29
+SourceChangeRefactoringRuleKind,
+FindSymbolOccurrencesRefactoringRuleKind
+  };

I might miss some context here. As per your comment in 
https://reviews.llvm.org/D36075#inline-323769, you'll try to remove this tag, 
so I think we will hold off this patch until that is done?



Comment at: unittests/Tooling/RefactoringActionRulesTest.cpp:167
+Expected
+findOccurrences(const std::unique_ptr ,
+RefactoringRuleContext ) {

I'm not a fan of passing a const smart_pointer& as function parameter. The 
function being called doesn't care about the lifetime management, so I'd use a 
raw pointer here.


Repository:
  rL LLVM

https://reviews.llvm.org/D37210



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-08-29 Thread Eric Liu via Phabricator via cfe-commits
ioeric accepted this revision.
ioeric added a comment.
This revision is now accepted and ready to land.

Lgtm


Repository:
  rL LLVM

https://reviews.llvm.org/D37210



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37210: [refactor] add a refactoring action rule that returns symbol occurrences

2017-08-28 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman created this revision.

This patch adds a second kind of refactoring action rule that produces symbol 
occurrences. It will be used by the updated `clang-refactor` patch at 
https://reviews.llvm.org/D36574.


Repository:
  rL LLVM

https://reviews.llvm.org/D37210

Files:
  include/clang/Tooling/Refactoring/RefactoringActionRule.h
  include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
  unittests/Tooling/RefactoringActionRulesTest.cpp

Index: unittests/Tooling/RefactoringActionRulesTest.cpp
===
--- unittests/Tooling/RefactoringActionRulesTest.cpp
+++ unittests/Tooling/RefactoringActionRulesTest.cpp
@@ -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"
@@ -162,4 +163,41 @@
   EXPECT_EQ(Message, "bad selection");
 }
 
+Expected
+findOccurrences(const std::unique_ptr ,
+RefactoringRuleContext ) {
+  return cast(*Rule)
+  .findSymbolOccurrences(Context);
+}
+
+TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) {
+  auto Rule = createRefactoringRule(
+  [](selection::SourceSelectionRange Selection)
+  -> Expected {
+SymbolOccurrences Occurrences;
+Occurrences.push_back(SymbolOccurrence(
+SymbolName("test"), SymbolOccurrence::MatchingSymbol,
+Selection.getRange().getBegin()));
+return Occurrences;
+  },
+  requiredSelection(
+  selection::identity()));
+
+  RefactoringRuleContext RefContext(Context.Sources);
+  SourceLocation Cursor =
+  Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID());
+  RefContext.setSelectionRange({Cursor, Cursor});
+  Expected Result =
+  findOccurrences(Rule, RefContext);
+
+  ASSERT_FALSE(!Result);
+  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
Index: include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
===
--- include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
+++ include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h
@@ -26,18 +26,26 @@
 /// 'perform' method from the specific refactoring method.
 template  struct SpecificRefactoringRuleAdapter {};
 
-template <>
-class SpecificRefactoringRuleAdapter
-: public SourceChangeRefactoringRule {
-public:
-  virtual Expected
-  perform(RefactoringRuleContext ) = 0;
-
-  Expected
-  createSourceReplacements(RefactoringRuleContext ) final override {
-return perform(Context);
-  }
-};
+#define CLANG_REFACTOR_DEFINE_RULE_ADAPTER(ResultType, RuleType, OverrideName) \
+  template <>  \
+  class SpecificRefactoringRuleAdapter : public RuleType { \
+  public:  \
+virtual Expected \
+perform(RefactoringRuleContext ) = 0;  \
+   \
+Expected \
+OverrideName(RefactoringRuleContext ) final override { \
+  return perform(Context); \
+}  \
+  };
+
+CLANG_REFACTOR_DEFINE_RULE_ADAPTER(AtomicChanges, SourceChangeRefactoringRule,
+   createSourceReplacements)
+CLANG_REFACTOR_DEFINE_RULE_ADAPTER(SymbolOccurrences,
+   FindSymbolOccurrencesRefactoringRule,
+   findSymbolOccurrences)
+
+#undef CLANG_REFACTOR_DEFINE_RULE_ADAPTER
 
 /// A specialized refactoring action rule that calls the stored function once
 /// all the of the requirements are fullfilled. The values produced during the
Index: include/clang/Tooling/Refactoring/RefactoringActionRule.h
===
--- include/clang/Tooling/Refactoring/RefactoringActionRule.h
+++ include/clang/Tooling/Refactoring/RefactoringActionRule.h
@@ -12,6 +12,7 @@
 
 #include "clang/Basic/LLVM.h"
 #include "clang/Tooling/Refactoring/AtomicChange.h"
+#include "clang/Tooling/Refactoring/Rename/SymbolOccurrences.h"