• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 template <typename T> class FormatAdapter : public detail::format_adapter {
22 protected:
FormatAdapter(T && Item)23   explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
24 
25   T Item;
26 };
27 
28 namespace detail {
29 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
30   AlignStyle Where;
31   size_t Amount;
32   char Fill;
33 
34 public:
AlignAdapter(T && Item,AlignStyle Where,size_t Amount,char Fill)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 
format(llvm::raw_ostream & Stream,StringRef Style)39   void format(llvm::raw_ostream &Stream, StringRef Style) {
40     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
41     FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
42   }
43 };
44 
45 template <typename T> class PadAdapter final : public FormatAdapter<T> {
46   size_t Left;
47   size_t Right;
48 
49 public:
PadAdapter(T && Item,size_t Left,size_t Right)50   PadAdapter(T &&Item, size_t Left, size_t Right)
51       : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
52 
format(llvm::raw_ostream & Stream,StringRef Style)53   void format(llvm::raw_ostream &Stream, StringRef Style) {
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 
61 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
62   size_t Count;
63 
64 public:
RepeatAdapter(T && Item,size_t Count)65   RepeatAdapter(T &&Item, size_t Count)
66       : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
67 
format(llvm::raw_ostream & Stream,StringRef Style)68   void format(llvm::raw_ostream &Stream, StringRef Style) {
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 
76 class ErrorAdapter : public FormatAdapter<Error> {
77 public:
ErrorAdapter(Error && Item)78   ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
79   ErrorAdapter(ErrorAdapter &&) = default;
~ErrorAdapter()80   ~ErrorAdapter() { consumeError(std::move(Item)); }
format(llvm::raw_ostream & Stream,StringRef Style)81   void format(llvm::raw_ostream &Stream, StringRef Style) { Stream << Item; }
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>
fmt_pad(T && Item,size_t Left,size_t Right)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>
fmt_repeat(T && Item,size_t Count)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.
fmt_consume(Error && Item)104 inline detail::ErrorAdapter fmt_consume(Error &&Item) {
105   return detail::ErrorAdapter(std::move(Item));
106 }
107 }
108 
109 #endif
110