LLVM  8.0.1
FormatAdapters.h
Go to the documentation of this file.
1 //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
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_FORMATADAPTERS_H
11 #define LLVM_SUPPORT_FORMATADAPTERS_H
12 
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Error.h"
19 
20 namespace llvm {
21 template <typename T> class FormatAdapter : public detail::format_adapter {
22 protected:
23  explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
24 
26 };
27 
28 namespace detail {
29 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
30  AlignStyle Where;
31  size_t Amount;
32  char Fill;
33 
34 public:
35  AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
36  : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
37  Fill(Fill) {}
38 
40  auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
41  FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
42  }
43 };
44 
45 template <typename T> class PadAdapter final : public FormatAdapter<T> {
46  size_t Left;
47  size_t Right;
48 
49 public:
50  PadAdapter(T &&Item, size_t Left, size_t Right)
51  : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
52 
54  auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
55  Stream.indent(Left);
56  Adapter.format(Stream, Style);
57  Stream.indent(Right);
58  }
59 };
60 
61 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
62  size_t Count;
63 
64 public:
65  RepeatAdapter(T &&Item, size_t Count)
66  : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
67 
69  auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
70  for (size_t I = 0; I < Count; ++I) {
71  Adapter.format(Stream, Style);
72  }
73  }
74 };
75 
76 class ErrorAdapter : public FormatAdapter<Error> {
77 public:
79  ErrorAdapter(ErrorAdapter &&) = default;
80  ~ErrorAdapter() { consumeError(std::move(Item)); }
81  void format(llvm::raw_ostream &Stream, StringRef Style) { Stream << Item; }
82 };
83 }
84 
85 template <typename T>
87  char Fill = ' ') {
88  return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
89 }
90 
91 template <typename T>
93  return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
94 }
95 
96 template <typename T>
98  return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
99 }
100 
101 // llvm::Error values must be consumed before being destroyed.
102 // Wrapping an error in fmt_consume explicitly indicates that the formatv_object
103 // should take ownership and consume it.
105  return detail::ErrorAdapter(std::move(Item));
106 }
107 }
108 
109 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:24
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
void format(raw_ostream &S, StringRef Options)
Definition: FormatCommon.h:30
Definition: BitVector.h:938
AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
AlignStyle
Definition: FormatCommon.h:18
void format(llvm::raw_ostream &Stream, StringRef Style)
void format(llvm::raw_ostream &Stream, StringRef Style)
detail::ErrorAdapter fmt_consume(Error &&Item)
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:982
void format(llvm::raw_ostream &Stream, StringRef Style)
void format(llvm::raw_ostream &Stream, StringRef Style)
detail::PadAdapter< T > fmt_pad(T &&Item, size_t Left, size_t Right)
PadAdapter(T &&Item, size_t Left, size_t Right)
RepeatAdapter(T &&Item, size_t Count)
#define I(x, y, z)
Definition: MD5.cpp:58
std::enable_if< uses_format_member< T >::value, T >::type build_format_adapter(T &&Item)
detail::RepeatAdapter< T > fmt_repeat(T &&Item, size_t Count)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
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