LLVM 20.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>
21protected:
22 explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
23
25};
26
27namespace support {
28namespace detail {
29template <typename T> class AlignAdapter final : public FormatAdapter<T> {
30 AlignStyle Where;
31 size_t Amount;
32 char Fill;
33
34public:
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
39 void format(llvm::raw_ostream &Stream, StringRef Style) override {
40 auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
41 FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
42 }
43};
44
45template <typename T> class PadAdapter final : public FormatAdapter<T> {
46 size_t Left;
47 size_t Right;
48
49public:
50 PadAdapter(T &&Item, size_t Left, size_t Right)
51 : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
52
53 void format(llvm::raw_ostream &Stream, StringRef Style) override {
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
61template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
62 size_t Count;
63
64public:
65 RepeatAdapter(T &&Item, size_t Count)
66 : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
67
68 void format(llvm::raw_ostream &Stream, StringRef Style) override {
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
76class ErrorAdapter : public FormatAdapter<Error> {
77public:
80 ~ErrorAdapter() { consumeError(std::move(Item)); }
81 void format(llvm::raw_ostream &Stream, StringRef Style) override {
82 Stream << Item;
83 }
84};
85} // namespace detail
86} // namespace support
87
88template <typename T>
90 size_t Amount, char Fill = ' ') {
91 return support::detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount,
92 Fill);
93}
94
95template <typename T>
97 return support::detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
98}
99
100template <typename T>
102 return support::detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
103}
104
105// llvm::Error values must be consumed before being destroyed.
106// Wrapping an error in fmt_consume explicitly indicates that the formatv_object
107// should take ownership and consume it.
109 return support::detail::ErrorAdapter(std::move(Item));
110}
111}
112
113#endif
#define I(x, y, z)
Definition: MD5.cpp:58
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
void format(llvm::raw_ostream &Stream, StringRef Style) override
AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
void format(llvm::raw_ostream &Stream, StringRef Style) override
ErrorAdapter(ErrorAdapter &&)=default
PadAdapter(T &&Item, size_t Left, size_t Right)
void format(llvm::raw_ostream &Stream, StringRef Style) override
void format(llvm::raw_ostream &Stream, StringRef Style) override
RepeatAdapter(T &&Item, size_t Count)
std::enable_if_t< uses_format_member< T >::value, T > build_format_adapter(T &&Item)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
support::detail::ErrorAdapter fmt_consume(Error &&Item)
AlignStyle
Definition: FormatCommon.h:17
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)
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:1856
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:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void format(raw_ostream &S, StringRef Options)
Definition: FormatCommon.h:29