[llvm-objcopy] [COFF] Implmement --strip-unneeded and -x/--discard-all for symbols
[llvm-complete.git] / tools / llvm-pdbutil / FormatUtil.h
blob9a003c9285c9484d6ab06aa508db891e16ead709
1 //===- FormatUtil.h ------------------------------------------- *- 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 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TOOLS_LLVMPDBUTIL_FORMAT_UTIL_H
11 #define LLVM_TOOLS_LLVMPDBUTIL_FORMAT_UTIL_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/DebugInfo/CodeView/CodeView.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/FormatAdapters.h"
18 #include "llvm/Support/FormatVariadic.h"
20 #include <string>
21 #include <type_traits>
23 namespace llvm {
24 namespace pdb {
26 std::string truncateStringBack(StringRef S, uint32_t MaxLen);
27 std::string truncateStringMiddle(StringRef S, uint32_t MaxLen);
28 std::string truncateStringFront(StringRef S, uint32_t MaxLen);
29 std::string truncateQuotedNameFront(StringRef Label, StringRef Name,
30 uint32_t MaxLen);
31 std::string truncateQuotedNameBack(StringRef Label, StringRef Name,
32 uint32_t MaxLen);
34 #define PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, Text) \
35 if (Enum::TheOpt == (Value & Mask)) \
36 Opts.push_back(Text);
38 #define PUSH_FLAG(Enum, TheOpt, Value, Text) \
39 PUSH_MASKED_FLAG(Enum, Enum::TheOpt, TheOpt, Value, Text)
41 #define RETURN_CASE(Enum, X, Ret) \
42 case Enum::X: \
43 return Ret;
45 template <typename T> std::string formatUnknownEnum(T Value) {
46 return formatv("unknown ({0})",
47 static_cast<typename std::underlying_type<T>::type>(Value))
48 .str();
51 std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset);
53 enum class CharacteristicStyle {
54 HeaderDefinition, // format as windows header definition
55 Descriptive, // format as human readable words
57 std::string formatSectionCharacteristics(
58 uint32_t IndentLevel, uint32_t C, uint32_t FlagsPerLine,
59 StringRef Separator,
60 CharacteristicStyle Style = CharacteristicStyle::HeaderDefinition);
62 std::string typesetItemList(ArrayRef<std::string> Opts, uint32_t IndentLevel,
63 uint32_t GroupSize, StringRef Sep);
65 std::string typesetStringList(uint32_t IndentLevel,
66 ArrayRef<StringRef> Strings);
68 std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
69 bool Friendly = true);
70 std::string formatSymbolKind(codeview::SymbolKind K);
71 StringRef formatTypeLeafKind(codeview::TypeLeafKind K);
73 /// Returns the number of digits in the given integer.
74 inline int NumDigits(uint64_t N) {
75 if (N < 10ULL)
76 return 1;
77 if (N < 100ULL)
78 return 2;
79 if (N < 1000ULL)
80 return 3;
81 if (N < 10000ULL)
82 return 4;
83 if (N < 100000ULL)
84 return 5;
85 if (N < 1000000ULL)
86 return 6;
87 if (N < 10000000ULL)
88 return 7;
89 if (N < 100000000ULL)
90 return 8;
91 if (N < 1000000000ULL)
92 return 9;
93 if (N < 10000000000ULL)
94 return 10;
95 if (N < 100000000000ULL)
96 return 11;
97 if (N < 1000000000000ULL)
98 return 12;
99 if (N < 10000000000000ULL)
100 return 13;
101 if (N < 100000000000000ULL)
102 return 14;
103 if (N < 1000000000000000ULL)
104 return 15;
105 if (N < 10000000000000000ULL)
106 return 16;
107 if (N < 100000000000000000ULL)
108 return 17;
109 if (N < 1000000000000000000ULL)
110 return 18;
111 if (N < 10000000000000000000ULL)
112 return 19;
113 return 20;
116 namespace detail {
117 template <typename T>
118 struct EndianAdapter final
119 : public FormatAdapter<support::detail::packed_endian_specific_integral<
120 T, support::little, support::unaligned>> {
121 using EndianType =
122 support::detail::packed_endian_specific_integral<T, support::little,
123 support::unaligned>;
125 explicit EndianAdapter(EndianType &&Item)
126 : FormatAdapter<EndianType>(std::move(Item)) {}
128 void format(llvm::raw_ostream &Stream, StringRef Style) {
129 format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
132 } // namespace detail
134 template <typename T>
135 detail::EndianAdapter<T>
136 fmtle(support::detail::packed_endian_specific_integral<T, support::little,
137 support::unaligned>
138 Value) {
139 return detail::EndianAdapter<T>(std::move(Value));
142 } // namespace llvm
143 #endif