Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_FORMATADAPTERS_H
0010 #define LLVM_SUPPORT_FORMATADAPTERS_H
0011 
0012 #include "llvm/ADT/StringRef.h"
0013 #include "llvm/Support/Error.h"
0014 #include "llvm/Support/FormatCommon.h"
0015 #include "llvm/Support/FormatVariadicDetails.h"
0016 #include "llvm/Support/raw_ostream.h"
0017 
0018 namespace llvm {
0019 template <typename T>
0020 class FormatAdapter : public support::detail::format_adapter {
0021 protected:
0022   explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
0023 
0024   T Item;
0025 };
0026 
0027 namespace support {
0028 namespace detail {
0029 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
0030   AlignStyle Where;
0031   size_t Amount;
0032   char Fill;
0033 
0034 public:
0035   AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
0036       : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
0037         Fill(Fill) {}
0038 
0039   void format(llvm::raw_ostream &Stream, StringRef Style) override {
0040     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
0041     FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
0042   }
0043 };
0044 
0045 template <typename T> class PadAdapter final : public FormatAdapter<T> {
0046   size_t Left;
0047   size_t Right;
0048 
0049 public:
0050   PadAdapter(T &&Item, size_t Left, size_t Right)
0051       : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
0052 
0053   void format(llvm::raw_ostream &Stream, StringRef Style) override {
0054     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
0055     Stream.indent(Left);
0056     Adapter.format(Stream, Style);
0057     Stream.indent(Right);
0058   }
0059 };
0060 
0061 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
0062   size_t Count;
0063 
0064 public:
0065   RepeatAdapter(T &&Item, size_t Count)
0066       : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
0067 
0068   void format(llvm::raw_ostream &Stream, StringRef Style) override {
0069     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
0070     for (size_t I = 0; I < Count; ++I) {
0071       Adapter.format(Stream, Style);
0072     }
0073   }
0074 };
0075 
0076 class ErrorAdapter : public FormatAdapter<Error> {
0077 public:
0078   ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
0079   ErrorAdapter(ErrorAdapter &&) = default;
0080   ~ErrorAdapter() { consumeError(std::move(Item)); }
0081   void format(llvm::raw_ostream &Stream, StringRef Style) override {
0082     Stream << Item;
0083   }
0084 };
0085 } // namespace detail
0086 } // namespace support
0087 
0088 template <typename T>
0089 support::detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where,
0090                                            size_t Amount, char Fill = ' ') {
0091   return support::detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount,
0092                                           Fill);
0093 }
0094 
0095 template <typename T>
0096 support::detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
0097   return support::detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
0098 }
0099 
0100 template <typename T>
0101 support::detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
0102   return support::detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
0103 }
0104 
0105 // llvm::Error values must be consumed before being destroyed.
0106 // Wrapping an error in fmt_consume explicitly indicates that the formatv_object
0107 // should take ownership and consume it.
0108 inline support::detail::ErrorAdapter fmt_consume(Error &&Item) {
0109   return support::detail::ErrorAdapter(std::move(Item));
0110 }
0111 }
0112 
0113 #endif