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 
13 #include "llvm/Support/ToolOutputFile.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Signals.h"
16 using namespace llvm;
17 
18 ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
19     : Filename(Filename), Keep(false) {
20   // Arrange for the file to be deleted if the process is killed.
21   if (Filename != "-")
22     sys::RemoveFileOnSignal(Filename);
23 }
24 
25 ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
26   // Delete the file if the client hasn't told us not to.
27   if (!Keep && Filename != "-")
28     sys::fs::remove(Filename);
29 
30   // Ok, the file is successfully written and closed, or deleted. There's no
31   // further need to clean it up on signals.
32   if (Filename != "-")
33     sys::DontRemoveFileOnSignal(Filename);
34 }
35 
36 ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
37                                sys::fs::OpenFlags Flags)
38     : Installer(Filename), OS(Filename, EC, Flags) {
39   // If open fails, no cleanup is needed.
40   if (EC)
41     Installer.Keep = true;
42 }
43 
44 ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
45     : Installer(Filename), OS(FD, true) {}
46