0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
147
|
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
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // This file implements the format() function, which can be used with other
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // LLVM subsystems to provide printf-style formatting. This gives all the power
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // and risk of printf. This can be used like this (with raw_ostreams as an
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 // example):
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 // OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 // Or if you prefer:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 // OS << format("mynumber: %4.5f\n", 1234.412);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #ifndef LLVM_SUPPORT_FORMAT_H
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 #define LLVM_SUPPORT_FORMAT_H
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24
|
120
|
25 #include "llvm/ADT/ArrayRef.h"
|
83
|
26 #include "llvm/ADT/STLExtras.h"
|
|
27 #include "llvm/ADT/StringRef.h"
|
|
28 #include "llvm/Support/DataTypes.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 #include <cassert>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 #include <cstdio>
|
83
|
31 #include <tuple>
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 namespace llvm {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34
|
77
|
35 /// This is a helper class used for handling formatted output. It is the
|
|
36 /// abstract base class of a templated derived class.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 class format_object_base {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 protected:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 const char *Fmt;
|
95
|
40 ~format_object_base() = default; // Disallow polymorphic deletion.
|
|
41 format_object_base(const format_object_base &) = default;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 virtual void home(); // Out of line virtual method.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
77
|
44 /// Call snprintf() for this object, on the given buffer and size.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 public:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 format_object_base(const char *fmt) : Fmt(fmt) {}
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49
|
77
|
50 /// Format the object into the specified buffer. On success, this returns
|
|
51 /// the length of the formatted string. If the buffer is too small, this
|
|
52 /// returns a length to retry with, which will be larger than BufferSize.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 unsigned print(char *Buffer, unsigned BufferSize) const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 assert(BufferSize && "Invalid buffer size!");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 // Print the string, leaving room for the terminating null.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 int N = snprint(Buffer, BufferSize);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 // VC++ and old GlibC return negative on overflow, just double the size.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 if (N < 0)
|
77
|
61 return BufferSize * 2;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62
|
77
|
63 // Other implementations yield number of bytes needed, not including the
|
|
64 // final '\0'.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 if (unsigned(N) >= BufferSize)
|
77
|
66 return N + 1;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 // Otherwise N is the length of output (not including the final '\0').
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 return N;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 };
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72
|
77
|
73 /// These are templated helper classes used by the format function that
|
120
|
74 /// capture the object to be formatted and the format string. When actually
|
77
|
75 /// printed, this synthesizes the string into a temporary buffer provided and
|
|
76 /// returns whether or not it is big enough.
|
|
77
|
120
|
78 // Helper to validate that format() parameters are scalars or pointers.
|
|
79 template <typename... Args> struct validate_format_parameters;
|
|
80 template <typename Arg, typename... Args>
|
|
81 struct validate_format_parameters<Arg, Args...> {
|
|
82 static_assert(std::is_scalar<Arg>::value,
|
|
83 "format can't be used with non fundamental / non pointer type");
|
|
84 validate_format_parameters() { validate_format_parameters<Args...>(); }
|
|
85 };
|
|
86 template <> struct validate_format_parameters<> {};
|
|
87
|
83
|
88 template <typename... Ts>
|
|
89 class format_object final : public format_object_base {
|
|
90 std::tuple<Ts...> Vals;
|
|
91
|
|
92 template <std::size_t... Is>
|
|
93 int snprint_tuple(char *Buffer, unsigned BufferSize,
|
|
94 index_sequence<Is...>) const {
|
|
95 #ifdef _MSC_VER
|
|
96 return _snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
|
|
97 #else
|
|
98 return snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
|
|
99 #endif
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 public:
|
83
|
103 format_object(const char *fmt, const Ts &... vals)
|
120
|
104 : format_object_base(fmt), Vals(vals...) {
|
|
105 validate_format_parameters<Ts...>();
|
|
106 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107
|
77
|
108 int snprint(char *Buffer, unsigned BufferSize) const override {
|
83
|
109 return snprint_tuple(Buffer, BufferSize, index_sequence_for<Ts...>());
|
77
|
110 }
|
|
111 };
|
|
112
|
|
113 /// These are helper functions used to produce formatted output. They use
|
|
114 /// template type deduction to construct the appropriate instance of the
|
|
115 /// format_object class to simplify their construction.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 ///
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
117 /// This is typically used like:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 /// \code
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 /// OS << format("%0.4f", myfloat) << '\n';
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 /// \endcode
|
77
|
121
|
83
|
122 template <typename... Ts>
|
|
123 inline format_object<Ts...> format(const char *Fmt, const Ts &... Vals) {
|
|
124 return format_object<Ts...>(Fmt, Vals...);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
126
|
121
|
127 /// This is a helper class for left_justify, right_justify, and center_justify.
|
83
|
128 class FormattedString {
|
121
|
129 public:
|
|
130 enum Justification { JustifyNone, JustifyLeft, JustifyRight, JustifyCenter };
|
|
131 FormattedString(StringRef S, unsigned W, Justification J)
|
|
132 : Str(S), Width(W), Justify(J) {}
|
|
133
|
|
134 private:
|
83
|
135 StringRef Str;
|
|
136 unsigned Width;
|
121
|
137 Justification Justify;
|
83
|
138 friend class raw_ostream;
|
|
139 };
|
|
140
|
|
141 /// left_justify - append spaces after string so total output is
|
|
142 /// \p Width characters. If \p Str is larger that \p Width, full string
|
|
143 /// is written with no padding.
|
|
144 inline FormattedString left_justify(StringRef Str, unsigned Width) {
|
121
|
145 return FormattedString(Str, Width, FormattedString::JustifyLeft);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147
|
83
|
148 /// right_justify - add spaces before string so total output is
|
|
149 /// \p Width characters. If \p Str is larger that \p Width, full string
|
|
150 /// is written with no padding.
|
|
151 inline FormattedString right_justify(StringRef Str, unsigned Width) {
|
121
|
152 return FormattedString(Str, Width, FormattedString::JustifyRight);
|
|
153 }
|
|
154
|
|
155 /// center_justify - add spaces before and after string so total output is
|
|
156 /// \p Width characters. If \p Str is larger that \p Width, full string
|
|
157 /// is written with no padding.
|
|
158 inline FormattedString center_justify(StringRef Str, unsigned Width) {
|
|
159 return FormattedString(Str, Width, FormattedString::JustifyCenter);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161
|
83
|
162 /// This is a helper class used for format_hex() and format_decimal().
|
|
163 class FormattedNumber {
|
|
164 uint64_t HexValue;
|
|
165 int64_t DecValue;
|
|
166 unsigned Width;
|
|
167 bool Hex;
|
|
168 bool Upper;
|
|
169 bool HexPrefix;
|
|
170 friend class raw_ostream;
|
95
|
171
|
83
|
172 public:
|
|
173 FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
|
|
174 bool Prefix)
|
|
175 : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
|
|
176 HexPrefix(Prefix) {}
|
|
177 };
|
|
178
|
|
179 /// format_hex - Output \p N as a fixed width hexadecimal. If number will not
|
|
180 /// fit in width, full number is still printed. Examples:
|
|
181 /// OS << format_hex(255, 4) => 0xff
|
|
182 /// OS << format_hex(255, 4, true) => 0xFF
|
|
183 /// OS << format_hex(255, 6) => 0x00ff
|
|
184 /// OS << format_hex(255, 2) => 0xff
|
|
185 inline FormattedNumber format_hex(uint64_t N, unsigned Width,
|
|
186 bool Upper = false) {
|
|
187 assert(Width <= 18 && "hex width must be <= 18");
|
|
188 return FormattedNumber(N, 0, Width, true, Upper, true);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190
|
83
|
191 /// format_hex_no_prefix - Output \p N as a fixed width hexadecimal. Does not
|
|
192 /// prepend '0x' to the outputted string. If number will not fit in width,
|
|
193 /// full number is still printed. Examples:
|
|
194 /// OS << format_hex_no_prefix(255, 2) => ff
|
120
|
195 /// OS << format_hex_no_prefix(255, 2, true) => FF
|
|
196 /// OS << format_hex_no_prefix(255, 4) => 00ff
|
|
197 /// OS << format_hex_no_prefix(255, 1) => ff
|
83
|
198 inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
|
|
199 bool Upper = false) {
|
120
|
200 assert(Width <= 16 && "hex width must be <= 16");
|
83
|
201 return FormattedNumber(N, 0, Width, true, Upper, false);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203
|
95
|
204 /// format_decimal - Output \p N as a right justified, fixed-width decimal. If
|
83
|
205 /// number will not fit in width, full number is still printed. Examples:
|
|
206 /// OS << format_decimal(0, 5) => " 0"
|
|
207 /// OS << format_decimal(255, 5) => " 255"
|
|
208 /// OS << format_decimal(-1, 3) => " -1"
|
|
209 /// OS << format_decimal(12345, 3) => "12345"
|
|
210 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
|
|
211 return FormattedNumber(0, N, Width, false, false, false);
|
77
|
212 }
|
|
213
|
120
|
214 class FormattedBytes {
|
|
215 ArrayRef<uint8_t> Bytes;
|
|
216
|
|
217 // If not None, display offsets for each line relative to starting value.
|
|
218 Optional<uint64_t> FirstByteOffset;
|
|
219 uint32_t IndentLevel; // Number of characters to indent each line.
|
|
220 uint32_t NumPerLine; // Number of bytes to show per line.
|
|
221 uint8_t ByteGroupSize; // How many hex bytes are grouped without spaces
|
|
222 bool Upper; // Show offset and hex bytes as upper case.
|
|
223 bool ASCII; // Show the ASCII bytes for the hex bytes to the right.
|
|
224 friend class raw_ostream;
|
|
225
|
|
226 public:
|
|
227 FormattedBytes(ArrayRef<uint8_t> B, uint32_t IL, Optional<uint64_t> O,
|
|
228 uint32_t NPL, uint8_t BGS, bool U, bool A)
|
|
229 : Bytes(B), FirstByteOffset(O), IndentLevel(IL), NumPerLine(NPL),
|
|
230 ByteGroupSize(BGS), Upper(U), ASCII(A) {
|
|
231
|
|
232 if (ByteGroupSize > NumPerLine)
|
|
233 ByteGroupSize = NumPerLine;
|
|
234 }
|
|
235 };
|
|
236
|
|
237 inline FormattedBytes
|
|
238 format_bytes(ArrayRef<uint8_t> Bytes, Optional<uint64_t> FirstByteOffset = None,
|
|
239 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
|
|
240 uint32_t IndentLevel = 0, bool Upper = false) {
|
|
241 return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,
|
|
242 ByteGroupSize, Upper, false);
|
|
243 }
|
|
244
|
|
245 inline FormattedBytes
|
|
246 format_bytes_with_ascii(ArrayRef<uint8_t> Bytes,
|
|
247 Optional<uint64_t> FirstByteOffset = None,
|
|
248 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
|
|
249 uint32_t IndentLevel = 0, bool Upper = false) {
|
|
250 return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,
|
|
251 ByteGroupSize, Upper, true);
|
|
252 }
|
|
253
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
254 } // end namespace llvm
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
255
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
256 #endif
|