forked from OSchip/llvm-project
[refactor] add a refactoring action rule that returns symbol occurrences
Differential Revision: https://reviews.llvm.org/D36574 llvm-svn: 313025
This commit is contained in:
parent
403fd9d636
commit
3529a94b43
|
@ -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>(
|
||||
|
|
|
@ -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, ReturnInitiationDiagnostic) {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue