LLVM API Documentation

ToolOutputFile.h
Go to the documentation of this file.
00001 //===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the tool_output_file class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
00015 #define LLVM_SUPPORT_TOOLOUTPUTFILE_H
00016 
00017 #include "llvm/Support/raw_ostream.h"
00018 
00019 namespace llvm {
00020 
00021 /// tool_output_file - This class contains a raw_fd_ostream and adds a
00022 /// few extra features commonly needed for compiler-like tool output files:
00023 ///   - The file is automatically deleted if the process is killed.
00024 ///   - The file is automatically deleted when the tool_output_file
00025 ///     object is destroyed unless the client calls keep().
00026 class tool_output_file {
00027   /// Installer - This class is declared before the raw_fd_ostream so that
00028   /// it is constructed before the raw_fd_ostream is constructed and
00029   /// destructed after the raw_fd_ostream is destructed. It installs
00030   /// cleanups in its constructor and uninstalls them in its destructor.
00031   class CleanupInstaller {
00032     /// The name of the file.
00033     std::string Filename;
00034   public:
00035     /// The flag which indicates whether we should not delete the file.
00036     bool Keep;
00037 
00038     explicit CleanupInstaller(StringRef ilename);
00039     ~CleanupInstaller();
00040   } Installer;
00041 
00042   /// OS - The contained stream. This is intentionally declared after
00043   /// Installer.
00044   raw_fd_ostream OS;
00045 
00046 public:
00047   /// This constructor's arguments are passed to to raw_fd_ostream's
00048   /// constructor.
00049   tool_output_file(StringRef Filename, std::error_code &EC,
00050                    sys::fs::OpenFlags Flags);
00051 
00052   tool_output_file(StringRef Filename, int FD);
00053 
00054   /// os - Return the contained raw_fd_ostream.
00055   raw_fd_ostream &os() { return OS; }
00056 
00057   /// keep - Indicate that the tool's job wrt this output file has been
00058   /// successful and the file should not be deleted.
00059   void keep() { Installer.Keep = true; }
00060 };
00061 
00062 } // end llvm namespace
00063 
00064 #endif