diff options
author | Nico Weber <nicolasweber@gmx.de> | 2017-02-27 22:59:58 +0000 |
---|---|---|
committer | Nico Weber <nicolasweber@gmx.de> | 2017-02-27 22:59:58 +0000 |
commit | e9ed616c6e1d52c53c5fe9b87c9d5f35b21471fb (patch) | |
tree | 9a2d1d3138f89508f7233c0cdced2f1eb93f5251 /tools/clang-format | |
parent | 2b9de2a12492f72ff26b796e6230a256c06697ca (diff) | |
download | clang-e9ed616c6e1d52c53c5fe9b87c9d5f35b21471fb.tar.gz |
clang-format: Don't leave behind temp files in -i mode on Windows, PR26125, reloaded
Second attempt after http://llvm.org/viewvc/llvm-project?rev=296166&view=rev
In the first attempt, Code (the memory buffer backing the input file) was reset
before overwriteChangedFiles() was called, but overwriteChangedFiles() still
reads from it. This time, load the whole input file into memory instead of
using mmap when formatting in-place.
(Since the test is identical to what was in the repo before chapuni's revert,
svn diff doesn't show it – see the above link for the test.)
https://reviews.llvm.org/D30385
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296408 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-format')
-rw-r--r-- | tools/clang-format/ClangFormat.cpp | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index 941f90396d..ac0d0a8512 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -236,8 +236,15 @@ static void outputReplacementsXML(const Replacements &Replaces) { // Returns true on error. static bool format(StringRef FileName) { + if (!OutputXML && Inplace && FileName == "-") { + errs() << "error: cannot use -i when reading from stdin.\n"; + return false; + } + // On Windows, overwriting a file with an open file mapping doesn't work, + // so read the whole file into memory when formatting in-place. ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = - MemoryBuffer::getFileOrSTDIN(FileName); + !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) : + MemoryBuffer::getFileOrSTDIN(FileName); if (std::error_code EC = CodeOrErr.getError()) { errs() << EC.message() << "\n"; return true; @@ -297,9 +304,7 @@ static bool format(StringRef FileName) { Rewriter Rewrite(Sources, LangOptions()); tooling::applyAllReplacements(Replaces, Rewrite); if (Inplace) { - if (FileName == "-") - errs() << "error: cannot use -i when reading from stdin.\n"; - else if (Rewrite.overwriteChangedFiles()) + if (Rewrite.overwriteChangedFiles()) return true; } else { if (Cursor.getNumOccurrences() != 0) |