LLVM  16.0.0git
ToolOutputFile.h
Go to the documentation of this file.
1 //===- ToolOutputFile.h - Output files for compiler-like tools --*- C++ -*-===//
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 #include <optional>
19 
20 namespace llvm {
21 
22 /// This class contains a raw_fd_ostream and adds a few extra features commonly
23 /// needed for compiler-like tool output files:
24 /// - The file is automatically deleted if the process is killed.
25 /// - The file is automatically deleted when the ToolOutputFile
26 /// object is destroyed unless the client calls keep().
28  /// This class is declared before the raw_fd_ostream so that it is constructed
29  /// before the raw_fd_ostream is constructed and destructed after the
30  /// raw_fd_ostream is destructed. It installs cleanups in its constructor and
31  /// uninstalls them in its destructor.
32  class CleanupInstaller {
33  public:
34  /// The name of the file.
35  std::string Filename;
36 
37  /// The flag which indicates whether we should not delete the file.
38  bool Keep;
39 
40  StringRef getFilename() { return Filename; }
41  explicit CleanupInstaller(StringRef Filename);
42  ~CleanupInstaller();
43  } Installer;
44 
45  /// Storage for the stream, if we're owning our own stream. This is
46  /// intentionally declared after Installer.
47  std::optional<raw_fd_ostream> OSHolder;
48 
49  /// The actual stream to use.
50  raw_fd_ostream *OS;
51 
52 public:
53  /// This constructor's arguments are passed to raw_fd_ostream's
54  /// constructor.
55  ToolOutputFile(StringRef Filename, std::error_code &EC,
56  sys::fs::OpenFlags Flags);
57 
58  ToolOutputFile(StringRef Filename, int FD);
59 
60  /// Return the contained raw_fd_ostream.
61  raw_fd_ostream &os() { return *OS; }
62 
63  /// Return the filename initialized with.
64  StringRef getFilename() { return Installer.getFilename(); }
65 
66  /// Indicate that the tool's job wrt this output file has been successful and
67  /// the file should not be deleted.
68  void keep() { Installer.Keep = true; }
69 
70  const std::string &outputFilename() { return Installer.Filename; }
71 };
72 
73 } // end llvm namespace
74 
75 #endif
llvm::ToolOutputFile::outputFilename
const std::string & outputFilename()
Definition: ToolOutputFile.h:70
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:27
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:68
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:441
llvm::ToolOutputFile::os
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
Definition: ToolOutputFile.h:61
llvm::sys::fs::OpenFlags
OpenFlags
Definition: FileSystem.h:756
llvm::FunctionReturnThunksKind::Keep
@ Keep
No function return thunk.
raw_ostream.h
llvm::ToolOutputFile::getFilename
StringRef getFilename()
Return the filename initialized with.
Definition: ToolOutputFile.h:64