[libcxx-commits] [libcxx] da6ff3a - [libc++][format] Uglyfies format buffer.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 17 08:50:28 PDT 2022


Author: Mark de Wever
Date: 2022-08-17T17:50:24+02:00
New Revision: da6ff3aecb73b2883b6a0b6f71b9e5c87e23b1a6

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

LOG: [libc++][format] Uglyfies format buffer.

While working on D129964 I noticed some code hadn't been uglyfied, this
rectifies the issue.

Depends on D129964

Reviewed By: #libc, philnik

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

Added: 
    

Modified: 
    libcxx/include/__format/buffer.h
    libcxx/include/format

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/buffer.h b/libcxx/include/__format/buffer.h
index 4f972264e481f..4a813e73efd76 100644
--- a/libcxx/include/__format/buffer.h
+++ b/libcxx/include/__format/buffer.h
@@ -55,30 +55,27 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
   using value_type = _CharT;
 
   template <class _Tp>
-  _LIBCPP_HIDE_FROM_ABI explicit __output_buffer(_CharT* __ptr,
-                                                 size_t __capacity, _Tp* __obj)
-      : __ptr_(__ptr), __capacity_(__capacity),
-        __flush_([](_CharT* __p, size_t __size, void* __o) {
-          static_cast<_Tp*>(__o)->flush(__p, __size);
-        }),
+  _LIBCPP_HIDE_FROM_ABI explicit __output_buffer(_CharT* __ptr, size_t __capacity, _Tp* __obj)
+      : __ptr_(__ptr),
+        __capacity_(__capacity),
+        __flush_([](_CharT* __p, size_t __n, void* __o) { static_cast<_Tp*>(__o)->__flush(__p, __n); }),
         __obj_(__obj) {}
 
-  _LIBCPP_HIDE_FROM_ABI void reset(_CharT* __ptr, size_t __capacity) {
+  _LIBCPP_HIDE_FROM_ABI void __reset(_CharT* __ptr, size_t __capacity) {
     __ptr_ = __ptr;
     __capacity_ = __capacity;
   }
 
-  _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() {
-    return back_insert_iterator{*this};
-  }
+  _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return std::back_insert_iterator{*this}; }
 
+  // Used in std::back_insert_iterator.
   _LIBCPP_HIDE_FROM_ABI void push_back(_CharT __c) {
     __ptr_[__size_++] = __c;
 
     // Profiling showed flushing after adding is more efficient than flushing
     // when entering the function.
     if (__size_ == __capacity_)
-      flush();
+      __flush();
   }
 
   /// Copies the input __str to the buffer.
@@ -117,7 +114,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
       __size_ = __chunk;
       __first += __chunk;
       __n -= __chunk;
-      flush();
+      __flush();
     } while (__n);
   }
 
@@ -145,7 +142,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
       __size_ = __chunk;
       __first += __chunk;
       __n -= __chunk;
-      flush();
+      __flush();
     } while (__n);
   }
 
@@ -166,11 +163,11 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
       _VSTD::fill_n(_VSTD::addressof(__ptr_[__size_]), __chunk, __value);
       __size_ = __chunk;
       __n -= __chunk;
-      flush();
+      __flush();
     } while (__n);
   }
 
-  _LIBCPP_HIDE_FROM_ABI void flush() {
+  _LIBCPP_HIDE_FROM_ABI void __flush() {
     __flush_(__ptr_, __size_, __obj_);
     __size_ = 0;
   }
@@ -191,7 +188,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
   /// if (__n <= __capacity_) {
   ///   // Flush when we really would overflow.
   ///   if (__size_ + __n >= __capacity_)
-  ///     flush();
+  ///     __flush();
   ///   ...
   /// }
   /// \endcode
@@ -199,7 +196,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
   /// This approach works for all cases but one:
   /// A __format_to_n_buffer_base where \ref __enable_direct_output is true.
   /// In that case the \ref __capacity_ of the buffer changes during the first
-  /// \ref flush. During that operation the output buffer switches from its
+  /// \ref __flush. During that operation the output buffer switches from its
   /// __writer_ to its __storage_. The \ref __capacity_ of the former depends
   /// on the value of n, of the latter is a fixed size. For example:
   /// - a format_to_n call with a 10'000 char buffer,
@@ -208,7 +205,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
   ///   changed and the \ref __capacity_ decreases from 10'000 to
   ///   __buffer_size (256 at the time of writing).
   ///
-  /// This means that the \ref flush for this class may need to copy a part of
+  /// This means that the \ref __flush for this class may need to copy a part of
   /// the internal buffer to the proper output. In this example there will be
   /// 500 characters that need this copy operation.
   ///
@@ -217,7 +214,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
   /// not the most common use case. Therefore the optimization isn't done.
   _LIBCPP_HIDE_FROM_ABI void __flush_on_overflow(size_t __n) {
     if (__size_ + __n >= __capacity_)
-      flush();
+      __flush();
   }
 };
 
