1 //===-- Write integer Converter for printf ----------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H 10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H 11 12 #include "src/stdio/printf_core/core_structs.h" 13 #include "src/stdio/printf_core/writer.h" 14 15 #include <inttypes.h> 16 #include <stddef.h> 17 18 namespace LIBC_NAMESPACE { 19 namespace printf_core { 20 convert_write_int(Writer * writer,const FormatSection & to_conv)21LIBC_INLINE int convert_write_int(Writer *writer, 22 const FormatSection &to_conv) { 23 24 #ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS 25 // This is an additional check added by LLVM-libc. 26 if (to_conv.conv_val_ptr == nullptr) 27 return NULLPTR_WRITE_ERROR; 28 #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS 29 30 int written = writer->get_chars_written(); 31 32 switch (to_conv.length_modifier) { 33 case LengthModifier::none: 34 *reinterpret_cast<int *>(to_conv.conv_val_ptr) = written; 35 break; 36 case LengthModifier::l: 37 *reinterpret_cast<long *>(to_conv.conv_val_ptr) = written; 38 break; 39 case LengthModifier::ll: 40 case LengthModifier::L: 41 *reinterpret_cast<long long *>(to_conv.conv_val_ptr) = written; 42 break; 43 case LengthModifier::h: 44 *reinterpret_cast<short *>(to_conv.conv_val_ptr) = 45 static_cast<short>(written); 46 break; 47 case LengthModifier::hh: 48 *reinterpret_cast<signed char *>(to_conv.conv_val_ptr) = 49 static_cast<signed char>(written); 50 break; 51 case LengthModifier::z: 52 *reinterpret_cast<size_t *>(to_conv.conv_val_ptr) = written; 53 break; 54 case LengthModifier::t: 55 *reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written; 56 break; 57 case LengthModifier::j: 58 case LengthModifier::w: 59 case LengthModifier::wf: 60 *reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written; 61 break; 62 } 63 return WRITE_OK; 64 } 65 66 } // namespace printf_core 67 } // namespace LIBC_NAMESPACE 68 69 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H 70