diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-09-11 12:25:57 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-09-11 12:25:57 +0000 |
commit | 9e649af004b2033c6dd802c80fc1b889f066c9ad (patch) | |
tree | fe5fa4091a1df2c5f7c97b48b56bfd5d044134ca /clang/lib/Format/WhitespaceManager.cpp | |
parent | fbcb5829420eae3777de7c7990327ce0b5505de4 (diff) | |
download | llvm-9e649af004b2033c6dd802c80fc1b889f066c9ad.zip llvm-9e649af004b2033c6dd802c80fc1b889f066c9ad.tar.gz llvm-9e649af004b2033c6dd802c80fc1b889f066c9ad.tar.bz2 |
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
llvm-svn: 190519
Diffstat (limited to 'clang/lib/Format/WhitespaceManager.cpp')
-rw-r--r-- | clang/lib/Format/WhitespaceManager.cpp | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 0f46e62..4138442 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -221,14 +221,14 @@ void WhitespaceManager::generateChanges() { 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 @@ void WhitespaceManager::storeReplacement(const SourceRange &Range, 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 |