1 //===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===//
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
7 //===----------------------------------------------------------------------===//
9 // This implements the ToolOutputFile class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/ToolOutputFile.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Signals.h"
18 ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename
)
19 : Filename(Filename
), Keep(false) {
20 // Arrange for the file to be deleted if the process is killed.
22 sys::RemoveFileOnSignal(Filename
);
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
);
30 // Ok, the file is successfully written and closed, or deleted. There's no
31 // further need to clean it up on signals.
33 sys::DontRemoveFileOnSignal(Filename
);
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.
41 Installer
.Keep
= true;
44 ToolOutputFile::ToolOutputFile(StringRef Filename
, int FD
)
45 : Installer(Filename
), OS(FD
, true) {}