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/__support/CPP/Limits.h" 13 #include "src/stdio/printf_core/core_structs.h" 14 #include "src/stdio/printf_core/writer.h" 15 16 #include <inttypes.h> 17 #include <stddef.h> 18 19 namespace __llvm_libc { 20 namespace printf_core { 21 convert_write_int(Writer * writer,const FormatSection & to_conv)22int inline convert_write_int(Writer *writer, const FormatSection &to_conv) { 23 24 // This is an additional check added by LLVM-libc. The reason it returns -3 is 25 // because printf uses negative return values for errors, and -1 and -2 are 26 // already in use by the file_writer class for file errors. 27 if (to_conv.conv_val_ptr == nullptr) 28 return NULLPTR_WRITE_ERROR; 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) = written; 45 break; 46 case LengthModifier::hh: 47 *reinterpret_cast<signed char *>(to_conv.conv_val_ptr) = written; 48 break; 49 case LengthModifier::z: 50 *reinterpret_cast<size_t *>(to_conv.conv_val_ptr) = written; 51 break; 52 case LengthModifier::t: 53 *reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written; 54 break; 55 case LengthModifier::j: 56 *reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written; 57 break; 58 } 59 return WRITE_OK; 60 } 61 62 } // namespace printf_core 63 } // namespace __llvm_libc 64 65 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H 66