Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1 | //===- FormatUtil.h ------------------------------------------- *- C++ --*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_TOOLS_LLVMPDBUTIL_FORMAT_UTIL_H |
| 10 | #define LLVM_TOOLS_LLVMPDBUTIL_FORMAT_UTIL_H |
| 11 | |
| 12 | #include "llvm/ADT/ArrayRef.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame] | 25 | std::string truncateStringBack(StringRef S, uint32_t MaxLen); |
Zachary Turner | f3b4b2d | 2017-07-07 18:45:37 +0000 | [diff] [blame] | 26 | std::string truncateStringMiddle(StringRef S, uint32_t MaxLen); |
David Blaikie | fd8777e | 2017-07-06 05:33:32 +0000 | [diff] [blame] | 27 | std::string truncateStringFront(StringRef S, uint32_t MaxLen); |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 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 | |
David Blaikie | fd8777e | 2017-07-06 05:33:32 +0000 | [diff] [blame] | 44 | template <typename T> std::string formatUnknownEnum(T Value) { |
Justin Lebar | 1bd6123 | 2020-02-10 20:33:08 -0800 | [diff] [blame] | 45 | return formatv("unknown ({0})", static_cast<std::underlying_type_t<T>>(Value)) |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 46 | .str(); |
| 47 | } |
| 48 | |
| 49 | std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset); |
| 50 | |
Zachary Turner | 28e31ee | 2017-08-11 20:46:28 +0000 | [diff] [blame] | 51 | enum class CharacteristicStyle { |
| 52 | HeaderDefinition, // format as windows header definition |
| 53 | Descriptive, // format as human readable words |
| 54 | }; |
| 55 | std::string formatSectionCharacteristics( |
| 56 | uint32_t IndentLevel, uint32_t C, uint32_t FlagsPerLine, |
| 57 | StringRef Separator, |
| 58 | CharacteristicStyle Style = CharacteristicStyle::HeaderDefinition); |
| 59 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 60 | std::string typesetItemList(ArrayRef<std::string> Opts, uint32_t IndentLevel, |
| 61 | uint32_t GroupSize, StringRef Sep); |
| 62 | |
| 63 | std::string typesetStringList(uint32_t IndentLevel, |
| 64 | ArrayRef<StringRef> Strings); |
| 65 | |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 66 | std::string formatChunkKind(codeview::DebugSubsectionKind Kind, |
| 67 | bool Friendly = true); |
| 68 | std::string formatSymbolKind(codeview::SymbolKind K); |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 69 | StringRef formatTypeLeafKind(codeview::TypeLeafKind K); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 70 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 71 | /// Returns the number of digits in the given integer. |
| 72 | inline int NumDigits(uint64_t N) { |
| 73 | if (N < 10ULL) |
| 74 | return 1; |
| 75 | if (N < 100ULL) |
| 76 | return 2; |
| 77 | if (N < 1000ULL) |
| 78 | return 3; |
| 79 | if (N < 10000ULL) |
| 80 | return 4; |
| 81 | if (N < 100000ULL) |
| 82 | return 5; |
| 83 | if (N < 1000000ULL) |
| 84 | return 6; |
| 85 | if (N < 10000000ULL) |
| 86 | return 7; |
| 87 | if (N < 100000000ULL) |
| 88 | return 8; |
| 89 | if (N < 1000000000ULL) |
| 90 | return 9; |
| 91 | if (N < 10000000000ULL) |
| 92 | return 10; |
| 93 | if (N < 100000000000ULL) |
| 94 | return 11; |
| 95 | if (N < 1000000000000ULL) |
| 96 | return 12; |
| 97 | if (N < 10000000000000ULL) |
| 98 | return 13; |
| 99 | if (N < 100000000000000ULL) |
| 100 | return 14; |
| 101 | if (N < 1000000000000000ULL) |
| 102 | return 15; |
| 103 | if (N < 10000000000000000ULL) |
| 104 | return 16; |
| 105 | if (N < 100000000000000000ULL) |
| 106 | return 17; |
| 107 | if (N < 1000000000000000000ULL) |
| 108 | return 18; |
| 109 | if (N < 10000000000000000000ULL) |
| 110 | return 19; |
| 111 | return 20; |
| 112 | } |
| 113 | |
| 114 | namespace detail { |
| 115 | template <typename T> |
| 116 | struct EndianAdapter final |
| 117 | : public FormatAdapter<support::detail::packed_endian_specific_integral< |
| 118 | T, support::little, support::unaligned>> { |
| 119 | using EndianType = |
| 120 | support::detail::packed_endian_specific_integral<T, support::little, |
| 121 | support::unaligned>; |
| 122 | |
| 123 | explicit EndianAdapter(EndianType &&Item) |
| 124 | : FormatAdapter<EndianType>(std::move(Item)) {} |
| 125 | |
Logan Smith | 3ee7fe4 | 2020-07-16 20:38:41 -0700 | [diff] [blame] | 126 | void format(llvm::raw_ostream &Stream, StringRef Style) override { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 127 | format_provider<T>::format(static_cast<T>(this->Item), Stream, Style); |
| 128 | } |
| 129 | }; |
| 130 | } // namespace detail |
| 131 | |
| 132 | template <typename T> |
| 133 | detail::EndianAdapter<T> |
| 134 | fmtle(support::detail::packed_endian_specific_integral<T, support::little, |
| 135 | support::unaligned> |
| 136 | Value) { |
| 137 | return detail::EndianAdapter<T>(std::move(Value)); |
| 138 | } |
| 139 | } |
| 140 | } // namespace llvm |
| 141 | #endif |