]> granicus.if.org Git - llvm/commitdiff
FileOutputBuffer: Handle "-" as stdout.
authorRui Ueyama <ruiu@google.com>
Tue, 22 Jan 2019 18:44:04 +0000 (18:44 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 22 Jan 2019 18:44:04 +0000 (18:44 +0000)
I was honestly a bit surprised that we didn't do this before. This
patch is to handle "-" as the stdout so that if you pass `-o -` to
lld, for example, it writes an output to stdout instead of file `-`.

I thought that we might want to handle this at a higher level than
FileOutputBuffer, because if we land this patch, we can no longer
create a file whose name is `-` (there's a workaround though; you can
pass `./-` instead of `-`). However, because raw_fd_ostream already
handles `-` as a special file name, I think it's okay and actually
consistent to handle `-` as a special name in FileOutputBuffer.

Differential Revision: https://reviews.llvm.org/D56940

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351852 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/FileOutputBuffer.cpp

index 8a2bb6c0651bc48c70b80c0a244a1805a0bbcf49..cf21cc19c0122a86a73c3d1b8e0fd9acccdb5e3f 100644 (file)
@@ -87,6 +87,12 @@ public:
   size_t getBufferSize() const override { return Buffer.size(); }
 
   Error commit() override {
+    if (FinalPath == "-") {
+      llvm::outs() << StringRef((const char *)Buffer.base(), Buffer.size());
+      llvm::outs().flush();
+      return Error::success();
+    }
+
     using namespace sys::fs;
     int FD;
     std::error_code EC;
@@ -149,6 +155,10 @@ createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
 // Create an instance of FileOutputBuffer.
 Expected<std::unique_ptr<FileOutputBuffer>>
 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
+  // Handle "-" as stdout just like llvm::raw_ostream does.
+  if (Path == "-")
+    return createInMemoryBuffer("-", Size, /*Mode=*/0);
+
   unsigned Mode = fs::all_read | fs::all_write;
   if (Flags & F_executable)
     Mode |= fs::all_exe;