[libcxx-commits] [libcxx] 5d55ffe - [libc++] Simplify the string structures a bit more

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sat May 14 13:08:21 PDT 2022


Author: Nikolas Klauser
Date: 2022-05-14T22:07:50+02:00
New Revision: 5d55ffe94dc947308059a7a2484321b7f1bd6270

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

LOG: [libc++] Simplify the string structures a bit more

This simplifies the string structs a bit more and the normal layout should not contain any undefined behaviour anymore. I don't think there is a way to achieve this in the alternate string mode without breaking the ABI.

Reviewed By: ldionne, #libc

Spies: libcxx-commits

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

Added: 
    

Modified: 
    libcxx/include/string

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string b/libcxx/include/string
index 961f8c6228cd8..1ccf4862bb50f 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -672,6 +672,7 @@ public:
     typedef std::reverse_iterator<const_iterator>        const_reverse_iterator;
 
 private:
+    static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
 
 #ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
 
@@ -689,14 +690,13 @@ private:
     struct __short
     {
         value_type __data_[__min_cap];
-        unsigned char __padding[sizeof(value_type) - 1];
-        unsigned char __size_ : 7;
-        unsigned char __is_long_ : 1;
+        unsigned char __padding_[sizeof(value_type) - 1];
+        size_type __size_ : 7;
+        size_type __is_long_ : 1;
     };
 
 // The __endian_factor is required because the field we use to store the size
-// (either size_type or unsigned char depending on long/short) has one fewer
-// bit than it would if it were not a bitfield.
+// has one fewer bit than it would if it were not a bitfield.
 //
 // If the LSB is used to store the short-flag in the short string representation,
 // we have to multiply the size by two when it is stored and divide it by two when
@@ -735,14 +735,9 @@ private:
 
     struct __short
     {
-        union
-        {
-            struct {
-                unsigned char __is_long_ : 1;
-                unsigned char __size_ : 7;
-            };
-            value_type __lx;
-        };
+        size_type __is_long_ : 1;
+        size_type __size_ : 7;
+        char __padding_[sizeof(value_type) - 1];
         value_type __data_[__min_cap];
     };
 


        


More information about the libcxx-commits mailing list