r299073 - Add `replace` interface with range in AtomicChange.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 30 06:07:39 PDT 2017
Author: hokein
Date: Thu Mar 30 08:07:38 2017
New Revision: 299073
URL: http://llvm.org/viewvc/llvm-project?rev=299073&view=rev
Log:
Add `replace` interface with range in AtomicChange.
Reviewers: ioeric
Reviewed By: ioeric
Subscribers: alexshap, klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D31492
Modified:
cfe/trunk/include/clang/Tooling/Refactoring/AtomicChange.h
cfe/trunk/lib/Tooling/Refactoring/AtomicChange.cpp
cfe/trunk/unittests/Tooling/RefactoringTest.cpp
Modified: cfe/trunk/include/clang/Tooling/Refactoring/AtomicChange.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/AtomicChange.h?rev=299073&r1=299072&r2=299073&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Refactoring/AtomicChange.h (original)
+++ cfe/trunk/include/clang/Tooling/Refactoring/AtomicChange.h Thu Mar 30 08:07:38 2017
@@ -70,6 +70,12 @@ public:
/// \brief Returns the error message or an empty string if it does not exist.
const std::string &getError() const { return Error; }
+ /// \brief Adds a replacement that replaces the given Range with
+ /// ReplacementText.
+ /// \returns An llvm::Error carrying ReplacementError on error.
+ llvm::Error replace(const SourceManager &SM, const CharSourceRange &Range,
+ llvm::StringRef ReplacementText);
+
/// \brief Adds a replacement that replaces range [Loc, Loc+Length) with
/// \p Text.
/// \returns An llvm::Error carrying ReplacementError on error.
Modified: cfe/trunk/lib/Tooling/Refactoring/AtomicChange.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/AtomicChange.cpp?rev=299073&r1=299072&r2=299073&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Refactoring/AtomicChange.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/AtomicChange.cpp Thu Mar 30 08:07:38 2017
@@ -132,6 +132,12 @@ AtomicChange AtomicChange::convertFromYA
return E;
}
+llvm::Error AtomicChange::replace(const SourceManager &SM,
+ const CharSourceRange &Range,
+ llvm::StringRef ReplacementText) {
+ return Replaces.add(Replacement(SM, Range, ReplacementText));
+}
+
llvm::Error AtomicChange::replace(const SourceManager &SM, SourceLocation Loc,
unsigned Length, llvm::StringRef Text) {
return Replaces.add(Replacement(SM, Loc, Length, Text));
Modified: cfe/trunk/unittests/Tooling/RefactoringTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RefactoringTest.cpp?rev=299073&r1=299072&r2=299073&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/RefactoringTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/RefactoringTest.cpp Thu Mar 30 08:07:38 2017
@@ -1092,7 +1092,7 @@ TEST(DeduplicateByFileTest, NonExistingF
class AtomicChangeTest : public ::testing::Test {
protected:
- void setUp() {
+ void SetUp() override {
DefaultFileID = Context.createInMemoryFile("input.cpp", DefaultCode);
DefaultLoc = Context.Sources.getLocForStartOfFile(DefaultFileID)
.getLocWithOffset(20);
@@ -1107,7 +1107,6 @@ class AtomicChangeTest : public ::testin
};
TEST_F(AtomicChangeTest, AtomicChangeToYAML) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err =
Change.insert(Context.Sources, DefaultLoc, "aa", /*InsertAfter=*/false);
@@ -1140,7 +1139,6 @@ TEST_F(AtomicChangeTest, AtomicChangeToY
}
TEST_F(AtomicChangeTest, YAMLToAtomicChange) {
- setUp();
std::string YamlContent = "---\n"
"Key: 'input.cpp:20'\n"
"FilePath: input.cpp\n"
@@ -1187,14 +1185,12 @@ TEST_F(AtomicChangeTest, YAMLToAtomicCha
}
TEST_F(AtomicChangeTest, CheckKeyAndKeyFile) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
EXPECT_EQ("input.cpp:20", Change.getKey());
EXPECT_EQ("input.cpp", Change.getFilePath());
}
TEST_F(AtomicChangeTest, Replace) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err = Change.replace(Context.Sources, DefaultLoc, 2, "aa");
ASSERT_TRUE(!Err);
@@ -1209,8 +1205,18 @@ TEST_F(AtomicChangeTest, Replace) {
EXPECT_EQ(Change.getReplacements().size(), 1u);
}
+TEST_F(AtomicChangeTest, ReplaceWithRange) {
+ AtomicChange Change(Context.Sources, DefaultLoc);
+ SourceLocation End = DefaultLoc.getLocWithOffset(20);
+ llvm::Error Err = Change.replace(
+ Context.Sources, CharSourceRange::getCharRange(DefaultLoc, End), "aa");
+ ASSERT_TRUE(!Err);
+ EXPECT_EQ(Change.getReplacements().size(), 1u);
+ EXPECT_EQ(*Change.getReplacements().begin(),
+ Replacement(Context.Sources, DefaultLoc, 20, "aa"));
+}
+
TEST_F(AtomicChangeTest, InsertBefore) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err = Change.insert(Context.Sources, DefaultLoc, "aa");
ASSERT_TRUE(!Err);
@@ -1225,7 +1231,6 @@ TEST_F(AtomicChangeTest, InsertBefore) {
}
TEST_F(AtomicChangeTest, InsertAfter) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err = Change.insert(Context.Sources, DefaultLoc, "aa");
ASSERT_TRUE(!Err);
@@ -1240,7 +1245,6 @@ TEST_F(AtomicChangeTest, InsertAfter) {
}
TEST_F(AtomicChangeTest, InsertBeforeWithInvalidLocation) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err =
Change.insert(Context.Sources, DefaultLoc, "a", /*InsertAfter=*/false);
@@ -1254,11 +1258,9 @@ TEST_F(AtomicChangeTest, InsertBeforeWit
std::move(Err), replacement_error::wrong_file_path,
Replacement(Context.Sources, DefaultLoc, 0, "a"),
Replacement(Context.Sources, SourceLocation(), 0, "a")));
-
}
TEST_F(AtomicChangeTest, InsertBeforeToWrongFile) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err =
Change.insert(Context.Sources, DefaultLoc, "a", /*InsertAfter=*/false);
@@ -1276,7 +1278,6 @@ TEST_F(AtomicChangeTest, InsertBeforeToW
}
TEST_F(AtomicChangeTest, InsertAfterWithInvalidLocation) {
- setUp();
AtomicChange Change(Context.Sources, DefaultLoc);
llvm::Error Err = Change.insert(Context.Sources, DefaultLoc, "a");
ASSERT_TRUE(!Err);
More information about the cfe-commits
mailing list