LLVM  13.0.0git
FormatAdapters.h
Go to the documentation of this file.
1 //===- FormatAdapters.h - Formatters for common LLVM types -----*- 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 #ifndef LLVM_SUPPORT_FORMATADAPTERS_H
10 #define LLVM_SUPPORT_FORMATADAPTERS_H
11 
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Error.h"
17 
18 namespace llvm {
19 template <typename T> class FormatAdapter : public detail::format_adapter {
20 protected:
21  explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
22 
24 };
25 
26 namespace detail {
27 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
28  AlignStyle Where;
29  size_t Amount;
30  char Fill;
31 
32 public:
33  AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
34  : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
35  Fill(Fill) {}
36 
37  void format(llvm::raw_ostream &Stream, StringRef Style) override {
38  auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
39  FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
40  }
41 };
42 
43 template <typename T> class PadAdapter final : public FormatAdapter<T> {
44  size_t Left;
45  size_t Right;
46 
47 public:
48  PadAdapter(T &&Item, size_t Left, size_t Right)
49  : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
50 
51  void format(llvm::raw_ostream &Stream, StringRef Style) override {
52  auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
53  Stream.indent(Left);
54  Adapter.format(Stream, Style);
55  Stream.indent(Right);
56  }
57 };
58 
59 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
60  size_t Count;
61 
62 public:
63  RepeatAdapter(T &&Item, size_t Count)
64  : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
65 
66  void format(llvm::raw_ostream &Stream, StringRef Style) override {
67  auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
68  for (size_t I = 0; I < Count; ++I) {
69  Adapter.format(Stream, Style);
70  }
71  }
72 };
73 
74 class ErrorAdapter : public FormatAdapter<Error> {
75 public:
77  ErrorAdapter(ErrorAdapter &&) = default;
79  void format(llvm::raw_ostream &Stream, StringRef Style) override {
80  Stream << Item;
81  }
82 };
83 }
84 
85 template <typename T>
86 detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
87  char Fill = ' ') {
88  return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
89 }
90 
91 template <typename T>
92 detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
93  return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
94 }
95 
96 template <typename T>
97 detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
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
llvm::detail::RepeatAdapter
Definition: FormatAdapters.h:59
llvm
Definition: AllocatorList.h:23
llvm::FormatAdapter::Item
T Item
Definition: FormatAdapters.h:23
llvm::FmtAlign::format
void format(raw_ostream &S, StringRef Options)
Definition: FormatCommon.h:29
StringRef.h
llvm::fmt_consume
detail::ErrorAdapter fmt_consume(Error &&Item)
Definition: FormatAdapters.h:104
llvm::detail::AlignAdapter::format
void format(llvm::raw_ostream &Stream, StringRef Style) override
Definition: FormatAdapters.h:37
llvm::detail::ErrorAdapter
Definition: FormatAdapters.h:74
Error.h
Right
Vector Shift Left Right
Definition: README_P9.txt:118
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::detail::build_format_adapter
std::enable_if_t< uses_format_member< T >::value, T > build_format_adapter(T &&Item)
Definition: FormatVariadicDetails.h:131
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1006
llvm::AlignStyle::Left
@ Left
llvm::detail::PadAdapter
Definition: FormatAdapters.h:43
llvm::detail::AlignAdapter
Definition: FormatAdapters.h:27
llvm::detail::PadAdapter::format
void format(llvm::raw_ostream &Stream, StringRef Style) override
Definition: FormatAdapters.h:51
llvm::AlignStyle
AlignStyle
Definition: FormatCommon.h:17
llvm::fmt_pad
detail::PadAdapter< T > fmt_pad(T &&Item, size_t Left, size_t Right)
Definition: FormatAdapters.h:92
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::FormatAdapter::FormatAdapter
FormatAdapter(T &&Item)
Definition: FormatAdapters.h:21
llvm::detail::RepeatAdapter::RepeatAdapter
RepeatAdapter(T &&Item, size_t Count)
Definition: FormatAdapters.h:63
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::fmt_repeat
detail::RepeatAdapter< T > fmt_repeat(T &&Item, size_t Count)
Definition: FormatAdapters.h:97
I
#define I(x, y, z)
Definition: MD5.cpp:59
FormatCommon.h
llvm::HexStyle::Style
Style
Definition: MCInstPrinter.h:32
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1540
FormatVariadicDetails.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::detail::RepeatAdapter::format
void format(llvm::raw_ostream &Stream, StringRef Style) override
Definition: FormatAdapters.h:66
llvm::detail::ErrorAdapter::~ErrorAdapter
~ErrorAdapter()
Definition: FormatAdapters.h:78
llvm::FormatAdapter
Definition: FormatAdapters.h:19
std
Definition: BitVector.h:838
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::detail::ErrorAdapter::format
void format(llvm::raw_ostream &Stream, StringRef Style) override
Definition: FormatAdapters.h:79
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:497
llvm::FmtAlign
Definition: FormatCommon.h:19
llvm::detail::ErrorAdapter::ErrorAdapter
ErrorAdapter(Error &&Item)
Definition: FormatAdapters.h:76
raw_ostream.h
llvm::fmt_align
detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
Definition: FormatAdapters.h:86
llvm::detail::PadAdapter::PadAdapter
PadAdapter(T &&Item, size_t Left, size_t Right)
Definition: FormatAdapters.h:48
llvm::detail::format_adapter
Definition: FormatVariadicDetails.h:22
llvm::detail::AlignAdapter::AlignAdapter
AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
Definition: FormatAdapters.h:33