Summary: If the output filename was specified as "-", the ToolOutputFile class would create a brand new raw_ostream object referring to the stdout. This patch changes it to reuse the llvm::outs() singleton. At the moment, this change should be "NFC", but it does enable other enhancements, like the automatic stdout/stderr synchronization as discussed on D80803. I've checked the history, and I did not find any indication that this class *has* to use a brand new stream object instead of outs() -- indeed, it is special-casing "-" in a number of places already, so this change fits the pattern pretty well. I suspect the main reason for the current state of affairs is that the class was originally introduced (r111595, in 2010) as a raw_fd_ostream subclass, which made any other solution impossible. Another potential benefit of this patch is that it makes it possible to move the raw_ostream class out of the business of special-casing "-" for stdout handling. That state of affairs does not seem appropriate because "-" is a valid filename (albeit hard to access with a lot of command line tools) on most systems. Handling "-" in ToolOutputFile seems more appropriate. To make this possible, this patch changes the return type of llvm::outs() and errs() to raw_fd_ostream&. Previously the functions were constructing objects of that type, but returning a generic raw_ostream reference. This makes it possible for new ToolOutputFile and other code to use raw_fd_ostream methods like error() on the outs() object. This does not seem like a bad thing (since stdout is a file descriptor which can be redirected to anywhere, it makes sense to ask it whether the writing was successful or if it supports seeking), and indeed a lot of code was already depending on this fact via the ToolOutputFile "back door". Reviewers: dblaikie, JDevlieghere, MaskRay, jhenderson Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D81078
68 lines
2.2 KiB
C++
68 lines
2.2 KiB
C++
//===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the ToolOutputFile class.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_SUPPORT_TOOLOUTPUTFILE_H
|
|
#define LLVM_SUPPORT_TOOLOUTPUTFILE_H
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace llvm {
|
|
|
|
/// This class contains a raw_fd_ostream and adds a few extra features commonly
|
|
/// needed for compiler-like tool output files:
|
|
/// - The file is automatically deleted if the process is killed.
|
|
/// - The file is automatically deleted when the ToolOutputFile
|
|
/// object is destroyed unless the client calls keep().
|
|
class ToolOutputFile {
|
|
/// This class is declared before the raw_fd_ostream so that it is constructed
|
|
/// before the raw_fd_ostream is constructed and destructed after the
|
|
/// raw_fd_ostream is destructed. It installs cleanups in its constructor and
|
|
/// uninstalls them in its destructor.
|
|
class CleanupInstaller {
|
|
/// The name of the file.
|
|
std::string Filename;
|
|
public:
|
|
/// The flag which indicates whether we should not delete the file.
|
|
bool Keep;
|
|
|
|
explicit CleanupInstaller(StringRef Filename);
|
|
~CleanupInstaller();
|
|
} Installer;
|
|
|
|
/// Storage for the stream, if we're owning our own stream. This is
|
|
/// intentionally declared after Installer.
|
|
Optional<raw_fd_ostream> OSHolder;
|
|
|
|
/// The actual stream to use.
|
|
raw_fd_ostream *OS;
|
|
|
|
public:
|
|
/// This constructor's arguments are passed to raw_fd_ostream's
|
|
/// constructor.
|
|
ToolOutputFile(StringRef Filename, std::error_code &EC,
|
|
sys::fs::OpenFlags Flags);
|
|
|
|
ToolOutputFile(StringRef Filename, int FD);
|
|
|
|
/// Return the contained raw_fd_ostream.
|
|
raw_fd_ostream &os() { return *OS; }
|
|
|
|
/// Indicate that the tool's job wrt this output file has been successful and
|
|
/// the file should not be deleted.
|
|
void keep() { Installer.Keep = true; }
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
#endif
|