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 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_TOOLS_LLVMPDBUTIL_FORMAT_UTIL_H
11 #define LLVM_TOOLS_LLVMPDBUTIL_FORMAT_UTIL_H
12
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"
19
20 #include <string>
21 #include <type_traits>
22
23 namespace llvm {
24 namespace pdb {
25
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);
33
34 #define PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, Text) \
35 if (Enum::TheOpt == (Value & Mask)) \
36 Opts.push_back(Text);
37
38 #define PUSH_FLAG(Enum, TheOpt, Value, Text) \
39 PUSH_MASKED_FLAG(Enum, Enum::TheOpt, TheOpt, Value, Text)
40
41 #define RETURN_CASE(Enum, X, Ret) \
42 case Enum::X: \
43 return Ret;
44
formatUnknownEnum(T Value)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();
49 }
50
51 std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset);
52
53 enum class CharacteristicStyle {
54 HeaderDefinition, // format as windows header definition
55 Descriptive, // format as human readable words
56 };
57 std::string formatSectionCharacteristics(
58 uint32_t IndentLevel, uint32_t C, uint32_t FlagsPerLine,
59 StringRef Separator,
60 CharacteristicStyle Style = CharacteristicStyle::HeaderDefinition);
61
62 std::string typesetItemList(ArrayRef<std::string> Opts, uint32_t IndentLevel,
63 uint32_t GroupSize, StringRef Sep);
64
65 std::string typesetStringList(uint32_t IndentLevel,
66 ArrayRef<StringRef> Strings);
67
68 std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
69 bool Friendly = true);
70 std::string formatSymbolKind(codeview::SymbolKind K);
71 StringRef formatTypeLeafKind(codeview::TypeLeafKind K);
72
73 /// Returns the number of digits in the given integer.
NumDigits(uint64_t N)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;
114 }
115
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>;
124
EndianAdapterfinal125 explicit EndianAdapter(EndianType &&Item)
126 : FormatAdapter<EndianType>(std::move(Item)) {}
127
formatfinal128 void format(llvm::raw_ostream &Stream, StringRef Style) {
129 format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
130 }
131 };
132 } // namespace detail
133
134 template <typename T>
135 detail::EndianAdapter<T>
fmtle(support::detail::packed_endian_specific_integral<T,support::little,support::unaligned> Value)136 fmtle(support::detail::packed_endian_specific_integral<T, support::little,
137 support::unaligned>
138 Value) {
139 return detail::EndianAdapter<T>(std::move(Value));
140 }
141 }
142 } // namespace llvm
143 #endif
144