[PATCH] D153741: [Tooling][Rewriter] Remove the redundant AtomicallyMovedFile Implementation.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 26 00:45:56 PDT 2023


hokein created this revision.
hokein added a reviewer: avl.
Herald added a project: All.
hokein requested review of this revision.
Herald added a project: clang.

Replace it with llvm::writeToOutput.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153741

Files:
  clang/lib/Rewrite/Rewriter.cpp


Index: clang/lib/Rewrite/Rewriter.cpp
===================================================================
--- clang/lib/Rewrite/Rewriter.cpp
+++ clang/lib/Rewrite/Rewriter.cpp
@@ -23,6 +23,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cassert>
@@ -410,68 +411,21 @@
   return false;
 }
 
-namespace {
-
-// A wrapper for a file stream that atomically overwrites the target.
-//
-// Creates a file output stream for a temporary file in the constructor,
-// which is later accessible via getStream() if ok() return true.
-// Flushes the stream and moves the temporary file to the target location
-// in the destructor.
-class AtomicallyMovedFile {
-public:
-  AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
-                      bool &AllWritten)
-      : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
-    TempFilename = Filename;
-    TempFilename += "-%%%%%%%%";
-    int FD;
-    if (llvm::sys::fs::createUniqueFile(TempFilename, FD, TempFilename)) {
-      AllWritten = false;
-      Diagnostics.Report(clang::diag::err_unable_to_make_temp)
-        << TempFilename;
-    } else {
-      FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));
-    }
-  }
-
-  ~AtomicallyMovedFile() {
-    if (!ok()) return;
-
-    // Close (will also flush) theFileStream.
-    FileStream->close();
-    if (std::error_code ec = llvm::sys::fs::rename(TempFilename, Filename)) {
-      AllWritten = false;
-      Diagnostics.Report(clang::diag::err_unable_to_rename_temp)
-        << TempFilename << Filename << ec.message();
-      // If the remove fails, there's not a lot we can do - this is already an
-      // error.
-      llvm::sys::fs::remove(TempFilename);
-    }
-  }
-
-  bool ok() { return (bool)FileStream; }
-  raw_ostream &getStream() { return *FileStream; }
-
-private:
-  DiagnosticsEngine &Diagnostics;
-  StringRef Filename;
-  SmallString<128> TempFilename;
-  std::unique_ptr<llvm::raw_fd_ostream> FileStream;
-  bool &AllWritten;
-};
-
-} // namespace
-
 bool Rewriter::overwriteChangedFiles() {
   bool AllWritten = true;
+  auto& Diag = getSourceMgr().getDiagnostics();
+  unsigned OverwriteFailure = Diag.getCustomDiagID(
+      DiagnosticsEngine::Error, "unable to overwrite file %0: %1");
   for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
-    const FileEntry *Entry =
-        getSourceMgr().getFileEntryForID(I->first);
-    AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),
-                             AllWritten);
-    if (File.ok()) {
-      I->second.write(File.getStream());
+    const FileEntry *Entry = getSourceMgr().getFileEntryForID(I->first);
+    if (auto Error =
+            llvm::writeToOutput(Entry->getName(), [&](raw_ostream &OS) {
+              I->second.write(OS);
+              return llvm::Error::success();
+            })) {
+      Diag.Report(OverwriteFailure)
+          << Entry->getName() << llvm::toString(std::move(Error));
+      AllWritten = false;
     }
   }
   return !AllWritten;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153741.534449.patch
Type: text/x-patch
Size: 3239 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230626/cc59e588/attachment.bin>


More information about the cfe-commits mailing list