From 6016911ce5d3226e538776620af31cde736c816f Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Tue, 3 Dec 2013 09:46:06 +0000 Subject: [PATCH] Preserve carriage return when using clang-format's XML interface. Patch by James Park. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196265 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/clang-format/ClangFormat.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index 513439649d..d8c0b51ee1 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -179,6 +179,26 @@ static bool fillRanges(SourceManager &Sources, FileID ID, return false; } +static void outputReplacementXML(StringRef Text) { + size_t From = 0; + size_t Index; + while ((Index = Text.find_first_of("\n\r", From)) != StringRef::npos) { + llvm::outs() << Text.substr(From, Index - From); + switch (Text[Index]) { + case '\n': + llvm::outs() << " "; + break; + case '\r': + llvm::outs() << " "; + break; + default: + llvm_unreachable("Unexpected character encountered!"); + } + From = Index + 1; + } + llvm::outs() << Text.substr(From); +} + // Returns true on error. static bool format(StringRef FileName) { FileManager Files((FileSystemOptions())); @@ -211,8 +231,9 @@ static bool format(StringRef FileName) { I != E; ++I) { llvm::outs() << "" - << I->getReplacementText() << "\n"; + << "length='" << I->getLength() << "'>"; + outputReplacementXML(I->getReplacementText()); + llvm::outs() << "\n"; } llvm::outs() << "\n"; } else { -- 2.50.1