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