[PATCH] D80301: [yaml][clang-tidy] Fix new line YAML serialization

Dmitry Polukhin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 20 08:43:17 PDT 2020


DmitryPolukhin created this revision.
DmitryPolukhin added reviewers: gribozavr, mgehre, yvvan.
DmitryPolukhin added projects: clang-tools-extra, clang.
Herald added subscribers: hiraditya, xazax.hun.
Herald added a project: LLVM.
DmitryPolukhin edited the summary of this revision.

Move new line duplication logic to YMAL string
serialization/deserilization level instead of Replacement that was
introduced in D63482 <https://reviews.llvm.org/D63482>. D63482 <https://reviews.llvm.org/D63482> led to duplicated
new lines if you apply replacements with clang-apply-replacements. New
line duplication happened only during serialisation and there was no
opposite transformation in deserialization.

Test Plan: check-all


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80301

Files:
  clang/include/clang/Tooling/ReplacementsYaml.h
  llvm/lib/Support/YAMLTraits.cpp
  llvm/unittests/Support/YAMLIOTest.cpp


Index: llvm/unittests/Support/YAMLIOTest.cpp
===================================================================
--- llvm/unittests/Support/YAMLIOTest.cpp
+++ llvm/unittests/Support/YAMLIOTest.cpp
@@ -285,7 +285,7 @@
     YOut << Original;
   }
   auto Expected = "---\n"
-                  "str1:            'a multiline string\n"
+                  "str1:            'a multiline string\n\n"
                   "foobarbaz'\n"
                   "str2:            'another one\r"
                   "foobarbaz'\n"
Index: llvm/lib/Support/YAMLTraits.cpp
===================================================================
--- llvm/lib/Support/YAMLTraits.cpp
+++ llvm/lib/Support/YAMLTraits.cpp
@@ -887,13 +887,32 @@
 }
 
 void ScalarTraits<std::string>::output(const std::string &Val, void *,
-                                     raw_ostream &Out) {
-  Out << Val;
+                                       raw_ostream &Out) {
+  size_t currentPos = 0;
+  size_t lineBreakPos = Val.find('\n');
+  while (lineBreakPos != std::string::npos) {
+    Out << StringRef(&Val[currentPos], lineBreakPos - currentPos + 1);
+    Out << '\n';
+    currentPos = lineBreakPos + 1;
+    lineBreakPos = Val.find('\n', currentPos);
+  }
+  Out << StringRef(&Val[currentPos], Val.size() - currentPos);
 }
 
 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
-                                         std::string &Val) {
-  Val = Scalar.str();
+                                           std::string &Val) {
+  Val.clear();
+  size_t currentPos = 0;
+  size_t lineBreakPos = Scalar.find('\n');
+  while (lineBreakPos != std::string::npos) {
+    // '\n\n' convert to '\n' and don't copy single '\n'.
+    if (currentPos + 1 < Scalar.size() && Scalar[lineBreakPos + 1] == '\n')
+      ++lineBreakPos;
+    Val += Scalar.substr(currentPos, lineBreakPos);
+    currentPos = lineBreakPos + 1;
+    lineBreakPos = Scalar.find('\n', currentPos);
+  }
+  Val += Scalar.substr(currentPos, Scalar.size() - currentPos);
   return StringRef();
 }
 
Index: clang/include/clang/Tooling/ReplacementsYaml.h
===================================================================
--- clang/include/clang/Tooling/ReplacementsYaml.h
+++ clang/include/clang/Tooling/ReplacementsYaml.h
@@ -35,13 +35,7 @@
 
     NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
         : FilePath(R.getFilePath()), Offset(R.getOffset()),
-          Length(R.getLength()), ReplacementText(R.getReplacementText()) {
-      size_t lineBreakPos = ReplacementText.find('\n');
-      while (lineBreakPos != std::string::npos) {
-        ReplacementText.replace(lineBreakPos, 1, "\n\n");
-        lineBreakPos = ReplacementText.find('\n', lineBreakPos + 2);
-      }
-    }
+          Length(R.getLength()), ReplacementText(R.getReplacementText()) {}
 
     clang::tooling::Replacement denormalize(const IO &) {
       return clang::tooling::Replacement(FilePath, Offset, Length,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80301.265260.patch
Type: text/x-patch
Size: 2955 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200520/76c5ed45/attachment-0001.bin>


More information about the cfe-commits mailing list