This is an archive of the discontinued LLVM Phabricator instance.

Add raw_fd_stream that supports reading/seeking/writing
ClosedPublic

Authored by stephan.yichao.zhao on Sep 1 2020, 12:25 AM.

Details

Summary

This is used by https://reviews.llvm.org/D86905 to support bitcode writer's incremental flush.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2020, 12:25 AM
stephan.yichao.zhao requested review of this revision.Sep 1 2020, 12:25 AM
stephan.yichao.zhao retitled this revision from Add raw_fd_stream that supports reading/seeking/writing to Add raw_fd_stream that supports reading/seeking/writing.

update

Other than a small style nit this LGTM

llvm/lib/Support/raw_ostream.cpp
919

nit: the llvm style guide says no braces around single-line statements. Same in raw_fd_stream::read below.

MaskRay added inline comments.Sep 1 2020, 12:17 PM
llvm/lib/Support/raw_ostream.cpp
919

If stdout happens to be seekable, can the Filename == "-" restriction be lifted?

926

Ret

927

no braces

stephan.yichao.zhao marked 4 inline comments as done.Sep 1 2020, 5:24 PM
stephan.yichao.zhao added inline comments.
llvm/lib/Support/raw_ostream.cpp
919

Thanks. Just learned that stdout is seekable if it is redirected to a normal file.

stephan.yichao.zhao marked an inline comment as done.

addressed comments

jhenderson added inline comments.Sep 2 2020, 12:45 AM
llvm/include/llvm/Support/raw_ostream.h
591–592

This comment doesn't seem to line up with the interface, where it talks about setting EC on failure. I'm guessing this isn't the EC from the constructor, since there's no state involved, and theres' no other EC parameter!

llvm/unittests/Support/raw_fd_stream_test.cpp
2

A couple of other test cases I'd like to see, depending on how practical it is (I suspect it's not practical, and if so, don't worry).

  1. An unseekable input (i.e. where supportsSeeking returns false).
  2. An unseekable input, but where EC was already in an error state (and so the new error wasn't reported).
39

Is this environment variable ever going to be set? I'm not sure what the purpose of the extra logic is...

44

I'm not sure I understand the need for the macro. Can this not just be the following?

ASSERT_FALSE(sys::fs::createTemporaryFile("foo", "bar", FD, PATH));

(with optional explicit operator bool call, if needed)

48

ec -> EC for LLVM style.

78

Ditto ec -> EC here too.

stephan.yichao.zhao marked 4 inline comments as done.

update

stephan.yichao.zhao marked an inline comment as done.Sep 2 2020, 2:07 PM
stephan.yichao.zhao added inline comments.
llvm/include/llvm/Support/raw_ostream.h
591–592

Updated comments.

llvm/unittests/Support/raw_fd_stream_test.cpp
2

Looked into gmock a bit. To mock supportsSeeking, the code under test needs to rewrite to use either virtual method or templete: https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#MockingNonVirtualMethods

Another way to test it is like this. At raw_fd_ostream::raw_fd_ostream, at a place where SupportsSeeking is set, we do

if GlobalMockValue is set:
  SupportsSeeking =  GlobalMockValue
else
  SupportsSeeking = ...

Here GlobalMockValue could be a global pointer. And the unit test can set it. EC can be tested in the same way.

But this also needs to change the code under test. Did any other LLVM test code do something similar?

39

This code refers to the test raw_pwrite_ostreamTest from llvm/unittests/Support/raw_pwrite_stream_test.cpp

That test has a death test at the end. This new test does not have death test. I removed unrelated code.

jhenderson accepted this revision.Sep 3 2020, 12:56 AM

LGTM.

llvm/unittests/Support/raw_fd_stream_test.cpp
2

Yeah, I didn't think this through hard enough, and I don't see a great way of doing this in the tests (I don't think rewriting the code for greater testability is necessary). If you can manually locally verify that the code works as desired, then that would be sufficient for my comment.

This revision is now accepted and ready to land.Sep 3 2020, 12:56 AM
MaskRay accepted this revision.Sep 4 2020, 2:19 PM

Looks great!

llvm/include/llvm/Support/raw_ostream.h
55

OK_LastFDStream is not used and should be deleted

stephan.yichao.zhao marked 2 inline comments as done.

addressed comments

addressed comments

Harbormaster completed remote builds in B70722: Diff 290065.