LLVM 19.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"
13#include "llvm/ADT/STLExtras.h"
15
16#include <type_traits>
17
18namespace llvm {
19template <typename T, typename Enable = void> struct format_provider {};
20class Error;
21
22namespace detail {
24 virtual void anchor();
25
26protected:
27 virtual ~format_adapter() = default;
28
29public:
30 virtual void format(raw_ostream &S, StringRef Options) = 0;
31};
32
33template <typename T> class provider_format_adapter : public format_adapter {
34 T Item;
35
36public:
37 explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
38
41 }
42};
43
44template <typename T>
46 T Item;
47
48public:
50 : Item(std::forward<T>(Item)) {}
51
52 void format(llvm::raw_ostream &S, StringRef) override { S << Item; }
53};
54
55template <typename T> class missing_format_adapter;
56
57// Test if format_provider<T> is defined on T and contains a member function
58// with the signature:
59// static void format(const T&, raw_stream &, StringRef);
60//
61template <class T> class has_FormatProvider {
62public:
63 using Decayed = std::decay_t<T>;
64 typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
65 StringRef);
66
67 template <typename U>
69
70 template <typename U> static double test(...);
71
72 static bool const value =
73 (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
74};
75
76// Test if raw_ostream& << T -> raw_ostream& is findable via ADL.
77template <class T> class has_StreamOperator {
78public:
79 using ConstRefT = const std::decay_t<T> &;
80
81 template <typename U>
82 static char test(std::enable_if_t<
83 std::is_same_v<decltype(std::declval<llvm::raw_ostream &>()
84 << std::declval<U>()),
86 int *>);
87
88 template <typename U> static double test(...);
89
90 static bool const value = (sizeof(test<ConstRefT>(nullptr)) == 1);
91};
92
93// Simple template that decides whether a type T should use the member-function
94// based format() invocation.
95template <typename T>
97 : public std::integral_constant<
98 bool, std::is_base_of_v<format_adapter, std::remove_reference_t<T>>> {
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.
104template <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.
112template <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.
122template <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
129template <typename T>
130std::enable_if_t<uses_format_member<T>::value, T>
132 return std::forward<T>(Item);
133}
134
135template <typename T>
136std::enable_if_t<uses_format_provider<T>::value, provider_format_adapter<T>>
138 return provider_format_adapter<T>(std::forward<T>(Item));
139}
140
141template <typename T>
142std::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_v<llvm::Error, std::remove_cv_t<T>>,
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
154template <typename T>
155std::enable_if_t<uses_missing_provider<T>::value, missing_format_adapter<T>>
158}
159}
160}
161
162#endif
static LVOptions Options
Definition: LVOptions.cpp:25
This file contains some templates that are useful if you are working with the STL at all.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
virtual void format(raw_ostream &S, StringRef Options)=0
virtual ~format_adapter()=default
static char test(SameType< Signature_format, &U::format > *)
void(* Signature_format)(const Decayed &, llvm::raw_ostream &, StringRef)
static char test(std::enable_if_t< std::is_same_v< decltype(std::declval< llvm::raw_ostream & >()<< std::declval< U >()), llvm::raw_ostream & >, int * >)
const std::decay_t< T > & ConstRefT
void format(llvm::raw_ostream &S, StringRef Options) override
void format(llvm::raw_ostream &S, StringRef) override
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
std::enable_if_t< uses_format_member< T >::value, T > build_format_adapter(T &&Item)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858