ToolOutputFile.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the ToolOutputFile class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
  18. #define LLVM_SUPPORT_TOOLOUTPUTFILE_H
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. namespace llvm {
  22. /// This class contains a raw_fd_ostream and adds a few extra features commonly
  23. /// needed for compiler-like tool output files:
  24. /// - The file is automatically deleted if the process is killed.
  25. /// - The file is automatically deleted when the ToolOutputFile
  26. /// object is destroyed unless the client calls keep().
  27. class ToolOutputFile {
  28. /// This class is declared before the raw_fd_ostream so that it is constructed
  29. /// before the raw_fd_ostream is constructed and destructed after the
  30. /// raw_fd_ostream is destructed. It installs cleanups in its constructor and
  31. /// uninstalls them in its destructor.
  32. class CleanupInstaller {
  33. public:
  34. /// The name of the file.
  35. std::string Filename;
  36. /// The flag which indicates whether we should not delete the file.
  37. bool Keep;
  38. StringRef getFilename() { return Filename; }
  39. explicit CleanupInstaller(StringRef Filename);
  40. ~CleanupInstaller();
  41. } Installer;
  42. /// Storage for the stream, if we're owning our own stream. This is
  43. /// intentionally declared after Installer.
  44. Optional<raw_fd_ostream> OSHolder;
  45. /// The actual stream to use.
  46. raw_fd_ostream *OS;
  47. public:
  48. /// This constructor's arguments are passed to raw_fd_ostream's
  49. /// constructor.
  50. ToolOutputFile(StringRef Filename, std::error_code &EC,
  51. sys::fs::OpenFlags Flags);
  52. ToolOutputFile(StringRef Filename, int FD);
  53. /// Return the contained raw_fd_ostream.
  54. raw_fd_ostream &os() { return *OS; }
  55. /// Return the filename initialized with.
  56. StringRef getFilename() { return Installer.getFilename(); }
  57. /// Indicate that the tool's job wrt this output file has been successful and
  58. /// the file should not be deleted.
  59. void keep() { Installer.Keep = true; }
  60. const std::string &outputFilename() { return Installer.Filename; }
  61. };
  62. } // end llvm namespace
  63. #endif
  64. #ifdef __GNUC__
  65. #pragma GCC diagnostic pop
  66. #endif