Thanks to visit codestin.com
Credit goes to llvm.org

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