1218885Sdim//===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim//  This file defines the tool_output_file class.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14249423Sdim#ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
15249423Sdim#define LLVM_SUPPORT_TOOLOUTPUTFILE_H
16218885Sdim
17218885Sdim#include "llvm/Support/raw_ostream.h"
18218885Sdim
19218885Sdimnamespace llvm {
20218885Sdim
21218885Sdim/// tool_output_file - This class contains a raw_fd_ostream and adds a
22218885Sdim/// few extra features commonly needed for compiler-like tool output files:
23218885Sdim///   - The file is automatically deleted if the process is killed.
24218885Sdim///   - The file is automatically deleted when the tool_output_file
25218885Sdim///     object is destroyed unless the client calls keep().
26218885Sdimclass tool_output_file {
27218885Sdim  /// Installer - This class is declared before the raw_fd_ostream so that
28218885Sdim  /// it is constructed before the raw_fd_ostream is constructed and
29218885Sdim  /// destructed after the raw_fd_ostream is destructed. It installs
30218885Sdim  /// cleanups in its constructor and uninstalls them in its destructor.
31218885Sdim  class CleanupInstaller {
32218885Sdim    /// Filename - The name of the file.
33218885Sdim    std::string Filename;
34218885Sdim  public:
35218885Sdim    /// Keep - The flag which indicates whether we should not delete the file.
36218885Sdim    bool Keep;
37218885Sdim
38218885Sdim    explicit CleanupInstaller(const char *filename);
39218885Sdim    ~CleanupInstaller();
40218885Sdim  } Installer;
41218885Sdim
42218885Sdim  /// OS - The contained stream. This is intentionally declared after
43218885Sdim  /// Installer.
44218885Sdim  raw_fd_ostream OS;
45218885Sdim
46218885Sdimpublic:
47218885Sdim  /// tool_output_file - This constructor's arguments are passed to
48218885Sdim  /// to raw_fd_ostream's constructor.
49218885Sdim  tool_output_file(const char *filename, std::string &ErrorInfo,
50263508Sdim                   sys::fs::OpenFlags Flags = sys::fs::F_None);
51218885Sdim
52263508Sdim  tool_output_file(const char *Filename, int FD);
53263508Sdim
54218885Sdim  /// os - Return the contained raw_fd_ostream.
55218885Sdim  raw_fd_ostream &os() { return OS; }
56218885Sdim
57218885Sdim  /// keep - Indicate that the tool's job wrt this output file has been
58218885Sdim  /// successful and the file should not be deleted.
59218885Sdim  void keep() { Installer.Keep = true; }
60218885Sdim};
61218885Sdim
62218885Sdim} // end llvm namespace
63218885Sdim
64218885Sdim#endif
65