RewriteBufferTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "clang/Rewrite/Core/RewriteBuffer.h"
  9. #include "gtest/gtest.h"
  10. using namespace llvm;
  11. using namespace clang;
  12. namespace {
  13. #define EXPECT_OUTPUT(Buf, Output) EXPECT_EQ(Output, writeOutput(Buf))
  14. static std::string writeOutput(const RewriteBuffer &Buf) {
  15. std::string Result;
  16. raw_string_ostream OS(Result);
  17. Buf.write(OS);
  18. OS.flush();
  19. return Result;
  20. }
  21. static void tagRange(unsigned Offset, unsigned Len, StringRef tagName,
  22. RewriteBuffer &Buf) {
  23. std::string BeginTag;
  24. raw_string_ostream(BeginTag) << '<' << tagName << '>';
  25. std::string EndTag;
  26. raw_string_ostream(EndTag) << "</" << tagName << '>';
  27. Buf.InsertTextAfter(Offset, BeginTag);
  28. Buf.InsertTextBefore(Offset+Len, EndTag);
  29. }
  30. TEST(RewriteBuffer, TagRanges) {
  31. StringRef Input = "hello world";
  32. const char *Output = "<outer><inner>hello</inner></outer> ";
  33. RewriteBuffer Buf;
  34. Buf.Initialize(Input);
  35. StringRef RemoveStr = "world";
  36. size_t Pos = Input.find(RemoveStr);
  37. Buf.RemoveText(Pos, RemoveStr.size());
  38. StringRef TagStr = "hello";
  39. Pos = Input.find(TagStr);
  40. tagRange(Pos, TagStr.size(), "outer", Buf);
  41. tagRange(Pos, TagStr.size(), "inner", Buf);
  42. EXPECT_OUTPUT(Buf, Output);
  43. }
  44. TEST(RewriteBuffer, DISABLED_RemoveLineIfEmpty_XFAIL) {
  45. StringRef Input = "def\n"
  46. "ghi\n"
  47. "jkl\n";
  48. RewriteBuffer Buf;
  49. Buf.Initialize(Input);
  50. // Insert "abc\n" at the start.
  51. Buf.InsertText(0, "abc\n");
  52. EXPECT_OUTPUT(Buf, "abc\n"
  53. "def\n"
  54. "ghi\n"
  55. "jkl\n");
  56. // Remove "def\n".
  57. //
  58. // After the removal of "def", we have:
  59. //
  60. // "abc\n"
  61. // "\n"
  62. // "ghi\n"
  63. // "jkl\n"
  64. //
  65. // Because removeLineIfEmpty=true, RemoveText has to remove the "\n" left on
  66. // the line. This happens correctly for the rewrite buffer itself, so the
  67. // next check below passes.
  68. //
  69. // However, RemoveText's implementation incorrectly records the delta for
  70. // removing the "\n" using the rewrite buffer offset, 4, where it was
  71. // supposed to use the original input offset, 3. Interpreted as an original
  72. // input offset, 4 points to "g" not to "\n". Thus, any future modifications
  73. // at the original input's "g" will incorrectly see "g" as having become an
  74. // empty string and so will map to the next character, "h", in the rewrite
  75. // buffer.
  76. StringRef RemoveStr0 = "def";
  77. Buf.RemoveText(Input.find(RemoveStr0), RemoveStr0.size(),
  78. /*removeLineIfEmpty*/ true);
  79. EXPECT_OUTPUT(Buf, "abc\n"
  80. "ghi\n"
  81. "jkl\n");
  82. // Try to remove "ghi\n".
  83. //
  84. // As discussed above, the original input offset for "ghi\n" incorrectly
  85. // maps to the rewrite buffer offset for "hi\nj", so we end up with:
  86. //
  87. // "abc\n"
  88. // "gkl\n"
  89. //
  90. // To show that removeLineIfEmpty=true is the culprit, change true to false
  91. // and append a newline to RemoveStr0 above. The test then passes.
  92. StringRef RemoveStr1 = "ghi\n";
  93. Buf.RemoveText(Input.find(RemoveStr1), RemoveStr1.size());
  94. EXPECT_OUTPUT(Buf, "abc\n"
  95. "jkl\n");
  96. }
  97. } // anonymous namespace