blob: 8ae977db6a1471e074faf71b5de8c9b050c3a0b6 [file] [log] [blame]
Dan Gohman0df7ea42010-10-07 20:32:40 +00001//===--- ToolOutputFile.cpp - Implement the tool_output_file class --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements the tool_output_file class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/ToolOutputFile.h"
Rafael Espindola702a80c2013-06-17 20:37:56 +000015#include "llvm/Support/FileSystem.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000016#include "llvm/Support/Signals.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000017using namespace llvm;
18
Rafael Espindola3fd1e992014-08-25 18:16:47 +000019tool_output_file::CleanupInstaller::CleanupInstaller(StringRef Filename)
20 : Filename(Filename), Keep(false) {
Dan Gohman0df7ea42010-10-07 20:32:40 +000021 // Arrange for the file to be deleted if the process is killed.
22 if (Filename != "-")
Rafael Espindola4f35da72013-06-13 21:16:58 +000023 sys::RemoveFileOnSignal(Filename);
Dan Gohman0df7ea42010-10-07 20:32:40 +000024}
25
26tool_output_file::CleanupInstaller::~CleanupInstaller() {
27 // Delete the file if the client hasn't told us not to.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000028 if (!Keep && Filename != "-")
29 sys::fs::remove(Filename);
Dan Gohman0df7ea42010-10-07 20:32:40 +000030
31 // Ok, the file is successfully written and closed, or deleted. There's no
32 // further need to clean it up on signals.
33 if (Filename != "-")
Rafael Espindola4f35da72013-06-13 21:16:58 +000034 sys::DontRemoveFileOnSignal(Filename);
Dan Gohman0df7ea42010-10-07 20:32:40 +000035}
36
Rafael Espindola3fd1e992014-08-25 18:16:47 +000037tool_output_file::tool_output_file(StringRef Filename, std::error_code &EC,
Rafael Espindola6d354812013-07-16 19:44:17 +000038 sys::fs::OpenFlags Flags)
Rafael Espindola3fd1e992014-08-25 18:16:47 +000039 : Installer(Filename), OS(Filename, EC, Flags) {
Dan Gohman0df7ea42010-10-07 20:32:40 +000040 // If open fails, no cleanup is needed.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000041 if (EC)
Dan Gohman0df7ea42010-10-07 20:32:40 +000042 Installer.Keep = true;
43}
Rafael Espindola40c908b2013-06-17 18:05:35 +000044
Rafael Espindola3fd1e992014-08-25 18:16:47 +000045tool_output_file::tool_output_file(StringRef Filename, int FD)
46 : Installer(Filename), OS(FD, true) {}