[PATCH] D80301: [yaml][clang-tidy] Fix new line YAML serialization
Dmitry Polukhin via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 4 13:51:21 PDT 2020
DmitryPolukhin updated this revision to Diff 268583.
DmitryPolukhin added a comment.
Apply suggested changes with string split
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D80301/new/
https://reviews.llvm.org/D80301
Files:
clang/include/clang/Tooling/ReplacementsYaml.h
llvm/lib/Support/YAMLTraits.cpp
llvm/unittests/Support/YAMLIOTest.cpp
Index: llvm/unittests/Support/YAMLIOTest.cpp
===================================================================
--- llvm/unittests/Support/YAMLIOTest.cpp
+++ llvm/unittests/Support/YAMLIOTest.cpp
@@ -274,8 +274,8 @@
TEST(YAMLIO, MultilineStrings) {
WithStringField Original;
- Original.str1 = "a multiline string\nfoobarbaz";
- Original.str2 = "another one\rfoobarbaz";
+ Original.str1 = "a\n\nmultiline\nstring\nfoobarbaz";
+ Original.str2 = "another one\rfoobarbaz\n";
Original.str3 = "a one-line string";
std::string Serialized;
@@ -285,10 +285,10 @@
YOut << Original;
}
auto Expected = "---\n"
- "str1: 'a multiline string\n"
+ "str1: 'a\n\n\n\nmultiline\n\nstring\n\n"
"foobarbaz'\n"
"str2: 'another one\r"
- "foobarbaz'\n"
+ "foobarbaz\n\n'\n"
"str3: a one-line string\n"
"...\n";
ASSERT_EQ(Serialized, Expected);
Index: llvm/lib/Support/YAMLTraits.cpp
===================================================================
--- llvm/lib/Support/YAMLTraits.cpp
+++ llvm/lib/Support/YAMLTraits.cpp
@@ -876,13 +876,33 @@
}
void ScalarTraits<std::string>::output(const std::string &Val, void *,
- raw_ostream &Out) {
- Out << Val;
+ raw_ostream &Out) {
+ SmallVector<StringRef, 8> Lines;
+ StringRef(Val).split(Lines, '\n');
+ bool First = true;
+ for (StringRef Line : Lines) {
+ if (First)
+ First = false;
+ else
+ Out << "\n\n";
+ Out << Line;
+ }
}
StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
- std::string &Val) {
- Val = Scalar.str();
+ std::string &Val) {
+ Val.clear();
+ size_t CurrentPos = 0;
+ size_t LineBreakPos = Scalar.find('\n');
+ while (LineBreakPos != std::string::npos) {
+ // '\n\n' convert to '\n' and don't copy single '\n'.
+ if (CurrentPos + 1 < Scalar.size() && Scalar[LineBreakPos + 1] == '\n')
+ ++LineBreakPos;
+ Val += Scalar.substr(CurrentPos, LineBreakPos - CurrentPos);
+ CurrentPos = LineBreakPos + 1;
+ LineBreakPos = Scalar.find('\n', CurrentPos);
+ }
+ Val += Scalar.substr(CurrentPos, Scalar.size() - CurrentPos);
return StringRef();
}
Index: clang/include/clang/Tooling/ReplacementsYaml.h
===================================================================
--- clang/include/clang/Tooling/ReplacementsYaml.h
+++ clang/include/clang/Tooling/ReplacementsYaml.h
@@ -35,13 +35,7 @@
NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
: FilePath(R.getFilePath()), Offset(R.getOffset()),
- 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);
- }
- }
+ Length(R.getLength()), ReplacementText(R.getReplacementText()) {}
clang::tooling::Replacement denormalize(const IO &) {
return clang::tooling::Replacement(FilePath, Offset, Length,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80301.268583.patch
Type: text/x-patch
Size: 3360 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200604/8167a0a9/attachment-0001.bin>
More information about the cfe-commits
mailing list