[ThinLTO] Add code comment. NFC
[llvm-complete.git] / lib / Support / ToolOutputFile.cpp
blobed3a247f011558f042dd231c51cd1e4149e39df7
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/ToolOutputFile.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Signals.h"
16 using namespace llvm;
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);
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.
32 if (Filename != "-")
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.
40 if (EC)
41 Installer.Keep = true;
44 ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
45 : Installer(Filename), OS(FD, true) {}