[libc-commits] [libc] a1c42cb - [reland][NFC][libc] Use span instead of ArrayRef
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Mon Aug 22 01:23:16 PDT 2022
Author: Guillaume Chatelet
Date: 2022-08-22T08:22:38Z
New Revision: a1c42cb80354cfa6ee99ea8cb1e5cab62c1f13d5
URL: https://github.com/llvm/llvm-project/commit/a1c42cb80354cfa6ee99ea8cb1e5cab62c1f13d5
DIFF: https://github.com/llvm/llvm-project/commit/a1c42cb80354cfa6ee99ea8cb1e5cab62c1f13d5.diff
LOG: [reland][NFC][libc] Use span instead of ArrayRef
Added:
Modified:
libc/src/__support/CMakeLists.txt
libc/src/__support/CPP/CMakeLists.txt
libc/src/__support/CPP/stringstream.h
libc/src/__support/File/CMakeLists.txt
libc/src/__support/File/dir.cpp
libc/src/__support/File/dir.h
libc/src/__support/File/linux_dir.cpp
libc/src/__support/integer_to_string.h
libc/src/pthread/CMakeLists.txt
libc/src/pthread/pthread_getname_np.cpp
libc/src/stdio/printf_core/CMakeLists.txt
libc/src/stdio/printf_core/int_converter.h
libc/test/src/__support/CPP/CMakeLists.txt
libc/test/src/__support/CPP/stringstream_test.cpp
libc/test/src/string/stpncpy_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index b50986f2c08bd..986ccc0f04c6e 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -37,6 +37,7 @@ add_header_library(
HDRS
integer_to_string.h
DEPENDS
+ libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits
)
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index a3734c382afb3..accd7a2042a9d 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -57,6 +57,7 @@ add_header_library(
HDRS
span.h
DEPENDS
+ .array
.type_traits
)
@@ -71,7 +72,7 @@ add_header_library(
HDRS
stringstream.h
DEPENDS
- .array_ref
+ .span
.string_view
libc.src.__support.integer_to_string
)
diff --git a/libc/src/__support/CPP/stringstream.h b/libc/src/__support/CPP/stringstream.h
index c9e276279d73b..c71945f1166bb 100644
--- a/libc/src/__support/CPP/stringstream.h
+++ b/libc/src/__support/CPP/stringstream.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
-#include "ArrayRef.h"
#include "StringView.h"
+#include "span.h"
#include "type_traits.h"
#include "src/__support/integer_to_string.h"
@@ -22,7 +22,7 @@ namespace cpp {
// without any dynamic memory allocation. There is no requirement to mimic the
// C++ standard library class std::stringstream.
class StringStream {
- MutableArrayRef<char> data;
+ span<char> data;
size_t write_ptr = 0; // The current write pointer
bool err = false; // If an error occurs while writing
@@ -41,7 +41,7 @@ class StringStream {
static constexpr char ENDS = '\0';
// Create a string stream which will write into |buf|.
- constexpr StringStream(const MutableArrayRef<char> &buf) : data(buf) {}
+ constexpr StringStream(const span<char> &buf) : data(buf) {}
// Return a StringView to the current characters in the stream. If a
// null terminator was not explicitly written, then the return value
diff --git a/libc/src/__support/File/CMakeLists.txt b/libc/src/__support/File/CMakeLists.txt
index b6ffcf9ae45f3..3d2e7047e87c5 100644
--- a/libc/src/__support/File/CMakeLists.txt
+++ b/libc/src/__support/File/CMakeLists.txt
@@ -23,7 +23,7 @@ add_object_library(
HDRS
dir.h
DEPENDS
- libc.src.__support.CPP.array_ref
+ libc.src.__support.CPP.span
libc.src.__support.threads.mutex
)
diff --git a/libc/src/__support/File/dir.cpp b/libc/src/__support/File/dir.cpp
index 0c0cc65511364..bfd9b4cf446f6 100644
--- a/libc/src/__support/File/dir.cpp
+++ b/libc/src/__support/File/dir.cpp
@@ -29,8 +29,7 @@ Dir *Dir::open(const char *path) {
struct ::dirent *Dir::read() {
MutexLock lock(&mutex);
if (readptr >= fillsize) {
- fillsize = platform_fetch_dirents(
- fd, cpp::MutableArrayRef<uint8_t>(buffer, BUFSIZE));
+ fillsize = platform_fetch_dirents(fd, buffer);
if (fillsize == 0)
return nullptr;
readptr = 0;
diff --git a/libc/src/__support/File/dir.h b/libc/src/__support/File/dir.h
index b16791af33cf3..c5f3750f96884 100644
--- a/libc/src/__support/File/dir.h
+++ b/libc/src/__support/File/dir.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_FILE_DIR_H
#define LLVM_LIBC_SRC_SUPPORT_FILE_DIR_H
-#include "src/__support/CPP/ArrayRef.h"
+#include "src/__support/CPP/span.h"
#include "src/__support/threads/mutex.h"
#include <dirent.h>
@@ -28,7 +28,7 @@ bool platform_closedir(int fd);
// Platform specific function which will fetch dirents in to buffer.
// Returns the number of bytes written into buffer
-size_t platform_fetch_dirents(int fd, cpp::MutableArrayRef<uint8_t> buffer);
+size_t platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer);
// This class is designed to allow implementation of the POSIX dirent.h API.
// By itself, it is platform independent but calls platform specific
diff --git a/libc/src/__support/File/linux_dir.cpp b/libc/src/__support/File/linux_dir.cpp
index bde75c7c12c12..99d2436ecaec4 100644
--- a/libc/src/__support/File/linux_dir.cpp
+++ b/libc/src/__support/File/linux_dir.cpp
@@ -34,7 +34,7 @@ int platform_opendir(const char *name) {
return fd;
}
-size_t platform_fetch_dirents(int fd, cpp::MutableArrayRef<uint8_t> buffer) {
+size_t platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer) {
long size =
__llvm_libc::syscall(SYS_getdents, fd, buffer.data(), buffer.size());
if (size < 0) {
diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 00bcbca6d22a6..b918570c9f75c 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -9,9 +9,9 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_INTEGER_TO_STRING_H
#define LLVM_LIBC_SRC_SUPPORT_INTEGER_TO_STRING_H
-#include "src/__support/CPP/ArrayRef.h"
#include "src/__support/CPP/StringView.h"
#include "src/__support/CPP/optional.h"
+#include "src/__support/CPP/span.h"
#include "src/__support/CPP/type_traits.h"
namespace __llvm_libc {
@@ -43,7 +43,7 @@ namespace __llvm_libc {
// auto str = IntegerToString::convert<30>(a, b30buf);
class IntegerToString {
static cpp::StringView convert_uintmax(uintmax_t uval,
- cpp::MutableArrayRef<char> &buffer,
+ cpp::span<char> &buffer,
bool lowercase,
const uint8_t conv_base) {
const char a = lowercase ? 'a' : 'A';
@@ -65,8 +65,7 @@ class IntegerToString {
return cpp::StringView(buffer.data() + buffer.size() - len, len);
}
- static cpp::StringView convert_intmax(intmax_t val,
- cpp::MutableArrayRef<char> &buffer,
+ static cpp::StringView convert_intmax(intmax_t val, cpp::span<char> &buffer,
bool lowercase,
const uint8_t conv_base) {
if (val >= 0)
@@ -137,8 +136,8 @@ class IntegerToString {
template <uint8_t BASE, typename T,
cpp::enable_if_t<2 <= BASE && BASE <= 36 && cpp::is_integral_v<T>,
int> = 0>
- static cpp::optional<cpp::StringView>
- convert(T val, cpp::MutableArrayRef<char> buffer, bool lowercase = true) {
+ static cpp::optional<cpp::StringView> convert(T val, cpp::span<char> buffer,
+ bool lowercase = true) {
if (buffer.size() < bufsize<BASE, T>())
return cpp::optional<cpp::StringView>();
if (cpp::is_signed_v<T>)
@@ -148,26 +147,23 @@ class IntegerToString {
}
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
- static cpp::optional<cpp::StringView> dec(T val,
- cpp::MutableArrayRef<char> buffer) {
+ static cpp::optional<cpp::StringView> dec(T val, cpp::span<char> buffer) {
return convert<10>(val, buffer);
}
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
- static cpp::optional<cpp::StringView>
- hex(T val, cpp::MutableArrayRef<char> buffer, bool lowercase = true) {
+ static cpp::optional<cpp::StringView> hex(T val, cpp::span<char> buffer,
+ bool lowercase = true) {
return convert<16>(val, buffer, lowercase);
}
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
- static cpp::optional<cpp::StringView> oct(T val,
- cpp::MutableArrayRef<char> buffer) {
+ static cpp::optional<cpp::StringView> oct(T val, cpp::span<char> buffer) {
return convert<8>(val, buffer);
}
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
- static cpp::optional<cpp::StringView> bin(T val,
- cpp::MutableArrayRef<char> buffer) {
+ static cpp::optional<cpp::StringView> bin(T val, cpp::span<char> buffer) {
return convert<2>(val, buffer);
}
};
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 43cffb6168c60..6c652a118c2d9 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -321,8 +321,8 @@ add_entrypoint_object(
pthread_setname_np.h
DEPENDS
libc.include.pthread
- libc.src.__support.CPP.array_ref
- libc.src.__support.CPP.string_view
+ libc.src.__support.CPP.span
+ libc.src.__support.CPP.stringstream
libc.src.__support.threads.thread
)
diff --git a/libc/src/pthread/pthread_getname_np.cpp b/libc/src/pthread/pthread_getname_np.cpp
index b4d5387482547..e1c3549974b6b 100644
--- a/libc/src/pthread/pthread_getname_np.cpp
+++ b/libc/src/pthread/pthread_getname_np.cpp
@@ -8,8 +8,8 @@
#include "pthread_getname_np.h"
-#include "src/__support/CPP/ArrayRef.h"
-#include "src/__support/CPP/StringView.h"
+#include "src/__support/CPP/span.h"
+#include "src/__support/CPP/stringstream.h"
#include "src/__support/common.h"
#include "src/__support/threads/thread.h"
@@ -24,7 +24,7 @@ static_assert(sizeof(pthread_t) == sizeof(__llvm_libc::Thread),
LLVM_LIBC_FUNCTION(int, pthread_getname_np,
(pthread_t th, char *buf, size_t len)) {
auto *thread = reinterpret_cast<__llvm_libc::Thread *>(&th);
- cpp::MutableArrayRef<char> name_buf(buf, len);
+ cpp::span<char> name_buf(buf, len);
cpp::StringStream name_stream(name_buf);
return thread->get_name(name_stream);
}
diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt
index 9c7a9fbe9c5ab..edf77fe0018ba 100644
--- a/libc/src/stdio/printf_core/CMakeLists.txt
+++ b/libc/src/stdio/printf_core/CMakeLists.txt
@@ -59,10 +59,11 @@ add_object_library(
DEPENDS
.writer
.core_structs
- libc.src.__support.integer_to_string
libc.src.__support.CPP.limits
+ libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.FPUtil.fputil
+ libc.src.__support.integer_to_string
)
diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h
index 25f158e1c36b0..96fbe4522239c 100644
--- a/libc/src/stdio/printf_core/int_converter.h
+++ b/libc/src/stdio/printf_core/int_converter.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
-#include "src/__support/CPP/ArrayRef.h"
+#include "src/__support/CPP/span.h"
#include "src/__support/CPP/StringView.h"
#include "src/__support/integer_to_string.h"
#include "src/stdio/printf_core/converter_utils.h"
@@ -28,7 +28,7 @@ constexpr char inline to_lower(char a) { return a | 32; }
constexpr bool inline is_lower(char a) { return (a & 32) > 0; }
cpp::optional<cpp::StringView> inline num_to_strview(
- uintmax_t num, cpp::MutableArrayRef<char> bufref, char conv_name) {
+ uintmax_t num, cpp::span<char> bufref, char conv_name) {
if (to_lower(conv_name) == 'x') {
return IntegerToString::hex(num, bufref, is_lower(conv_name));
} else if (conv_name == 'o') {
diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt
index 1a766c99e23be..130db2064c5ce 100644
--- a/libc/test/src/__support/CPP/CMakeLists.txt
+++ b/libc/test/src/__support/CPP/CMakeLists.txt
@@ -78,7 +78,7 @@ add_libc_unittest(
SRCS
stringstream_test.cpp
DEPENDS
- libc.src.__support.CPP.array_ref
+ libc.src.__support.CPP.span
libc.src.__support.CPP.stringstream
)
diff --git a/libc/test/src/__support/CPP/stringstream_test.cpp b/libc/test/src/__support/CPP/stringstream_test.cpp
index 9664b42631c0b..33b8b392782e6 100644
--- a/libc/test/src/__support/CPP/stringstream_test.cpp
+++ b/libc/test/src/__support/CPP/stringstream_test.cpp
@@ -6,23 +6,22 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/CPP/ArrayRef.h"
+#include "src/__support/CPP/span.h"
#include "src/__support/CPP/stringstream.h"
#include "utils/UnitTest/Test.h"
-using __llvm_libc::cpp::MutableArrayRef;
+using __llvm_libc::cpp::span;
using __llvm_libc::cpp::StringStream;
TEST(LlvmLibcStringStreamTest, Simple) {
char buf[256];
- MutableArrayRef<char> bufref(reinterpret_cast<void *>(buf), 256);
- StringStream ss1(bufref);
+ StringStream ss1(buf);
ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS;
ASSERT_FALSE(ss1.overflow());
ASSERT_STREQ(ss1.str().data(), "Hello, Stream - 123");
- StringStream ss2(bufref);
+ StringStream ss2(buf);
ss2 << 'a' << 'b' << 'c' << StringStream::ENDS;
ASSERT_FALSE(ss2.overflow());
ASSERT_STREQ(ss2.str().data(), "abc");
@@ -31,14 +30,13 @@ TEST(LlvmLibcStringStreamTest, Simple) {
TEST(LlvmLibcStringStreamTest, Overflow) {
constexpr size_t BUFSIZE = 8;
char buf[BUFSIZE];
- MutableArrayRef<char> bufref(reinterpret_cast<void *>(buf), BUFSIZE);
- StringStream ss1(bufref);
+ StringStream ss1(buf);
ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS;
ASSERT_TRUE(ss1.overflow());
ASSERT_EQ(ss1.str().size(), BUFSIZE);
- StringStream ss2(bufref);
+ StringStream ss2(buf);
ss2 << "7777777";
ASSERT_FALSE(ss2.overflow());
ASSERT_EQ(ss2.str().size(), size_t(7));
diff --git a/libc/test/src/string/stpncpy_test.cpp b/libc/test/src/string/stpncpy_test.cpp
index 855be58941cec..09f977a46e67e 100644
--- a/libc/test/src/string/stpncpy_test.cpp
+++ b/libc/test/src/string/stpncpy_test.cpp
@@ -6,16 +6,16 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/CPP/ArrayRef.h"
+#include "src/__support/CPP/span.h"
#include "src/string/stpncpy.h"
#include "utils/UnitTest/Test.h"
#include <stddef.h> // For size_t.
class LlvmLibcStpncpyTest : public __llvm_libc::testing::Test {
public:
- void check_stpncpy(__llvm_libc::cpp::MutableArrayRef<char> dst,
- const __llvm_libc::cpp::ArrayRef<char> src, size_t n,
- const __llvm_libc::cpp::ArrayRef<char> expected,
+ void check_stpncpy(__llvm_libc::cpp::span<char> dst,
+ const __llvm_libc::cpp::span<const char> src, size_t n,
+ const __llvm_libc::cpp::span<const char> expected,
size_t expectedCopied) {
// Making sure we don't overflow buffer.
ASSERT_GE(dst.size(), n);
More information about the libc-commits
mailing list