[PATCH] D79338: [Support] Allow FileOutputBuffer::create to create an empty file

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 5 08:36:37 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG9d53db2aa098: [Support] Allow FileOutputBuffer::create to create an empty file (authored by MaskRay).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79338/new/

https://reviews.llvm.org/D79338

Files:
  llvm/lib/Support/FileOutputBuffer.cpp
  llvm/unittests/Support/FileOutputBufferTest.cpp


Index: llvm/unittests/Support/FileOutputBufferTest.cpp
===================================================================
--- llvm/unittests/Support/FileOutputBufferTest.cpp
+++ llvm/unittests/Support/FileOutputBufferTest.cpp
@@ -140,6 +140,21 @@
   ASSERT_NO_ERROR(fs::file_size(Twine(File5), File5Size));
   ASSERT_EQ(File5Size, 8000ULL);
   ASSERT_NO_ERROR(fs::remove(File5.str()));
+
+  // TEST 6: Create an empty file.
+  SmallString<128> File6(TestDirectory);
+  File6.append("/file6");
+  {
+    Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File6, 0);
+    ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
+    ASSERT_NO_ERROR(errorToErrorCode((*BufferOrErr)->commit()));
+  }
+  uint64_t File6Size;
+  ASSERT_NO_ERROR(fs::file_size(Twine(File6), File6Size));
+  ASSERT_EQ(File6Size, 0ULL);
+  ASSERT_NO_ERROR(fs::remove(File6.str()));
+
   // Clean up.
   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
 }
Index: llvm/lib/Support/FileOutputBuffer.cpp
===================================================================
--- llvm/lib/Support/FileOutputBuffer.cpp
+++ llvm/lib/Support/FileOutputBuffer.cpp
@@ -172,6 +172,10 @@
   if (Flags & F_executable)
     Mode |= fs::all_exe;
 
+  // If Size is zero, don't use mmap which will fail with EINVAL.
+  if (Size == 0)
+    return createInMemoryBuffer(Path, Size, Mode);
+
   fs::file_status Stat;
   fs::status(Path, Stat);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79338.262123.patch
Type: text/x-patch
Size: 1444 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200505/9df4b4f1/attachment.bin>


More information about the llvm-commits mailing list