LLVM 23.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/STLExtras.h"
14#include "llvm/ADT/StringRef.h"
17
18#include <type_traits>
19
20namespace llvm {
21template <typename T, typename Enable = void> struct format_provider {};
22class Error;
23
24namespace support {
25namespace detail {
26
28
29template <typename T> class FormatFunctor {
30 // If the caller passed an Error by value, then we would be responsible for
31 // consuming it. Make the caller opt into this by calling fmt_consume().
32 static_assert(
33 !std::is_same_v<llvm::Error, std::remove_cv_t<T>>,
34 "llvm::Error-by-value must be wrapped in fmt_consume() for formatv");
35
36 T Item;
37
38 using DecayedT = std::decay_t<T>;
39 using Signature_format = void (*)(const DecayedT &, llvm::raw_ostream &,
40 StringRef);
41
42 template <typename U>
43 using MemberFormatCheck = decltype(std::declval<U>().format(
44 std::declval<llvm::raw_ostream &>(), std::declval<llvm::StringRef>()));
45 template <typename U>
46 using StaticFormatCheck = SameType<Signature_format, &U::format>;
47 template <typename U>
48 using StreamCheck = std::is_same<decltype(std::declval<llvm::raw_ostream &>()
49 << std::declval<U>()),
51
52public:
53 static constexpr bool HasMemberProvider =
55 static constexpr bool HasFormatProvider =
56 llvm::is_detected<StaticFormatCheck,
58 static constexpr bool HasStreamProvider =
60
62 "type has no format provider");
63
64 explicit FormatFunctor(T &&Item) : Item(std::forward<T>(Item)) {}
65
67 if constexpr (HasMemberProvider)
68 Item.format(S, Options);
69 else if constexpr (HasFormatProvider)
71 else if constexpr (HasStreamProvider)
72 S << Item;
73 }
74};
75
76template <typename T> FormatFunctor(T &&) -> FormatFunctor<T>;
77
78} // namespace detail
79} // namespace support
80} // namespace llvm
81
82#endif
static LVOptions Options
Definition LVOptions.cpp:25
#define T
This file contains some templates that are useful if you are working with the STL at all.
This file contains library features backported from future STL versions.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void operator()(llvm::raw_ostream &S, StringRef Options)
function_ref< void(llvm::raw_ostream &, StringRef)> FormatFunctorRef
FormatFunctor(T &&) -> FormatFunctor< T >
This is an optimization pass for GlobalISel generic memory operations.
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860