[PATCH] D56940: FileOutputBuffer: Treat "-" as the stdout.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 18 14:27:08 PST 2019


ruiu created this revision.
ruiu added reviewers: zturner, thakis.
Herald added subscribers: hiraditya, arichardson, emaste.
Herald added a reviewer: espindola.

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, then it write 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
handle `-` as a special name, I think it's okay and actually more
consistent to handle `-` as a special name in FileOutputBuffer as well.


https://reviews.llvm.org/D56940

Files:
  lld/test/ELF/stdout.s
  llvm/lib/Support/FileOutputBuffer.cpp


Index: llvm/lib/Support/FileOutputBuffer.cpp
===================================================================
--- llvm/lib/Support/FileOutputBuffer.cpp
+++ llvm/lib/Support/FileOutputBuffer.cpp
@@ -76,32 +76,26 @@
 // output file on commit(). This is used only when we cannot use OnDiskBuffer.
 class InMemoryBuffer : public FileOutputBuffer {
 public:
-  InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
-      : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
+  InMemoryBuffer(StringRef Path, std::unique_ptr<raw_fd_ostream> &OS,
+                 MemoryBlock MB)
+      : FileOutputBuffer(Path), OS(std::move(OS)), MB(MB) {}
 
-  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
+  uint8_t *getBufferStart() const override { return (uint8_t *)MB.base(); }
 
   uint8_t *getBufferEnd() const override {
-    return (uint8_t *)Buffer.base() + Buffer.size();
+    return (uint8_t *)MB.base() + MB.size();
   }
 
-  size_t getBufferSize() const override { return Buffer.size(); }
+  size_t getBufferSize() const override { return MB.size(); }
 
   Error commit() override {
-    using namespace sys::fs;
-    int FD;
-    std::error_code EC;
-    if (auto EC =
-            openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
-      return errorCodeToError(EC);
-    raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
-    OS << StringRef((const char *)Buffer.base(), Buffer.size());
+    *OS << StringRef((const char *)MB.base(), MB.size());
     return Error::success();
   }
 
 private:
-  OwningMemoryBlock Buffer;
-  unsigned Mode;
+  std::unique_ptr<raw_fd_ostream> OS;
+  OwningMemoryBlock MB;
 };
 } // namespace
 
@@ -112,7 +106,15 @@
       Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
   if (EC)
     return errorCodeToError(EC);
-  return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
+
+  int FD;
+  if (auto EC = sys::fs::openFileForWrite(Path, FD, sys::fs::CD_CreateAlways,
+                                          sys::fs::OF_None, Mode))
+    return errorCodeToError(EC);
+  auto OS = make_unique<raw_fd_ostream>(FD, /*shouldClose=*/true,
+                                        /*unbuffered=*/true);
+
+  return llvm::make_unique<InMemoryBuffer>(Path, OS, MB);
 }
 
 static Expected<std::unique_ptr<OnDiskBuffer>>
@@ -150,6 +152,19 @@
 // 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 == "-") {
+    std::error_code EC;
+    MemoryBlock MB = Memory::allocateMappedMemory(
+        Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
+    if (EC)
+      return errorCodeToError(EC);
+
+    auto OS = make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
+    assert(!EC);
+    return llvm::make_unique<InMemoryBuffer>(Path, OS, MB);
+  }
+
   unsigned Mode = fs::all_read | fs::all_write;
   if (Flags & F_executable)
     Mode |= fs::all_exe;
Index: lld/test/ELF/stdout.s
===================================================================
--- /dev/null
+++ lld/test/ELF/stdout.s
@@ -0,0 +1,12 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o -o - > %t
+# RUN: llvm-objdump -d %t | FileCheck %s
+
+# CHECK: 0000000000201000 _start:
+# CHECK: 201000: 90 nop
+
+.globl _start
+_start:
+  nop


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56940.182610.patch
Type: text/x-patch
Size: 3478 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190118/a566813c/attachment-0001.bin>


More information about the llvm-commits mailing list