1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00
llvm-mirror/include/llvm/Support/FormatCommon.h
Pavel Labath 1d2d65504a Simplify format member detection in FormatVariadic
Summary:
This replaces the format member search, which was quite complicated, with a more
direct approach to detecting whether a class should be formatted using the
format-member method. Instead we use a special type llvm::format_adapter, which
every adapter must inherit from. Then the search can be simply implemented with
the is_base_of type trait.

Aside from the simplification, I like this way more because it makes it more
explicit that you are supposed to use this type only for adapter-like
formattings, and the other approach (format_provider overloads) should be used
as a default (a mistake I made when first trying to use this library).

The only slight change in behaviour here is that now choose the format-adapter
branch even if the format member invocation will fail to compile (e.g. because it is a
non-const member function and we are passing a const adapter), whereas
previously we would have gone on to search for format_providers for the type.
However, I think that is actually a good thing, as it probably means the
programmer did something wrong.

Reviewers: zturner, inglorion

Subscribers: llvm-commits

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

llvm-svn: 289795
2016-12-15 09:40:27 +00:00

70 lines
1.9 KiB
C++

//===- FormatAdapters.h - Formatters for common LLVM types -----*- 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_FORMATCOMMON_H
#define LLVM_SUPPORT_FORMATCOMMON_H
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FormatVariadicDetails.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
enum class AlignStyle { Left, Center, Right };
struct FmtAlign {
detail::format_adapter &Adapter;
AlignStyle Where;
size_t Amount;
FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount)
: Adapter(Adapter), Where(Where), Amount(Amount) {}
void format(raw_ostream &S, StringRef Options) {
// If we don't need to align, we can format straight into the underlying
// stream. Otherwise we have to go through an intermediate stream first
// in order to calculate how long the output is so we can align it.
// TODO: Make the format method return the number of bytes written, that
// way we can also skip the intermediate stream for left-aligned output.
if (Amount == 0) {
Adapter.format(S, Options);
return;
}
SmallString<64> Item;
raw_svector_ostream Stream(Item);
Adapter.format(Stream, Options);
if (Amount <= Item.size()) {
S << Item;
return;
}
size_t PadAmount = Amount - Item.size();
switch (Where) {
case AlignStyle::Left:
S << Item;
S.indent(PadAmount);
break;
case AlignStyle::Center: {
size_t X = PadAmount / 2;
S.indent(X);
S << Item;
S.indent(PadAmount - X);
break;
}
default:
S.indent(PadAmount);
S << Item;
break;
}
}
};
}
#endif