summaryrefslogtreecommitdiff
path: root/tools/clang-format
diff options
context:
space:
mode:
authorAlp Toker <alp@nuanti.com>2013-11-08 08:07:19 +0000
committerAlp Toker <alp@nuanti.com>2013-11-08 08:07:19 +0000
commit4a795861e1288de4ac888e787c9e466df21b82b3 (patch)
treeda86b0bb6332fe4566ec365b195ba33186fb770b /tools/clang-format
parent9a8a5fc5990ee8b06ad30e1c7b55b9dcf36ed140 (diff)
downloadclang-4a795861e1288de4ac888e787c9e466df21b82b3.tar.gz
clang-4a795861e1288de4ac888e787c9e466df21b82b3.tar.bz2
clang-4a795861e1288de4ac888e787c9e466df21b82b3.tar.xz
clang-format: Write files atomically
Switch clang-format over to Rewriter::overwriteChangedFiles(). The previous implementation was attempting to stream back directly to the original file and failing if it was already memory mapped by MemoryBuffer, an operation unsupported by Windows. MemoryBuffer generally mmaps files larger than the physical page size so this will have been difficult to reproduce consistently. This change also reduces flicker in code editors and IDEs on all platforms when reformatting in-place. Note that other incorrect uses of MemoryBuffer exist in LLVM/clang and will need a similar fix. A test should be added for Windows when libFormat performance issues are fixed (it takes longer than a day to format a 1MB file at present!) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194250 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-format')
-rw-r--r--tools/clang-format/ClangFormat.cpp12
1 files changed, 1 insertions, 11 deletions
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp
index 2ca2bed61a..85c47ab95c 100644
--- a/tools/clang-format/ClangFormat.cpp
+++ b/tools/clang-format/ClangFormat.cpp
@@ -213,18 +213,8 @@ static bool format(std::string FileName) {
Rewriter Rewrite(Sources, LangOptions());
tooling::applyAllReplacements(Replaces, Rewrite);
if (Inplace) {
- if (Replaces.size() == 0)
- return false; // Nothing changed, don't touch the file.
-
- std::string ErrorInfo;
- llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo,
- llvm::sys::fs::F_Binary);
- if (!ErrorInfo.empty()) {
- llvm::errs() << "Error while writing file: " << ErrorInfo << "\n";
+ if (Rewrite.overwriteChangedFiles())
return true;
- }
- Rewrite.getEditBuffer(ID).write(FileStream);
- FileStream.flush();
} else {
if (Cursor.getNumOccurrences() != 0)
outs() << "{ \"Cursor\": " << tooling::shiftedCodePosition(