From: Ivan Donchevskii Date: Wed, 3 Jul 2019 10:21:50 +0000 (+0000) Subject: [clang-tidy] Fix the YAML created for checks like modernize-pass-by-value X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eb541f13e75b36dfeb411012e67f90d41589fc20;p=clang [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 --- diff --git a/include/clang/Tooling/ReplacementsYaml.h b/include/clang/Tooling/ReplacementsYaml.h index 83e35d6232..2e3e401652 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 ab9f6c9b5d..e05a7fd591 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"