]> granicus.if.org Git - clang/commitdiff
[clang-tidy] Fix the YAML created for checks like modernize-pass-by-value
authorIvan Donchevskii <yv.ivan@gmail.com>
Wed, 3 Jul 2019 10:21:50 +0000 (10:21 +0000)
committerIvan Donchevskii <yv.ivan@gmail.com>
Wed, 3 Jul 2019 10:21:50 +0000 (10:21 +0000)
Currently this check generates the replacement with the newline in the end.
The proper way to export it to YAML is to have two \n\n instead of one.
Without this fix clients should reinterpret the replacement as
"#include <utility> " instead of "#include <utility>\n"

Differential Revision: https://reviews.llvm.org/D63482

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@365017 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 83e35d6232555675b09b5161331159966fc8c03b..2e3e401652e22c50196203fb0d940d685b87977e 100644 (file)
@@ -35,7 +35,13 @@ template <> struct MappingTraits<clang::tooling::Replacement> {
 
     NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
         : FilePath(R.getFilePath()), Offset(R.getOffset()),
-          Length(R.getLength()), ReplacementText(R.getReplacementText()) {}
+          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);
+      }
+    }
 
     clang::tooling::Replacement denormalize(const IO &) {
       return clang::tooling::Replacement(FilePath, Offset, Length,
index ab9f6c9b5d33b4c39b49b59d750f5dde9fa48469..e05a7fd5919b21d4f019fd54ce527647040846bd 100644 (file)
@@ -46,6 +46,30 @@ TEST(ReplacementsYamlTest, serializesReplacements) {
                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"