Support for CR LF newlines.
Summary:
reformat() tries to determine the newline style used in the input
(either LF or CR LF), and uses it for the output. Maybe not every single case is
supported, but at least the bug described in http://llvm.org/PR17182 should be
resolved.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D1643
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190519 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp
index 0f46e62..4138442 100644
--- a/lib/Format/WhitespaceManager.cpp
+++ b/lib/Format/WhitespaceManager.cpp
@@ -221,14 +221,14 @@
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
const Change &C = Changes[i];
if (C.CreateReplacement) {
- std::string ReplacementText =
- C.PreviousLinePostfix +
- (C.ContinuesPPDirective
- ? getNewlineText(C.NewlinesBefore, C.Spaces,
- C.PreviousEndOfTokenColumn,
- C.EscapedNewlineColumn)
- : getNewlineText(C.NewlinesBefore, C.Spaces)) +
- C.CurrentLinePrefix;
+ std::string ReplacementText = C.PreviousLinePostfix;
+ if (C.ContinuesPPDirective)
+ appendNewlineText(ReplacementText, C.NewlinesBefore,
+ C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
+ else
+ appendNewlineText(ReplacementText, C.NewlinesBefore);
+ appendIndentText(ReplacementText, C.Spaces);
+ ReplacementText.append(C.CurrentLinePrefix);
storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
}
}
@@ -246,34 +246,33 @@
SourceMgr, CharSourceRange::getCharRange(Range), Text));
}
-std::string WhitespaceManager::getNewlineText(unsigned Newlines,
- unsigned Spaces) {
- return std::string(Newlines, '\n') + getIndentText(Spaces);
+void WhitespaceManager::appendNewlineText(std::string &Text,
+ unsigned Newlines) {
+ for (unsigned i = 0; i < Newlines; ++i)
+ Text.append(UseCRLF ? "\r\n" : "\n");
}
-std::string WhitespaceManager::getNewlineText(unsigned Newlines,
- unsigned Spaces,
- unsigned PreviousEndOfTokenColumn,
- unsigned EscapedNewlineColumn) {
- std::string NewlineText;
+void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
+ unsigned PreviousEndOfTokenColumn,
+ unsigned EscapedNewlineColumn) {
if (Newlines > 0) {
unsigned Offset =
std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
for (unsigned i = 0; i < Newlines; ++i) {
- NewlineText += std::string(EscapedNewlineColumn - Offset - 1, ' ');
- NewlineText += "\\\n";
+ Text.append(std::string(EscapedNewlineColumn - Offset - 1, ' '));
+ Text.append(UseCRLF ? "\\\r\n" : "\\\n");
Offset = 0;
}
}
- return NewlineText + getIndentText(Spaces);
}
-std::string WhitespaceManager::getIndentText(unsigned Spaces) {
- if (!Style.UseTab)
- return std::string(Spaces, ' ');
-
- return std::string(Spaces / Style.TabWidth, '\t') +
- std::string(Spaces % Style.TabWidth, ' ');
+void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces) {
+ if (!Style.UseTab) {
+ Text.append(std::string(Spaces, ' '));
+ } else {
+ Text.append(std::string(Spaces / Style.TabWidth, '\t'));
+ Text.append(std::string(Spaces % Style.TabWidth, ' '));
+ }
}
} // namespace format