LLVM 23.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
18namespace llvm {
19template <typename T> class FormatAdapter {
20protected:
21 explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
22
24};
25
26namespace support {
27namespace detail {
28template <typename T> class AlignAdapter final : public FormatAdapter<T> {
29 AlignStyle Where;
30 size_t Amount;
31 char Fill;
32
33public:
34 AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
35 : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
36 Fill(Fill) {}
37
38 void format(llvm::raw_ostream &Stream, StringRef Style) {
39 auto Adapter = detail::FormatFunctor(std::forward<T>(this->Item));
40 FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
41 }
42};
43
44template <typename T> class PadAdapter final : public FormatAdapter<T> {
45 size_t Left;
46 size_t Right;
47
48public:
49 PadAdapter(T &&Item, size_t Left, size_t Right)
50 : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
51
52 void format(llvm::raw_ostream &Stream, StringRef Style) {
53 auto Adapter = detail::FormatFunctor(std::forward<T>(this->Item));
54 Stream.indent(Left);
55 Adapter(Stream, Style);
56 Stream.indent(Right);
57 }
58};
59
60template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
61 size_t Count;
62
63public:
64 RepeatAdapter(T &&Item, size_t Count)
65 : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
66
67 void format(llvm::raw_ostream &Stream, StringRef Style) {
68 auto Adapter = detail::FormatFunctor(std::forward<T>(this->Item));
69 for (size_t I = 0; I < Count; ++I) {
70 Adapter(Stream, Style);
71 }
72 }
73};
74
75class ErrorAdapter : public FormatAdapter<Error> {
76public:
79 ~ErrorAdapter() { consumeError(std::move(Item)); }
80 void format(llvm::raw_ostream &Stream, StringRef Style) { Stream << Item; }
81};
82} // namespace detail
83} // namespace support
84
85template <typename T>
87 size_t Amount, char Fill = ' ') {
88 return support::detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount,
89 Fill);
90}
91
92template <typename T>
94 return support::detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
95}
96
97template <typename T>
99 return support::detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
100}
101
102// llvm::Error values must be consumed before being destroyed.
103// Wrapping an error in fmt_consume explicitly indicates that the formatv_object
104// should take ownership and consume it.
106 return support::detail::ErrorAdapter(std::move(Item));
107}
108}
109
110#endif
#define I(x, y, z)
Definition MD5.cpp:57
#define T
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
void format(llvm::raw_ostream &Stream, StringRef Style)
void format(llvm::raw_ostream &Stream, StringRef Style)
ErrorAdapter(ErrorAdapter &&)=default
void format(llvm::raw_ostream &Stream, StringRef Style)
PadAdapter(T &&Item, size_t Left, size_t Right)
void format(llvm::raw_ostream &Stream, StringRef Style)
RepeatAdapter(T &&Item, size_t Count)
FormatFunctor(T &&) -> FormatFunctor< T >
This is an optimization pass for GlobalISel generic memory operations.
support::detail::ErrorAdapter fmt_consume(Error &&Item)
support::detail::PadAdapter< T > fmt_pad(T &&Item, size_t Left, size_t Right)
support::detail::RepeatAdapter< T > fmt_repeat(T &&Item, size_t Count)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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:1917
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
Helper class to format to a Width wide field, with alignment Where within that field.
void format(raw_ostream &S, StringRef Options)