[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 13:11:16 PDT 2020


DmitryPolukhin updated this revision to Diff 265329.
DmitryPolukhin added a comment.

Fixed second string after new line + more test case


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80301/new/

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
@@ -274,8 +274,8 @@
 
 TEST(YAMLIO, MultilineStrings) {
   WithStringField Original;
-  Original.str1 = "a multiline string\nfoobarbaz";
-  Original.str2 = "another one\rfoobarbaz";
+  Original.str1 = "a\n\nmultiline\nstring\nfoobarbaz";
+  Original.str2 = "another one\rfoobarbaz\n";
   Original.str3 = "a one-line string";
 
   std::string Serialized;
@@ -285,10 +285,10 @@
     YOut << Original;
   }
   auto Expected = "---\n"
-                  "str1:            'a multiline string\n"
+                  "str1:            'a\n\n\n\nmultiline\n\nstring\n\n"
                   "foobarbaz'\n"
                   "str2:            'another one\r"
-                  "foobarbaz'\n"
+                  "foobarbaz\n\n'\n"
                   "str3:            a one-line string\n"
                   "...\n";
   ASSERT_EQ(Serialized, Expected);
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);
+    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.265329.patch
Type: text/x-patch
Size: 3494 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200520/e001871d/attachment-0001.bin>


More information about the cfe-commits mailing list