blob: 7804a1f0e23054443edc225284fe8002f1228008 [file] [log] [blame]
Zachary Turner63055452017-06-15 22:24:24 +00001//===- 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"
Zachary Turnerd1de2f42017-08-21 14:53:25 +000015#include "llvm/DebugInfo/CodeView/CodeView.h"
Zachary Turner63055452017-06-15 22:24:24 +000016#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
23namespace llvm {
24namespace pdb {
25
Zachary Turner8120ebf2017-07-05 21:54:58 +000026std::string truncateStringBack(StringRef S, uint32_t MaxLen);
Zachary Turnerf3b4b2d2017-07-07 18:45:37 +000027std::string truncateStringMiddle(StringRef S, uint32_t MaxLen);
David Blaikiefd8777e2017-07-06 05:33:32 +000028std::string truncateStringFront(StringRef S, uint32_t MaxLen);
Zachary Turner8120ebf2017-07-05 21:54:58 +000029std::string truncateQuotedNameFront(StringRef Label, StringRef Name,
30 uint32_t MaxLen);
31std::string truncateQuotedNameBack(StringRef Label, StringRef Name,
32 uint32_t MaxLen);
33
Zachary Turner63055452017-06-15 22:24:24 +000034#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
David Blaikiefd8777e2017-07-06 05:33:32 +000045template <typename T> std::string formatUnknownEnum(T Value) {
Zachary Turner63055452017-06-15 22:24:24 +000046 return formatv("unknown ({0})",
47 static_cast<typename std::underlying_type<T>::type>(Value))
48 .str();
49}
50
51std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset);
52
Zachary Turner28e31ee2017-08-11 20:46:28 +000053enum class CharacteristicStyle {
54 HeaderDefinition, // format as windows header definition
55 Descriptive, // format as human readable words
56};
57std::string formatSectionCharacteristics(
58 uint32_t IndentLevel, uint32_t C, uint32_t FlagsPerLine,
59 StringRef Separator,
60 CharacteristicStyle Style = CharacteristicStyle::HeaderDefinition);
61
Zachary Turner63055452017-06-15 22:24:24 +000062std::string typesetItemList(ArrayRef<std::string> Opts, uint32_t IndentLevel,
63 uint32_t GroupSize, StringRef Sep);
64
65std::string typesetStringList(uint32_t IndentLevel,
66 ArrayRef<StringRef> Strings);
67
Zachary Turnerd1de2f42017-08-21 14:53:25 +000068std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
69 bool Friendly = true);
70std::string formatSymbolKind(codeview::SymbolKind K);
71
Zachary Turner63055452017-06-15 22:24:24 +000072/// Returns the number of digits in the given integer.
73inline 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;
113}
114
115namespace detail {
116template <typename T>
117struct 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>;
123
124 explicit EndianAdapter(EndianType &&Item)
125 : FormatAdapter<EndianType>(std::move(Item)) {}
126
127 void format(llvm::raw_ostream &Stream, StringRef Style) {
128 format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
129 }
130};
131} // namespace detail
132
133template <typename T>
134detail::EndianAdapter<T>
135fmtle(support::detail::packed_endian_specific_integral<T, support::little,
136 support::unaligned>
137 Value) {
138 return detail::EndianAdapter<T>(std::move(Value));
139}
140}
141} // namespace llvm
142#endif