Skip to content

[libc][NFC] clean up printf_core and scanf_core #74535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions libc/src/stdio/printf_core/char_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
Expand All @@ -21,10 +19,10 @@ namespace printf_core {
LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) {
char c = static_cast<char>(to_conv.conv_val_raw);

constexpr int string_len = 1;
constexpr int STRING_LEN = 1;

size_t padding_spaces =
to_conv.min_width > string_len ? to_conv.min_width - string_len : 0;
to_conv.min_width > STRING_LEN ? to_conv.min_width - STRING_LEN : 0;

// If the padding is on the left side, write the spaces first.
if (padding_spaces > 0 &&
Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/printf_core/converter_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H

#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/core_structs.h"

#include <inttypes.h>
Expand Down
12 changes: 6 additions & 6 deletions libc/src/stdio/printf_core/core_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct FormatSection {

// This operator is only used for testing and should be automatically
// optimized out for release builds.
bool operator==(const FormatSection &other) const {
LIBC_INLINE bool operator==(const FormatSection &other) const {
if (has_conv != other.has_conv)
return false;

Expand Down Expand Up @@ -93,11 +93,11 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
if constexpr (cpp::is_same_v<T, void>) {
return TypeDesc{0, PrimaryType::Unknown};
} else {
constexpr bool isPointer = cpp::is_pointer_v<T>;
constexpr bool isFloat = cpp::is_floating_point_v<T>;
return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer
: isFloat ? PrimaryType::Float
: PrimaryType::Integer};
constexpr bool IS_POINTER = cpp::is_pointer_v<T>;
constexpr bool IS_FLOAT = cpp::is_floating_point_v<T>;
return TypeDesc{sizeof(T), IS_POINTER ? PrimaryType::Pointer
: IS_FLOAT ? PrimaryType::Float
: PrimaryType::Integer};
}
}

Expand Down
35 changes: 16 additions & 19 deletions libc/src/stdio/printf_core/float_dec_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_DEC_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/FloatProperties.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt.h"
#include "src/__support/UInt128.h"
#include "src/__support/common.h"
#include "src/__support/float_to_string.h"
#include "src/__support/integer_to_string.h"
#include "src/__support/libc_assert.h"
Expand Down Expand Up @@ -103,14 +99,14 @@ class PaddingWriter {
size_t min_width = 0;

public:
PaddingWriter() {}
PaddingWriter(const FormatSection &to_conv, char init_sign_char)
LIBC_INLINE PaddingWriter() {}
LIBC_INLINE PaddingWriter(const FormatSection &to_conv, char init_sign_char)
: left_justified((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) > 0),
leading_zeroes((to_conv.flags & FormatFlags::LEADING_ZEROES) > 0),
sign_char(init_sign_char),
min_width(to_conv.min_width > 0 ? to_conv.min_width : 0) {}

int write_left_padding(Writer *writer, size_t total_digits) {
LIBC_INLINE int write_left_padding(Writer *writer, size_t total_digits) {
// The pattern is (spaces) (sign) (zeroes), but only one of spaces and
// zeroes can be written, and only if the padding amount is positive.
int padding_amount =
Expand All @@ -133,7 +129,7 @@ class PaddingWriter {
return 0;
}

int write_right_padding(Writer *writer, size_t total_digits) {
LIBC_INLINE int write_right_padding(Writer *writer, size_t total_digits) {
// If and only if the conversion is left justified, there may be trailing
// spaces.
int padding_amount =
Expand Down Expand Up @@ -170,7 +166,7 @@ class FloatWriter {
Writer *writer; // Writes to the final output.
PaddingWriter padding_writer; // Handles prefixes/padding, uses total_digits.

int flush_buffer(bool round_up_max_blocks = false) {
LIBC_INLINE int flush_buffer(bool round_up_max_blocks = false) {
const char MAX_BLOCK_DIGIT = (round_up_max_blocks ? '0' : '9');

// Write the most recent buffered block, and mark has_written
Expand Down Expand Up @@ -249,17 +245,18 @@ class FloatWriter {
(sizeof(int) * 8));

public:
FloatWriter(Writer *init_writer, bool init_has_decimal_point,
const PaddingWriter &init_padding_writer)
LIBC_INLINE FloatWriter(Writer *init_writer, bool init_has_decimal_point,
const PaddingWriter &init_padding_writer)
: has_decimal_point(init_has_decimal_point), writer(init_writer),
padding_writer(init_padding_writer) {}

void init(size_t init_total_digits, size_t init_digits_before_decimal) {
LIBC_INLINE void init(size_t init_total_digits,
size_t init_digits_before_decimal) {
total_digits = init_total_digits;
digits_before_decimal = init_digits_before_decimal;
}

void write_first_block(BlockInt block, bool exp_format = false) {
LIBC_INLINE void write_first_block(BlockInt block, bool exp_format = false) {
const DecimalString buf(block);
const cpp::string_view int_to_str = buf.view();
size_t digits_buffered = int_to_str.size();
Expand All @@ -280,7 +277,7 @@ class FloatWriter {
}
}

int write_middle_block(BlockInt block) {
LIBC_INLINE int write_middle_block(BlockInt block) {
if (block == MAX_BLOCK) { // Buffer max blocks in case of rounding
++max_block_count;
} else { // If a non-max block has been found
Expand All @@ -301,9 +298,9 @@ class FloatWriter {
return 0;
}

int write_last_block(BlockInt block, size_t block_digits,
RoundDirection round, int exponent = 0,
char exp_char = '\0') {
LIBC_INLINE int write_last_block(BlockInt block, size_t block_digits,
RoundDirection round, int exponent = 0,
char exp_char = '\0') {
bool has_exp = (exp_char != '\0');

char end_buff[BLOCK_SIZE];
Expand Down Expand Up @@ -458,13 +455,13 @@ class FloatWriter {
return WRITE_OK;
}

int write_zeroes(uint32_t num_zeroes) {
LIBC_INLINE int write_zeroes(uint32_t num_zeroes) {
RET_IF_RESULT_NEGATIVE(flush_buffer());
RET_IF_RESULT_NEGATIVE(writer->write('0', num_zeroes));
return 0;
}

int right_pad() {
LIBC_INLINE int right_pad() {
return padding_writer.write_right_padding(writer, total_digits);
}
};
Expand Down
2 changes: 0 additions & 2 deletions libc/src/stdio/printf_core/float_hex_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_HEX_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/float_inf_nan_converter.h"
Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/printf_core/float_inf_nan_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_INF_NAN_CONVERTER_H

#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/printf_core/int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/__support/integer_to_string.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
Expand Down
3 changes: 0 additions & 3 deletions libc/src/stdio/printf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PARSER_H

#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/arg_list.h"
#include "src/__support/common.h"
#include "src/__support/str_to_integer.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/printf_config.h"
Expand Down
3 changes: 0 additions & 3 deletions libc/src/stdio/printf_core/ptr_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/int_converter.h"
#include "src/stdio/printf_core/string_converter.h"
Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/printf_core/string_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/printf_core/write_int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H

#include "src/__support/CPP/limits.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"

Expand Down
2 changes: 0 additions & 2 deletions libc/src/stdio/printf_core/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

#include "writer.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/optimization.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/memory_utils/inline_memset.h"
#include <stddef.h>

Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/printf_core/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct WriteBuffer {
// write as much of new_str to the buffer as it can. The current position in
// the buffer will be reset iff stream_writer is called. Calling this with an
// empty string will flush the buffer if relevant.
int overflow_write(cpp::string_view new_str) {
LIBC_INLINE int overflow_write(cpp::string_view new_str) {
// If there is a stream_writer, write the contents of the buffer, then
// new_str, then clear the buffer.
if (stream_writer != nullptr) {
Expand Down
2 changes: 0 additions & 2 deletions libc/src/stdio/scanf_core/converter_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H

#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/str_to_float.h"
#include "src/stdio/scanf_core/core_structs.h"
#include "src/stdio/scanf_core/reader.h"

#include <stddef.h>

Expand Down
3 changes: 1 addition & 2 deletions libc/src/stdio/scanf_core/core_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "src/__support/CPP/bitset.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/FPUtil/FPBits.h"

#include <inttypes.h>
#include <stddef.h>
Expand Down Expand Up @@ -46,7 +45,7 @@ struct FormatSection {

cpp::bitset<256> scan_set;

bool operator==(const FormatSection &other) {
LIBC_INLINE bool operator==(const FormatSection &other) {
if (has_conv != other.has_conv)
return false;

Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/scanf_core/current_pos_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CURRENT_POS_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CURRENT_POS_CONVERTER_H

#include "src/__support/common.h"
#include "src/stdio/scanf_core/converter_utils.h"
#include "src/stdio/scanf_core/core_structs.h"
#include "src/stdio/scanf_core/reader.h"
Expand Down
1 change: 0 additions & 1 deletion libc/src/stdio/scanf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_PARSER_H

#include "src/__support/arg_list.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/str_to_integer.h"
#include "src/stdio/scanf_core/core_structs.h"
Expand Down
9 changes: 5 additions & 4 deletions libc/src/stdio/scanf_core/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class Reader {

public:
// TODO: Set buff_len with a proper constant
Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
LIBC_INLINE Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}

Reader(void *stream, StreamGetc stream_getc_in, StreamUngetc stream_ungetc_in,
ReadBuffer *stream_buffer = nullptr)
LIBC_INLINE Reader(void *stream, StreamGetc stream_getc_in,
StreamUngetc stream_ungetc_in,
ReadBuffer *stream_buffer = nullptr)
: rb(stream_buffer), input_stream(stream), stream_getc(stream_getc_in),
stream_ungetc(stream_ungetc_in) {}

Expand All @@ -63,7 +64,7 @@ class Reader {
// this is a file reader, else c is ignored.
void ungetc(char c);

size_t chars_read() { return cur_chars_read; }
LIBC_INLINE size_t chars_read() { return cur_chars_read; }
};

} // namespace scanf_core
Expand Down