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