clang-format: Use Rewriter again to write the output files.

This has two advantages:
1. Atomic writes.
2. Proper handling of line endings (hopefully solving llvm.org/PR24999

llvm-svn: 248904
This commit is contained in:
Daniel Jasper 2015-09-30 13:59:29 +00:00
parent 648f3c7efa
commit 867a938246
1 changed files with 18 additions and 18 deletions

View File

@ -19,6 +19,7 @@
#include "clang/Basic/SourceManager.h" #include "clang/Basic/SourceManager.h"
#include "clang/Basic/Version.h" #include "clang/Basic/Version.h"
#include "clang/Format/Format.h" #include "clang/Format/Format.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
@ -77,7 +78,7 @@ AssumeFileName("assume-filename",
cl::desc("When reading from stdin, clang-format assumes this\n" cl::desc("When reading from stdin, clang-format assumes this\n"
"filename to look for a style config file (with\n" "filename to look for a style config file (with\n"
"-style=file) and to determine the language."), "-style=file) and to determine the language."),
cl::cat(ClangFormatCategory)); cl::init("<stdin>"), cl::cat(ClangFormatCategory));
static cl::opt<bool> Inplace("i", static cl::opt<bool> Inplace("i",
cl::desc("Inplace edit <file>s, if specified."), cl::desc("Inplace edit <file>s, if specified."),
@ -109,8 +110,7 @@ namespace format {
static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
SourceManager &Sources, FileManager &Files) { SourceManager &Sources, FileManager &Files) {
const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" : const FileEntry *Entry = Files.getVirtualFile(FileName,
FileName,
Source->getBufferSize(), 0); Source->getBufferSize(), 0);
Sources.overrideFileContents(Entry, Source, true); Sources.overrideFileContents(Entry, Source, true);
return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
@ -132,7 +132,7 @@ static bool fillRanges(MemoryBuffer *Code,
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions); new DiagnosticOptions);
SourceManager Sources(Diagnostics, Files); SourceManager Sources(Diagnostics, Files);
FileID ID = createInMemoryFile("-", Code, Sources, Files); FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files);
if (!LineRanges.empty()) { if (!LineRanges.empty()) {
if (!Offsets.empty() || !Lengths.empty()) { if (!Offsets.empty() || !Lengths.empty()) {
llvm::errs() << "error: cannot use -lines with -offset/-length\n"; llvm::errs() << "error: cannot use -lines with -offset/-length\n";
@ -255,8 +255,8 @@ static bool format(StringRef FileName) {
bool IncompleteFormat = false; bool IncompleteFormat = false;
Replaces = tooling::mergeReplacements( Replaces = tooling::mergeReplacements(
Replaces, Replaces, reformat(FormatStyle, ChangedCode, Ranges, AssumedFileName,
reformat(FormatStyle, ChangedCode, Ranges, FileName, &IncompleteFormat)); &IncompleteFormat));
if (OutputXML) { if (OutputXML) {
llvm::outs() << "<?xml version='1.0'?>\n<replacements " llvm::outs() << "<?xml version='1.0'?>\n<replacements "
"xml:space='preserve' incomplete_format='" "xml:space='preserve' incomplete_format='"
@ -269,27 +269,26 @@ static bool format(StringRef FileName) {
outputReplacementsXML(Replaces); outputReplacementsXML(Replaces);
llvm::outs() << "</replacements>\n"; llvm::outs() << "</replacements>\n";
} else { } else {
std::string FormattedCode = FileManager Files((FileSystemOptions()));
applyAllReplacements(Code->getBuffer(), Replaces); DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceManager Sources(Diagnostics, Files);
FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files);
Rewriter Rewrite(Sources, LangOptions());
tooling::applyAllReplacements(Replaces, Rewrite);
if (Inplace) { if (Inplace) {
if (FileName == "-") if (FileName == "-")
llvm::errs() << "error: cannot use -i when reading from stdin.\n"; llvm::errs() << "error: cannot use -i when reading from stdin.\n";
else { else if (Rewrite.overwriteChangedFiles())
std::error_code EC; return true;
raw_fd_ostream FileOut(FileName, EC, llvm::sys::fs::F_Text);
if (EC) {
llvm::errs() << EC.message() << "\n";
return true;
}
FileOut << FormattedCode;
}
} else { } else {
if (Cursor.getNumOccurrences() != 0) if (Cursor.getNumOccurrences() != 0)
outs() << "{ \"Cursor\": " outs() << "{ \"Cursor\": "
<< tooling::shiftedCodePosition(Replaces, Cursor) << tooling::shiftedCodePosition(Replaces, Cursor)
<< ", \"IncompleteFormat\": " << ", \"IncompleteFormat\": "
<< (IncompleteFormat ? "true" : "false") << " }\n"; << (IncompleteFormat ? "true" : "false") << " }\n";
outs() << FormattedCode; Rewrite.getEditBuffer(ID).write(outs());
} }
} }
return false; return false;
@ -350,3 +349,4 @@ int main(int argc, const char **argv) {
} }
return Error ? 1 : 0; return Error ? 1 : 0;
} }