Skip to content

Commit 848c700

Browse files
[libc][NFC] reorganize structs in printf
Previously the type description structs were defined in the parser. For the fuzzing targets we'll need to use those, so I've moved them into core_structs.h. Additionally I've renamed the function for determining the TypeDesc from a given type. Previously it shared its name with get_type_desc which is a related but separate function that is a part of the parser. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D143595
1 parent 7fb9bbe commit 848c700

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

libc/src/stdio/printf_core/core_structs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ struct FormatSection {
7777
}
7878
};
7979

80+
enum PrimaryType : uint8_t { Integer = 0, Float = 1, Pointer = 2 };
81+
82+
// TypeDesc stores the information about a type that is relevant to printf in
83+
// a relatively compact manner.
84+
struct TypeDesc {
85+
uint8_t size;
86+
PrimaryType primary_type;
87+
LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const {
88+
return (size == other.size) && (primary_type == other.primary_type);
89+
}
90+
};
91+
92+
template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
93+
if constexpr (cpp::is_same_v<T, void>) {
94+
return TypeDesc{0, PrimaryType::Integer};
95+
} 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};
101+
}
102+
}
103+
80104
// This is the value to be returned by conversions when no error has occurred.
81105
constexpr int WRITE_OK = 0;
82106
// These are the printf return values for when an error has occurred. They are

libc/src/stdio/printf_core/parser.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/__support/FPUtil/FPBits.h"
1818
#include "src/__support/ctype_utils.h"
1919
#include "src/__support/str_to_integer.h"
20+
#include "src/stdio/printf_core/core_structs.h"
2021

