Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ToolOutputFile.h - Output files for compiler-like tools -----------===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the ToolOutputFile class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H |
| 14 | #define LLVM_SUPPORT_TOOLOUTPUTFILE_H |
| 15 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 16 | #include "llvm/ADT/Optional.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | /// This class contains a raw_fd_ostream and adds a few extra features commonly |
| 22 | /// needed for compiler-like tool output files: |
| 23 | /// - The file is automatically deleted if the process is killed. |
| 24 | /// - The file is automatically deleted when the ToolOutputFile |
| 25 | /// object is destroyed unless the client calls keep(). |
| 26 | class ToolOutputFile { |
| 27 | /// This class is declared before the raw_fd_ostream so that it is constructed |
| 28 | /// before the raw_fd_ostream is constructed and destructed after the |
| 29 | /// raw_fd_ostream is destructed. It installs cleanups in its constructor and |
| 30 | /// uninstalls them in its destructor. |
| 31 | class CleanupInstaller { |
| 32 | /// The name of the file. |
| 33 | std::string Filename; |
| 34 | public: |
| 35 | /// The flag which indicates whether we should not delete the file. |
| 36 | bool Keep; |
| 37 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 38 | explicit CleanupInstaller(StringRef Filename); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 39 | ~CleanupInstaller(); |
| 40 | } Installer; |
| 41 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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 | |
| 46 | /// The actual stream to use. |
| 47 | raw_fd_ostream *OS; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | /// This constructor's arguments are passed to raw_fd_ostream's |
| 51 | /// constructor. |
| 52 | ToolOutputFile(StringRef Filename, std::error_code &EC, |
| 53 | sys::fs::OpenFlags Flags); |
| 54 | |
| 55 | ToolOutputFile(StringRef Filename, int FD); |
| 56 | |
| 57 | /// Return the contained raw_fd_ostream. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 58 | raw_fd_ostream &os() { return *OS; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | |
| 60 | /// Indicate that the tool's job wrt this output file has been successful and |
| 61 | /// the file should not be deleted. |
| 62 | void keep() { Installer.Keep = true; } |
| 63 | }; |
| 64 | |
| 65 | } // end llvm namespace |
| 66 | |
| 67 | #endif |