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