Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / stdio / printf_core / converter.cpp
blob74a36cbf7432fbe53e447b47ffb77b48983048e1
1 //===-- Format specifier converter implmentation 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 //===----------------------------------------------------------------------===//
9 #include "src/stdio/printf_core/converter.h"
11 #include "src/stdio/printf_core/core_structs.h"
12 #include "src/stdio/printf_core/writer.h"
14 // This option allows for replacing all of the conversion functions with custom
15 // replacements. This allows conversions to be replaced at compile time.
16 #ifndef LIBC_COPT_PRINTF_CONV_ATLAS
17 #include "src/stdio/printf_core/converter_atlas.h"
18 #else
19 #include LIBC_COPT_PRINTF_CONV_ATLAS
20 #endif
22 #include <stddef.h>
24 namespace LIBC_NAMESPACE {
25 namespace printf_core {
27 int convert(Writer *writer, const FormatSection &to_conv) {
28 if (!to_conv.has_conv)
29 return writer->write(to_conv.raw_string);
31 #if !defined(LIBC_COPT_PRINTF_DISABLE_FLOAT) && \
32 defined(LIBC_COPT_PRINTF_HEX_LONG_DOUBLE)
33 if (to_conv.length_modifier == LengthModifier::L) {
34 switch (to_conv.conv_name) {
35 case 'f':
36 case 'F':
37 case 'e':
38 case 'E':
39 case 'g':
40 case 'G':
41 return convert_float_hex_exp(writer, to_conv);
42 default:
43 break;
46 #endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
48 switch (to_conv.conv_name) {
49 case '%':
50 return writer->write("%");
51 case 'c':
52 return convert_char(writer, to_conv);
53 case 's':
54 return convert_string(writer, to_conv);
55 case 'd':
56 case 'i':
57 case 'u':
58 case 'o':
59 case 'x':
60 case 'X':
61 return convert_int(writer, to_conv);
62 #ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
63 case 'f':
64 case 'F':
65 return convert_float_decimal(writer, to_conv);
66 case 'e':
67 case 'E':
68 return convert_float_dec_exp(writer, to_conv);
69 case 'a':
70 case 'A':
71 return convert_float_hex_exp(writer, to_conv);
72 case 'g':
73 case 'G':
74 return convert_float_dec_auto(writer, to_conv);
75 #endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
76 #ifndef LIBC_COPT_PRINTF_DISABLE_WRITE_INT
77 case 'n':
78 return convert_write_int(writer, to_conv);
79 #endif // LIBC_COPT_PRINTF_DISABLE_WRITE_INT
80 case 'p':
81 return convert_pointer(writer, to_conv);
82 default:
83 return writer->write(to_conv.raw_string);
85 return -1;
88 } // namespace printf_core
89 } // namespace LIBC_NAMESPACE