1 //===- unittests/Rewrite/RewriteBufferTest.cpp - RewriteBuffer tests ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/RewriteBuffer.h"
10 #include "gtest/gtest.h"
16 #define EXPECT_OUTPUT(Buf, Output) EXPECT_EQ(Output, writeOutput(Buf))
18 static std::string
writeOutput(const RewriteBuffer
&Buf
) {
20 raw_string_ostream
OS(Result
);
25 static void tagRange(unsigned Offset
, unsigned Len
, StringRef tagName
,
28 raw_string_ostream(BeginTag
) << '<' << tagName
<< '>';
30 raw_string_ostream(EndTag
) << "</" << tagName
<< '>';
32 Buf
.InsertTextAfter(Offset
, BeginTag
);
33 Buf
.InsertTextBefore(Offset
+ Len
, EndTag
);
36 TEST(RewriteBuffer
, TagRanges
) {
37 StringRef Input
= "hello world";
38 const char *Output
= "<outer><inner>hello</inner></outer> ";
41 Buf
.Initialize(Input
);
42 StringRef RemoveStr
= "world";
43 size_t Pos
= Input
.find(RemoveStr
);
44 Buf
.RemoveText(Pos
, RemoveStr
.size());
46 StringRef TagStr
= "hello";
47 Pos
= Input
.find(TagStr
);
48 tagRange(Pos
, TagStr
.size(), "outer", Buf
);
49 tagRange(Pos
, TagStr
.size(), "inner", Buf
);
51 EXPECT_OUTPUT(Buf
, Output
);
54 TEST(RewriteBuffer
, DISABLED_RemoveLineIfEmpty_XFAIL
) {
55 StringRef Input
= "def\n"
59 Buf
.Initialize(Input
);
61 // Insert "abc\n" at the start.
62 Buf
.InsertText(0, "abc\n");
63 EXPECT_OUTPUT(Buf
, "abc\n"
70 // After the removal of "def", we have:
77 // Because removeLineIfEmpty=true, RemoveText has to remove the "\n" left on
78 // the line. This happens correctly for the rewrite buffer itself, so the
79 // next check below passes.
81 // However, RemoveText's implementation incorrectly records the delta for
82 // removing the "\n" using the rewrite buffer offset, 4, where it was
83 // supposed to use the original input offset, 3. Interpreted as an original
84 // input offset, 4 points to "g" not to "\n". Thus, any future modifications
85 // at the original input's "g" will incorrectly see "g" as having become an
86 // empty string and so will map to the next character, "h", in the rewrite
88 StringRef RemoveStr0
= "def";
89 Buf
.RemoveText(Input
.find(RemoveStr0
), RemoveStr0
.size(),
90 /*removeLineIfEmpty*/ true);
91 EXPECT_OUTPUT(Buf
, "abc\n"
95 // Try to remove "ghi\n".
97 // As discussed above, the original input offset for "ghi\n" incorrectly
98 // maps to the rewrite buffer offset for "hi\nj", so we end up with:
103 // To show that removeLineIfEmpty=true is the culprit, change true to false
104 // and append a newline to RemoveStr0 above. The test then passes.
105 StringRef RemoveStr1
= "ghi\n";
106 Buf
.RemoveText(Input
.find(RemoveStr1
), RemoveStr1
.size());
107 EXPECT_OUTPUT(Buf
, "abc\n"
111 } // anonymous namespace