LLVM  14.0.0git
FormatCommon.h
Go to the documentation of this file.
1 //===- FormatCommon.h - Formatters for common LLVM types --------*- 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_FORMATCOMMON_H
10 #define LLVM_SUPPORT_FORMATCOMMON_H
11 
12 #include "llvm/ADT/SmallString.h"
15 
16 namespace llvm {
17 enum class AlignStyle { Left, Center, Right };
18 
19 struct FmtAlign {
22  size_t Amount;
23  char Fill;
24 
26  char Fill = ' ')
28 
30  // If we don't need to align, we can format straight into the underlying
31  // stream. Otherwise we have to go through an intermediate stream first
32  // in order to calculate how long the output is so we can align it.
33  // TODO: Make the format method return the number of bytes written, that
34  // way we can also skip the intermediate stream for left-aligned output.
35  if (Amount == 0) {
37  return;
38  }
39  SmallString<64> Item;
40  raw_svector_ostream Stream(Item);
41 
42  Adapter.format(Stream, Options);
43  if (Amount <= Item.size()) {
44  S << Item;
45  return;
46  }
47 
48  size_t PadAmount = Amount - Item.size();
49  switch (Where) {
50  case AlignStyle::Left:
51  S << Item;
52  fill(S, PadAmount);
53  break;
54  case AlignStyle::Center: {
55  size_t X = PadAmount / 2;
56  fill(S, X);
57  S << Item;
58  fill(S, PadAmount - X);
59  break;
60  }
61  default:
62  fill(S, PadAmount);
63  S << Item;
64  break;
65  }
66  }
67 
68 private:
69  void fill(llvm::raw_ostream &S, uint32_t Count) {
70  for (uint32_t I = 0; I < Count; ++I)
71  S << Fill;
72  }
73 };
74 }
75 
76 #endif
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::FmtAlign::format
void format(raw_ostream &S, StringRef Options)
Definition: FormatCommon.h:29
llvm::detail::format_adapter::format
virtual void format(raw_ostream &S, StringRef Options)=0
llvm::AlignStyle::Left
@ Left
SmallString.h
llvm::FmtAlign::Fill
char Fill
Definition: FormatCommon.h:23
llvm::AlignStyle
AlignStyle
Definition: FormatCommon.h:17
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::SmallString< 64 >
llvm::AlignStyle::Center
@ Center
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::FmtAlign::Amount
size_t Amount
Definition: FormatCommon.h:22
FormatVariadicDetails.h
llvm::FmtAlign::FmtAlign
FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount, char Fill=' ')
Definition: FormatCommon.h:25
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::FmtAlign::Adapter
detail::format_adapter & Adapter
Definition: FormatCommon.h:20
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
llvm::FmtAlign
Definition: FormatCommon.h:19
llvm::FmtAlign::Where
AlignStyle Where
Definition: FormatCommon.h:21
llvm::AlignStyle::Right
@ Right
raw_ostream.h
llvm::detail::format_adapter
Definition: FormatVariadicDetails.h:22