1e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
2e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//
3e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//                     The LLVM Compiler Infrastructure
4e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//
5e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman// This file is distributed under the University of Illinois Open Source
6e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman// License. See LICENSE.TXT for details.
7e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//
8e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//===----------------------------------------------------------------------===//
9e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//
10e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//  This file defines the tool_output_file class.
11e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//
12e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman//===----------------------------------------------------------------------===//
13e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_SUPPORT_TOOLOUTPUTFILE_H
16e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
17e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman#include "llvm/Support/raw_ostream.h"
18e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
19e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohmannamespace llvm {
20e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
21e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman/// tool_output_file - This class contains a raw_fd_ostream and adds a
22e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman/// few extra features commonly needed for compiler-like tool output files:
23e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman///   - The file is automatically deleted if the process is killed.
24e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman///   - The file is automatically deleted when the tool_output_file
25e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman///     object is destroyed unless the client calls keep().
26e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohmanclass tool_output_file {
27e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// Installer - This class is declared before the raw_fd_ostream so that
28e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// it is constructed before the raw_fd_ostream is constructed and
29e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// destructed after the raw_fd_ostream is destructed. It installs
30e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// cleanups in its constructor and uninstalls them in its destructor.
31e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  class CleanupInstaller {
32e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman    /// Filename - The name of the file.
33e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman    std::string Filename;
34e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  public:
35e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman    /// Keep - The flag which indicates whether we should not delete the file.
36e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman    bool Keep;
37e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
38e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman    explicit CleanupInstaller(const char *filename);
39e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman    ~CleanupInstaller();
40e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  } Installer;
41e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
42e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// OS - The contained stream. This is intentionally declared after
43e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// Installer.
44e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  raw_fd_ostream OS;
45e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
46e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohmanpublic:
47e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// tool_output_file - This constructor's arguments are passed to
48e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// to raw_fd_ostream's constructor.
49e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  tool_output_file(const char *filename, std::string &ErrorInfo,
50c1b49b56d4132efa2e06deb8f23508d0de4c8800Rafael Espindola                   sys::fs::OpenFlags Flags = sys::fs::F_None);
51e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
5268c0efac35021516bf46b2793e56e0d9d804c9e8Rafael Espindola  tool_output_file(const char *Filename, int FD);
5368c0efac35021516bf46b2793e56e0d9d804c9e8Rafael Espindola
54e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// os - Return the contained raw_fd_ostream.
55e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  raw_fd_ostream &os() { return OS; }
56e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
57e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// keep - Indicate that the tool's job wrt this output file has been
58e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  /// successful and the file should not be deleted.
59e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman  void keep() { Installer.Keep = true; }
60e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman};
61e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
62e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman} // end llvm namespace
63e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman
64e4f1a9b8a272ff7452759019ee7774e9dbdf1568Dan Gohman#endif
65