1 //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_FORMATADAPTERS_H
10 #define LLVM_SUPPORT_FORMATADAPTERS_H
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/FormatCommon.h"
15 #include "llvm/Support/FormatVariadicDetails.h"
16 #include "llvm/Support/raw_ostream.h"
19 template <typename T> class FormatAdapter : public detail::format_adapter {
21 explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
27 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
33 AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
34 : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
37 void format(llvm::raw_ostream &Stream, StringRef Style) override {
38 auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
39 FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
43 template <typename T> class PadAdapter final : public FormatAdapter<T> {
48 PadAdapter(T &&Item, size_t Left, size_t Right)
49 : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
51 void format(llvm::raw_ostream &Stream, StringRef Style) override {
52 auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
54 Adapter.format(Stream, Style);
59 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
63 RepeatAdapter(T &&Item, size_t Count)
64 : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
66 void format(llvm::raw_ostream &Stream, StringRef Style) override {
67 auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
68 for (size_t I = 0; I < Count; ++I) {
69 Adapter.format(Stream, Style);
74 class ErrorAdapter : public FormatAdapter<Error> {
76 ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
77 ErrorAdapter(ErrorAdapter &&) = default;
78 ~ErrorAdapter() { consumeError(std::move(Item)); }
79 void format(llvm::raw_ostream &Stream, StringRef Style) override {
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));