From 867a93824629589b07c9634897d7726942d58c50 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 30 Sep 2015 13:59:29 +0000 Subject: [PATCH] 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 --- clang/tools/clang-format/ClangFormat.cpp | 36 ++++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index 3d6fef0d0994..8917818a0a87 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -19,6 +19,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Basic/Version.h" #include "clang/Format/Format.h" +#include "clang/Rewrite/Core/Rewriter.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -77,7 +78,7 @@ AssumeFileName("assume-filename", cl::desc("When reading from stdin, clang-format assumes this\n" "filename to look for a style config file (with\n" "-style=file) and to determine the language."), - cl::cat(ClangFormatCategory)); + cl::init(""), cl::cat(ClangFormatCategory)); static cl::opt Inplace("i", cl::desc("Inplace edit s, if specified."), @@ -109,8 +110,7 @@ namespace format { static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, SourceManager &Sources, FileManager &Files) { - const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "" : - FileName, + const FileEntry *Entry = Files.getVirtualFile(FileName, Source->getBufferSize(), 0); Sources.overrideFileContents(Entry, Source, true); return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); @@ -132,7 +132,7 @@ static bool fillRanges(MemoryBuffer *Code, IntrusiveRefCntPtr(new DiagnosticIDs), new DiagnosticOptions); SourceManager Sources(Diagnostics, Files); - FileID ID = createInMemoryFile("-", Code, Sources, Files); + FileID ID = createInMemoryFile("", Code, Sources, Files); if (!LineRanges.empty()) { if (!Offsets.empty() || !Lengths.empty()) { llvm::errs() << "error: cannot use -lines with -offset/-length\n"; @@ -255,8 +255,8 @@ static bool format(StringRef FileName) { bool IncompleteFormat = false; Replaces = tooling::mergeReplacements( - Replaces, - reformat(FormatStyle, ChangedCode, Ranges, FileName, &IncompleteFormat)); + Replaces, reformat(FormatStyle, ChangedCode, Ranges, AssumedFileName, + &IncompleteFormat)); if (OutputXML) { llvm::outs() << "\n