From 848c700b66f569adc893518c37d500f80a5412e2 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Fri, 3 Feb 2023 13:13:23 -0800 Subject: [PATCH] [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 --- libc/src/stdio/printf_core/core_structs.h | 24 ++++++++++++++ libc/src/stdio/printf_core/parser.cpp | 53 ++++++++++++++++--------------- libc/src/stdio/printf_core/parser.h | 27 +--------------- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/libc/src/stdio/printf_core/core_structs.h b/libc/src/stdio/printf_core/core_structs.h index fb8c8bb..47d488c 100644 --- a/libc/src/stdio/printf_core/core_structs.h +++ b/libc/src/stdio/printf_core/core_structs.h @@ -77,6 +77,30 @@ struct FormatSection { } }; +enum PrimaryType : uint8_t { Integer = 0, Float = 1, Pointer = 2 }; + +// TypeDesc stores the information about a type that is relevant to printf in +// a relatively compact manner. +struct TypeDesc { + uint8_t size; + PrimaryType primary_type; + LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const { + return (size == other.size) && (primary_type == other.primary_type); + } +}; + +template LIBC_INLINE constexpr TypeDesc type_desc_from_type() { + if constexpr (cpp::is_same_v) { + return TypeDesc{0, PrimaryType::Integer}; + } else { + constexpr bool isPointer = cpp::is_pointer_v; + constexpr bool isFloat = cpp::is_floating_point_v; + return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer + : isFloat ? PrimaryType::Float + : PrimaryType::Integer}; + } +} + // This is the value to be returned by conversions when no error has occurred. constexpr int WRITE_OK = 0; // These are the printf return values for when an error has occurred. They are diff --git a/libc/src/stdio/printf_core/parser.cpp b/libc/src/stdio/printf_core/parser.cpp index 0637eda..ef2063a 100644 --- a/libc/src/stdio/printf_core/parser.cpp +++ b/libc/src/stdio/printf_core/parser.cpp @@ -17,6 +17,7 @@ #include "src/__support/FPUtil/FPBits.h" #include "src/__support/ctype_utils.h" #include "src/__support/str_to_integer.h" +#include "src/stdio/printf_core/core_structs.h" namespace __llvm_libc { namespace printf_core { @@ -246,7 +247,7 @@ size_t Parser::parse_index(size_t *local_pos) { return 0; } -Parser::TypeDesc Parser::get_type_desc(size_t index) { +TypeDesc Parser::get_type_desc(size_t index) { // index mode is assumed, and the indicies start at 1, so an index // of 0 is invalid. size_t local_pos = 0; @@ -266,9 +267,9 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { ++local_pos; size_t width_index = parse_index(&local_pos); - set_type_desc(width_index, get_type_desc()); + set_type_desc(width_index, type_desc_from_type()); if (width_index == index) - return get_type_desc(); + return type_desc_from_type(); } else if (internal::isdigit(str[local_pos])) { while (internal::isdigit(str[local_pos])) @@ -282,9 +283,9 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { ++local_pos; size_t precision_index = parse_index(&local_pos); - set_type_desc(precision_index, get_type_desc()); + set_type_desc(precision_index, type_desc_from_type()); if (precision_index == index) - return get_type_desc(); + return type_desc_from_type(); } else if (internal::isdigit(str[local_pos])) { while (internal::isdigit(str[local_pos])) @@ -303,13 +304,13 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { continue; } - TypeDesc conv_size = get_type_desc(); + TypeDesc conv_size = type_desc_from_type(); switch (str[local_pos]) { case ('%'): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case ('c'): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case ('d'): case ('i'): @@ -321,24 +322,24 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { case (LengthModifier::hh): case (LengthModifier::h): case (LengthModifier::none): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::l): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::ll): case (LengthModifier::L): // This isn't in the standard, but is in other // libc implementations. - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::j): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::z): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::t): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; } break; @@ -352,9 +353,9 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { case ('g'): case ('G'): if (lm != LengthModifier::L) - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); else - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; #endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT #ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT @@ -362,10 +363,10 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { #endif // LLVM_LIBC_PRINTF_DISABLE_WRITE_INT case ('p'): case ('s'): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; default: - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; } @@ -381,7 +382,7 @@ Parser::TypeDesc Parser::get_type_desc(size_t index) { // If there is no size for the requested index, then just guess that it's an // int. - return get_type_desc(); + return type_desc_from_type(); } void Parser::args_to_index(size_t index) { @@ -391,26 +392,26 @@ void Parser::args_to_index(size_t index) { } while (args_index < index) { - Parser::TypeDesc cur_type_desc = get_type_desc(); + TypeDesc cur_type_desc = type_desc_from_type(); if (args_index <= DESC_ARR_LEN) cur_type_desc = desc_arr[args_index - 1]; - if (cur_type_desc == get_type_desc()) + if (cur_type_desc == type_desc_from_type()) cur_type_desc = get_type_desc(args_index); - if (cur_type_desc == get_type_desc()) + if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); #ifndef LLVM_LIBC_PRINTF_DISABLE_FLOAT // Floating point numbers are stored separately from the other arguments. - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); #endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT // pointers may be stored separately from normal values. - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); else args_cur.next_var(); diff --git a/libc/src/stdio/printf_core/parser.h b/libc/src/stdio/printf_core/parser.h index 10787678..6bb0064 100644 --- a/libc/src/stdio/printf_core/parser.h +++ b/libc/src/stdio/printf_core/parser.h @@ -33,18 +33,6 @@ class Parser { internal::ArgList args_start; size_t args_index = 1; - enum PrimaryType : uint8_t { Integer = 0, Float = 1, Pointer = 2 }; - - // TypeDesc stores the information about a type that is relevant to printf in - // a relatively compact manner. - struct TypeDesc { - uint8_t size; - PrimaryType primary_type; - LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const { - return (size == other.size) && (primary_type == other.primary_type); - } - }; - // Defined in printf_config.h static constexpr size_t DESC_ARR_LEN = LLVM_LIBC_PRINTF_INDEX_ARR_LEN; @@ -105,19 +93,6 @@ private: // local_pos. size_t parse_index(size_t *local_pos); - template LIBC_INLINE static constexpr TypeDesc get_type_desc() { - if constexpr (cpp::is_same_v) { - return TypeDesc{0, PrimaryType::Integer}; - } else { - constexpr bool isPointer = cpp::is_same_v; - constexpr bool isFloat = - cpp::is_same_v || cpp::is_same_v; - return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer - : isFloat ? PrimaryType::Float - : PrimaryType::Integer}; - } - } - LIBC_INLINE void set_type_desc(size_t index, TypeDesc value) { if (index != 0 && index <= DESC_ARR_LEN) desc_arr[index - 1] = value; @@ -130,7 +105,7 @@ private: if (!(index == 0 || index == args_index)) args_to_index(index); - set_type_desc(index, get_type_desc()); + set_type_desc(index, type_desc_from_type()); ++args_index; return get_next_arg_value(); -- 2.7.4