blob: ed3a247f011558f042dd231c51cd1e4149e39df7 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohman0df7ea42010-10-07 20:32:40 +00006//
7//===----------------------------------------------------------------------===//
8//
Reid Kleckner3fc649c2017-09-23 01:03:17 +00009// This implements the ToolOutputFile class.
Dan Gohman0df7ea42010-10-07 20:32:40 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/ToolOutputFile.h"
Rafael Espindola702a80c2013-06-17 20:37:56 +000014#include "llvm/Support/FileSystem.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/Signals.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000016using namespace llvm;
17
Reid Kleckner3fc649c2017-09-23 01:03:17 +000018ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
Rafael Espindola3fd1e992014-08-25 18:16:47 +000019 : Filename(Filename), Keep(false) {
Dan Gohman0df7ea42010-10-07 20:32:40 +000020 // Arrange for the file to be deleted if the process is killed.
21 if (Filename != "-")
Rafael Espindola4f35da72013-06-13 21:16:58 +000022 sys::RemoveFileOnSignal(Filename);
Dan Gohman0df7ea42010-10-07 20:32:40 +000023}
24
Reid Kleckner3fc649c2017-09-23 01:03:17 +000025ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
Dan Gohman0df7ea42010-10-07 20:32:40 +000026 // Delete the file if the client hasn't told us not to.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000027 if (!Keep && Filename != "-")
28 sys::fs::remove(Filename);
Dan Gohman0df7ea42010-10-07 20:32:40 +000029
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 != "-")
Rafael Espindola4f35da72013-06-13 21:16:58 +000033 sys::DontRemoveFileOnSignal(Filename);
Dan Gohman0df7ea42010-10-07 20:32:40 +000034}
35
Reid Kleckner3fc649c2017-09-23 01:03:17 +000036ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
37 sys::fs::OpenFlags Flags)
Rafael Espindola3fd1e992014-08-25 18:16:47 +000038 : Installer(Filename), OS(Filename, EC, Flags) {
Dan Gohman0df7ea42010-10-07 20:32:40 +000039 // If open fails, no cleanup is needed.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000040 if (EC)
Dan Gohman0df7ea42010-10-07 20:32:40 +000041 Installer.Keep = true;
42}
Rafael Espindola40c908b2013-06-17 18:05:35 +000043
Reid Kleckner3fc649c2017-09-23 01:03:17 +000044ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
Rafael Espindola3fd1e992014-08-25 18:16:47 +000045 : Installer(Filename), OS(FD, true) {}