diff options
author | Jan Korous <jkorous@apple.com> | 2019-09-13 20:08:27 +0000 |
---|---|---|
committer | Jan Korous <jkorous@apple.com> | 2019-09-13 20:08:27 +0000 |
commit | f69c91780fbb0e9c0e95f70a079f578efdca0bfa (patch) | |
tree | 8c22ed25e585979a3fda35911273cf1e62de3707 /clang/lib/Frontend/ASTUnit.cpp | |
parent | c6ffefd2d1a95b7312741fbd3a9972e5f918173b (diff) | |
download | bcm5719-llvm-f69c91780fbb0e9c0e95f70a079f578efdca0bfa.tar.gz bcm5719-llvm-f69c91780fbb0e9c0e95f70a079f578efdca0bfa.zip |
[Support] Add overload writeFileAtomically(std::function Writer)
Differential Revision: https://reviews.llvm.org/D67424
llvm-svn: 371890
Diffstat (limited to 'clang/lib/Frontend/ASTUnit.cpp')
-rw-r--r-- | clang/lib/Frontend/ASTUnit.cpp | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 35348ffe13b..f5e291b7fe1 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -84,6 +84,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Timer.h" #include "llvm/Support/VirtualFileSystem.h" @@ -2301,26 +2302,19 @@ bool ASTUnit::Save(StringRef File) { SmallString<128> TempPath; TempPath = File; TempPath += "-%%%%%%%%"; - int fd; - if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath)) - return true; - // FIXME: Can we somehow regenerate the stat cache here, or do we need to // unconditionally create a stat cache when we parse the file? - llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); - - serialize(Out); - Out.close(); - if (Out.has_error()) { - Out.clear_error(); - return true; - } - if (llvm::sys::fs::rename(TempPath, File)) { - llvm::sys::fs::remove(TempPath); + if (llvm::Error Err = llvm::writeFileAtomically( + TempPath, File, [this](llvm::raw_ostream &Out) { + return serialize(Out) ? llvm::make_error<llvm::StringError>( + "ASTUnit serialization failed", + llvm::inconvertibleErrorCode()) + : llvm::Error::success(); + })) { + consumeError(std::move(Err)); return true; } - return false; } |