[llvm] r244848 - Return ErrorOr from FileOutputBuffer::create. NFC.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 17:31:39 PDT 2015


Author: rafael
Date: Wed Aug 12 19:31:39 2015
New Revision: 244848

URL: http://llvm.org/viewvc/llvm-project?rev=244848&view=rev
Log:
Return ErrorOr from FileOutputBuffer::create. NFC.

Modified:
    llvm/trunk/include/llvm/Support/FileOutputBuffer.h
    llvm/trunk/lib/Support/FileOutputBuffer.cpp
    llvm/trunk/unittests/Support/FileOutputBufferTest.cpp

Modified: llvm/trunk/include/llvm/Support/FileOutputBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileOutputBuffer.h?rev=244848&r1=244847&r2=244848&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileOutputBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/FileOutputBuffer.h Wed Aug 12 19:31:39 2015
@@ -17,6 +17,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/DataTypes.h"
+#include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/FileSystem.h"
 
 namespace llvm {
@@ -37,9 +38,8 @@ public:
   /// Factory method to create an OutputBuffer object which manages a read/write
   /// buffer of the specified size. When committed, the buffer will be written
   /// to the file at the specified path.
-  static std::error_code create(StringRef FilePath, size_t Size,
-                                std::unique_ptr<FileOutputBuffer> &Result,
-                                unsigned Flags = 0);
+  static ErrorOr<std::unique_ptr<FileOutputBuffer>>
+  create(StringRef FilePath, size_t Size, unsigned Flags = 0);
 
   /// Returns a pointer to the start of the buffer.
   uint8_t *getBufferStart() {

Modified: llvm/trunk/lib/Support/FileOutputBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileOutputBuffer.cpp?rev=244848&r1=244847&r2=244848&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileOutputBuffer.cpp (original)
+++ llvm/trunk/lib/Support/FileOutputBuffer.cpp Wed Aug 12 19:31:39 2015
@@ -34,10 +34,8 @@ FileOutputBuffer::~FileOutputBuffer() {
   sys::fs::remove(Twine(TempPath));
 }
 
-std::error_code
-FileOutputBuffer::create(StringRef FilePath, size_t Size,
-                         std::unique_ptr<FileOutputBuffer> &Result,
-                         unsigned Flags) {
+ErrorOr<std::unique_ptr<FileOutputBuffer>>
+FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
   // If file already exists, it must be a regular file (to be mappable).
   sys::fs::file_status Stat;
   std::error_code EC = sys::fs::status(FilePath, Stat);
@@ -95,10 +93,9 @@ FileOutputBuffer::create(StringRef FileP
   if (Ret)
     return std::error_code(errno, std::generic_category());
 
-  Result.reset(
+  std::unique_ptr<FileOutputBuffer> Buf(
       new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
-
-  return std::error_code();
+  return std::move(Buf);
 }
 
 std::error_code FileOutputBuffer::commit() {

Modified: llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/FileOutputBufferTest.cpp?rev=244848&r1=244847&r2=244848&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/FileOutputBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/FileOutputBufferTest.cpp Wed Aug 12 19:31:39 2015
@@ -39,8 +39,10 @@ TEST(FileOutputBuffer, Test) {
   SmallString<128> File1(TestDirectory);
 	File1.append("/file1");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File1, 8192);
+    ASSERT_NO_ERROR(BufferOrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
     // Start buffer with special header.
     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Write to end of buffer to verify it is writable.
@@ -59,8 +61,10 @@ TEST(FileOutputBuffer, Test) {
   SmallString<128> File2(TestDirectory);
 	File2.append("/file2");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer2;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr =
+        FileOutputBuffer::create(File2, 8192);
+    ASSERT_NO_ERROR(Buffer2OrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr;
     // Fill buffer with special header.
     memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Do *not* commit buffer.
@@ -74,8 +78,10 @@ TEST(FileOutputBuffer, Test) {
   SmallString<128> File3(TestDirectory);
 	File3.append("/file3");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File3, 8192000);
+    ASSERT_NO_ERROR(BufferOrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
     // Start buffer with special header.
     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Write to end of buffer to verify it is writable.
@@ -93,9 +99,10 @@ TEST(FileOutputBuffer, Test) {
   SmallString<128> File4(TestDirectory);
 	File4.append("/file4");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
-                                              FileOutputBuffer::F_executable));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable);
+    ASSERT_NO_ERROR(BufferOrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
     // Start buffer with special header.
     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Commit buffer.




More information about the llvm-commits mailing list