@@ -228,7 +225,7 @@ class _LIBCPP_TEMPLATE_VIS __output_buffer {
 template <__formatter::__char_type _CharT>
 class _LIBCPP_TEMPLATE_VIS __internal_storage {
 public:
-  _LIBCPP_HIDE_FROM_ABI _CharT* begin() { return __buffer_; }
+  _LIBCPP_HIDE_FROM_ABI _CharT* __begin() { return __buffer_; }
 
   static constexpr size_t __buffer_size = 256 / sizeof(_CharT);
 
@@ -259,12 +256,12 @@ class _LIBCPP_TEMPLATE_VIS __writer_direct {
   _LIBCPP_HIDE_FROM_ABI explicit __writer_direct(_OutIt __out_it)
       : __out_it_(__out_it) {}
 
-  _LIBCPP_HIDE_FROM_ABI auto out() { return __out_it_; }
+  _LIBCPP_HIDE_FROM_ABI _OutIt __out_it() { return __out_it_; }
 
-  _LIBCPP_HIDE_FROM_ABI void flush(_CharT*, size_t __size) {
+  _LIBCPP_HIDE_FROM_ABI void __flush(_CharT*, size_t __n) {
     // _OutIt can be a __wrap_iter<CharT*>. Therefore the original iterator
     // is adjusted.
-    __out_it_ += __size;
+    __out_it_ += __n;
   }
 
 private:
@@ -278,10 +275,10 @@ class _LIBCPP_TEMPLATE_VIS __writer_iterator {
   _LIBCPP_HIDE_FROM_ABI explicit __writer_iterator(_OutIt __out_it)
       : __out_it_{_VSTD::move(__out_it)} {}
 
-  _LIBCPP_HIDE_FROM_ABI auto out() { return __out_it_; }
+  _LIBCPP_HIDE_FROM_ABI _OutIt __out_it() { return __out_it_; }
 
-  _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
-    __out_it_ = _VSTD::copy_n(__ptr, __size, _VSTD::move(__out_it_));
+  _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
+    __out_it_ = _VSTD::copy_n(__ptr, __n, _VSTD::move(__out_it_));
   }
 
 private:
@@ -321,10 +318,10 @@ class _LIBCPP_TEMPLATE_VIS __writer_container {
   _LIBCPP_HIDE_FROM_ABI explicit __writer_container(back_insert_iterator<_Container> __out_it)
       : __container_{__out_it.__get_container()} {}
 
-  _LIBCPP_HIDE_FROM_ABI auto out() { return back_inserter(*__container_); }
+  _LIBCPP_HIDE_FROM_ABI auto __out_it() { return std::back_inserter(*__container_); }
 
-  _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
-    __container_->insert(__container_->end(), __ptr, __ptr + __size);
+  _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
+    __container_->insert(__container_->end(), __ptr, __ptr + __n);
   }
 
 private:
@@ -353,24 +350,20 @@ requires(output_iterator<_OutIt, const _CharT&>) class _LIBCPP_TEMPLATE_VIS
 public:
   _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it)
     requires(same_as<_Storage, __internal_storage<_CharT>>)
-  : __output_(__storage_.begin(), __storage_.__buffer_size, this), __writer_(_VSTD::move(__out_it)) {}
+      : __output_(__storage_.__begin(), __storage_.__buffer_size, this), __writer_(_VSTD::move(__out_it)) {}
 
   _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it) requires(
       same_as<_Storage, __direct_storage<_CharT>>)
       : __output_(_VSTD::__unwrap_iter(__out_it), size_t(-1), this),
         __writer_(_VSTD::move(__out_it)) {}
 
-  _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() {
-    return __output_.make_output_iterator();
-  }
+  _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return __output_.__make_output_iterator(); }
 
-  _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
-    __writer_.flush(__ptr, __size);
-  }
+  _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) { __writer_.__flush(__ptr, __n); }
 
