Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:34

0001 //===- ToolOutputFile.h - Output files for compiler-like tools --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines the ToolOutputFile class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
0014 #define LLVM_SUPPORT_TOOLOUTPUTFILE_H
0015 
0016 #include "llvm/Support/raw_ostream.h"
0017 #include <optional>
0018 
0019 namespace llvm {
0020 
0021 class CleanupInstaller {
0022 public:
0023   /// The name of the file.
0024   std::string Filename;
0025 
0026   /// The flag which indicates whether we should not delete the file.
0027   bool Keep;
0028 
0029   StringRef getFilename() { return Filename; }
0030   explicit CleanupInstaller(StringRef Filename);
0031   ~CleanupInstaller();
0032 };
0033 
0034 /// This class contains a raw_fd_ostream and adds a few extra features commonly
0035 /// needed for compiler-like tool output files:
0036 ///   - The file is automatically deleted if the process is killed.
0037 ///   - The file is automatically deleted when the ToolOutputFile
0038 ///     object is destroyed unless the client calls keep().
0039 class ToolOutputFile {
0040   /// This class is declared before the raw_fd_ostream so that it is constructed
0041   /// before the raw_fd_ostream is constructed and destructed after the
0042   /// raw_fd_ostream is destructed. It installs cleanups in its constructor and
0043   /// uninstalls them in its destructor.
0044   CleanupInstaller Installer;
0045 
0046   /// Storage for the stream, if we're owning our own stream. This is
0047   /// intentionally declared after Installer.
0048   std::optional<raw_fd_ostream> OSHolder;
0049 
0050   /// The actual stream to use.
0051   raw_fd_ostream *OS;
0052 
0053 public:
0054   /// This constructor's arguments are passed to raw_fd_ostream's
0055   /// constructor.
0056   ToolOutputFile(StringRef Filename, std::error_code &EC,
0057                  sys::fs::OpenFlags Flags);
0058 
0059   ToolOutputFile(StringRef Filename, int FD);
0060 
0061   /// Return the contained raw_fd_ostream.
0062   raw_fd_ostream &os() { return *OS; }
0063 
0064   /// Return the filename initialized with.
0065   StringRef getFilename() { return Installer.getFilename(); }
0066 
0067   /// Indicate that the tool's job wrt this output file has been successful and
0068   /// the file should not be deleted.
0069   void keep() { Installer.Keep = true; }
0070 
0071   const std::string &outputFilename() { return Installer.Filename; }
0072 };
0073 
0074 } // end llvm namespace
0075 
0076 #endif