1 //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_FORMATADAPTERS_H
11 #define LLVM_SUPPORT_FORMATADAPTERS_H
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"
21 template <typename T> class FormatAdapter : public detail::format_adapter {
23 explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
29 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
35 AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
36 : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
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);
45 template <typename T> class PadAdapter final : public FormatAdapter<T> {
50 PadAdapter(T &&Item, size_t Left, size_t Right)
51 : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
53 void format(llvm::raw_ostream &Stream, StringRef Style) {
54 auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
56 Adapter.format(Stream, Style);
61 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
65 RepeatAdapter(T &&Item, size_t Count)
66 : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
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);
76 class ErrorAdapter : public FormatAdapter<Error> {
78 ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
79 ErrorAdapter(ErrorAdapter &&) = default;
80 ~ErrorAdapter() { consumeError(std::move(Item)); }
81 void format(llvm::raw_ostream &Stream, StringRef Style) { Stream << Item; }
86 detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
88 return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
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);
97 detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
98 return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
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.
104 inline detail::ErrorAdapter fmt_consume(Error &&Item) {
105 return detail::ErrorAdapter(std::move(Item));