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 "clang/Rewrite/Core/RewriteBuffer.h"
10 #include "gtest/gtest.h"
13 using namespace clang
;
17 #define EXPECT_OUTPUT(Buf, Output) EXPECT_EQ(Output, writeOutput(Buf))
19 static std::string
writeOutput(const RewriteBuffer
&Buf
) {
21 raw_string_ostream
OS(Result
);
27 static void tagRange(unsigned Offset
, unsigned Len
, StringRef tagName
,
30 raw_string_ostream(BeginTag
) << '<' << tagName
<< '>';
32 raw_string_ostream(EndTag
) << "</" << tagName
<< '>';
34 Buf
.InsertTextAfter(Offset
, BeginTag
);
35 Buf
.InsertTextBefore(Offset
+Len
, EndTag
);
38 TEST(RewriteBuffer
, TagRanges
) {
39 StringRef Input
= "hello world";
40 const char *Output
= "<outer><inner>hello</inner></outer> ";
43 Buf
.Initialize(Input
);
44 StringRef RemoveStr
= "world";
45 size_t Pos
= Input
.find(RemoveStr
);
46 Buf
.RemoveText(Pos
, RemoveStr
.size());
48 StringRef TagStr
= "hello";
49 Pos
= Input
.find(TagStr
);
50 tagRange(Pos
, TagStr
.size(), "outer", Buf
);
51 tagRange(Pos
, TagStr
.size(), "inner", Buf
);
53 EXPECT_OUTPUT(Buf
, Output
);
56 TEST(RewriteBuffer
, DISABLED_RemoveLineIfEmpty_XFAIL
) {
57 StringRef Input
= "def\n"
61 Buf
.Initialize(Input
);
63 // Insert "abc\n" at the start.
64 Buf
.InsertText(0, "abc\n");
65 EXPECT_OUTPUT(Buf
, "abc\n"
72 // After the removal of "def", we have:
79 // Because removeLineIfEmpty=true, RemoveText has to remove the "\n" left on
80 // the line. This happens correctly for the rewrite buffer itself, so the
81 // next check below passes.
83 // However, RemoveText's implementation incorrectly records the delta for
84 // removing the "\n" using the rewrite buffer offset, 4, where it was
85 // supposed to use the original input offset, 3. Interpreted as an original
86 // input offset, 4 points to "g" not to "\n". Thus, any future modifications
87 // at the original input's "g" will incorrectly see "g" as having become an
88 // empty string and so will map to the next character, "h", in the rewrite
90 StringRef RemoveStr0
= "def";
91 Buf
.RemoveText(Input
.find(RemoveStr0
), RemoveStr0
.size(),
92 /*removeLineIfEmpty*/ true);
93 EXPECT_OUTPUT(Buf
, "abc\n"
97 // Try to remove "ghi\n".
99 // As discussed above, the original input offset for "ghi\n" incorrectly
100 // maps to the rewrite buffer offset for "hi\nj", so we end up with:
105 // To show that removeLineIfEmpty=true is the culprit, change true to false
106 // and append a newline to RemoveStr0 above. The test then passes.
107 StringRef RemoveStr1
= "ghi\n";
108 Buf
.RemoveText(Input
.find(RemoveStr1
), RemoveStr1
.size());
109 EXPECT_OUTPUT(Buf
, "abc\n"
113 } // anonymous namespace