-  _LIBCPP_HIDE_FROM_ABI _OutIt out() && {
-    __output_.flush();
-    return _VSTD::move(__writer_).out();
+  _LIBCPP_HIDE_FROM_ABI _OutIt __out_it() && {
+    __output_.__flush();
+    return _VSTD::move(__writer_).__out_it();
   }
 
 private:
@@ -386,18 +379,18 @@ requires(output_iterator<_OutIt, const _CharT&>) class _LIBCPP_TEMPLATE_VIS
 template <__formatter::__char_type _CharT>
 class _LIBCPP_TEMPLATE_VIS __formatted_size_buffer {
 public:
-  _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() { return __output_.make_output_iterator(); }
+  _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return __output_.__make_output_iterator(); }
 
-  _LIBCPP_HIDE_FROM_ABI void flush(const _CharT*, size_t __size) { __size_ += __size; }
+  _LIBCPP_HIDE_FROM_ABI void __flush(const _CharT*, size_t __n) { __size_ += __n; }
 
-  _LIBCPP_HIDE_FROM_ABI size_t result() && {
-    __output_.flush();
+  _LIBCPP_HIDE_FROM_ABI size_t __result() && {
+    __output_.__flush();
     return __size_;
   }
 
 private:
   __internal_storage<_CharT> __storage_;
-  __output_buffer<_CharT> __output_{__storage_.begin(), __storage_.__buffer_size, this};
+  __output_buffer<_CharT> __output_{__storage_.__begin(), __storage_.__buffer_size, this};
   size_t __size_{0};
 };
 
@@ -411,15 +404,15 @@ struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base {
   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __max_size)
       : __writer_(_VSTD::move(__out_it)), __max_size_(_VSTD::max(_Size(0), __max_size)) {}
 
-  _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
+  _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
     if (_Size(__size_) <= __max_size_)
-      __writer_.flush(__ptr, _VSTD::min(_Size(__size), __max_size_ - __size_));
-    __size_ += __size;
+      __writer_.__flush(__ptr, _VSTD::min(_Size(__n), __max_size_ - __size_));
+    __size_ += __n;
   }
 
 protected:
   __internal_storage<_CharT> __storage_;
-  __output_buffer<_CharT> __output_{__storage_.begin(), __storage_.__buffer_size, this};
+  __output_buffer<_CharT> __output_{__storage_.__begin(), __storage_.__buffer_size, this};
   typename __writer_selector<_OutIt, _CharT>::type __writer_;
 
   _Size __max_size_;
@@ -443,33 +436,33 @@ class _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base<_OutIt, _CharT, true> {
         __writer_(_VSTD::move(__out_it)),
         __max_size_(__max_size) {
     if (__max_size <= 0) [[unlikely]]
-      __output_.reset(__storage_.begin(), __storage_.__buffer_size);
+      __output_.__reset(__storage_.__begin(), __storage_.__buffer_size);
   }
 
-  _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
-    // A flush to the direct writer happens in the following occasions:
+  _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
+    // A __flush to the direct writer happens in the following occasions:
     // - The format function has written the maximum number of allowed code
     //   units. At this point it's no longer valid to write to this writer. So
     //   switch to the internal storage. This internal storage doesn't need to
-    //   be written anywhere so the flush for that storage writes no output.
+    //   be written anywhere so the __flush for that storage writes no output.
     // - Like above, but the next "mass write" operation would overflow the
     //   buffer. In that case the buffer is pre-emptively switched. The still
     //   valid code units will be written separately.
     // - The format_to_n function is finished. In this case there's no need to
     //   switch the buffer, but for simplicity the buffers are still switched.
     // When the __max_size <= 0 the constructor already switched the buffers.
-    if (__size_ == 0 && __ptr != __storage_.begin()) {
-      __writer_.flush(__ptr, __size);
-      __output_.reset(__storage_.begin(), __storage_.__buffer_size);
+    if (__size_ == 0 && __ptr != __storage_.__begin()) {
+      __writer_.__flush(__ptr, __n);
+      __output_.__reset(__storage_.__begin(), __storage_.__buffer_size);
     } else if (__size_ < __max_size_) {
       // Copies a part of the internal buffer to the output up to n characters.
       // See __output_buffer<_CharT>::__flush_on_overflow for more information.
-      _Size __s = _VSTD::min(_Size(__size), __max_size_ - __size_);
-      std::copy_n(__ptr, __s, __writer_.out());
-      __writer_.flush(__ptr, __s);
+      _Size __s = _VSTD::min(_Size(__n), __max_size_ - __size_);
+      std::copy_n(__ptr, __s, __writer_.__out_it());
+      __writer_.__flush(__ptr, __s);
     }
 
-    __size_ += __size;
+    __size_ += __n;
   }
 
 protected:
@@ -492,11 +485,11 @@ struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer final
 public:
   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer(_OutIt __out_it, _Size __max_size)
       : _Base(_VSTD::move(__out_it), __max_size) {}
-  _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() { return this->__output_.make_output_iterator(); }
+  _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return this->__output_.__make_output_iterator(); }
 
