LLVM  15.0.0git
ToolOutputFile.h
Go to the documentation of this file.
1 //===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ToolOutputFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
14 #define LLVM_SUPPORT_TOOLOUTPUTFILE_H
15 
16 #include "llvm/ADT/Optional.h"
18 
19 namespace llvm {
20 
21 /// This class contains a raw_fd_ostream and adds a few extra features commonly
22 /// needed for compiler-like tool output files:
23 /// - The file is automatically deleted if the process is killed.
24 /// - The file is automatically deleted when the ToolOutputFile
25 /// object is destroyed unless the client calls keep().
27  /// This class is declared before the raw_fd_ostream so that it is constructed
28  /// before the raw_fd_ostream is constructed and destructed after the
29  /// raw_fd_ostream is destructed. It installs cleanups in its constructor and
30  /// uninstalls them in its destructor.
31  class CleanupInstaller {
32  public:
33  /// The name of the file.
34  std::string Filename;
35 
36  /// The flag which indicates whether we should not delete the file.
37  bool Keep;
38 
39  StringRef getFilename() { return Filename; }
40  explicit CleanupInstaller(StringRef Filename);
41  ~CleanupInstaller();
42  } Installer;
43 
44  /// Storage for the stream, if we're owning our own stream. This is
45  /// intentionally declared after Installer.
46  Optional<raw_fd_ostream> OSHolder;
47 
48  /// The actual stream to use.
49  raw_fd_ostream *OS;
50 
51 public:
52  /// This constructor's arguments are passed to raw_fd_ostream's
53  /// constructor.
54  ToolOutputFile(StringRef Filename, std::error_code &EC,
55  sys::fs::OpenFlags Flags);
56 
57  ToolOutputFile(StringRef Filename, int FD);
58 
59  /// Return the contained raw_fd_ostream.
60  raw_fd_ostream &os() { return *OS; }
61 
62  /// Return the filename initialized with.
63  StringRef getFilename() { return Installer.getFilename(); }
64 
65  /// Indicate that the tool's job wrt this output file has been successful and
66  /// the file should not be deleted.
67  void keep() { Installer.Keep = true; }
68 
69  const std::string &outputFilename() { return Installer.Filename; }
70 };
71 
72 } // end llvm namespace
73 
74 #endif
llvm::ToolOutputFile::outputFilename
const std::string & outputFilename()
Definition: ToolOutputFile.h:69
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
Optional.h
llvm::ToolOutputFile
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
Definition: ToolOutputFile.h:26
llvm::ToolOutputFile::ToolOutputFile
ToolOutputFile(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
This constructor's arguments are passed to raw_fd_ostream's constructor.
Definition: ToolOutputFile.cpp:40
llvm::ToolOutputFile::keep
void keep()
Indicate that the tool's job wrt this output file has been successful and the file should not be dele...
Definition: ToolOutputFile.h:67
llvm::Optional
Definition: APInt.h:33
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:444
llvm::ToolOutputFile::os
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
Definition: ToolOutputFile.h:60
llvm::sys::fs::OpenFlags
OpenFlags
Definition: FileSystem.h:756
raw_ostream.h
llvm::ToolOutputFile::getFilename
StringRef getFilename()
Return the filename initialized with.
Definition: ToolOutputFile.h:63