From 798cf2aceac4279fe3e27454d2d673f7d0717d34 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Wed, 3 Jul 2019 10:21:50 +0000 Subject: [PATCH] [clang-tidy] Fix the YAML created for checks like modernize-pass-by-value 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 " instead of "#include \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 | 8 +++++++- unittests/Tooling/ReplacementsYamlTest.cpp | 24 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/clang/Tooling/ReplacementsYaml.h b/include/clang/Tooling/ReplacementsYaml.h index 83e35d62325..2e3e401652e 100644 --- a/include/clang/Tooling/ReplacementsYaml.h +++ b/include/clang/Tooling/ReplacementsYaml.h @@ -35,7 +35,13 @@ template <> struct MappingTraits { 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, diff --git a/unittests/Tooling/ReplacementsYamlTest.cpp b/unittests/Tooling/ReplacementsYamlTest.cpp index ab9f6c9b5d3..e05a7fd5919 100644 --- a/unittests/Tooling/ReplacementsYamlTest.cpp +++ b/unittests/Tooling/ReplacementsYamlTest.cpp @@ -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 \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 \n\n'\n" + "...\n", + YamlContentStream.str().c_str()); +} + TEST(ReplacementsYamlTest, deserializesReplacements) { std::string YamlContent = "---\n" "MainSourceFile: /path/to/source.cpp\n"