[libc-commits] [PATCH] D117258: [libc] add core parsing for printf

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Jan 21 14:34:29 PST 2022

sivachandra added a comment.

I have not read through everything yet. But, I think we should have a high level design which makes use of C++ facilities where possible to make the whole system neater and manageable.

1. Separate the actions of parsing, formatting and production.
2. Viewing the parsing aspect as a separate action is straightforward. The parsing system itself needs a design of its own. I think we can tackle it after we tackle the formatting and production actions.
3. With respect to formatting, we should have a `Stream` class like this:

  typedef void BufferWriter(void *, char *, size_t);
  class Stream {
    Stream(void *buffer, BufferWriter *writer);
    template <typename T>
    Stream &operator<<(T);

There should be various specializations of the `operator<<` method. In fact, considering we need to accommodate a number of formatting features, we should imitate much of the C++ `std::ostream` functionality (like being able to change encoding with `std::hex`, being able to set minimum width, etc.)  Also, some of the specializations should probably live in their own object files so that they can be selectively excluded. For example, we might want to exclude floating point number formatters.

4. The production aspect refers to the buffer and the writer function. For example, buffer can be a `FILE *` or a `char *`. The writer functions for these would be something like:

  void char_writer(void *buf, char *data, size_t size) {
    char *dst = static_cast<char *>(buf);
    // Write |size| bytes from |data| to |dst|
  void file_writer(void *bug, char*data, size_t size) {
    FILE *file = static_cast<FILE *>(buf);
    // Write |size| bytes from |data| to |file|

Note that the `Stream` class and the buffer writers can be written and tested without requiring a parser.

  rG LLVM Github Monorepo



More information about the libc-commits mailing list