Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.8 KiB
C++
Raw Permalink Normal View History

//===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===//
2010-10-07 20:32:40 +00:00
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2010-10-07 20:32:40 +00:00
//
//===----------------------------------------------------------------------===//
//
// This implements the ToolOutputFile class.
2010-10-07 20:32:40 +00:00
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/FileSystem.h"
2010-11-29 18:16:10 +00:00
#include "llvm/Support/Signals.h"
2010-10-07 20:32:40 +00:00
using namespace llvm;
2020-06-03 12:08:45 +02:00
static bool isStdout(StringRef Filename) { return Filename == "-"; }
ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
: Filename(std::string(Filename)), Keep(false) {
2010-10-07 20:32:40 +00:00
// Arrange for the file to be deleted if the process is killed.
2020-06-03 12:08:45 +02:00
if (!isStdout(Filename))
2013-06-13 21:16:58 +00:00
sys::RemoveFileOnSignal(Filename);
2010-10-07 20:32:40 +00:00
}
ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
2020-06-03 12:08:45 +02:00
if (isStdout(Filename))
return;
2010-10-07 20:32:40 +00:00
// Delete the file if the client hasn't told us not to.
2020-06-03 12:08:45 +02:00
if (!Keep)
sys::fs::remove(Filename);
2010-10-07 20:32:40 +00:00
// Ok, the file is successfully written and closed, or deleted. There's no
// further need to clean it up on signals.
2020-06-03 12:08:45 +02:00
sys::DontRemoveFileOnSignal(Filename);
2010-10-07 20:32:40 +00:00
}
ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags)
2020-06-03 12:08:45 +02:00
: Installer(Filename) {
if (isStdout(Filename)) {
OS = &outs();
EC = std::error_code();
return;
}
OSHolder.emplace(Filename, EC, Flags);
OS = OSHolder.getPointer();
2010-10-07 20:32:40 +00:00
// If open fails, no cleanup is needed.
if (EC)
2010-10-07 20:32:40 +00:00
Installer.Keep = true;
}
2013-06-17 18:05:35 +00:00
ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
2020-06-03 12:08:45 +02:00
: Installer(Filename) {
OSHolder.emplace(FD, true);
OS = OSHolder.getPointer();
}