Skip to content

Commit b37c048

Browse files
[libc][NFC] clean up printf_core and scanf_core (#74535)
Add LIBC_INLINE annotations to functions and fix variable cases within printf_core and scanf_core.
1 parent f94adfd commit b37c048

18 files changed

+31
-55
lines changed

libc/src/stdio/printf_core/char_converter.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H
1111

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

24-
constexpr int string_len = 1;
22+
constexpr int STRING_LEN = 1;
2523

2624
size_t padding_spaces =
27-
to_conv.min_width > string_len ? to_conv.min_width - string_len : 0;
25+
to_conv.min_width > STRING_LEN ? to_conv.min_width - STRING_LEN : 0;
2826

2927
// If the padding is on the left side, write the spaces first.
3028
if (padding_spaces > 0 &&

libc/src/stdio/printf_core/converter_utils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H
1111

1212
#include "src/__support/CPP/limits.h"
13-
#include "src/__support/common.h"
1413
#include "src/stdio/printf_core/core_structs.h"
1514

1615
#include <inttypes.h>

libc/src/stdio/printf_core/core_structs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct FormatSection {
5353

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

@@ -93,11 +93,11 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
9393
if constexpr (cpp::is_same_v<T, void>) {
9494
return TypeDesc{0, PrimaryType::Unknown};
9595
} else {
96-
constexpr bool isPointer = cpp::is_pointer_v<T>;
97-
constexpr bool isFloat = cpp::is_floating_point_v<T>;
98-
return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer
99-
: isFloat ? PrimaryType::Float
100-
: PrimaryType::Integer};
96+
constexpr bool IS_POINTER = cpp::is_pointer_v<T>;
97+
constexpr bool IS_FLOAT = cpp::is_floating_point_v<T>;
98+
return TypeDesc{sizeof(T), IS_POINTER ? PrimaryType::Pointer
99+
: IS_FLOAT ? PrimaryType::Float
100+
: PrimaryType::Integer};
101101
}
102102
}
103103

libc/src/stdio/printf_core/float_dec_converter.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_DEC_CONVERTER_H
1111

1212
#include "src/__support/CPP/string_view.h"
13-
#include "src/__support/FPUtil/FEnvImpl.h"
1413
#include "src/__support/FPUtil/FPBits.h"
1514
#include "src/__support/FPUtil/FloatProperties.h"
1615
#include "src/__support/FPUtil/rounding_mode.h"
17-
#include "src/__support/UInt.h"
18-
#include "src/__support/UInt128.h"
19-
#include "src/__support/common.h"
2016
#include "src/__support/float_to_string.h"
2117
#include "src/__support/integer_to_string.h"
2218
#include "src/__support/libc_assert.h"
@@ -103,14 +99,14 @@ class PaddingWriter {
10399
size_t min_width = 0;
104100

105101
public:
106-
PaddingWriter() {}
107-
PaddingWriter(const FormatSection &to_conv, char init_sign_char)
102+
LIBC_INLINE PaddingWriter() {}
103+
LIBC_INLINE PaddingWriter(const FormatSection &to_conv, char init_sign_char)
108104
: left_justified((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) > 0),
109105
leading_zeroes((to_conv.flags & FormatFlags::LEADING_ZEROES) > 0),
110106
sign_char(init_sign_char),
111107
min_width(to_conv.min_width > 0 ? to_conv.min_width : 0) {}
112108

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

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

173-
int flush_buffer(bool round_up_max_blocks = false) {
169+
LIBC_INLINE int flush_buffer(bool round_up_max_blocks = false) {
174170
const char MAX_BLOCK_DIGIT = (round_up_max_blocks ? '0' : '9');
175171

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

251247
public:
252-
FloatWriter(Writer *init_writer, bool init_has_decimal_point,
253-
const PaddingWriter &init_padding_writer)
248+
LIBC_INLINE FloatWriter(Writer *init_writer, bool init_has_decimal_point,
249+
const PaddingWriter &init_padding_writer)
254250
: has_decimal_point(init_has_decimal_point), writer(init_writer),
255251
padding_writer(init_padding_writer) {}
256252

257-
void init(size_t init_total_digits, size_t init_digits_before_decimal) {
253+
LIBC_INLINE void init(size_t init_total_digits,
254+
size_t init_digits_before_decimal) {
258255
total_digits = init_total_digits;
259256
digits_before_decimal = init_digits_before_decimal;
260257
}
261258

262-
void write_first_block(BlockInt block, bool exp_format = false) {
259+
LIBC_INLINE void write_first_block(BlockInt block, bool exp_format = false) {
263260
const DecimalString buf(block);
264261
const cpp::string_view int_to_str = buf.view();
265262
size_t digits_buffered = int_to_str.size();
@@ -280,7 +277,7 @@ class FloatWriter {
280277
}
281278
}
282279

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

304-
int write_last_block(BlockInt block, size_t block_digits,
305-
RoundDirection round, int exponent = 0,
306-
char exp_char = '\0') {
301+
LIBC_INLINE int write_last_block(BlockInt block, size_t block_digits,
302+
RoundDirection round, int exponent = 0,
303+
char exp_char = '\0') {
307304
bool has_exp = (exp_char != '\0');
308305

309306
char end_buff[BLOCK_SIZE];
@@ -458,13 +455,13 @@ class FloatWriter {
458455
return WRITE_OK;
459456
}
460457

461-
int write_zeroes(uint32_t num_zeroes) {
458+
LIBC_INLINE int write_zeroes(uint32_t num_zeroes) {
462459
RET_IF_RESULT_NEGATIVE(flush_buffer());
463460
RET_IF_RESULT_NEGATIVE(writer->write('0', num_zeroes));
464461
return 0;
465462
}
466463

467-
int right_pad() {
464+
LIBC_INLINE int right_pad() {
468465
return padding_writer.write_right_padding(writer, total_digits);
469466
}
470467
};

libc/src/stdio/printf_core/float_hex_converter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_HEX_CONVERTER_H
1111

1212
#include "src/__support/CPP/string_view.h"
13-
#include "src/__support/FPUtil/FEnvImpl.h"
1413
#include "src/__support/FPUtil/FPBits.h"
1514
#include "src/__support/FPUtil/rounding_mode.h"
16-
#include "src/__support/common.h"
1715
#include "src/stdio/printf_core/converter_utils.h"
1816
#include "src/stdio/printf_core/core_structs.h"
1917
#include "src/stdio/printf_core/float_inf_nan_converter.h"

libc/src/stdio/printf_core/float_inf_nan_converter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_INF_NAN_CONVERTER_H
1111

1212
#include "src/__support/FPUtil/FPBits.h"
13-
#include "src/__support/common.h"
1413
#include "src/stdio/printf_core/converter_utils.h"
1514
#include "src/stdio/printf_core/core_structs.h"
1615
#include "src/stdio/printf_core/writer.h"

libc/src/stdio/printf_core/int_converter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "src/__support/CPP/span.h"
1313
#include "src/__support/CPP/string_view.h"
14-
#include "src/__support/common.h"
1514
#include "src/__support/integer_to_string.h"
1615
#include "src/stdio/printf_core/converter_utils.h"
1716
#include "src/stdio/printf_core/core_structs.h"

libc/src/stdio/printf_core/parser.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PARSER_H
1111

1212
#include "src/__support/CPP/optional.h"
13-
#include "src/__support/CPP/type_traits.h"
14-
#include "src/__support/arg_list.h"
15-
#include "src/__support/common.h"
1613
#include "src/__support/str_to_integer.h"
1714
#include "src/stdio/printf_core/core_structs.h"
1815
#include "src/stdio/printf_core/printf_config.h"

libc/src/stdio/printf_core/ptr_converter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
1111

12-
#include "src/__support/CPP/string_view.h"
13-
#include "src/__support/common.h"
14-
#include "src/stdio/printf_core/converter_utils.h"
1512
#include "src/stdio/printf_core/core_structs.h"
1613
#include "src/stdio/printf_core/int_converter.h"
1714
#include "src/stdio/printf_core/string_converter.h"

libc/src/stdio/printf_core/string_converter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_CONVERTER_H
1111

1212
#include "src/__support/CPP/string_view.h"
13-
#include "src/__support/common.h"
1413
#include "src/stdio/printf_core/converter_utils.h"
1514
#include "src/stdio/printf_core/core_structs.h"
1615
#include "src/stdio/printf_core/writer.h"

libc/src/stdio/printf_core/write_int_converter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
1111

12-
#include "src/__support/CPP/limits.h"
1312
#include "src/stdio/printf_core/core_structs.h"
1413
#include "src/stdio/printf_core/writer.h"
1514

libc/src/stdio/printf_core/writer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
#include "writer.h"
1010
#include "src/__support/CPP/string_view.h"
11-
#include "src/__support/macros/optimization.h"
1211
#include "src/stdio/printf_core/core_structs.h"
13-
#include "src/string/memory_utils/inline_memcpy.h"
1412
#include "src/string/memory_utils/inline_memset.h"
1513
#include <stddef.h>
1614

libc/src/stdio/printf_core/writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct WriteBuffer {
4545
// write as much of new_str to the buffer as it can. The current position in
4646
// the buffer will be reset iff stream_writer is called. Calling this with an
4747
// empty string will flush the buffer if relevant.
48-
int overflow_write(cpp::string_view new_str) {
48+
LIBC_INLINE int overflow_write(cpp::string_view new_str) {
4949
// If there is a stream_writer, write the contents of the buffer, then
5050
// new_str, then clear the buffer.
5151
if (stream_writer != nullptr) {

libc/src/stdio/scanf_core/converter_utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H
1010
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H
1111

12-
#include "src/__support/common.h"
1312
#include "src/__support/ctype_utils.h"
1413
#include "src/__support/str_to_float.h"
1514
#include "src/stdio/scanf_core/core_structs.h"
16-
#include "src/stdio/scanf_core/reader.h"
1715

1816
#include <stddef.h>
1917

libc/src/stdio/scanf_core/core_structs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "src/__support/CPP/bitset.h"
1313
#include "src/__support/CPP/string_view.h"
14-
#include "src/__support/FPUtil/FPBits.h"
1514

1615
#include <inttypes.h>
1716
#include <stddef.h>
@@ -46,7 +45,7 @@ struct FormatSection {
4645

4746
cpp::bitset<256> scan_set;
4847

49-
bool operator==(const FormatSection &other) {
48+
LIBC_INLINE bool operator==(const FormatSection &other) {
5049
if (has_conv != other.has_conv)
5150
return false;
5251

libc/src/stdio/scanf_core/current_pos_converter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CURRENT_POS_CONVERTER_H
1010
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CURRENT_POS_CONVERTER_H
1111

12-
#include "src/__support/common.h"
1312
#include "src/stdio/scanf_core/converter_utils.h"
1413
#include "src/stdio/scanf_core/core_structs.h"
1514
#include "src/stdio/scanf_core/reader.h"

libc/src/stdio/scanf_core/parser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_PARSER_H
1111

1212
#include "src/__support/arg_list.h"
13-
#include "src/__support/common.h"
1413
#include "src/__support/ctype_utils.h"
1514
#include "src/__support/str_to_integer.h"
1615
#include "src/stdio/scanf_core/core_structs.h"

libc/src/stdio/scanf_core/reader.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ class Reader {
3838

3939
public:
4040
// TODO: Set buff_len with a proper constant
41-
Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
41+
LIBC_INLINE Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
4242

43-
Reader(void *stream, StreamGetc stream_getc_in, StreamUngetc stream_ungetc_in,
44-
ReadBuffer *stream_buffer = nullptr)
43+
LIBC_INLINE Reader(void *stream, StreamGetc stream_getc_in,
44+
StreamUngetc stream_ungetc_in,
45+
ReadBuffer *stream_buffer = nullptr)
4546
: rb(stream_buffer), input_stream(stream), stream_getc(stream_getc_in),
4647
stream_ungetc(stream_ungetc_in) {}
4748

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

66-
size_t chars_read() { return cur_chars_read; }
67+
LIBC_INLINE size_t chars_read() { return cur_chars_read; }
6768
};
6869

6970
} // namespace scanf_core

0 commit comments

Comments
 (0)