1 //===-- Pointer 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_PTR_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
11 
12 #include "src/stdio/printf_core/converter_utils.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 #include "src/stdio/printf_core/hex_converter.h"
15 #include "src/stdio/printf_core/writer.h"
16 
17 namespace __llvm_libc {
18 namespace printf_core {
19 
convert_pointer(Writer * writer,const FormatSection & to_conv)20 int inline convert_pointer(Writer *writer, const FormatSection &to_conv) {
21   if (to_conv.conv_val_ptr == (void *)(nullptr)) {
22     const char ZERO_STR[] = "(nullptr)";
23     // subtract 1 from sizeof to remove the null byte at the end.
24     RET_IF_RESULT_NEGATIVE(writer->write(ZERO_STR, sizeof(ZERO_STR) - 1));
25   } else {
26     FormatSection hex_conv;
27     hex_conv.has_conv = true;
28     hex_conv.conv_name = 'x';
29     hex_conv.flags = FormatFlags::ALTERNATE_FORM;
30     hex_conv.conv_val_raw = reinterpret_cast<uintptr_t>(to_conv.conv_val_ptr);
31     return convert_hex(writer, hex_conv);
32   }
33   return WRITE_OK;
34 }
35 
36 } // namespace printf_core
37 } // namespace __llvm_libc
38 
39 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
40