ToolOutputFile.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
  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 file defines the tool_output_file class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
  14. #define LLVM_SUPPORT_TOOLOUTPUTFILE_H
  15. #include "llvm/Support/raw_ostream.h"
  16. namespace llvm {
  17. /// This class contains a raw_fd_ostream and adds a few extra features commonly
  18. /// needed for compiler-like tool output files:
  19. /// - The file is automatically deleted if the process is killed.
  20. /// - The file is automatically deleted when the tool_output_file
  21. /// object is destroyed unless the client calls keep().
  22. class tool_output_file {
  23. /// This class is declared before the raw_fd_ostream so that it is constructed
  24. /// before the raw_fd_ostream is constructed and destructed after the
  25. /// raw_fd_ostream is destructed. It installs cleanups in its constructor and
  26. /// uninstalls them in its destructor.
  27. class CleanupInstaller {
  28. /// The name of the file.
  29. std::string Filename;
  30. public:
  31. /// The flag which indicates whether we should not delete the file.
  32. bool Keep;
  33. explicit CleanupInstaller(StringRef ilename);
  34. ~CleanupInstaller();
  35. } Installer;
  36. /// The contained stream. This is intentionally declared after Installer.
  37. raw_fd_ostream OS;
  38. public:
  39. /// This constructor's arguments are passed to to raw_fd_ostream's
  40. /// constructor.
  41. tool_output_file(StringRef Filename, std::error_code &EC,
  42. sys::fs::OpenFlags Flags);
  43. tool_output_file(StringRef Filename, int FD);
  44. /// Return the contained raw_fd_ostream.
  45. raw_fd_ostream &os() { return OS; }
  46. /// Indicate that the tool's job wrt this output file has been successful and
  47. /// the file should not be deleted.
  48. void keep() { Installer.Keep = true; }
  49. };
  50. } // end llvm namespace
  51. #endif