2122
namespace __llvm_libc {
2223
namespace printf_core {
@@ -246,7 +247,7 @@ size_t Parser::parse_index(size_t *local_pos) {
246247
return 0;
247248
}
248249

249-
Parser::TypeDesc Parser::get_type_desc(size_t index) {
250+
TypeDesc Parser::get_type_desc(size_t index) {
250251
// index mode is assumed, and the indicies start at 1, so an index
251252
// of 0 is invalid.
252253
size_t local_pos = 0;
@@ -266,9 +267,9 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) {
266267
++local_pos;
267268

268269
size_t width_index = parse_index(&local_pos);
269-
set_type_desc(width_index, get_type_desc<int>());
270+
set_type_desc(width_index, type_desc_from_type<int>());
270271
if (width_index == index)
271-
return get_type_desc<int>();
272+
return type_desc_from_type<int>();
272273

273274
} else if (internal::isdigit(str[local_pos])) {
274275
while (internal::isdigit(str[local_pos]))
@@ -282,9 +283,9 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) {
282283
++local_pos;
283284

284285
size_t precision_index = parse_index(&local_pos);
285-
set_type_desc(precision_index, get_type_desc<int>());
286+
set_type_desc(precision_index, type_desc_from_type<int>());
286287
if (precision_index == index)
287-
return get_type_desc<int>();
288+
return type_desc_from_type<int>();
288289

289290
} else if (internal::isdigit(str[local_pos])) {
290291
while (internal::isdigit(str[local_pos]))
@@ -303,13 +304,13 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) {
303304
continue;
304305
}
305306

306-
TypeDesc conv_size = get_type_desc<void>();
307+
TypeDesc conv_size = type_desc_from_type<void>();
307308
switch (str[local_pos]) {
308309
case ('%'):
309-
conv_size = get_type_desc<void>();
310+
conv_size = type_desc_from_type<void>();
310311
break;
311312
case ('c'):
312-
conv_size = get_type_desc<int>();
313+
conv_size = type_desc_from_type<int>();
313314
break;
314315
case ('d'):
315316
case ('i'):
@@ -321,24 +322,24 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) {
321322
case (LengthModifier::hh):
322323
case (LengthModifier::h):
323324
case (LengthModifier::none):
324-
conv_size = get_type_desc<int>();
325+
conv_size = type_desc_from_type<int>();
325326
break;
326327
case (LengthModifier::l):
327-
conv_size = get_type_desc<long>();
328+
conv_size = type_desc_from_type<long>();
328329
break;
329330
case (LengthModifier::ll):
330331
case (LengthModifier::L): // This isn't in the standard, but is in other
331332
// libc implementations.
332-
conv_size = get_type_desc<long long>();
333+
conv_size = type_desc_from_type<long long>();
333334
break;
334335
case (LengthModifier::j):
335-
conv_size = get_type_desc<intmax_t>();
336+
conv_size = type_desc_from_type<intmax_t>();
336337
break;
337338
case (LengthModifier::z):
338-
conv_size = get_type_desc<size_t>();
339+
conv_size = type_desc_from_type<size_t>();
339340
break;
340341
case (LengthModifier::t):
341-
conv_size = get_type_desc<ptrdiff_t>();
342+
conv_size = type_desc_from_type<ptrdiff_t>();
342343
break;
343344
}
344345
break;
@@ -352,20 +353,20 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) {
352353
case ('g'):
353354
case ('G'):
354355
if (lm != LengthModifier::L)
355-
conv_size = get_type_desc<double>();
356+
conv_size = type_desc_from_type<double>();
356357
else
357-
conv_size = get_type_desc<long double>();
358+
conv_size = type_desc_from_type<long double>();
358359
break;
359360
#endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT
360361
#ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
361362
case ('n'):
362363
#endif // LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
363364
case ('p'):
364365
case ('s'):
365-
conv_size = get_type_desc<void *>();
366+
conv_size = type_desc_from_type<void *>();
366367
break;
367368
default:
368-
conv_size = get_type_desc<int>();
369+
conv_size = type_desc_from_type<int>();
369370
break;
370371
}
371372

@@ -381,7 +382,7 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) {
381382

382383
// If there is no size for the requested index, then just guess that it's an
383384
// int.
384-
return get_type_desc<int>();
385+
return type_desc_from_type<int>();
385386
}
386387

387388
void Parser::args_to_index(size_t index) {
@@ -391,26 +392,26 @@ void Parser::args_to_index(size_t index) {
391392
}
392393

393394
while (args_index < index) {
394-
Parser::TypeDesc cur_type_desc = get_type_desc<void>();
395+
TypeDesc cur_type_desc = type_desc_from_type<void>();
395396
if (args_index <= DESC_ARR_LEN)
396397
cur_type_desc = desc_arr[args_index - 1];
397398

398-
if (cur_type_desc == get_type_desc<void>())
399+
if (cur_type_desc == type_desc_from_type<void>())
399400
cur_type_desc = get_type_desc(args_index);
400401

401-
if (cur_type_desc == get_type_desc<uint32_t>())
402+
if (cur_type_desc == type_desc_from_type<uint32_t>())
402403
args_cur.next_var<uint32_t>();
403-
else if (cur_type_desc == get_type_desc<uint64_t>())
404+
else if (cur_type_desc == type_desc_from_type<uint64_t>())
404405
args_cur.next_var<uint64_t>();
405406
#ifndef LLVM_LIBC_PRINTF_DISABLE_FLOAT
406407
// Floating point numbers are stored separately from the other arguments.
407-
else if (cur_type_desc == get_type_desc<double>())
408+
else if (cur_type_desc == type_desc_from_type<double>())
408409
args_cur.next_var<double>();
409-
else if (cur_type_desc == get_type_desc<long double>())
410+
else if (cur_type_desc == type_desc_from_type<long double>())
410411
args_cur.next_var<long double>();
411412
#endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT
412413
// pointers may be stored separately from normal values.
413-
else if (cur_type_desc == get_type_desc<void *>())
414+
else if (cur_type_desc == type_desc_from_type<void *>())
414415
args_cur.next_var<void *>();
415416
else
416417
args_cur.next_var<uint32_t>();

libc/src/stdio/printf_core/parser.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ class Parser {
3333
internal::ArgList args_start;
3434
size_t args_index = 1;
3535

36-
enum PrimaryType : uint8_t { Integer = 0, Float = 1, Pointer = 2 };
37-
38-
// TypeDesc stores the information about a type that is relevant to printf in
39-
// a relatively compact manner.
40-
struct TypeDesc {
41-
uint8_t size;
42-
PrimaryType primary_type;
43-
LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const {
44-
return (size == other.size) && (primary_type == other.primary_type);
45-
}
46-
};
47-
4836
// Defined in printf_config.h
4937
static constexpr size_t DESC_ARR_LEN = LLVM_LIBC_PRINTF_INDEX_ARR_LEN;
5038

@@ -105,19 +93,6 @@ class Parser {
10593
// local_pos.
10694
size_t parse_index(size_t *local_pos);
10795

108-
template <typename T> LIBC_INLINE static constexpr TypeDesc get_type_desc() {
109-
if constexpr (cpp::is_same_v<T, void>) {
110-
return TypeDesc{0, PrimaryType::Integer};
111-
} else {
112-
constexpr bool isPointer = cpp::is_same_v<T, void *>;
113-
constexpr bool isFloat =
114-
cpp::is_same_v<T, double> || cpp::is_same_v<T, long double>;
115-
return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer
116-
: isFloat ? PrimaryType::Float
117-
: PrimaryType::Integer};
118-
}
119-
}
120-
12196
LIBC_INLINE void set_type_desc(size_t index, TypeDesc value) {
12297
if (index != 0 && index <= DESC_ARR_LEN)
12398
desc_arr[index - 1] = value;
@@ -130,7 +105,7 @@ class Parser {
130105
if (!(index == 0 || index == args_index))
131106
args_to_index(index);
132107

133-
set_type_desc(index, get_type_desc<T>());
108+
set_type_desc(index, type_desc_from_type<T>());
134109

135110
++args_index;
136111
return get_next_arg_value<T>();

0 commit comments

Comments
 (0)