blob: 9a003c9285c9484d6ab06aa508db891e16ead709 [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);
Zachary Turner99c69822017-08-31 20:43:22 +000071StringRef formatTypeLeafKind(codeview::TypeLeafKind K);
Zachary Turnerd1de2f42017-08-21 14:53:25 +000072
Zachary Turner63055452017-06-15 22:24:24 +000073/// Returns the number of digits in the given integer.
74inline int NumDigits(uint64_t N) {
75 if (N < 10ULL)
76 return 1;
77 if (N < 100ULL)
78 return 2;
79 if (N < 1000ULL)
80 return 3;
81 if (N < 10000ULL)
82 return 4;
83 if (N < 100000ULL)
84 return 5;
85 if (N < 1000000ULL)
86 return 6;
87 if (N < 10000000ULL)
88 return 7;
89 if (N < 100000000ULL)
90 return 8;
91 if (N < 1000000000ULL)
92 return 9;
93 if (N < 10000000000ULL)
94 return 10;
95 if (N < 100000000000ULL)
96 return 11;
97 if (N < 1000000000000ULL)
98 return 12;
99 if (N < 10000000000000ULL)
100 return 13;
101 if (N < 100000000000000ULL)
102 return 14;
103 if (N < 1000000000000000ULL)
104 return 15;
105 if (N < 10000000000000000ULL)
106 return 16;
107 if (N < 100000000000000000ULL)
108 return 17;
109 if (N < 1000000000000000000ULL)
110 return 18;
111 if (N < 10000000000000000000ULL)
112 return 19;
113 return 20;
114}
115
116namespace detail {
117template <typename T>
118struct EndianAdapter final
119 : public FormatAdapter<support::detail::packed_endian_specific_integral<
120 T, support::little, support::unaligned>> {
121 using EndianType =
122 support::detail::packed_endian_specific_integral<T, support::little,
123 support::unaligned>;
124
125 explicit EndianAdapter(EndianType &&Item)
126 : FormatAdapter<EndianType>(std::move(Item)) {}
127
128 void format(llvm::raw_ostream &Stream, StringRef Style) {
129 format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
130 }
131};
132} // namespace detail
133
134template <typename T>
135detail::EndianAdapter<T>
136fmtle(support::detail::packed_endian_specific_integral<T, support::little,
137 support::unaligned>
138 Value) {
139 return detail::EndianAdapter<T>(std::move(Value));
140}
141}
142} // namespace llvm
143#endif