Line data Source code
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"
16 : #include "llvm/Support/FormatCommon.h"
17 : #include "llvm/Support/FormatVariadicDetails.h"
18 : #include "llvm/Support/raw_ostream.h"
19 :
20 : namespace llvm {
21 8586 : template <typename T> class FormatAdapter : public detail::format_adapter {
22 : protected:
23 2757 : explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
24 :
25 : T Item;
26 : };
27 :
28 : namespace detail {
29 7903 : 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 2585 : Fill(Fill) {}
38 :
39 2840 : void format(llvm::raw_ostream &Stream, StringRef Style) {
40 2840 : auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
41 5680 : FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
42 2840 : }
43 17 : };
44 17 :
45 39 : template <typename T> class PadAdapter final : public FormatAdapter<T> {
46 17 : size_t Left;
47 42 : size_t Right;
48 42 :
49 84 : public:
50 42 : PadAdapter(T &&Item, size_t Left, size_t Right)
51 59 : : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
52 58 :
53 123 : void format(llvm::raw_ostream &Stream, StringRef Style) {
54 64 : auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
55 7 : Stream.indent(Left);
56 6 : Adapter.format(Stream, Style);
57 7 : Stream.indent(Right);
58 7 : }
59 8 : };
60 8 :
61 388 : template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
62 8 : size_t Count;
63 16 :
64 16 : public:
65 21 : RepeatAdapter(T &&Item, size_t Count)
66 136 : : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
67 13 :
68 282 : void format(llvm::raw_ostream &Stream, StringRef Style) {
69 25 : auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
70 10848 : for (size_t I = 0; I < Count; ++I) {
71 10567 : Adapter.format(Stream, Style);
72 2 : }
73 285 : }
74 2 : };
75 3 :
76 3 : class ErrorAdapter : public FormatAdapter<Error> {
77 6 : public:
78 10 : ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
79 15 : ErrorAdapter(ErrorAdapter &&) = default;
80 62 : ~ErrorAdapter() { consumeError(std::move(Item)); }
81 6 : void format(llvm::raw_ostream &Stream, StringRef Style) { Stream << Item; }
82 18 : };
83 15 : }
84 :
85 2 : template <typename T>
86 1 : detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
87 1 : char Fill = ' ') {
88 6 : return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
89 5 : }
90 :
91 1 : template <typename T>
92 1 : 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 6 : }
95 5 :
96 : template <typename T>
97 450 : 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 145 : // Wrapping an error in fmt_consume explicitly indicates that the formatv_object
103 3 : // should take ownership and consume it.
104 12 : inline detail::ErrorAdapter fmt_consume(Error &&Item) {
105 1 : return detail::ErrorAdapter(std::move(Item));
106 0 : }
107 0 : }
108 :
109 0 : #endif
|