xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/PDB/Native/FormatUtil.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_FORMATUTIL_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_FORMATUTIL_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLForwardCompat.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/DebugInfo/CodeView/CodeView.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/FormatAdapters.h"
19 #include "llvm/Support/FormatVariadic.h"
20 
21 #include <string>
22 #include <type_traits>
23 
24 namespace llvm {
25 namespace pdb {
26 
27 #define PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, Text)                      \
28   if (Enum::TheOpt == (Value & Mask))                                          \
29     Opts.push_back(Text);
30 
31 #define PUSH_FLAG(Enum, TheOpt, Value, Text)                                   \
32   PUSH_MASKED_FLAG(Enum, Enum::TheOpt, TheOpt, Value, Text)
33 
34 #define RETURN_CASE(Enum, X, Ret)                                              \
35   case Enum::X:                                                                \
36     return Ret;
37 
formatUnknownEnum(T Value)38 template <typename T> std::string formatUnknownEnum(T Value) {
39   return formatv("unknown ({0})", llvm::to_underlying(Value)).str();
40 }
41 
42 LLVM_ABI std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset);
43 
44 enum class CharacteristicStyle {
45   HeaderDefinition, // format as windows header definition
46   Descriptive,      // format as human readable words
47 };
48 LLVM_ABI std::string formatSectionCharacteristics(
49     uint32_t IndentLevel, uint32_t C, uint32_t FlagsPerLine,
50     StringRef Separator,
51     CharacteristicStyle Style = CharacteristicStyle::HeaderDefinition);
52 
53 LLVM_ABI std::string typesetItemList(ArrayRef<std::string> Opts,
54                                      uint32_t IndentLevel, uint32_t GroupSize,
55                                      StringRef Sep);
56 
57 LLVM_ABI std::string typesetStringList(uint32_t IndentLevel,
58                                        ArrayRef<StringRef> Strings);
59 
60 LLVM_ABI std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
61                                      bool Friendly = true);
62 LLVM_ABI std::string formatSymbolKind(codeview::SymbolKind K);
63 LLVM_ABI std::string formatTypeLeafKind(codeview::TypeLeafKind K);
64 
65 /// Returns the number of digits in the given integer.
NumDigits(uint64_t N)66 inline int NumDigits(uint64_t N) {
67   if (N < 10ULL)
68     return 1;
69   if (N < 100ULL)
70     return 2;
71   if (N < 1000ULL)
72     return 3;
73   if (N < 10000ULL)
74     return 4;
75   if (N < 100000ULL)
76     return 5;
77   if (N < 1000000ULL)
78     return 6;
79   if (N < 10000000ULL)
80     return 7;
81   if (N < 100000000ULL)
82     return 8;
83   if (N < 1000000000ULL)
84     return 9;
85   if (N < 10000000000ULL)
86     return 10;
87   if (N < 100000000000ULL)
88     return 11;
89   if (N < 1000000000000ULL)
90     return 12;
91   if (N < 10000000000000ULL)
92     return 13;
93   if (N < 100000000000000ULL)
94     return 14;
95   if (N < 1000000000000000ULL)
96     return 15;
97   if (N < 10000000000000000ULL)
98     return 16;
99   if (N < 100000000000000000ULL)
100     return 17;
101   if (N < 1000000000000000000ULL)
102     return 18;
103   if (N < 10000000000000000000ULL)
104     return 19;
105   return 20;
106 }
107 
108 namespace detail {
109 template <typename T>
110 struct EndianAdapter final
111     : public FormatAdapter<support::detail::packed_endian_specific_integral<
112           T, llvm::endianness::little, support::unaligned>> {
113   using EndianType = support::detail::packed_endian_specific_integral<
114       T, llvm::endianness::little, support::unaligned>;
115 
EndianAdapterfinal116   explicit EndianAdapter(EndianType &&Item)
117       : FormatAdapter<EndianType>(std::move(Item)) {}
118 
formatfinal119   void format(llvm::raw_ostream &Stream, StringRef Style) override {
120     format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
121   }
122 };
123 } // namespace detail
124 
125 template <typename T>
fmtle(support::detail::packed_endian_specific_integral<T,llvm::endianness::little,support::unaligned> Value)126 detail::EndianAdapter<T> fmtle(support::detail::packed_endian_specific_integral<
127                                T, llvm::endianness::little, support::unaligned>
128                                    Value) {
129   return detail::EndianAdapter<T>(std::move(Value));
130 }
131 } // namespace pdb
132 } // namespace llvm
133 #endif
134