FormatVariadicDetails.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //===- FormatVariadicDetails.h - Helpers for FormatVariadic.h ----*- 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. #ifndef LLVM_SUPPORT_FORMATVARIADICDETAILS_H
  9. #define LLVM_SUPPORT_FORMATVARIADICDETAILS_H
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <type_traits>
  13. namespace llvm {
  14. template <typename T, typename Enable = void> struct format_provider {};
  15. class Error;
  16. namespace detail {
  17. class format_adapter {
  18. virtual void anchor();
  19. protected:
  20. virtual ~format_adapter() {}
  21. public:
  22. virtual void format(raw_ostream &S, StringRef Options) = 0;
  23. };
  24. template <typename T> class provider_format_adapter : public format_adapter {
  25. T Item;
  26. public:
  27. explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
  28. void format(llvm::raw_ostream &S, StringRef Options) override {
  29. format_provider<std::decay_t<T>>::format(Item, S, Options);
  30. }
  31. };
  32. template <typename T>
  33. class stream_operator_format_adapter : public format_adapter {
  34. T Item;
  35. public:
  36. explicit stream_operator_format_adapter(T &&Item)
  37. : Item(std::forward<T>(Item)) {}
  38. void format(llvm::raw_ostream &S, StringRef) override { S << Item; }
  39. };
  40. template <typename T> class missing_format_adapter;
  41. // Test if format_provider<T> is defined on T and contains a member function
  42. // with the signature:
  43. // static void format(const T&, raw_stream &, StringRef);
  44. //
  45. template <class T> class has_FormatProvider {
  46. public:
  47. using Decayed = std::decay_t<T>;
  48. typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
  49. StringRef);
  50. template <typename U>
  51. static char test(SameType<Signature_format, &U::format> *);
  52. template <typename U> static double test(...);
  53. static bool const value =
  54. (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
  55. };
  56. // Test if raw_ostream& << T -> raw_ostream& is findable via ADL.
  57. template <class T> class has_StreamOperator {
  58. public:
  59. using ConstRefT = const std::decay_t<T> &;
  60. template <typename U>
  61. static char test(
  62. std::enable_if_t<std::is_same<decltype(std::declval<llvm::raw_ostream &>()
  63. << std::declval<U>()),
  64. llvm::raw_ostream &>::value,
  65. int *>);
  66. template <typename U> static double test(...);
  67. static bool const value = (sizeof(test<ConstRefT>(nullptr)) == 1);
  68. };
  69. // Simple template that decides whether a type T should use the member-function
  70. // based format() invocation.
  71. template <typename T>
  72. struct uses_format_member
  73. : public std::integral_constant<
  74. bool,
  75. std::is_base_of<format_adapter, std::remove_reference_t<T>>::value> {
  76. };
  77. // Simple template that decides whether a type T should use the format_provider
  78. // based format() invocation. The member function takes priority, so this test
  79. // will only be true if there is not ALSO a format member.
  80. template <typename T>
  81. struct uses_format_provider
  82. : public std::integral_constant<
  83. bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
  84. };
  85. // Simple template that decides whether a type T should use the operator<<
  86. // based format() invocation. This takes last priority.
  87. template <typename T>
  88. struct uses_stream_operator
  89. : public std::integral_constant<bool, !uses_format_member<T>::value &&
  90. !uses_format_provider<T>::value &&
  91. has_StreamOperator<T>::value> {};
  92. // Simple template that decides whether a type T has neither a member-function
  93. // nor format_provider based implementation that it can use. Mostly used so
  94. // that the compiler spits out a nice diagnostic when a type with no format
  95. // implementation can be located.
  96. template <typename T>
  97. struct uses_missing_provider
  98. : public std::integral_constant<bool, !uses_format_member<T>::value &&
  99. !uses_format_provider<T>::value &&
  100. !uses_stream_operator<T>::value> {
  101. };
  102. template <typename T>
  103. std::enable_if_t<uses_format_member<T>::value, T>
  104. build_format_adapter(T &&Item) {
  105. return std::forward<T>(Item);
  106. }
  107. template <typename T>
  108. std::enable_if_t<uses_format_provider<T>::value, provider_format_adapter<T>>
  109. build_format_adapter(T &&Item) {
  110. return provider_format_adapter<T>(std::forward<T>(Item));
  111. }
  112. template <typename T>
  113. std::enable_if_t<uses_stream_operator<T>::value,
  114. stream_operator_format_adapter<T>>
  115. build_format_adapter(T &&Item) {
  116. // If the caller passed an Error by value, then stream_operator_format_adapter
  117. // would be responsible for consuming it.
  118. // Make the caller opt into this by calling fmt_consume().
  119. static_assert(
  120. !std::is_same<llvm::Error, std::remove_cv_t<T>>::value,
  121. "llvm::Error-by-value must be wrapped in fmt_consume() for formatv");
  122. return stream_operator_format_adapter<T>(std::forward<T>(Item));
  123. }
  124. template <typename T>
  125. std::enable_if_t<uses_missing_provider<T>::value, missing_format_adapter<T>>
  126. build_format_adapter(T &&) {
  127. return missing_format_adapter<T>();
  128. }
  129. }
  130. }
  131. #endif