Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 8933d16

Browse files
author
Eric Liu
committed
Supports adding insertion around non-insertion replacements.
Summary: Extend `tooling::Replacements::add()` to support adding order-independent replacements. Two replacements are considered order-independent if one of the following conditions is true: - They do not overlap. (This is already supported.) - One replacement is insertion, and the other is a replacement with length > 0, and the insertion is adjecent to but not contained in the other replacement. In this case, the replacement should always change the original code instead of the inserted text. Reviewers: klimek, djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D24515 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281457 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b19f8fa commit 8933d16

File tree

3 files changed

+116
-23
lines changed

3 files changed

+116
-23
lines changed

include/clang/Tooling/Core/Replacement.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,18 @@ class Replacements {
158158

159159
/// \brief Adds a new replacement \p R to the current set of replacements.
160160
/// \p R must have the same file path as all existing replacements.
161-
/// Returns true if the replacement is successfully inserted; otherwise,
161+
/// Returns `success` if the replacement is successfully inserted; otherwise,
162162
/// it returns an llvm::Error, i.e. there is a conflict between R and the
163-
/// existing replacements or R's file path is different from the filepath of
164-
/// existing replacements. Callers must explicitly check the Error returned.
165-
/// This prevents users from adding order-dependent replacements. To control
166-
/// the order in which order-dependent replacements are applied, use
167-
/// merge({R}) with R referring to the changed code after applying all
168-
/// existing replacements.
163+
/// existing replacements (i.e. they are order-dependent) or R's file path is
164+
/// different from the filepath of existing replacements. Callers must
165+
/// explicitly check the Error returned. This prevents users from adding
166+
/// order-dependent replacements. To control the order in which
167+
/// order-dependent replacements are applied, use merge({R}) with R referring
168+
/// to the changed code after applying all existing replacements.
169+
/// Two replacements are considered order-independent if they:
170+
/// - don't overlap (being directly adjacent is fine) and
171+
/// - aren't both inserts at the same code location (would be
172+
/// order-dependent).
169173
/// Replacements with offset UINT_MAX are special - we do not detect conflicts
170174
/// for such replacements since users may add them intentionally as a special
171175
/// category of replacements.

lib/Tooling/Core/Replacement.cpp

+33-11
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ void Replacement::setFromSourceRange(const SourceManager &Sources,
137137
ReplacementText);
138138
}
139139