-  _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> result() && {
-    this->__output_.flush();
-    return {_VSTD::move(this->__writer_).out(), this->__size_};
+  _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __result() && {
+    this->__output_.__flush();
+    return {_VSTD::move(this->__writer_).__out_it(), this->__size_};
   }
 };
 } // namespace __format

diff  --git a/libcxx/include/format b/libcxx/include/format
index 6b14570bc6272..aad98892a06ba 100644
--- a/libcxx/include/format
+++ b/libcxx/include/format
@@ -541,9 +541,9 @@ requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
     __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)};
     _VSTD::__format::__vformat_to(
         basic_format_parse_context{__fmt, __args.__size()},
-        _VSTD::__format_context_create(__buffer.make_output_iterator(),
+        _VSTD::__format_context_create(__buffer.__make_output_iterator(),
                                        __args));
-    return _VSTD::move(__buffer).out();
+    return _VSTD::move(__buffer).__out_it();
   }
 }
 
@@ -616,8 +616,8 @@ _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __vformat_to_n(_OutIt __out_it,
                                                                 basic_format_args<_Context> __args) {
   __format::__format_to_n_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it), __n};
   _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()},
-                                _VSTD::__format_context_create(__buffer.make_output_iterator(), __args));
-  return _VSTD::move(__buffer).result();
+                                _VSTD::__format_context_create(__buffer.__make_output_iterator(), __args));
+  return _VSTD::move(__buffer).__result();
 }
 
 template <output_iterator<const char&> _OutIt, class... _Args>
@@ -639,8 +639,8 @@ template <class _CharT>
 _LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(basic_string_view<_CharT> __fmt, auto __args) {
   __format::__formatted_size_buffer<_CharT> __buffer;
   _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()},
-                                _VSTD::__format_context_create(__buffer.make_output_iterator(), __args));
-  return _VSTD::move(__buffer).result();
+                                _VSTD::__format_context_create(__buffer.__make_output_iterator(), __args));
+  return _VSTD::move(__buffer).__result();
 }
 
 template <class... _Args>
@@ -673,9 +673,9 @@ requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
     __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)};
     _VSTD::__format::__vformat_to(
         basic_format_parse_context{__fmt, __args.__size()},
-        _VSTD::__format_context_create(__buffer.make_output_iterator(),
+        _VSTD::__format_context_create(__buffer.__make_output_iterator(),
                                        __args, _VSTD::move(__loc)));
-    return _VSTD::move(__buffer).out();
+    return _VSTD::move(__buffer).__out_it();
   }
 }
 
@@ -753,8 +753,8 @@ _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __vformat_to_n(_OutIt __out_it,
   __format::__format_to_n_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it), __n};
   _VSTD::__format::__vformat_to(
       basic_format_parse_context{__fmt, __args.__size()},
-      _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc)));
-  return _VSTD::move(__buffer).result();
+      _VSTD::__format_context_create(__buffer.__make_output_iterator(), __args, _VSTD::move(__loc)));
+  return _VSTD::move(__buffer).__result();
 }
 
 template <output_iterator<const char&> _OutIt, class... _Args>
@@ -780,8 +780,8 @@ _LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(locale __loc, basic_string_view<_
   __format::__formatted_size_buffer<_CharT> __buffer;
   _VSTD::__format::__vformat_to(
       basic_format_parse_context{__fmt, __args.__size()},
-      _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc)));
-  return _VSTD::move(__buffer).result();
+      _VSTD::__format_context_create(__buffer.__make_output_iterator(), __args, _VSTD::move(__loc)));
+  return _VSTD::move(__buffer).__result();
 }
 
 template <class... _Args>


        


More information about the libcxx-commits mailing list