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