annotate include/llvm/Support/Format.h @ 148:63bd29f05246

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