FormatAdapters.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_FORMATADAPTERS_H
  14. #define LLVM_SUPPORT_FORMATADAPTERS_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/Error.h"
  17. #include "llvm/Support/FormatCommon.h"
  18. #include "llvm/Support/FormatVariadicDetails.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. namespace llvm {
  21. template <typename T> class FormatAdapter : public detail::format_adapter {
  22. protected:
  23. explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
  24. T Item;
  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. public:
  32. AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
  33. : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
  34. Fill(Fill) {}
  35. void format(llvm::raw_ostream &Stream, StringRef Style) override {
  36. auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
  37. FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
  38. }
  39. };
  40. template <typename T> class PadAdapter final : public FormatAdapter<T> {
  41. size_t Left;
  42. size_t Right;
  43. public:
  44. PadAdapter(T &&Item, size_t Left, size_t Right)
  45. : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
  46. void format(llvm::raw_ostream &Stream, StringRef Style) override {
  47. auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
  48. Stream.indent(Left);
  49. Adapter.format(Stream, Style);
  50. Stream.indent(Right);
  51. }
  52. };
  53. template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
  54. size_t Count;
  55. public:
  56. RepeatAdapter(T &&Item, size_t Count)
  57. : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
  58. void format(llvm::raw_ostream &Stream, StringRef Style) override {
  59. auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
  60. for (size_t I = 0; I < Count; ++I) {
  61. Adapter.format(Stream, Style);
  62. }
  63. }
  64. };
  65. class ErrorAdapter : public FormatAdapter<Error> {
  66. public:
  67. ErrorAdapter(Error &&Item) : FormatAdapter(std::move(Item)) {}
  68. ErrorAdapter(ErrorAdapter &&) = default;
  69. ~ErrorAdapter() { consumeError(std::move(Item)); }
  70. void format(llvm::raw_ostream &Stream, StringRef Style) override {
  71. Stream << Item;
  72. }
  73. };
  74. }
  75. template <typename T>
  76. detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
  77. char Fill = ' ') {
  78. return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
  79. }
  80. template <typename T>
  81. detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
  82. return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
  83. }
  84. template <typename T>
  85. detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
  86. return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
  87. }
  88. // llvm::Error values must be consumed before being destroyed.
  89. // Wrapping an error in fmt_consume explicitly indicates that the formatv_object
  90. // should take ownership and consume it.
  91. inline detail::ErrorAdapter fmt_consume(Error &&Item) {
  92. return detail::ErrorAdapter(std::move(Item));
  93. }
  94. }
  95. #endif
  96. #ifdef __GNUC__
  97. #pragma GCC diagnostic pop
  98. #endif