xref: /freebsd/contrib/llvm-project/llvm/lib/Support/ToolOutputFile.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This implements the ToolOutputFile class.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
14*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/Signals.h"
16*0b57cec5SDimitry Andric using namespace llvm;
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
19*0b57cec5SDimitry Andric     : Filename(Filename), Keep(false) {
20*0b57cec5SDimitry Andric   // Arrange for the file to be deleted if the process is killed.
21*0b57cec5SDimitry Andric   if (Filename != "-")
22*0b57cec5SDimitry Andric     sys::RemoveFileOnSignal(Filename);
23*0b57cec5SDimitry Andric }
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
26*0b57cec5SDimitry Andric   // Delete the file if the client hasn't told us not to.
27*0b57cec5SDimitry Andric   if (!Keep && Filename != "-")
28*0b57cec5SDimitry Andric     sys::fs::remove(Filename);
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric   // Ok, the file is successfully written and closed, or deleted. There's no
31*0b57cec5SDimitry Andric   // further need to clean it up on signals.
32*0b57cec5SDimitry Andric   if (Filename != "-")
33*0b57cec5SDimitry Andric     sys::DontRemoveFileOnSignal(Filename);
34*0b57cec5SDimitry Andric }
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
37*0b57cec5SDimitry Andric                                sys::fs::OpenFlags Flags)
38*0b57cec5SDimitry Andric     : Installer(Filename), OS(Filename, EC, Flags) {
39*0b57cec5SDimitry Andric   // If open fails, no cleanup is needed.
40*0b57cec5SDimitry Andric   if (EC)
41*0b57cec5SDimitry Andric     Installer.Keep = true;
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
45*0b57cec5SDimitry Andric     : Installer(Filename), OS(FD, true) {}
46