ToolOutputFile.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===--- ToolOutputFile.cpp - Implement the tool_output_file class --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This implements the tool_output_file class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/ToolOutputFile.h"
  14. #include "llvm/Support/FileSystem.h"
  15. #include "llvm/Support/Signals.h"
  16. using namespace llvm;
  17. tool_output_file::CleanupInstaller::CleanupInstaller(StringRef Filename)
  18. : Filename(Filename), Keep(false) {
  19. // Arrange for the file to be deleted if the process is killed.
  20. if (Filename != "-")
  21. sys::RemoveFileOnSignal(Filename);
  22. }
  23. tool_output_file::CleanupInstaller::~CleanupInstaller() {
  24. // Delete the file if the client hasn't told us not to.
  25. if (!Keep && Filename != "-")
  26. sys::fs::remove(Filename);
  27. // Ok, the file is successfully written and closed, or deleted. There's no
  28. // further need to clean it up on signals.
  29. if (Filename != "-")
  30. sys::DontRemoveFileOnSignal(Filename);
  31. }
  32. tool_output_file::tool_output_file(StringRef Filename, std::error_code &EC,
  33. sys::fs::OpenFlags Flags)
  34. : Installer(Filename), OS(Filename, EC, Flags) {
  35. // If open fails, no cleanup is needed.
  36. if (EC)
  37. Installer.Keep = true;
  38. }
  39. tool_output_file::tool_output_file(StringRef Filename, int FD)
  40. : Installer(Filename), OS(FD, true) {}