Preserve carriage return when using clang-format's XML interface.

Patch by James Park.

llvm-svn: 196265
This commit is contained in:
Manuel Klimek 2013-12-03 09:46:06 +00:00
parent 907eb5b630
commit f54dcbc4e6
1 changed files with 23 additions and 2 deletions

View File

@ -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() << "&#10;";
break;
case '\r':
llvm::outs() << "&#13;";
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() << "<replacement "
<< "offset='" << I->getOffset() << "' "
<< "length='" << I->getLength() << "'>"
<< I->getReplacementText() << "</replacement>\n";
<< "length='" << I->getLength() << "'>";
outputReplacementXML(I->getReplacementText());
llvm::outs() << "</replacement>\n";
}
llvm::outs() << "</replacements>\n";
} else {