[libc-commits] [libc] bb2ebbd - [libc][NFC] Simplify string-table generation internals.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Mon May 8 11:36:22 PDT 2023


Author: Siva Chandra Reddy
Date: 2023-05-08T18:36:08Z
New Revision: bb2ebbd190b4bcce797571a1d5363225ef6372f9

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

LOG: [libc][NFC] Simplify string-table generation internals.

Reviewed By: michaelrj

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

Added: 
    

Modified: 
    libc/src/__support/StringUtil/error_to_string.cpp
    libc/src/__support/StringUtil/message_mapper.h
    libc/src/__support/StringUtil/signal_to_string.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/StringUtil/error_to_string.cpp b/libc/src/__support/StringUtil/error_to_string.cpp
index cd314cb3d4e6c..6a35b359220a0 100644
--- a/libc/src/__support/StringUtil/error_to_string.cpp
+++ b/libc/src/__support/StringUtil/error_to_string.cpp
@@ -37,9 +37,7 @@ constexpr size_t max_buff_size() {
 constexpr size_t ERR_BUFFER_SIZE = max_buff_size();
 thread_local char error_buffer[ERR_BUFFER_SIZE];
 
-constexpr size_t RAW_ARRAY_LEN = PLATFORM_ERRORS.size();
-constexpr size_t TOTAL_STR_LEN =
-    total_str_len(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN);
+constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_ERRORS);
 
 // Since the StringMappings array is a map from error numbers to their
 // corresponding strings, we have to have an array large enough we can use the
@@ -47,11 +45,10 @@ constexpr size_t TOTAL_STR_LEN =
 // the maximum value being 133 (41 and 58 are skipped). If other platforms use
 // negative numbers or discontiguous ranges, then the array should be turned
 // into a proper hashmap.
-constexpr size_t ERR_ARRAY_SIZE =
-    max_key_val(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN) + 1;
+constexpr size_t ERR_ARRAY_SIZE = max_key_val(PLATFORM_ERRORS) + 1;
 
-static constexpr MessageMapper<ERR_ARRAY_SIZE, TOTAL_STR_LEN>
-    error_mapper(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN);
+constexpr MessageMapper<ERR_ARRAY_SIZE, TOTAL_STR_LEN>
+    error_mapper(PLATFORM_ERRORS);
 
 cpp::string_view build_error_string(int err_num, cpp::span<char> buffer) {
   // if the buffer can't hold "Unknown error" + ' ' + num_str, then just

diff  --git a/libc/src/__support/StringUtil/message_mapper.h b/libc/src/__support/StringUtil/message_mapper.h
index 26727cccd8d5a..91b13549a2000 100644
--- a/libc/src/__support/StringUtil/message_mapper.h
+++ b/libc/src/__support/StringUtil/message_mapper.h
@@ -29,20 +29,22 @@ struct MsgMapping {
   }
 };
 
-constexpr size_t total_str_len(const MsgMapping *array, size_t len) {
+template <size_t N> using MsgTable = cpp::array<MsgMapping, N>;
+
+template <size_t N> constexpr size_t total_str_len(const MsgTable<N> &table) {
   size_t total = 0;
-  for (size_t i = 0; i < len; ++i) {
+  for (size_t i = 0; i < table.size(); ++i) {
     // add 1 for the null terminator.
-    total += array[i].msg.size() + 1;
+    total += table[i].msg.size() + 1;
   }
   return total;
 }
 
-constexpr size_t max_key_val(const MsgMapping *array, size_t len) {
+template <size_t N> constexpr size_t max_key_val(const MsgTable<N> &table) {
   int max = 0;
-  for (size_t i = 0; i < len; ++i) {
-    if (array[i].num > max) {
-      max = array[i].num;
+  for (size_t i = 0; i < table.size(); ++i) {
+    if (table[i].num > max) {
+      max = table[i].num;
     }
   }
   // max will never be negative since the starting value is 0. This is good,
@@ -55,10 +57,10 @@ template <size_t ARR_SIZE, size_t TOTAL_STR_LEN> class MessageMapper {
   char string_array[TOTAL_STR_LEN] = {'\0'};
 
 public:
-  constexpr MessageMapper(const MsgMapping raw_array[], size_t raw_array_len) {
+  template <size_t N> constexpr MessageMapper(const MsgTable<N> &table) {
     cpp::string_view string_mappings[ARR_SIZE] = {""};
-    for (size_t i = 0; i < raw_array_len; ++i)
-      string_mappings[raw_array[i].num] = raw_array[i].msg;
+    for (size_t i = 0; i < table.size(); ++i)
+      string_mappings[table[i].num] = table[i].msg;
 
     int string_array_index = 0;
     for (size_t cur_num = 0; cur_num < ARR_SIZE; ++cur_num) {
@@ -86,8 +88,6 @@ template <size_t ARR_SIZE, size_t TOTAL_STR_LEN> class MessageMapper {
   }
 };
 
-template <size_t N> using MsgTable = cpp::array<MsgMapping, N>;
-
 template <size_t N1, size_t N2>
 constexpr MsgTable<N1 + N2> operator+(const MsgTable<N1> &t1,
                                       const MsgTable<N2> &t2) {

diff  --git a/libc/src/__support/StringUtil/signal_to_string.cpp b/libc/src/__support/StringUtil/signal_to_string.cpp
index cfe979b02feea..b3201380af8f0 100644
--- a/libc/src/__support/StringUtil/signal_to_string.cpp
+++ b/libc/src/__support/StringUtil/signal_to_string.cpp
@@ -34,15 +34,12 @@ constexpr size_t max_buff_size() {
 constexpr size_t SIG_BUFFER_SIZE = max_buff_size();
 thread_local char signal_buffer[SIG_BUFFER_SIZE];
 
-constexpr size_t RAW_ARRAY_LEN = PLATFORM_SIGNALS.size();
-constexpr size_t TOTAL_STR_LEN =
-    total_str_len(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN);
+constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_SIGNALS);
 
-constexpr size_t SIG_ARRAY_SIZE =
-    max_key_val(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN) + 1;
+constexpr size_t SIG_ARRAY_SIZE = max_key_val(PLATFORM_SIGNALS) + 1;
 
-static constexpr MessageMapper<SIG_ARRAY_SIZE, TOTAL_STR_LEN>
-    signal_mapper(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN);
+constexpr MessageMapper<SIG_ARRAY_SIZE, TOTAL_STR_LEN>
+    signal_mapper(PLATFORM_SIGNALS);
 
 cpp::string_view build_signal_string(int sig_num, cpp::span<char> buffer) {
   cpp::string_view base_str;


        


More information about the libc-commits mailing list