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

Commit e119918

Browse files
committed
Revert r281457 "Supports adding insertion around non-insertion replacements."
Commit was breaking our internal tests. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281557 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 19dd934 commit e119918

File tree

3 files changed

+23
-116
lines changed

3 files changed

+23
-116
lines changed

Diff for: include/clang/Tooling/Core/Replacement.h

+7-11
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,14 @@ 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 `success` if the replacement is successfully inserted; otherwise,
161+
/// Returns true 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 (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).
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.
173169
/// Replacements with offset UINT_MAX are special - we do not detect conflicts
174170
/// for such replacements since users may add them intentionally as a special
175171
/// category of replacements.

Diff for: lib/Tooling/Core/Replacement.cpp

+11-33
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,6 @@ 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-
148140
llvm::Error Replacements::add(const Replacement &R) {
149141
// Check the file path.
150142
if (!Replaces.empty() && R.getFilePath() != Replaces.begin()->getFilePath())
@@ -171,22 +163,11 @@ llvm::Error Replacements::add(const Replacement &R) {
171163
// entries that start at the end can still be conflicting if R is an
172164
// insertion.
173165
auto I = Replaces.lower_bound(AtEnd);
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-
}
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;
190171

191172
// I is the smallest iterator whose entry cannot overlap.
192173
// If that is begin(), there are no overlaps.
@@ -197,19 +178,16 @@ llvm::Error Replacements::add(const Replacement &R) {
197178
--I;
198179
// If the previous entry does not overlap, we know that entries before it
199180
// can also not overlap.
200-
if (!Range(R.getOffset(), R.getLength())
181+
if (R.getOffset() != I->getOffset() &&
182+
!Range(R.getOffset(), R.getLength())
201183
.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`.
209184
Replaces.insert(R);
210185
return llvm::Error::success();
211186
}
212-
return makeConflictReplacementsError(R, *I);
187+
return llvm::make_error<llvm::StringError>(
188+
"New replacement:\n" + R.toString() +
189+
"\nconflicts with existing replacement:\n" + I->toString(),
190+
llvm::inconvertibleErrorCode());
213191
}
214192

215193
namespace {

Diff for: unittests/Tooling/RefactoringTest.cpp

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

118-
TEST_F(ReplacementTest, AddAdjacentInsertionAndReplacement) {
118+
TEST_F(ReplacementTest, FailAddOverlappingInsertions) {
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(!Err);
125+
EXPECT_TRUE((bool)Err);
126126
llvm::consumeError(std::move(Err));
127-
EXPECT_EQ(Replaces.size(), 2u);
128127

129128
Replaces.clear();
130129
// Test overlap with an existing insertion.
131130
Err = Replaces.add(Replacement("x.cc", 10, 0, "insert"));
132131
EXPECT_TRUE(!Err);
133132
llvm::consumeError(std::move(Err));
134133
Err = Replaces.add(Replacement("x.cc", 10, 3, "replace"));
135-
EXPECT_TRUE(!Err);
134+
EXPECT_TRUE((bool)Err);
136135
llvm::consumeError(std::move(Err));
137-
EXPECT_EQ(Replaces.size(), 2u);
138136
}
139137

140138
TEST_F(ReplacementTest, FailAddRegression) {
@@ -159,24 +157,14 @@ TEST_F(ReplacementTest, FailAddRegression) {
159157
llvm::consumeError(std::move(Err));
160158
}
161159

162-
TEST_F(ReplacementTest, InsertAtOffsetOfReplacement) {
160+
TEST_F(ReplacementTest, FailAddInsertAtOffsetOfReplacement) {
163161
Replacements Replaces;
164162
auto Err = Replaces.add(Replacement("x.cc", 10, 2, ""));
165163
EXPECT_TRUE(!Err);
166164
llvm::consumeError(std::move(Err));
167165
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
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);
166+
EXPECT_TRUE((bool)Err);
178167
llvm::consumeError(std::move(Err));
179-
EXPECT_EQ(Replaces.size(), 2u);
180168
}
181169

182170
TEST_F(ReplacementTest, FailAddInsertAtOtherInsert) {
@@ -187,38 +175,6 @@ TEST_F(ReplacementTest, FailAddInsertAtOtherInsert) {
187175
Err = Replaces.add(Replacement("x.cc", 10, 0, "b"));
188176
EXPECT_TRUE((bool)Err);
189177
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));
222178
}
223179

224180
TEST_F(ReplacementTest, CanApplyReplacements) {
@@ -233,29 +189,6 @@ TEST_F(ReplacementTest, CanApplyReplacements) {
233189
EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
234190
}
235191

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-
259192
TEST_F(ReplacementTest, SkipsDuplicateReplacements) {
260193
FileID ID = Context.createInMemoryFile("input.cpp",
261194
"line1\nline2\nline3\nline4");

0 commit comments

Comments
 (0)