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
isStdout(StringRef Filename)18*0b57cec5SDimitry Andric static bool isStdout(StringRef Filename) { return Filename == "-"; }
19*0b57cec5SDimitry Andric
CleanupInstaller(StringRef Filename)20*0b57cec5SDimitry Andric ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
21*0b57cec5SDimitry Andric : Filename(std::string(Filename)), Keep(false) {
22*0b57cec5SDimitry Andric // Arrange for the file to be deleted if the process is killed.
23*0b57cec5SDimitry Andric if (!isStdout(Filename))
24*0b57cec5SDimitry Andric sys::RemoveFileOnSignal(Filename);
25*0b57cec5SDimitry Andric }
26*0b57cec5SDimitry Andric
~CleanupInstaller()27*0b57cec5SDimitry Andric ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
28*0b57cec5SDimitry Andric if (isStdout(Filename))
29*0b57cec5SDimitry Andric return;
30*0b57cec5SDimitry Andric
31*0b57cec5SDimitry Andric // Delete the file if the client hasn't told us not to.
32*0b57cec5SDimitry Andric if (!Keep)
33*0b57cec5SDimitry Andric sys::fs::remove(Filename);
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric // Ok, the file is successfully written and closed, or deleted. There's no
36*0b57cec5SDimitry Andric // further need to clean it up on signals.
37*0b57cec5SDimitry Andric sys::DontRemoveFileOnSignal(Filename);
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric
ToolOutputFile(StringRef Filename,std::error_code & EC,sys::fs::OpenFlags Flags)40*0b57cec5SDimitry Andric ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
41*0b57cec5SDimitry Andric sys::fs::OpenFlags Flags)
42*0b57cec5SDimitry Andric : Installer(Filename) {
43*0b57cec5SDimitry Andric if (isStdout(Filename)) {
44*0b57cec5SDimitry Andric OS = &outs();
45*0b57cec5SDimitry Andric EC = std::error_code();
46 return;
47 }
48 OSHolder.emplace(Filename, EC, Flags);
49 OS = &*OSHolder;
50 // If open fails, no cleanup is needed.
51 if (EC)
52 Installer.Keep = true;
53 }
54
ToolOutputFile(StringRef Filename,int FD)55 ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
56 : Installer(Filename) {
57 OSHolder.emplace(FD, true);
58 OS = &*OSHolder;
59 }
60