xref: /llvm-project/llvm/unittests/ADT/RewriteBufferTest.cpp (revision 459a82e6890ff41e30d486f36c8c7ec22628bb7a)
1 //===- unittests/Rewrite/RewriteBufferTest.cpp - RewriteBuffer tests ------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/RewriteBuffer.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
15 
16 #define EXPECT_OUTPUT(Buf, Output) EXPECT_EQ(Output, writeOutput(Buf))
17 
18 static std::string writeOutput(const RewriteBuffer &Buf) {
19   std::string Result;
20   raw_string_ostream OS(Result);
21   Buf.write(OS);
22   return Result;
23 }
24 
25 static void tagRange(unsigned Offset, unsigned Len, StringRef tagName,
26                      RewriteBuffer &Buf) {
27   std::string BeginTag;
28   raw_string_ostream(BeginTag) << '<' << tagName << '>';
29   std::string EndTag;
30   raw_string_ostream(EndTag) << "</" << tagName << '>';
31 
32   Buf.InsertTextAfter(Offset, BeginTag);
33   Buf.InsertTextBefore(Offset + Len, EndTag);
34 }
35 
36 TEST(RewriteBuffer, TagRanges) {
37   StringRef Input = "hello world";
38   const char *Output = "<outer><inner>hello</inner></outer> ";
39 
40   RewriteBuffer Buf;
41   Buf.Initialize(Input);
42   StringRef RemoveStr = "world";
43   size_t Pos = Input.find(RemoveStr);
44   Buf.RemoveText(Pos, RemoveStr.size());
45 
46   StringRef TagStr = "hello";
47   Pos = Input.find(TagStr);
48   tagRange(Pos, TagStr.size(), "outer", Buf);
49   tagRange(Pos, TagStr.size(), "inner", Buf);
50 
51   EXPECT_OUTPUT(Buf, Output);
52 }
53 
54 TEST(RewriteBuffer, DISABLED_RemoveLineIfEmpty_XFAIL) {
55   StringRef Input = "def\n"
56                     "ghi\n"
57                     "jkl\n";
58   RewriteBuffer Buf;
59   Buf.Initialize(Input);
60 
61   // Insert "abc\n" at the start.
62   Buf.InsertText(0, "abc\n");
63   EXPECT_OUTPUT(Buf, "abc\n"
64                      "def\n"
65                      "ghi\n"
66                      "jkl\n");
67 
68   // Remove "def\n".
69   //
70   // After the removal of "def", we have:
71   //
72   //   "abc\n"
73   //   "\n"
74   //   "ghi\n"
75   //   "jkl\n"
76   //
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.
80   //
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
87   // buffer.
88   StringRef RemoveStr0 = "def";
89   Buf.RemoveText(Input.find(RemoveStr0), RemoveStr0.size(),
90                  /*removeLineIfEmpty*/ true);
91   EXPECT_OUTPUT(Buf, "abc\n"
92                      "ghi\n"
93                      "jkl\n");
94 
95   // Try to remove "ghi\n".
96   //
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:
99   //
100   //   "abc\n"
101   //   "gkl\n"
102   //
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"
108                      "jkl\n");
109 }
110 
111 } // anonymous namespace
112