[libc-commits] [libc] b95ed8b - [libc] Remove operator T from cpp::expected.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Tue Jun 6 10:58:01 PDT 2023


Author: Tue Ly
Date: 2023-06-06T13:57:44-04:00
New Revision: b95ed8b6d9354fa10094b6425ecc84dd33682a58

URL: https://github.com/llvm/llvm-project/commit/b95ed8b6d9354fa10094b6425ecc84dd33682a58
DIFF: https://github.com/llvm/llvm-project/commit/b95ed8b6d9354fa10094b6425ecc84dd33682a58.diff

LOG: [libc] Remove operator T from cpp::expected.

The libc's equivalent of std::expected has a non-standard and
non-explicit operator T - https://github.com/llvm/llvm-project/issues/62738

Reviewed By: sivachandra

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

Added: 
    

Modified: 
    libc/src/__support/CPP/expected.h
    libc/src/__support/File/dir.cpp
    libc/src/dirent/readdir.cpp
    libc/test/src/__support/File/platform_file_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/expected.h b/libc/src/__support/CPP/expected.h
index aab80c2d983d6..2fdcc5246d473 100644
--- a/libc/src/__support/CPP/expected.h
+++ b/libc/src/__support/CPP/expected.h
@@ -38,7 +38,6 @@ template <class T, class E> class expected {
   constexpr T value() { return exp; }
   constexpr E error() { return unexp; }
 
-  constexpr operator T() { return exp; }
   constexpr operator bool() { return is_expected; }
 
   constexpr T &operator*() { return exp; }

diff  --git a/libc/src/__support/File/dir.cpp b/libc/src/__support/File/dir.cpp
index e632c9293d617..e7cc4ca0b19b6 100644
--- a/libc/src/__support/File/dir.cpp
+++ b/libc/src/__support/File/dir.cpp
@@ -20,7 +20,7 @@ ErrorOr<Dir *> Dir::open(const char *path) {
     return __llvm_libc::Error(fd.error());
 
   __llvm_libc::AllocChecker ac;
-  Dir *dir = new (ac) Dir(fd);
+  Dir *dir = new (ac) Dir(fd.value());
   if (!ac)
     return __llvm_libc::Error(ENOMEM);
   return dir;
@@ -32,7 +32,7 @@ ErrorOr<struct ::dirent *> Dir::read() {
     auto readsize = platform_fetch_dirents(fd, buffer);
     if (!readsize)
       return __llvm_libc::Error(readsize.error());
-    fillsize = readsize;
+    fillsize = readsize.value();
     readptr = 0;
   }
   if (fillsize == 0)

diff  --git a/libc/src/dirent/readdir.cpp b/libc/src/dirent/readdir.cpp
index 10040aa15b63a..a9f9491d47e59 100644
--- a/libc/src/dirent/readdir.cpp
+++ b/libc/src/dirent/readdir.cpp
@@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(struct ::dirent *, readdir, (::DIR * dir)) {
     libc_errno = dirent_val.error();
     return nullptr;
   }
-  return dirent_val;
+  return dirent_val.value();
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/test/src/__support/File/platform_file_test.cpp b/libc/test/src/__support/File/platform_file_test.cpp
index 98fae412000cb..55ab4855a98cf 100644
--- a/libc/test/src/__support/File/platform_file_test.cpp
+++ b/libc/test/src/__support/File/platform_file_test.cpp
@@ -15,14 +15,19 @@ using File = __llvm_libc::File;
 constexpr char TEXT[] = "Hello, File";
 constexpr size_t TEXT_SIZE = sizeof(TEXT) - 1; // Ignore the null terminator
 
+LIBC_INLINE File *openfile(const char *file_name, const char *mode) {
+  auto error_or_file = __llvm_libc::openfile(file_name, mode);
+  return error_or_file.has_value() ? error_or_file.value() : nullptr;
+}
+
 TEST(LlvmLibcPlatformFileTest, CreateWriteCloseAndReadBack) {
   constexpr char FILENAME[] = "testdata/create_write_close_and_readback.test";
-  File *file = __llvm_libc::openfile(FILENAME, "w");
+  File *file = openfile(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "r");
+  file = openfile(FILENAME, "r");
   ASSERT_FALSE(file == nullptr);
   char data[sizeof(TEXT)];
   ASSERT_EQ(file->read(data, TEXT_SIZE).value, TEXT_SIZE);
@@ -38,7 +43,7 @@ TEST(LlvmLibcPlatformFileTest, CreateWriteCloseAndReadBack) {
 
 TEST(LlvmLibcPlatformFileTest, CreateWriteSeekAndReadBack) {
   constexpr char FILENAME[] = "testdata/create_write_seek_and_readback.test";
-  File *file = __llvm_libc::openfile(FILENAME, "w+");
+  File *file = openfile(FILENAME, "w+");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
 
@@ -58,19 +63,19 @@ TEST(LlvmLibcPlatformFileTest, CreateWriteSeekAndReadBack) {
 
 TEST(LlvmLibcPlatformFileTest, CreateAppendCloseAndReadBack) {
   constexpr char FILENAME[] = "testdata/create_append_close_and_readback.test";
-  File *file = __llvm_libc::openfile(FILENAME, "w");
+  File *file = openfile(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "a");
+  file = openfile(FILENAME, "a");
   ASSERT_FALSE(file == nullptr);
   constexpr char APPEND_TEXT[] = " Append Text";
   constexpr size_t APPEND_TEXT_SIZE = sizeof(APPEND_TEXT) - 1;
   ASSERT_EQ(file->write(APPEND_TEXT, APPEND_TEXT_SIZE).value, APPEND_TEXT_SIZE);
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "r");
+  file = openfile(FILENAME, "r");
   ASSERT_FALSE(file == nullptr);
   constexpr size_t READ_SIZE = TEXT_SIZE + APPEND_TEXT_SIZE;
   char data[READ_SIZE + 1];
@@ -87,12 +92,12 @@ TEST(LlvmLibcPlatformFileTest, CreateAppendCloseAndReadBack) {
 
 TEST(LlvmLibcPlatformFileTest, CreateAppendSeekAndReadBack) {
   constexpr char FILENAME[] = "testdata/create_append_seek_and_readback.test";
-  File *file = __llvm_libc::openfile(FILENAME, "w");
+  File *file = openfile(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "a+");
+  file = openfile(FILENAME, "a+");
   ASSERT_FALSE(file == nullptr);
   constexpr char APPEND_TEXT[] = " Append Text";
   constexpr size_t APPEND_TEXT_SIZE = sizeof(APPEND_TEXT) - 1;
@@ -119,7 +124,7 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
     write_data[i] = BYTE;
 
   constexpr char FILENAME[] = "testdata/large_file.test";
-  File *file = __llvm_libc::openfile(FILENAME, "w");
+  File *file = openfile(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
 
   constexpr int REPEAT = 5;
@@ -128,7 +133,7 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
   }
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "r");
+  file = openfile(FILENAME, "r");
   ASSERT_FALSE(file == nullptr);
   constexpr size_t READ_SIZE = DATA_SIZE * REPEAT;
   char data[READ_SIZE] = {0};
@@ -146,14 +151,14 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
 
 TEST(LlvmLibcPlatformFileTest, ReadSeekCurAndRead) {
   constexpr char FILENAME[] = "testdata/read_seek_cur_and_read.test";
-  File *file = __llvm_libc::openfile(FILENAME, "w");
+  File *file = openfile(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
   constexpr char CONTENT[] = "1234567890987654321";
   ASSERT_EQ(sizeof(CONTENT) - 1,
             file->write(CONTENT, sizeof(CONTENT) - 1).value);
   ASSERT_EQ(0, File::cleanup(file));
 
-  file = __llvm_libc::openfile(FILENAME, "r");
+  file = openfile(FILENAME, "r");
   ASSERT_FALSE(file == nullptr);
 
   constexpr size_t READ_SIZE = 5;
@@ -175,21 +180,21 @@ TEST(LlvmLibcPlatformFileTest, IncorrectOperation) {
   constexpr char FILENAME[] = "testdata/incorrect_operation.test";
   char data[1] = {123};
 
-  File *file = __llvm_libc::openfile(FILENAME, "w");
+  File *file = openfile(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->read(data, 1).value, size_t(0)); // Cannot read
   ASSERT_FALSE(file->iseof());
   ASSERT_TRUE(file->error());
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "r");
+  file = openfile(FILENAME, "r");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->write(data, 1).value, size_t(0)); // Cannot write
   ASSERT_FALSE(file->iseof());
   ASSERT_TRUE(file->error());
   ASSERT_EQ(File::cleanup(file), 0);
 
-  file = __llvm_libc::openfile(FILENAME, "a");
+  file = openfile(FILENAME, "a");
   ASSERT_FALSE(file == nullptr);
   ASSERT_EQ(file->read(data, 1).value, size_t(0)); // Cannot read
   ASSERT_FALSE(file->iseof());


        


More information about the libc-commits mailing list