[PATCH] D63482: [clang-tidy] Fix the YAML created for checks like modernize-pass-by-value

Ivan Donchevskii via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 28 07:23:27 PDT 2019


yvvan updated this revision to Diff 207062.
yvvan added a comment.

Sorry for delay.
Test added, redundant comments removed.


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

https://reviews.llvm.org/D63482

Files:
  clang/include/clang/Tooling/ReplacementsYaml.h
  clang/unittests/Tooling/ReplacementsYamlTest.cpp


Index: clang/unittests/Tooling/ReplacementsYamlTest.cpp
===================================================================
--- clang/unittests/Tooling/ReplacementsYamlTest.cpp
+++ clang/unittests/Tooling/ReplacementsYamlTest.cpp
@@ -46,6 +46,30 @@
                YamlContentStream.str().c_str());
 }
 
+TEST(ReplacementsYamlTest, serializesNewLines) {
+  TranslationUnitReplacements Doc;
+
+  Doc.MainSourceFile = "/path/to/source.cpp";
+  Doc.Replacements.emplace_back("/path/to/file1.h", 0, 0, "#include <utility>\n");
+
+  std::string YamlContent;
+  llvm::raw_string_ostream YamlContentStream(YamlContent);
+
+  yaml::Output YAML(YamlContentStream);
+  YAML << Doc;
+
+  // NOTE: If this test starts to fail for no obvious reason, check whitespace.
+  ASSERT_STREQ("---\n"
+               "MainSourceFile:  '/path/to/source.cpp'\n"
+               "Replacements:    \n" // Extra whitespace here!
+               "  - FilePath:        '/path/to/file1.h'\n"
+               "    Offset:          0\n"
+               "    Length:          0\n"
+               "    ReplacementText: '#include <utility>\n\n'\n"
+               "...\n",
+               YamlContentStream.str().c_str());
+}
+
 TEST(ReplacementsYamlTest, deserializesReplacements) {
   std::string YamlContent = "---\n"
                             "MainSourceFile:      /path/to/source.cpp\n"
Index: clang/include/clang/Tooling/ReplacementsYaml.h
===================================================================
--- clang/include/clang/Tooling/ReplacementsYaml.h
+++ clang/include/clang/Tooling/ReplacementsYaml.h
@@ -31,7 +31,13 @@
   /// access to its data members.
   struct NormalizedReplacement {
     NormalizedReplacement(const IO &)
-        : FilePath(""), Offset(0), Length(0), ReplacementText("") {}
+        : FilePath(""), Offset(0), Length(0), ReplacementText("") {
+      size_t lineBreakPos = ReplacementText.find('\n');
+      while (lineBreakPos != std::string::npos) {
+        ReplacementText.replace(lineBreakPos, 1, "\n\n");
+        lineBreakPos = ReplacementText.find('\n', lineBreakPos + 2);
+      }
+    }
 
     NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
         : FilePath(R.getFilePath()), Offset(R.getOffset()),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63482.207062.patch
Type: text/x-patch
Size: 2244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190628/6a6f3b95/attachment.bin>


More information about the cfe-commits mailing list