[all-commits] [llvm/llvm-project] 5e6316: [libc] Refactor static polymorphism in WriteBuffer...

Alexey Samsonov via All-commits all-commits at lists.llvm.org
Thu Dec 11 15:31:42 PST 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 5e63167965224e523ed69770754330c8cb22e90f
      https://github.com/llvm/llvm-project/commit/5e63167965224e523ed69770754330c8cb22e90f
  Author: Alexey Samsonov <vonosmas at gmail.com>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M libc/src/__support/RPC/rpc_server.h
    M libc/src/stdio/baremetal/printf.cpp
    M libc/src/stdio/baremetal/vprintf.cpp
    M libc/src/stdio/printf_core/vasprintf_internal.h
    M libc/src/stdio/printf_core/vfprintf_internal.h
    M libc/src/stdio/printf_core/writer.h
    M libc/src/stdio/snprintf.cpp
    M libc/src/stdio/sprintf.cpp
    M libc/src/stdio/vsnprintf.cpp
    M libc/src/stdio/vsprintf.cpp
    M libc/src/stdlib/strfromd.cpp
    M libc/src/stdlib/strfromf.cpp
    M libc/src/stdlib/strfroml.cpp
    M libc/src/time/strftime.cpp
    M libc/src/time/strftime_l.cpp
    M libc/test/src/stdio/printf_core/converter_test.cpp
    M libc/test/src/stdio/printf_core/writer_test.cpp

  Log Message:
  -----------
  [libc] Refactor static polymorphism in WriteBuffer (NFC). (#169089)

There are three flavors of WriteBuffer currently, all of which could be
passed into `printf_core::Writer` class. It's a tricky class, since it
chooses a flavor-specific logic either based on runtime dispatch (to
save code size and prevent generating three versions of the entirety of
printf_core), or based on template arguments (to avoid dealing with
function pointers in codegen for `FILL_BUFF_AND_DROP_OVERFLOW` path).

Refactor this somewhat convoluted logic to have three concrete
subclasses inheriting from the templated base class, and use static
polymorphism with `reinterpret_cast` to implement dispatching above. Now
we can actually have flavor-specific fields, constructors, and methods
(e.g. `flush_to_stream` is now a method of `FlushingBuffer`), and the
code on the user side is cleaner: the complexity of enabling/disabling
runtime-dispatch and using proper template arguments is now localized in
`writer.h`.

This code will need to be further templatized to support buffers of type
`wchar_t` to implement `swprintf()` and friends. This change would make
it (ever so slightly) easier.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list