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