LLVM 19.0.0git
Public Member Functions | Protected Member Functions | List of all members
llvm::raw_fd_ostream Class Reference

A raw_ostream that writes to a file descriptor. More...

#include "llvm/Support/raw_ostream.h"

Inheritance diagram for llvm::raw_fd_ostream:
Inheritance graph
[legend]

Public Member Functions

 raw_fd_ostream (StringRef Filename, std::error_code &EC)
 Open the specified file for writing.
 
 raw_fd_ostream (StringRef Filename, std::error_code &EC, sys::fs::CreationDisposition Disp)
 
 raw_fd_ostream (StringRef Filename, std::error_code &EC, sys::fs::FileAccess Access)
 
 raw_fd_ostream (StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
 
 raw_fd_ostream (StringRef Filename, std::error_code &EC, sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, sys::fs::OpenFlags Flags)
 
 raw_fd_ostream (int fd, bool shouldClose, bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
 FD is the file descriptor that this writes to.
 
 ~raw_fd_ostream () override
 
void close ()
 Manually flush the stream and close the file.
 
bool supportsSeeking () const
 
bool isRegularFile () const
 
uint64_t seek (uint64_t off)
 Flushes the stream and repositions the underlying file descriptor position to the offset specified from the beginning of the file.
 
bool is_displayed () const override
 This function determines if this stream is connected to a "tty" or "console" window.
 
bool has_colors () const override
 This function determines if this stream is displayed and supports colors.
 
std::error_code error () const
 
bool has_error () const
 Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encountered.
 
void clear_error ()
 Set the flag read by has_error() to false.
 
Expected< sys::fs::FileLockerlock ()
 Locks the underlying file.
 
Expected< sys::fs::FileLockertryLockFor (Duration const &Timeout)
 Tries to lock the underlying file within the specified period.
 
- Public Member Functions inherited from llvm::raw_pwrite_stream
 raw_pwrite_stream (bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
 
void pwrite (const char *Ptr, size_t Size, uint64_t Offset)
 
- Public Member Functions inherited from llvm::raw_ostream
 raw_ostream (bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
 
 raw_ostream (const raw_ostream &)=delete
 
void operator= (const raw_ostream &)=delete
 
virtual ~raw_ostream ()
 
uint64_t tell () const
 tell - Return the current offset with the file.
 
OStreamKind get_kind () const
 
virtual void reserveExtraSpace (uint64_t ExtraSize)
 If possible, pre-allocate ExtraSize bytes for stream data.
 
void SetBuffered ()
 Set the stream to be buffered, with an automatically determined buffer size.
 
void SetBufferSize (size_t Size)
 Set the stream to be buffered, using the specified buffer size.
 
size_t GetBufferSize () const
 
void SetUnbuffered ()
 Set the stream to be unbuffered.
 
size_t GetNumBytesInBuffer () const
 
void flush ()
 
raw_ostreamoperator<< (char C)
 
raw_ostreamoperator<< (unsigned char C)
 
raw_ostreamoperator<< (signed char C)
 
raw_ostreamoperator<< (StringRef Str)
 
raw_ostreamoperator<< (const char *Str)
 
raw_ostreamoperator<< (const std::string &Str)
 
raw_ostreamoperator<< (const std::string_view &Str)
 
raw_ostreamoperator<< (const SmallVectorImpl< char > &Str)
 
raw_ostreamoperator<< (unsigned long N)
 
raw_ostreamoperator<< (long N)
 
raw_ostreamoperator<< (unsigned long long N)
 
raw_ostreamoperator<< (long long N)
 
raw_ostreamoperator<< (const void *P)
 
raw_ostreamoperator<< (unsigned int N)
 
raw_ostreamoperator<< (int N)
 
raw_ostreamoperator<< (double N)
 
raw_ostreamwrite_hex (unsigned long long N)
 Output N in hexadecimal, without any prefix or padding.
 
raw_ostreamoperator<< (Colors C)
 
raw_ostreamwrite_uuid (const uuid_t UUID)
 
raw_ostreamwrite_escaped (StringRef Str, bool UseHexEscapes=false)
 Output Str, turning '\', '\t', '
', '"', and anything that doesn't satisfy llvm::isPrint into an escape sequence.
 
raw_ostreamwrite (unsigned char C)
 
raw_ostreamwrite (const char *Ptr, size_t Size)
 
raw_ostreamoperator<< (const format_object_base &Fmt)
 
raw_ostreamoperator<< (const FormattedString &)
 
raw_ostreamoperator<< (const FormattedNumber &)
 
raw_ostreamoperator<< (const formatv_object_base &)
 
raw_ostreamoperator<< (const FormattedBytes &)
 
raw_ostreamindent (unsigned NumSpaces)
 indent - Insert 'NumSpaces' spaces.
 
raw_ostreamwrite_zeros (unsigned NumZeros)
 write_zeros - Insert 'NumZeros' nulls.
 
virtual raw_ostreamchangeColor (enum Colors Color, bool Bold=false, bool BG=false)
 Changes the foreground color of text that will be output from this point forward.
 
virtual raw_ostreamresetColor ()
 Resets the colors to terminal defaults.
 
virtual raw_ostreamreverseColor ()
 Reverses the foreground and background colors.
 
virtual bool is_displayed () const
 This function determines if this stream is connected to a "tty" or "console" window.
 
virtual bool has_colors () const
 This function determines if this stream is displayed and supports colors.
 
virtual void enable_colors (bool enable)
 
bool colors_enabled () const
 
void tie (raw_ostream *TieTo)
 Tie this stream to the specified stream.
 

Protected Member Functions

void error_detected (std::error_code EC)
 Set the flag indicating that an output error has been encountered.
 
int get_fd () const
 Return the file descriptor.
 
void inc_pos (uint64_t Delta)
 
- Protected Member Functions inherited from llvm::raw_ostream
void SetBuffer (char *BufferStart, size_t Size)
 Use the provided buffer as the raw_ostream buffer.
 
virtual size_t preferred_buffer_size () const
 Return an efficient buffer size for the underlying output mechanism.
 
const chargetBufferStart () const
 Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
 

Additional Inherited Members

- Public Types inherited from llvm::raw_ostream
enum class  OStreamKind { OK_OStream , OK_FDStream }
 
enum class  Colors {
  BLACK = 0 , RED , GREEN , YELLOW ,
  BLUE , MAGENTA , CYAN , WHITE ,
  BRIGHT_BLACK , BRIGHT_RED , BRIGHT_GREEN , BRIGHT_YELLOW ,
  BRIGHT_BLUE , BRIGHT_MAGENTA , BRIGHT_CYAN , BRIGHT_WHITE ,
  SAVEDCOLOR , RESET
}
 
using uuid_t = uint8_t[16]
 Output a formatted UUID with dash separators.
 
- Static Public Attributes inherited from llvm::raw_ostream
static constexpr Colors BLACK = Colors::BLACK
 
static constexpr Colors RED = Colors::RED
 
static constexpr Colors GREEN = Colors::GREEN
 
static constexpr Colors YELLOW = Colors::YELLOW
 
static constexpr Colors BLUE = Colors::BLUE
 
static constexpr Colors MAGENTA = Colors::MAGENTA
 
static constexpr Colors CYAN = Colors::CYAN
 
static constexpr Colors WHITE = Colors::WHITE
 
static constexpr Colors BRIGHT_BLACK = Colors::BRIGHT_BLACK
 
static constexpr Colors BRIGHT_RED = Colors::BRIGHT_RED
 
static constexpr Colors BRIGHT_GREEN = Colors::BRIGHT_GREEN
 
static constexpr Colors BRIGHT_YELLOW = Colors::BRIGHT_YELLOW
 
static constexpr Colors BRIGHT_BLUE = Colors::BRIGHT_BLUE
 
static constexpr Colors BRIGHT_MAGENTA = Colors::BRIGHT_MAGENTA
 
static constexpr Colors BRIGHT_CYAN = Colors::BRIGHT_CYAN
 
static constexpr Colors BRIGHT_WHITE = Colors::BRIGHT_WHITE
 
static constexpr Colors SAVEDCOLOR = Colors::SAVEDCOLOR
 
static constexpr Colors RESET = Colors::RESET
 

Detailed Description

A raw_ostream that writes to a file descriptor.

Definition at line 470 of file raw_ostream.h.

Constructor & Destructor Documentation

◆ raw_fd_ostream() [1/6]

raw_fd_ostream::raw_fd_ostream ( StringRef  Filename,
std::error_code &  EC 
)

Open the specified file for writing.

If an error occurs, information about the error is put into EC, and the stream should be immediately destroyed; Flags allows optional flags to control how the file will be opened.

As a special case, if Filename is "-", then the stream will use STDOUT_FILENO instead of opening a file. This will not close the stdout descriptor.

Definition at line 599 of file raw_ostream.cpp.

◆ raw_fd_ostream() [2/6]

raw_fd_ostream::raw_fd_ostream ( StringRef  Filename,
std::error_code &  EC,
sys::fs::CreationDisposition  Disp 
)

Definition at line 603 of file raw_ostream.cpp.

◆ raw_fd_ostream() [3/6]

raw_fd_ostream::raw_fd_ostream ( StringRef  Filename,
std::error_code &  EC,
sys::fs::FileAccess  Access 
)

Definition at line 607 of file raw_ostream.cpp.

◆ raw_fd_ostream() [4/6]

raw_fd_ostream::raw_fd_ostream ( StringRef  Filename,
std::error_code &  EC,
sys::fs::OpenFlags  Flags 
)

Definition at line 612 of file raw_ostream.cpp.

◆ raw_fd_ostream() [5/6]

raw_fd_ostream::raw_fd_ostream ( StringRef  Filename,
std::error_code &  EC,
sys::fs::CreationDisposition  Disp,
sys::fs::FileAccess  Access,
sys::fs::OpenFlags  Flags 
)

Definition at line 617 of file raw_ostream.cpp.

◆ raw_fd_ostream() [6/6]

raw_fd_ostream::raw_fd_ostream ( int  fd,
bool  shouldClose,
bool  unbuffered = false,
OStreamKind  K = OStreamKind::OK_OStream 
)

FD is the file descriptor that this writes to.

If ShouldClose is true, this closes the file when the stream is destroyed. If FD is for stdout or stderr, it will not be closed.

If ShouldClose is true, this closes the file when the stream is destroyed.

Definition at line 625 of file raw_ostream.cpp.

References llvm::raw_ostream::enable_colors(), and llvm::sys::fs::regular_file.

◆ ~raw_fd_ostream()

raw_fd_ostream::~raw_fd_ostream ( )
override

Member Function Documentation

◆ clear_error()

void llvm::raw_fd_ostream::clear_error ( )
inline

Set the flag read by has_error() to false.

If the error flag is set at the time when this raw_ostream's destructor is called, report_fatal_error is called to report the error. Use clear_error() after handling the error to avoid this behavior.

"Errors should never pass silently. Unless explicitly silenced."

  • from The Zen of Python, by Tim Peters

Definition at line 572 of file raw_ostream.h.

Referenced by llvm::LockFileManager::LockFileManager(), and llvm::LTOCodeGenerator::writeMergedModules().

◆ close()

void raw_fd_ostream::close ( )

Manually flush the stream and close the file.

Note that this does not call fsync.

Definition at line 809 of file raw_ostream.cpp.

References assert(), error_detected(), llvm::raw_ostream::flush(), and llvm::sys::Process::SafelyCloseFileDescriptor().

Referenced by LLVMPrintModuleToFile(), llvm::LockFileManager::LockFileManager(), and llvm::LTOCodeGenerator::writeMergedModules().

◆ error()

std::error_code llvm::raw_fd_ostream::error ( ) const
inline

◆ error_detected()

void llvm::raw_fd_ostream::error_detected ( std::error_code  EC)
inlineprotected

Set the flag indicating that an output error has been encountered.

Definition at line 503 of file raw_ostream.h.

Referenced by close(), llvm::raw_fd_stream::read(), seek(), and ~raw_fd_ostream().

◆ get_fd()

int llvm::raw_fd_ostream::get_fd ( ) const
inlineprotected

Return the file descriptor.

Definition at line 506 of file raw_ostream.h.

Referenced by llvm::raw_fd_stream::read().

◆ has_colors()

bool raw_fd_ostream::has_colors ( ) const
overridevirtual

This function determines if this stream is displayed and supports colors.

The result is unaffected by calls to enable_color().

Reimplemented from llvm::raw_ostream.

Definition at line 869 of file raw_ostream.cpp.

References llvm::sys::Process::FileDescriptorHasColors().

◆ has_error()

bool llvm::raw_fd_ostream::has_error ( ) const
inline

Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encountered.

This doesn't implicitly flush any pending output. Also, it doesn't guarantee to detect all errors unless the stream has been closed.

Definition at line 561 of file raw_ostream.h.

Referenced by LLVMPrintModuleToFile(), llvm::LockFileManager::LockFileManager(), llvm::LTOCodeGenerator::writeMergedModules(), and ~raw_fd_ostream().

◆ inc_pos()

void llvm::raw_fd_ostream::inc_pos ( uint64_t  Delta)
inlineprotected

Definition at line 509 of file raw_ostream.h.

Referenced by llvm::raw_fd_stream::read().

◆ is_displayed()

bool raw_fd_ostream::is_displayed ( ) const
overridevirtual

This function determines if this stream is connected to a "tty" or "console" window.

That is, the output would be displayed to the user rather than being put on a pipe or stored in a file.

Reimplemented from llvm::raw_ostream.

Definition at line 865 of file raw_ostream.cpp.

References llvm::sys::Process::FileDescriptorIsDisplayed().

◆ isRegularFile()

bool llvm::raw_fd_ostream::isRegularFile ( ) const
inline

Definition at line 545 of file raw_ostream.h.

Referenced by llvm::raw_fd_stream::raw_fd_stream().

◆ lock()

Expected< sys::fs::FileLocker > raw_fd_ostream::lock ( )

Locks the underlying file.

Returns
RAII object that releases the lock upon leaving the scope, if the locking was successful. Otherwise returns corresponding error code.

The function blocks the current thread until the lock become available or error occurs.

Possible use of this function may be as follows:

if (auto L = stream.lock()) {
// ... do action that require file to be locked.
} else {
handleAllErrors(std::move(L.takeError()), [&](ErrorInfoBase &EIB) {
// ... handle lock error.
});
}
Base class for error info classes.
Definition: Error.h:45
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:970

Definition at line 875 of file raw_ostream.cpp.

References llvm::errorCodeToError(), and llvm::sys::fs::lockFile().

◆ seek()

uint64_t raw_fd_ostream::seek ( uint64_t  off)

Flushes the stream and repositions the underlying file descriptor position to the offset specified from the beginning of the file.

Definition at line 818 of file raw_ostream.cpp.

References assert(), llvm::errnoAsErrorCode(), error_detected(), and llvm::raw_ostream::flush().

Referenced by llvm::TarWriter::append(), and llvm::ProfOStream::patch().

◆ supportsSeeking()

bool llvm::raw_fd_ostream::supportsSeeking ( ) const
inline

Definition at line 543 of file raw_ostream.h.

◆ tryLockFor()

Expected< sys::fs::FileLocker > raw_fd_ostream::tryLockFor ( Duration const Timeout)

Tries to lock the underlying file within the specified period.

Returns
RAII object that releases the lock upon leaving the scope, if the locking was successful. Otherwise returns corresponding error code.

It is used as lock.

Definition at line 883 of file raw_ostream.cpp.

References llvm::errorCodeToError(), llvm::Duration::getDuration(), and llvm::sys::fs::tryLockFile().


The documentation for this class was generated from the following files: