Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
[clang-tidy] Fix the YAML created for checks like modernize-pass-by-v…
Browse files Browse the repository at this point in the history
…alue

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
  • Loading branch information
yvvan authored and cyndyishida committed Oct 3, 2019
1 parent eeafed2 commit 798cf2a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion include/clang/Tooling/ReplacementsYaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions unittests/Tooling/ReplacementsYamlTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 798cf2a

Please sign in to comment.