[PATCH] D63859: [Support] Add getFileOrSTDIN which calls a callback if stdin is a tty

Alex Brachet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 23:03:39 PDT 2019


abrachet created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Adds an overload of MemoryBuffer::getFileOrSTDIN which takes a callback ( void(*)(void) ) which is called if Filename is "-" and stdin has not had a file or pipe redirected to it, and exiting with return code 1. This is useful for programs which wish to read from stdin, but where it doesn't make sense to read from a character device. These programs can pass a callback to getFileOrSTDIN to do something like print help message.

Notably, although cl::printHelpMessage has two optional booleans, it cannot be converted to void(void). It's pretty awkward considering having `inline void PrintHelpMessage() { PrintHelpMessage(); }` will upset the compiler, but it cannot convert to void(void). I'm not sure what the easiest way to work around this is. Perhaps getFileOrSTDIN should not call exit on its own but let the callback do it and a PrintHelpAndExit function could be added to CommandLine.h. cl::printHelpMessage is an obvious candidate for this use case.

This patch depends on D63858 <https://reviews.llvm.org/D63858> and uses sys::fs::is_tty in it currently.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63859

Files:
  llvm/include/llvm/Support/MemoryBuffer.h
  llvm/lib/Support/MemoryBuffer.cpp
  llvm/unittests/Support/MemoryBufferTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63859.206780.patch
Type: text/x-patch
Size: 3781 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190627/17712d03/attachment.bin>


More information about the llvm-commits mailing list