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/SmallString.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/FormatCommon.h"
16 #include "llvm/Support/FormatVariadicDetails.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 namespace llvm {
20 template <typename T> class FormatAdapter : public detail::format_adapter {
21 protected:
FormatAdapter(T && Item)22   explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
23 
24   T Item;
25 };
26 
27 namespace detail {
28 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
29   AlignStyle Where;
30   size_t Amount;
31   char Fill;
32 
33 public:
AlignAdapter(T && Item,AlignStyle Where,size_t Amount,char Fill)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 
format(llvm::raw_ostream & Stream,StringRef Style)38   void format(llvm::raw_ostream &Stream, StringRef Style) {
39     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
40     FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
41   }
42 };
43 
44 template <typename T> class PadAdapter final : public FormatAdapter<T> {
45   size_t Left;
46   size_t Right;
47 
48 public:
PadAdapter(T && Item,size_t Left,size_t Right)49   PadAdapter(T &&Item, size_t Left, size_t Right)
50       : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
51 
format(llvm::raw_ostream & Stream,StringRef Style)52   void format(llvm::raw_ostream &Stream, StringRef Style) {
53     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
54     Stream.indent(Left);
55     Adapter.format(Stream, Style);
56     Stream.indent(Right);
57   }
58 };
59 
60 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
61   size_t Count;
62 
63 public:
RepeatAdapter(T && Item,size_t Count)64   RepeatAdapter(T &&Item, size_t Count)
65       : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
66 
format(llvm::raw_ostream & Stream,StringRef Style)67   void format(llvm::raw_ostream &Stream, StringRef Style) {
68     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
69     for (size_t I = 0; I < Count; ++I) {
70       Adapter.format(Stream, Style);
71     }
72   }
73 };
74 
75 class ErrorAdapter : public FormatAdapter<Error> {
76 public:
ErrorAdapter(Error && Item)77   ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
78   ErrorAdapter(ErrorAdapter &&) = default;
~ErrorAdapter()79   ~ErrorAdapter() { consumeError(std::move(Item)); }
format(llvm::raw_ostream & Stream,StringRef Style)80   void format(llvm::raw_ostream &Stream, StringRef Style) { Stream << Item; }
81 };
82 }
83 
84 template <typename T>
85 detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
86                                   char Fill = ' ') {
87   return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
88 }
89 
90 template <typename T>
fmt_pad(T && Item,size_t Left,size_t Right)91 detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
92   return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
93 }
94 
95 template <typename T>
fmt_repeat(T && Item,size_t Count)96 detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
97   return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
98 }
99 
100 // llvm::Error values must be consumed before being destroyed.
101 // Wrapping an error in fmt_consume explicitly indicates that the formatv_object
102 // should take ownership and consume it.
fmt_consume(Error && Item)103 inline detail::ErrorAdapter fmt_consume(Error &&Item) {
104   return detail::ErrorAdapter(std::move(Item));
105 }
106 }
107 
108 #endif
109