LLVM  8.0.1
ScopedPrinter.h
Go to the documentation of this file.
1 //===-- ScopedPrinter.h ---------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_SCOPEDPRINTER_H
11 #define LLVM_SUPPORT_SCOPEDPRINTER_H
12 
13 #include "llvm/ADT/APSInt.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Endian.h"
20 #include <algorithm>
21 
22 namespace llvm {
23 
24 template <typename T> struct EnumEntry {
26  // While Name suffices in most of the cases, in certain cases
27  // GNU style and LLVM style of ELFDumper do not
28  // display same string for same enum. The AltName if initialized appropriately
29  // will hold the string that GNU style emits.
30  // Example:
31  // "EM_X86_64" string on LLVM style for Elf_Ehdr->e_machine corresponds to
32  // "Advanced Micro Devices X86-64" on GNU style
35  EnumEntry(StringRef N, StringRef A, T V) : Name(N), AltName(A), Value(V) {}
36  EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
37 };
38 
39 struct HexNumber {
40  // To avoid sign-extension we have to explicitly cast to the appropriate
41  // unsigned type. The overloads are here so that every type that is implicitly
42  // convertible to an integer (including enums and endian helpers) can be used
43  // without requiring type traits or call-site changes.
44  HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) {}
45  HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) {}
46  HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) {}
47  HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) {}
48  HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) {}
49  HexNumber(signed long long Value)
50  : Value(static_cast<unsigned long long>(Value)) {}
51  HexNumber(unsigned char Value) : Value(Value) {}
52  HexNumber(unsigned short Value) : Value(Value) {}
53  HexNumber(unsigned int Value) : Value(Value) {}
54  HexNumber(unsigned long Value) : Value(Value) {}
55  HexNumber(unsigned long long Value) : Value(Value) {}
56  uint64_t Value;
57 };
58 
60 const std::string to_hexString(uint64_t Value, bool UpperCase = true);
61 
62 template <class T> const std::string to_string(const T &Value) {
63  std::string number;
64  llvm::raw_string_ostream stream(number);
65  stream << Value;
66  return stream.str();
67 }
68 
70 public:
71  ScopedPrinter(raw_ostream &OS) : OS(OS), IndentLevel(0) {}
72 
73  void flush() { OS.flush(); }
74 
75  void indent(int Levels = 1) { IndentLevel += Levels; }
76 
77  void unindent(int Levels = 1) {
78  IndentLevel = std::max(0, IndentLevel - Levels);
79  }
80 
81  void resetIndent() { IndentLevel = 0; }
82 
83  int getIndentLevel() { return IndentLevel; }
84 
85  void setPrefix(StringRef P) { Prefix = P; }
86 
87  void printIndent() {
88  OS << Prefix;
89  for (int i = 0; i < IndentLevel; ++i)
90  OS << " ";
91  }
92 
93  template <typename T> HexNumber hex(T Value) { return HexNumber(Value); }
94 
95  template <typename T, typename TEnum>
96  void printEnum(StringRef Label, T Value,
97  ArrayRef<EnumEntry<TEnum>> EnumValues) {
99  bool Found = false;
100  for (const auto &EnumItem : EnumValues) {
101  if (EnumItem.Value == Value) {
102  Name = EnumItem.Name;
103  Found = true;
104  break;
105  }
106  }
107 
108  if (Found) {
109  startLine() << Label << ": " << Name << " (" << hex(Value) << ")\n";
110  } else {
111  startLine() << Label << ": " << hex(Value) << "\n";
112  }
113  }
114 
115  template <typename T, typename TFlag>
117  TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
118  TFlag EnumMask3 = {}) {
119  typedef EnumEntry<TFlag> FlagEntry;
120  typedef SmallVector<FlagEntry, 10> FlagVector;
121  FlagVector SetFlags;
122 
123  for (const auto &Flag : Flags) {
124  if (Flag.Value == 0)
125  continue;
126 
127  TFlag EnumMask{};
128  if (Flag.Value & EnumMask1)
129  EnumMask = EnumMask1;
130  else if (Flag.Value & EnumMask2)
131  EnumMask = EnumMask2;
132  else if (Flag.Value & EnumMask3)
133  EnumMask = EnumMask3;
134  bool IsEnum = (Flag.Value & EnumMask) != 0;
135  if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
136  (IsEnum && (Value & EnumMask) == Flag.Value)) {
137  SetFlags.push_back(Flag);
138  }
139  }
140 
141  llvm::sort(SetFlags, &flagName<TFlag>);
142 
143  startLine() << Label << " [ (" << hex(Value) << ")\n";
144  for (const auto &Flag : SetFlags) {
145  startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
146  }
147  startLine() << "]\n";
148  }
149 
150  template <typename T> void printFlags(StringRef Label, T Value) {
151  startLine() << Label << " [ (" << hex(Value) << ")\n";
152  uint64_t Flag = 1;
153  uint64_t Curr = Value;
154  while (Curr > 0) {
155  if (Curr & 1)
156  startLine() << " " << hex(Flag) << "\n";
157  Curr >>= 1;
158  Flag <<= 1;
159  }
160  startLine() << "]\n";
161  }
162 
163  void printNumber(StringRef Label, uint64_t Value) {
164  startLine() << Label << ": " << Value << "\n";
165  }
166 
167  void printNumber(StringRef Label, uint32_t Value) {
168  startLine() << Label << ": " << Value << "\n";
169  }
170 
171  void printNumber(StringRef Label, uint16_t Value) {
172  startLine() << Label << ": " << Value << "\n";
173  }
174 
175  void printNumber(StringRef Label, uint8_t Value) {
176  startLine() << Label << ": " << unsigned(Value) << "\n";
177  }
178 
179  void printNumber(StringRef Label, int64_t Value) {
180  startLine() << Label << ": " << Value << "\n";
181  }
182 
183  void printNumber(StringRef Label, int32_t Value) {
184  startLine() << Label << ": " << Value << "\n";
185  }
186 
187  void printNumber(StringRef Label, int16_t Value) {
188  startLine() << Label << ": " << Value << "\n";
189  }
190 
191  void printNumber(StringRef Label, int8_t Value) {
192  startLine() << Label << ": " << int(Value) << "\n";
193  }
194 
195  void printNumber(StringRef Label, const APSInt &Value) {
196  startLine() << Label << ": " << Value << "\n";
197  }
198 
199  void printBoolean(StringRef Label, bool Value) {
200  startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
201  }
202 
203  template <typename... T> void printVersion(StringRef Label, T... Version) {
204  startLine() << Label << ": ";
205  printVersionInternal(Version...);
206  getOStream() << "\n";
207  }
208 
209  template <typename T> void printList(StringRef Label, const T &List) {
210  startLine() << Label << ": [";
211  bool Comma = false;
212  for (const auto &Item : List) {
213  if (Comma)
214  OS << ", ";
215  OS << Item;
216  Comma = true;
217  }
218  OS << "]\n";
219  }
220 
221  template <typename T, typename U>
222  void printList(StringRef Label, const T &List, const U &Printer) {
223  startLine() << Label << ": [";
224  bool Comma = false;
225  for (const auto &Item : List) {
226  if (Comma)
227  OS << ", ";
228  Printer(OS, Item);
229  Comma = true;
230  }
231  OS << "]\n";
232  }
233 
234  template <typename T> void printHexList(StringRef Label, const T &List) {
235  startLine() << Label << ": [";
236  bool Comma = false;
237  for (const auto &Item : List) {
238  if (Comma)
239  OS << ", ";
240  OS << hex(Item);
241  Comma = true;
242  }
243  OS << "]\n";
244  }
245 
246  template <typename T> void printHex(StringRef Label, T Value) {
247  startLine() << Label << ": " << hex(Value) << "\n";
248  }
249 
250  template <typename T> void printHex(StringRef Label, StringRef Str, T Value) {
251  startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n";
252  }
253 
254  template <typename T>
256  startLine() << Label << ": " << Symbol << '+' << hex(Value) << '\n';
257  }
258 
259  void printString(StringRef Value) { startLine() << Value << "\n"; }
260 
261  void printString(StringRef Label, StringRef Value) {
262  startLine() << Label << ": " << Value << "\n";
263  }
264 
265  void printString(StringRef Label, const std::string &Value) {
266  printString(Label, StringRef(Value));
267  }
268 
269  void printString(StringRef Label, const char* Value) {
270  printString(Label, StringRef(Value));
271  }
272 
273  template <typename T>
274  void printNumber(StringRef Label, StringRef Str, T Value) {
275  startLine() << Label << ": " << Str << " (" << Value << ")\n";
276  }
277 
279  printBinaryImpl(Label, Str, Value, false);
280  }
281 
282  void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
283  auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
284  Value.size());
285  printBinaryImpl(Label, Str, V, false);
286  }
287 
289  printBinaryImpl(Label, StringRef(), Value, false);
290  }
291 
292  void printBinary(StringRef Label, ArrayRef<char> Value) {
293  auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
294  Value.size());
295  printBinaryImpl(Label, StringRef(), V, false);
296  }
297 
298  void printBinary(StringRef Label, StringRef Value) {
299  auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
300  Value.size());
301  printBinaryImpl(Label, StringRef(), V, false);
302  }
303 
305  uint32_t StartOffset) {
306  printBinaryImpl(Label, StringRef(), Value, true, StartOffset);
307  }
308 
310  printBinaryImpl(Label, StringRef(), Value, true);
311  }
312 
313  void printBinaryBlock(StringRef Label, StringRef Value) {
314  auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
315  Value.size());
316  printBinaryImpl(Label, StringRef(), V, true);
317  }
318 
319  template <typename T> void printObject(StringRef Label, const T &Value) {
320  startLine() << Label << ": " << Value << "\n";
321  }
322 
324  printIndent();
325  return OS;
326  }
327 
328  raw_ostream &getOStream() { return OS; }
329 
330 private:
331  template <typename T> void printVersionInternal(T Value) {
332  getOStream() << Value;
333  }
334 
335  template <typename S, typename T, typename... TArgs>
336  void printVersionInternal(S Value, T Value2, TArgs... Args) {
337  getOStream() << Value << ".";
338  printVersionInternal(Value2, Args...);
339  }
340 
341  template <typename T>
342  static bool flagName(const EnumEntry<T> &lhs, const EnumEntry<T> &rhs) {
343  return lhs.Name < rhs.Name;
344  }
345 
346  void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
347  bool Block, uint32_t StartOffset = 0);
348 
349  raw_ostream &OS;
350  int IndentLevel;
352 };
353 
354 template <>
355 inline void
356 ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
358  startLine() << Label << ": " << hex(Value) << "\n";
359 }
360 
361 template<char Open, char Close>
363  explicit DelimitedScope(ScopedPrinter &W) : W(W) {
364  W.startLine() << Open << '\n';
365  W.indent();
366  }
367 
369  W.startLine() << N;
370  if (!N.empty())
371  W.getOStream() << ' ';
372  W.getOStream() << Open << '\n';
373  W.indent();
374  }
375 
377  W.unindent();
378  W.startLine() << Close << '\n';
379  }
380 
382 };
383 
384 using DictScope = DelimitedScope<'{', '}'>;
386 
387 } // namespace llvm
388 
389 #endif
void printHexList(StringRef Label, const T &List)
StringRef AltName
Definition: ScopedPrinter.h:33
HexNumber(signed long Value)
Definition: ScopedPrinter.h:48
void printEnum(StringRef Label, T Value, ArrayRef< EnumEntry< TEnum >> EnumValues)
Definition: ScopedPrinter.h:96
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void printBinary(StringRef Label, ArrayRef< char > Value)
void printBinary(StringRef Label, StringRef Value)
void printSymbolOffset(StringRef Label, StringRef Symbol, T Value)
void printBoolean(StringRef Label, bool Value)
void push_back(const T &Elt)
Definition: SmallVector.h:218
StringRef Name
Definition: ScopedPrinter.h:25
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
HexNumber(signed char Value)
Definition: ScopedPrinter.h:45
void printNumber(StringRef Label, int32_t Value)
print alias Alias Set Printer
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
HexNumber(char Value)
Definition: ScopedPrinter.h:44
void printString(StringRef Label, StringRef Value)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
void indent(int Levels=1)
Definition: ScopedPrinter.h:75
DelimitedScope(ScopedPrinter &W)
HexNumber(signed short Value)
Definition: ScopedPrinter.h:46
void printString(StringRef Label, const char *Value)
raw_ostream & getOStream()
EnumEntry(StringRef N, StringRef A, T V)
Definition: ScopedPrinter.h:35
void printBinary(StringRef Label, StringRef Str, ArrayRef< uint8_t > Value)
#define T
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void printNumber(StringRef Label, uint8_t Value)
void printFlags(StringRef Label, T Value)
void printBinary(StringRef Label, StringRef Str, ArrayRef< char > Value)
void printBinaryBlock(StringRef Label, StringRef Value)
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:118
HexNumber(unsigned char Value)
Definition: ScopedPrinter.h:51
#define P(N)
void printString(StringRef Label, const std::string &Value)
HexNumber(unsigned short Value)
Definition: ScopedPrinter.h:52
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
HexNumber(signed int Value)
Definition: ScopedPrinter.h:47
raw_ostream & startLine()
void printNumber(StringRef Label, const APSInt &Value)
void printList(StringRef Label, const T &List)
void printVersion(StringRef Label, T... Version)
EnumEntry(StringRef N, T V)
Definition: ScopedPrinter.h:36
HexNumber(signed long long Value)
Definition: ScopedPrinter.h:49
void printHex(StringRef Label, T Value)
HexNumber(unsigned int Value)
Definition: ScopedPrinter.h:53
ScopedPrinter & W
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:499
const T * data() const
Definition: ArrayRef.h:146
HexNumber hex(T Value)
Definition: ScopedPrinter.h:93
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1116
void printNumber(StringRef Label, uint32_t Value)
void printString(StringRef Value)
void printNumber(StringRef Label, uint64_t Value)
DelimitedScope(ScopedPrinter &W, StringRef N)
void printObject(StringRef Label, const T &Value)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
void printNumber(StringRef Label, int8_t Value)
void printBinary(StringRef Label, ArrayRef< uint8_t > Value)
void printNumber(StringRef Label, int16_t Value)
const std::string to_hexString(uint64_t Value, bool UpperCase=true)
HexNumber(unsigned long Value)
Definition: ScopedPrinter.h:54
void printHex(StringRef Label, StringRef Str, T Value)
void setPrefix(StringRef P)
Definition: ScopedPrinter.h:85
void unindent(int Levels=1)
Definition: ScopedPrinter.h:77
void printFlags(StringRef Label, T Value, ArrayRef< EnumEntry< TFlag >> Flags, TFlag EnumMask1={}, TFlag EnumMask2={}, TFlag EnumMask3={})
const NodeList & List
Definition: RDFGraph.cpp:210
ScopedPrinter(raw_ostream &OS)
Definition: ScopedPrinter.h:71
#define N
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2039
void printNumber(StringRef Label, uint16_t Value)
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:62
void printList(StringRef Label, const T &List, const U &Printer)
void printBinaryBlock(StringRef Label, ArrayRef< uint8_t > Value, uint32_t StartOffset)
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void printNumber(StringRef Label, int64_t Value)
void printNumber(StringRef Label, StringRef Str, T Value)
HexNumber(unsigned long long Value)
Definition: ScopedPrinter.h:55
const uint64_t Version
Definition: InstrProf.h:895
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
void printBinaryBlock(StringRef Label, ArrayRef< uint8_t > Value)