LLVM  14.0.0git
ToolOutputFile.cpp
Go to the documentation of this file.
1 //===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===//
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 implements the ToolOutputFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/Triple.h"
16 #include "llvm/Support/Signals.h"
17 using namespace llvm;
18 
19 static bool isStdout(StringRef Filename) { return Filename == "-"; }
20 
21 ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
22  : Filename(std::string(Filename)), Keep(false) {
23  // Arrange for the file to be deleted if the process is killed.
24  if (!isStdout(Filename))
25  sys::RemoveFileOnSignal(Filename);
26 }
27 
28 ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
29  if (isStdout(Filename))
30  return;
31 
32  // Delete the file if the client hasn't told us not to.
33  if (!Keep)
34  sys::fs::remove(Filename);
35 
36  // Ok, the file is successfully written and closed, or deleted. There's no
37  // further need to clean it up on signals.
39 }
40 
41 ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
42  sys::fs::OpenFlags Flags)
43  : Installer(Filename) {
44  if (isStdout(Filename)) {
45  OS = &outs();
46  EC = std::error_code();
47  return;
48  }
49  OSHolder.emplace(Filename, EC, Flags);
50  OS = OSHolder.getPointer();
51  // If open fails, no cleanup is needed.
52  if (EC)
53  Installer.Keep = true;
54 }
55 
57  : Installer(Filename) {
58  OSHolder.emplace(FD, true);
59  OS = OSHolder.getPointer();
60 }
Signals.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
FileSystem.h
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:41
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:884
false
Definition: StackSlotColoring.cpp:142
ToolOutputFile.h
isStdout
static bool isStdout(StringRef Filename)
Definition: ToolOutputFile.cpp:19
Triple.h
llvm::sys::fs::remove
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
std
Definition: BitVector.h:838
llvm::sys::fs::OpenFlags
OpenFlags
Definition: FileSystem.h:756
llvm::sys::DontRemoveFileOnSignal
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
llvm::sys::RemoveFileOnSignal
bool RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg=nullptr)
This function registers signal handlers to ensure that if a signal gets delivered that the named file...
SpecialSubKind::string
@ string