LLVM  4.0.0
FormatCommon.h
Go to the documentation of this file.
1 //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_FORMATCOMMON_H
11 #define LLVM_SUPPORT_FORMATCOMMON_H
12 
13 #include "llvm/ADT/SmallString.h"
16 
17 namespace llvm {
18 enum class AlignStyle { Left, Center, Right };
19 
20 struct FmtAlign {
23  size_t Amount;
24 
26  : Adapter(Adapter), Where(Where), Amount(Amount) {}
27 
28  void format(raw_ostream &S, StringRef Options) {
29  // If we don't need to align, we can format straight into the underlying
30  // stream. Otherwise we have to go through an intermediate stream first
31  // in order to calculate how long the output is so we can align it.
32  // TODO: Make the format method return the number of bytes written, that
33  // way we can also skip the intermediate stream for left-aligned output.
34  if (Amount == 0) {
35  Adapter.format(S, Options);
36  return;
37  }
38  SmallString<64> Item;
39  raw_svector_ostream Stream(Item);
40 
41  Adapter.format(Stream, Options);
42  if (Amount <= Item.size()) {
43  S << Item;
44  return;
45  }
46 
47  size_t PadAmount = Amount - Item.size();
48  switch (Where) {
49  case AlignStyle::Left:
50  S << Item;
51  S.indent(PadAmount);
52  break;
53  case AlignStyle::Center: {
54  size_t X = PadAmount / 2;
55  S.indent(X);
56  S << Item;
57  S.indent(PadAmount - X);
58  break;
59  }
60  default:
61  S.indent(PadAmount);
62  S << Item;
63  break;
64  }
65  }
66 };
67 }
68 
69 #endif
AlignStyle Where
Definition: FormatCommon.h:22
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
virtual void format(raw_ostream &S, StringRef Options)=0
void format(raw_ostream &S, StringRef Options)
Definition: FormatCommon.h:28
FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount)
Definition: FormatCommon.h:25
AlignStyle
Definition: FormatCommon.h:18
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
detail::format_adapter & Adapter
Definition: FormatCommon.h:21
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47