blob: e12d9e824f7edbac06309543ba26e9aed7c29f3b [file] [log] [blame]
Reid Kleckner3fc649c2017-09-23 01:03:17 +00001//===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===//
Dan Gohman0df7ea42010-10-07 20:32:40 +00002//
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//
Reid Kleckner3fc649c2017-09-23 01:03:17 +000010// This implements the ToolOutputFile class.
Dan Gohman0df7ea42010-10-07 20:32:40 +000011//
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
Reid Kleckner3fc649c2017-09-23 01:03:17 +000019ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
Rafael Espindola3fd1e992014-08-25 18:16:47 +000020 : 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
Reid Kleckner3fc649c2017-09-23 01:03:17 +000026ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
Dan Gohman0df7ea42010-10-07 20:32:40 +000027 // 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
Reid Kleckner3fc649c2017-09-23 01:03:17 +000037ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
38 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
Reid Kleckner3fc649c2017-09-23 01:03:17 +000045ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
Rafael Espindola3fd1e992014-08-25 18:16:47 +000046 : Installer(Filename), OS(FD, true) {}