1218885Sdim//===- ToolOutputFile.h - Output files for compiler-like tools -----------===// 2218885Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6218885Sdim// 7218885Sdim//===----------------------------------------------------------------------===// 8218885Sdim// 9327952Sdim// This file defines the ToolOutputFile class. 10218885Sdim// 11218885Sdim//===----------------------------------------------------------------------===// 12218885Sdim 13249423Sdim#ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H 14249423Sdim#define LLVM_SUPPORT_TOOLOUTPUTFILE_H 15218885Sdim 16218885Sdim#include "llvm/Support/raw_ostream.h" 17218885Sdim 18218885Sdimnamespace llvm { 19218885Sdim 20288943Sdim/// This class contains a raw_fd_ostream and adds a few extra features commonly 21288943Sdim/// needed for compiler-like tool output files: 22218885Sdim/// - The file is automatically deleted if the process is killed. 23327952Sdim/// - The file is automatically deleted when the ToolOutputFile 24218885Sdim/// object is destroyed unless the client calls keep(). 25327952Sdimclass ToolOutputFile { 26288943Sdim /// This class is declared before the raw_fd_ostream so that it is constructed 27288943Sdim /// before the raw_fd_ostream is constructed and destructed after the 28288943Sdim /// raw_fd_ostream is destructed. It installs cleanups in its constructor and 29288943Sdim /// uninstalls them in its destructor. 30218885Sdim class CleanupInstaller { 31280031Sdim /// The name of the file. 32218885Sdim std::string Filename; 33218885Sdim public: 34280031Sdim /// The flag which indicates whether we should not delete the file. 35218885Sdim bool Keep; 36218885Sdim 37341825Sdim explicit CleanupInstaller(StringRef Filename); 38218885Sdim ~CleanupInstaller(); 39218885Sdim } Installer; 40218885Sdim 41288943Sdim /// The contained stream. This is intentionally declared after Installer. 42218885Sdim raw_fd_ostream OS; 43218885Sdim 44218885Sdimpublic: 45341825Sdim /// This constructor's arguments are passed to raw_fd_ostream's 46280031Sdim /// constructor. 47327952Sdim ToolOutputFile(StringRef Filename, std::error_code &EC, 48327952Sdim sys::fs::OpenFlags Flags); 49218885Sdim 50327952Sdim ToolOutputFile(StringRef Filename, int FD); 51261991Sdim 52288943Sdim /// Return the contained raw_fd_ostream. 53218885Sdim raw_fd_ostream &os() { return OS; } 54218885Sdim 55288943Sdim /// Indicate that the tool's job wrt this output file has been successful and 56288943Sdim /// the file should not be deleted. 57218885Sdim void keep() { Installer.Keep = true; } 58218885Sdim}; 59218885Sdim 60218885Sdim} // end llvm namespace 61218885Sdim 62218885Sdim#endif 63