]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbutil/FormatUtil.h
Merge ^/head r319973 through 321382.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbutil / FormatUtil.h
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/Support/Endian.h"
16 #include "llvm/Support/FormatAdapters.h"
17 #include "llvm/Support/FormatVariadic.h"
18
19 #include <string>
20 #include <type_traits>
21
22 namespace llvm {
23 namespace pdb {
24
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);
32
33 #define PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, Text)                      \
34   if (Enum::TheOpt == (Value & Mask))                                          \
35     Opts.push_back(Text);
36
37 #define PUSH_FLAG(Enum, TheOpt, Value, Text)                                   \
38   PUSH_MASKED_FLAG(Enum, Enum::TheOpt, TheOpt, Value, Text)
39
40 #define RETURN_CASE(Enum, X, Ret)                                              \
41   case Enum::X:                                                                \
42     return Ret;
43
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();
48 }
49
50 std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset);
51
52 std::string typesetItemList(ArrayRef<std::string> Opts, uint32_t IndentLevel,
53                             uint32_t GroupSize, StringRef Sep);
54
55 std::string typesetStringList(uint32_t IndentLevel,
56                               ArrayRef<StringRef> Strings);
57
58 /// Returns the number of digits in the given integer.
59 inline int NumDigits(uint64_t N) {
60   if (N < 10ULL)
61     return 1;
62   if (N < 100ULL)
63     return 2;
64   if (N < 1000ULL)
65     return 3;
66   if (N < 10000ULL)
67     return 4;
68   if (N < 100000ULL)
69     return 5;
70   if (N < 1000000ULL)
71     return 6;
72   if (N < 10000000ULL)
73     return 7;
74   if (N < 100000000ULL)
75     return 8;
76   if (N < 1000000000ULL)
77     return 9;
78   if (N < 10000000000ULL)
79     return 10;
80   if (N < 100000000000ULL)
81     return 11;
82   if (N < 1000000000000ULL)
83     return 12;
84   if (N < 10000000000000ULL)
85     return 13;
86   if (N < 100000000000000ULL)
87     return 14;
88   if (N < 1000000000000000ULL)
89     return 15;
90   if (N < 10000000000000000ULL)
91     return 16;
92   if (N < 100000000000000000ULL)
93     return 17;
94   if (N < 1000000000000000000ULL)
95     return 18;
96   if (N < 10000000000000000000ULL)
97     return 19;
98   return 20;
99 }
100
101 namespace detail {
102 template <typename T>
103 struct EndianAdapter final
104     : public FormatAdapter<support::detail::packed_endian_specific_integral<
105           T, support::little, support::unaligned>> {
106   using EndianType =
107       support::detail::packed_endian_specific_integral<T, support::little,
108                                                        support::unaligned>;
109
110   explicit EndianAdapter(EndianType &&Item)
111       : FormatAdapter<EndianType>(std::move(Item)) {}
112
113   void format(llvm::raw_ostream &Stream, StringRef Style) {
114     format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
115   }
116 };
117 } // namespace detail
118
119 template <typename T>
120 detail::EndianAdapter<T>
121 fmtle(support::detail::packed_endian_specific_integral<T, support::little,
122                                                        support::unaligned>
123           Value) {
124   return detail::EndianAdapter<T>(std::move(Value));
125 }
126 }
127 } // namespace llvm
128 #endif