LLVM  14.0.0git
FormatVariadicDetails.h
Go to the documentation of this file.
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 
9 #ifndef LLVM_SUPPORT_FORMATVARIADICDETAILS_H
10 #define LLVM_SUPPORT_FORMATVARIADICDETAILS_H
11 
12 #include "llvm/ADT/StringRef.h"
14 
15 #include <type_traits>
16 
17 namespace llvm {
18 template <typename T, typename Enable = void> struct format_provider {};
19 class Error;
20 
21 namespace detail {
23  virtual void anchor();
24 
25 protected:
26  virtual ~format_adapter() {}
27 
28 public:
29  virtual void format(raw_ostream &S, StringRef Options) = 0;
30 };
31 
32 template <typename T> class provider_format_adapter : public format_adapter {
33  T Item;
34 
35 public:
36  explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
37 
40  }
41 };
42 
43 template <typename T>
45  T Item;
46 
47 public:
48  explicit stream_operator_format_adapter(T &&Item)
49  : Item(std::forward<T>(Item)) {}
50 
51  void format(llvm::raw_ostream &S, StringRef) override { S << Item; }
52 };
53 
54 template <typename T> class missing_format_adapter;
55 
56 // Test if format_provider<T> is defined on T and contains a member function
57 // with the signature:
58 // static void format(const T&, raw_stream &, StringRef);
59 //
60 template <class T> class has_FormatProvider {
61 public:
62  using Decayed = std::decay_t<T>;
63  typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
64  StringRef);
65 
66  template <typename U>
68 
69  template <typename U> static double test(...);
70 
71  static bool const value =
72  (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
73 };
74 
75 // Test if raw_ostream& << T -> raw_ostream& is findable via ADL.
76 template <class T> class has_StreamOperator {
77 public:
78  using ConstRefT = const std::decay_t<T> &;
79 
80  template <typename U>
81  static char test(
82  std::enable_if_t<std::is_same<decltype(std::declval<llvm::raw_ostream &>()
83  << std::declval<U>()),
85  int *>);
86 
87  template <typename U> static double test(...);
88 
89  static bool const value = (sizeof(test<ConstRefT>(nullptr)) == 1);
90 };
91 
92 // Simple template that decides whether a type T should use the member-function
93 // based format() invocation.
94 template <typename T>
96  : public std::integral_constant<
97  bool,
98  std::is_base_of<format_adapter, std::remove_reference_t<T>>::value> {
99 };
100 
101 // Simple template that decides whether a type T should use the format_provider
102 // based format() invocation. The member function takes priority, so this test
103 // will only be true if there is not ALSO a format member.
104 template <typename T>
106  : public std::integral_constant<
107  bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
108 };
109 
110 // Simple template that decides whether a type T should use the operator<<
111 // based format() invocation. This takes last priority.
112 template <typename T>
114  : public std::integral_constant<bool, !uses_format_member<T>::value &&
115  !uses_format_provider<T>::value &&
116  has_StreamOperator<T>::value> {};
117 
118 // Simple template that decides whether a type T has neither a member-function
119 // nor format_provider based implementation that it can use. Mostly used so
120 // that the compiler spits out a nice diagnostic when a type with no format
121 // implementation can be located.
122 template <typename T>
124  : public std::integral_constant<bool, !uses_format_member<T>::value &&
125  !uses_format_provider<T>::value &&
126  !uses_stream_operator<T>::value> {
127 };
128 
129 template <typename T>
130 std::enable_if_t<uses_format_member<T>::value, T>
132  return std::forward<T>(Item);
133 }
134 
135 template <typename T>
136 std::enable_if_t<uses_format_provider<T>::value, provider_format_adapter<T>>
138  return provider_format_adapter<T>(std::forward<T>(Item));
139 }
140 
141 template <typename T>
142 std::enable_if_t<uses_stream_operator<T>::value,
143  stream_operator_format_adapter<T>>
145  // If the caller passed an Error by value, then stream_operator_format_adapter
146  // would be responsible for consuming it.
147  // Make the caller opt into this by calling fmt_consume().
148  static_assert(
149  !std::is_same<llvm::Error, std::remove_cv_t<T>>::value,
150  "llvm::Error-by-value must be wrapped in fmt_consume() for formatv");
151  return stream_operator_format_adapter<T>(std::forward<T>(Item));
152 }
153 
154 template <typename T>
155 std::enable_if_t<uses_missing_provider<T>::value, missing_format_adapter<T>>
157  return missing_format_adapter<T>();
158 }
159 }
160 }
161 
162 #endif
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::detail::provider_format_adapter::format
void format(llvm::raw_ostream &S, StringRef Options) override
Definition: FormatVariadicDetails.h:38
llvm::format_provider
Definition: FormatVariadicDetails.h:18
StringRef.h
llvm::detail::uses_missing_provider
Definition: FormatVariadicDetails.h:123
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::detail::has_StreamOperator::ConstRefT
const std::decay_t< T > & ConstRefT
Definition: FormatVariadicDetails.h:78
llvm::detail::build_format_adapter
std::enable_if_t< uses_format_member< T >::value, T > build_format_adapter(T &&Item)
Definition: FormatVariadicDetails.h:131
llvm::detail::format_adapter::format
virtual void format(raw_ostream &S, StringRef Options)=0
llvm::detail::format_adapter::~format_adapter
virtual ~format_adapter()
Definition: FormatVariadicDetails.h:26
llvm::detail::has_StreamOperator
Definition: FormatVariadicDetails.h:76
llvm::detail::has_StreamOperator::value
static const bool value
Definition: FormatVariadicDetails.h:89
llvm::detail::uses_format_provider
Definition: FormatVariadicDetails.h:105
llvm::detail::provider_format_adapter
Definition: FormatVariadicDetails.h:32
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::detail::stream_operator_format_adapter
Definition: FormatVariadicDetails.h:44
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::detail::has_FormatProvider::test
static char test(SameType< Signature_format, &U::format > *)
llvm::detail::stream_operator_format_adapter::format
void format(llvm::raw_ostream &S, StringRef) override
Definition: FormatVariadicDetails.h:51
llvm::detail::stream_operator_format_adapter::stream_operator_format_adapter
stream_operator_format_adapter(T &&Item)
Definition: FormatVariadicDetails.h:48
llvm::detail::has_FormatProvider
Definition: FormatVariadicDetails.h:60
llvm::detail::provider_format_adapter::provider_format_adapter
provider_format_adapter(T &&Item)
Definition: FormatVariadicDetails.h:36
llvm::detail::missing_format_adapter
Definition: FormatVariadicDetails.h:54
llvm::detail::has_FormatProvider::Signature_format
void(* Signature_format)(const Decayed &, llvm::raw_ostream &, StringRef)
Definition: FormatVariadicDetails.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::detail::has_FormatProvider::Decayed
std::decay_t< T > Decayed
Definition: FormatVariadicDetails.h:62
std
Definition: BitVector.h:838
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::detail::has_FormatProvider::value
static const bool value
Definition: FormatVariadicDetails.h:71
llvm::SameType
Definition: STLExtras.h:47
llvm::detail::uses_stream_operator
Definition: FormatVariadicDetails.h:113
llvm::detail::has_StreamOperator::test
static char test(std::enable_if_t< std::is_same< decltype(std::declval< llvm::raw_ostream & >()<< std::declval< U >()), llvm::raw_ostream & >::value, int * >)
raw_ostream.h
llvm::detail::uses_format_member
Definition: FormatVariadicDetails.h:95
llvm::detail::format_adapter
Definition: FormatVariadicDetails.h:22