FormatVariadicDetails.h 5.5 KB

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