This is an archive of the discontinued LLVM Phabricator instance.

Add seek support into llvm::raw_ostream that can be conditionally enabled.
AbandonedPublic

Authored by clayborg on Jul 22 2019, 8:30 AM.

Details

Summary

While working on a patch for GSYM: https://reviews.llvm.org/D63828 there is a FileWriter class that is being used to write binary data to a stand alone file. The patch was directly using a std::ostream to do the writing, and multiple people in the comments of the patch suggested that we use llvm::raw_ostream. In order to be able to use raw_ostream in the patch we need to be able to seek to locations in the the stream when writing binary data to apply fixes to offsets.

This patch puts the ability to support seeking into llvm::raw_ostream so that we can end up using it in the FileWriter class in the unit tests. It does this by adding two virtual methods to llvm::raw_ostream:

/// Seek support is optional.
virtual bool supportsSeeking() { return false; }
 
/// Optional seek support. Defaults to not changing the file position.
virtual uint64_t seek(uint64_t off) { return current_pos(); };

There are default implementations that return false and current_pos() respectively so only changes need to be made to raw_ostream subclasses that need to add seek support. It also fixed raw_fd_ostream and raw_os_ostream to override these methods.

Diff Detail

Event Timeline

clayborg created this revision.Jul 22 2019, 8:30 AM
MaskRay added inline comments.Jul 22 2019, 9:25 AM
include/llvm/Support/raw_os_ostream.h
31

return OS.tellp() != -1;

Would raw_pwrite_stream suffice?

Would raw_pwrite_stream suffice?

It actually would as I only need to fixup a few things for FileWriter in GSYM. Keeping this patch does allow us to use raw_ostream more file a seekable file, where the pwrite forces you to just do minimal fixups here and there. I am fine abandoning this if no one else thinks it is useful. Comments?

Would raw_pwrite_stream suffice?

It actually would as I only need to fixup a few things for FileWriter in GSYM. Keeping this patch does allow us to use raw_ostream more file a seekable file, where the pwrite forces you to just do minimal fixups here and there. I am fine abandoning this if no one else thinks it is useful. Comments?

I think if we want more seekable file API - might be more suitable to expand/rename raw_pwrite_stream - so the seekability is part of the static type rather than a dynamically queried property, etc.

(but yeah, doesn't seem like a super high priority for this extra functionality)

clayborg abandoned this revision.Jul 22 2019, 2:38 PM

Abandoning since GSYM's FileWriter can get by with llvm::raw_pwrite_stream