140+
llvm::Error makeConflictReplacementsError(const Replacement &New,
141+
const Replacement &Existing) {
142+
return llvm::make_error<llvm::StringError>(
143+
"New replacement:\n" + New.toString() +
144+
"\nconflicts with existing replacement:\n" + Existing.toString(),
145+
llvm::inconvertibleErrorCode());
146+
}
147+
140148
llvm::Error Replacements::add(const Replacement &R) {
141149
// Check the file path.
142150
if (!Replaces.empty() && R.getFilePath() != Replaces.begin()->getFilePath())
@@ -163,11 +171,22 @@ llvm::Error Replacements::add(const Replacement &R) {
163171
// entries that start at the end can still be conflicting if R is an
164172
// insertion.
165173
auto I = Replaces.lower_bound(AtEnd);
166-
// If it starts at the same offset as R (can only happen if R is an
167-
// insertion), we have a conflict. In that case, increase I to fall through
168-
// to the conflict check.
169-
if (I != Replaces.end() && R.getOffset() == I->getOffset())
170-
++I;
174+
// If `I` starts at the same offset as `R`, `R` must be an insertion.
175+
if (I != Replaces.end() && R.getOffset() == I->getOffset()) {
176+
assert(R.getLength() == 0);
177+
// `I` is also an insertion, `R` and `I` conflict.
178+
if (I->getLength() == 0)
179+
return makeConflictReplacementsError(R, *I);
180+
// Insertion `R` is adjacent to a non-insertion replacement `I`, so they
181+
// are order-independent. It is safe to assume that `R` will not conflict
182+
// with any replacement before `I` since all replacements before `I` must
183+
// either end before `R` or end at `R` but has length > 0 (if the
184+
// replacement before `I` is an insertion at `R`, it would have been `I`
185+
// since it is a lower bound of `AtEnd` and ordered before the current `I`
186+
// in the set).
187+
Replaces.insert(R);
188+
return llvm::Error::success();
189+
}
171190

172191
// I is the smallest iterator whose entry cannot overlap.
173192
// If that is begin(), there are no overlaps.
@@ -178,16 +197,19 @@ llvm::Error Replacements::add(const Replacement &R) {
178197
--I;
179198
// If the previous entry does not overlap, we know that entries before it
180199
// can also not overlap.
181-
if (R.getOffset() != I->getOffset() &&
182-
!Range(R.getOffset(), R.getLength())
200+
if (!Range(R.getOffset(), R.getLength())
183201
.overlapsWith(Range(I->getOffset(), I->getLength()))) {
202+
// If `R` and `I` do not have the same offset, it is safe to add `R` since
203+
// it must come after `I`. Otherwise:
204+
// - If `R` is an insertion, `I` must not be an insertion since it would
205+
// have come after `AtEnd` if it has length 0.
206+
// - If `R` is not an insertion, `I` must be an insertion; otherwise, `R`
207+
// and `I` would have overlapped.
208+
// In either case, we can safely insert `R`.
184209
Replaces.insert(R);
185210
return llvm::Error::success();
186211
}
187-
return llvm::make_error<llvm::StringError>(
188-
"New replacement:\n" + R.toString() +
189-
"\nconflicts with existing replacement:\n" + I->toString(),
190-
llvm::inconvertibleErrorCode());
212+
return makeConflictReplacementsError(R, *I);
191213
}
192214

193215
namespace {

unittests/Tooling/RefactoringTest.cpp

+72-5
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,26 @@ TEST_F(ReplacementTest, FailAddReplacements) {
115115
llvm::consumeError(std::move(Err));
116116
}
117117

118-
TEST_F(ReplacementTest, FailAddOverlappingInsertions) {
118+
TEST_F(ReplacementTest, AddAdjacentInsertionAndReplacement) {
119119
Replacements Replaces;
120120
// Test adding an insertion at the offset of an existing replacement.
121121
auto Err = Replaces.add(Replacement("x.cc", 10, 3, "replace"));
122122
EXPECT_TRUE(!Err);
123123
llvm::consumeError(std::move(Err));
124124
Err = Replaces.add(Replacement("x.cc", 10, 0, "insert"));
125-
EXPECT_TRUE((bool)Err);
125+
EXPECT_TRUE(!Err);
126126
llvm::consumeError(std::move(Err));
127+
EXPECT_EQ(Replaces.size(), 2u);
127128

128129
Replaces.clear();
129130
// Test overlap with an existing insertion.
130131
Err = Replaces.add(Replacement("x.cc", 10, 0, "insert"));
131132
EXPECT_TRUE(!Err);
132133
llvm::consumeError(std::move(Err));
133134
Err = Replaces.add(Replacement("x.cc", 10, 3, "replace"));
134-
EXPECT_TRUE((bool)Err);
135+
EXPECT_TRUE(!Err);
135136
llvm::consumeError(std::move(Err));
137+
EXPECT_EQ(Replaces.size(), 2u);
136138
}
137139

138140
TEST_F(ReplacementTest, FailAddRegression) {
@@ -157,14 +159,24 @@ TEST_F(ReplacementTest, FailAddRegression) {
157159
llvm::consumeError(std::move(Err));
158160
}
159161

160-
TEST_F(ReplacementTest, FailAddInsertAtOffsetOfReplacement) {
162+
TEST_F(ReplacementTest, InsertAtOffsetOfReplacement) {
161163
Replacements Replaces;
162164
auto Err = Replaces.add(Replacement("x.cc", 10, 2, ""));
163165
EXPECT_TRUE(!Err);
164166
llvm::consumeError(std::move(Err));
165167
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
166-
EXPECT_TRUE((bool)Err);
168+
EXPECT_TRUE(!Err);
169+
llvm::consumeError(std::move(Err));
170+
EXPECT_EQ(Replaces.size(), 2u);
171+
172+
Replaces.clear();
173+
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
174+
EXPECT_TRUE(!Err);
175+
llvm::consumeError(std::move(Err));
176+
Err = Replaces.add(Replacement("x.cc", 10, 2, ""));
177+
EXPECT_TRUE(!Err);
167178
llvm::consumeError(std::move(Err));
179+
EXPECT_EQ(Replaces.size(), 2u);
168180
}
169181

170182
TEST_F(ReplacementTest, FailAddInsertAtOtherInsert) {
@@ -175,6 +187,38 @@ TEST_F(ReplacementTest, FailAddInsertAtOtherInsert) {
175187
Err = Replaces.add(Replacement("x.cc", 10, 0, "b"));
176188
EXPECT_TRUE((bool)Err);
177189
llvm::consumeError(std::move(Err));
190+
191+
Replaces.clear();
192+
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
193+
EXPECT_TRUE(!Err);
194+
llvm::consumeError(std::move(Err));
195+
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
196+
EXPECT_TRUE((bool)Err);
197+
llvm::consumeError(std::move(Err));
198+
199+
Replaces.clear();
200+
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
201+
EXPECT_TRUE(!Err);
202+
llvm::consumeError(std::move(Err));
203+
Err = Replaces.add(Replacement("x.cc", 10, 3, ""));
204+
EXPECT_TRUE(!Err);
205+
llvm::consumeError(std::move(Err));
206+
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
207+
EXPECT_TRUE((bool)Err);
208+
llvm::consumeError(std::move(Err));
209+
}
210+
211+
TEST_F(ReplacementTest, InsertBetweenAdjacentReplacements) {
212+
Replacements Replaces;
213+
auto Err = Replaces.add(Replacement("x.cc", 10, 5, "a"));
214+
EXPECT_TRUE(!Err);
215+
llvm::consumeError(std::move(Err));
216+
Err = Replaces.add(Replacement("x.cc", 8, 2, "a"));
217+
EXPECT_TRUE(!Err);
218+
llvm::consumeError(std::move(Err));
219+
Err = Replaces.add(Replacement("x.cc", 10, 0, "b"));
220+
EXPECT_TRUE(!Err);
221+
llvm::consumeError(std::move(Err));
178222
}
179223

180224
TEST_F(ReplacementTest, CanApplyReplacements) {
@@ -189,6 +233,29 @@ TEST_F(ReplacementTest, CanApplyReplacements) {
189233
EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
190234
}
191235

236+
// Verifies that replacement/deletion is applied before insertion at the same
237+
// offset.
238+
TEST_F(ReplacementTest, InsertAndDelete) {
239+
FileID ID = Context.createInMemoryFile("input.cpp",
240+
"line1\nline2\nline3\nline4");
241+
Replacements Replaces = toReplacements(
242+
{Replacement(Context.Sources, Context.getLocation(ID, 2, 1), 6, ""),
243+
Replacement(Context.Sources, Context.getLocation(ID, 2, 1), 0,
244+
"other\n")});
245+
EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
246+
EXPECT_EQ("line1\nother\nline3\nline4", Context.getRewrittenText(ID));
247+
}
248+
249+
TEST_F(ReplacementTest, AdjacentReplacements) {
250+
FileID ID = Context.createInMemoryFile("input.cpp",
251+
"ab");
252+
Replacements Replaces = toReplacements(
253+
{Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 1, "x"),
254+
Replacement(Context.Sources, Context.getLocation(ID, 1, 2), 1, "y")});
255+
EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
256+
EXPECT_EQ("xy", Context.getRewrittenText(ID));
257+
}
258+
192259
TEST_F(ReplacementTest, SkipsDuplicateReplacements) {
193260
FileID ID = Context.createInMemoryFile("input.cpp",
194261
"line1\nline2\nline3\nline4");

0 commit comments

Comments
 (0)