1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-02-01 05:01:59 +01:00
llvm-mirror/include/llvm/Support/FormatVariadicDetails.h
Pavel Labath 883b408865 Improve format member detection in llvm::formatv
Summary:
The existing detection of a format member function has a couple of deficiencies:
- the member function does not get detected if one calls formatv with an lvalue,
  because the template parameter gets deduced as T&, which fails the is_class
  check.
- it also did not work if the function was called with a const variable because
  the template parameter would get deduced as const T&, again failing the
  is_class check.

This fixes the problem by stripping the references in the uses_format_member
template, to make sure the type is correctly detected as class. It also provides
specializations of the has_FormatMember template for const and non-const members
of the types in order to enable declaring the format member as a "const"
function. I have added tests that verify that formatv can be now called in these
scenarios. As some scenarios could not be verified at runtime (e.g. making sure
that calling a non-const format member on a const object does *not* compile), I
have also added some static_asserts which test the behaviour of the template
classes used internally by formatv().

Reviewers: zturner

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D27525

llvm-svn: 289040
2016-12-08 11:31:19 +00:00

170 lines
5.3 KiB
C++

//===- FormatVariadicDetails.h - Helpers for FormatVariadic.h ----*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
#define LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include <type_traits>
namespace llvm {
template <typename T, typename Enable = void> struct format_provider {};
namespace detail {
class format_wrapper {
protected:
virtual ~format_wrapper() {}
public:
virtual void format(llvm::raw_ostream &S, StringRef Options) = 0;
};
template <typename T> class member_format_wrapper : public format_wrapper {
T Item;
public:
explicit member_format_wrapper(T &&Item) : Item(Item) {}
void format(llvm::raw_ostream &S, StringRef Options) override {
Item.format(S, Options);
}
};
template <typename T> class provider_format_wrapper : public format_wrapper {
T Item;
public:
explicit provider_format_wrapper(T &&Item) : Item(Item) {}
void format(llvm::raw_ostream &S, StringRef Options) override {
format_provider<typename std::decay<T>::type>::format(Item, S, Options);
}
};
template <typename T> class missing_format_wrapper;
// Test if T is a class that contains a member function with the signature:
//
// void format(raw_ostream &, StringRef);
//
// It is assumed T is a non-reference type.
template <class T, class Enable = void> class has_FormatMember {
public:
static bool const value = false;
};
template <class T>
class has_FormatMember<T,
typename std::enable_if<std::is_class<T>::value &&
std::is_const<T>::value>::type> {
using CleanT = typename std::remove_volatile<T>::type;
using Signature_format = void (CleanT::*)(llvm::raw_ostream &S,
StringRef Options) const;
template <typename U>
static char test2(SameType<Signature_format, &U::format> *);
template <typename U> static double test2(...);
public:
static bool const value = (sizeof(test2<CleanT>(nullptr)) == 1);
};
template <class T>
class has_FormatMember<
T, typename std::enable_if<std::is_class<T>::value &&
!std::is_const<T>::value>::type> {
using CleanT = typename std::remove_cv<T>::type;
using Signature_format = void (CleanT::*)(llvm::raw_ostream &S,
StringRef Options);
template <typename U>
static char test2(SameType<Signature_format, &U::format> *);
template <typename U> static double test2(...);
public:
static bool const value =
(sizeof(test2<CleanT>(nullptr)) == 1) || has_FormatMember<const T>::value;
};
// Test if format_provider<T> is defined on T and contains a member function
// with the signature:
// static void format(const T&, raw_stream &, StringRef);
//
template <class T> class has_FormatProvider {
public:
using Decayed = typename std::decay<T>::type;
typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
StringRef);
template <typename U>
static char test(SameType<Signature_format, &U::format> *);
template <typename U> static double test(...);
static bool const value =
(sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
};
// Simple template that decides whether a type T should use the member-function
// based format() invocation.
template <typename T>
struct uses_format_member
: public std::integral_constant<
bool,
has_FormatMember<typename std::remove_reference<T>::type>::value> {};
// Simple template that decides whether a type T should use the format_provider
// based format() invocation. The member function takes priority, so this test
// will only be true if there is not ALSO a format member.
template <typename T>
struct uses_format_provider
: public std::integral_constant<
bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
};
// Simple template that decides whether a type T has neither a member-function
// nor format_provider based implementation that it can use. Mostly used so
// that the compiler spits out a nice diagnostic when a type with no format
// implementation can be located.
template <typename T>
struct uses_missing_provider
: public std::integral_constant<bool,
!uses_format_member<T>::value &&
!uses_format_provider<T>::value> {};
template <typename T>
typename std::enable_if<uses_format_member<T>::value,
member_format_wrapper<T>>::type
build_format_wrapper(T &&Item) {
return member_format_wrapper<T>(std::forward<T>(Item));
}
template <typename T>
typename std::enable_if<uses_format_provider<T>::value,
provider_format_wrapper<T>>::type
build_format_wrapper(T &&Item) {
return provider_format_wrapper<T>(std::forward<T>(Item));
}
template <typename T>
typename std::enable_if<uses_missing_provider<T>::value,
missing_format_wrapper<T>>::type
build_format_wrapper(T &&Item) {
return missing_format_wrapper<T>();
}
}
}
#endif