ToolOutputFile.h revision 353358
1//===- ToolOutputFile.h - Output files for compiler-like tools -----------===// 2// 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 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 16#include "llvm/Support/raw_ostream.h" 17 18namespace llvm { 19 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(). 25class 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. 32 std::string Filename; 33 public: 34 /// The flag which indicates whether we should not delete the file. 35 bool Keep; 36 37 explicit CleanupInstaller(StringRef Filename); 38 ~CleanupInstaller(); 39 } Installer; 40 41 /// The contained stream. This is intentionally declared after Installer. 42 raw_fd_ostream OS; 43 44public: 45 /// This constructor's arguments are passed to raw_fd_ostream's 46 /// constructor. 47 ToolOutputFile(StringRef Filename, std::error_code &EC, 48 sys::fs::OpenFlags Flags); 49 50 ToolOutputFile(StringRef Filename, int FD); 51 52 /// Return the contained raw_fd_ostream. 53 raw_fd_ostream &os() { return OS; } 54 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; } 58}; 59 60} // end llvm namespace 61 62#endif 63