Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions llvm/include/llvm/Support/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ class LLVM_ABI MemoryBuffer {
getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");

/// Read all of stdin into a file buffer, and return it.
static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getSTDIN(bool RequiresNullTerminator = true);

/// Open the specified file as a MemoryBuffer, or open stdin if the Filename
/// is "-".
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true,
bool RequiresNullTerminator = false,
std::optional<Align> Alignment = std::nullopt);

/// Map a subrange of the specified file as a MemoryBuffer.
Expand Down
31 changes: 26 additions & 5 deletions llvm/lib/Support/MemoryBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText,
StringRef NameRef = Filename.toStringRef(NameBuf);

if (NameRef == "-")
return getSTDIN();
return getSTDIN(RequiresNullTerminator);
return getFile(Filename, IsText, RequiresNullTerminator,
/*IsVolatile=*/false, Alignment);
}
Expand Down Expand Up @@ -567,12 +567,33 @@ ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getOpenFileSlice(
IsVolatile, Alignment);
}

ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
ErrorOr<std::unique_ptr<MemoryBuffer>>
MemoryBuffer::getSTDIN(bool RequiresNullTerminator) {
// Read in all of the data from stdin, we cannot mmap stdin.
//
// FIXME: That isn't necessarily true, we should try to mmap stdin and
// fallback if it fails.
sys::ChangeStdinMode(sys::fs::OF_Text);
std::error_code EC;
sys::fs::file_type Type;
sys::fs::file_status Status;
EC = sys::fs::status(sys::fs::getStdinHandle(), Status);
if (EC)
return EC;

Type = Status.type();
// If the FD is regular file or block file,
// we try to create a mmap buffer first.
// If failed, rollback to read and copy.
if ((Type == sys::fs::file_type::regular_file ||
Type == sys::fs::file_type::block_file) &&
shouldUseMmap(sys::fs::getStdinHandle(), Status.getSize(),
Status.getSize(), 0, RequiresNullTerminator,
sys::Process::getPageSizeEstimate(), false)) {
std::unique_ptr<MemoryBuffer> Result(
new (NamedBufferAlloc("<stdin>")) MemoryBufferMMapFile<MemoryBuffer>(
RequiresNullTerminator, sys::fs::getStdinHandle(), Status.getSize(),
0, EC));
if (!EC && (!RequiresNullTerminator || *Result->getBufferEnd() == '\0'))
return std::move(Result);
}

return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>");
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-strings/llvm-strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ int main(int argc, char **argv) {

for (const auto &File : InputFileNames) {
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/true);
MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/true,
/*RequiresNullTerminator=*/false);
if (std::error_code EC = Buffer.getError())
errs() << File << ": " << EC.message() << '\n';
else
Expand Down
Loading