FormatUtil.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FormatUtil.h ------------------------------------------- *- C++ --*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_PDB_NATIVE_FORMATUTIL_H
  14. #define LLVM_DEBUGINFO_PDB_NATIVE_FORMATUTIL_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/DebugInfo/CodeView/CodeView.h"
  18. #include "llvm/Support/Endian.h"
  19. #include "llvm/Support/FormatAdapters.h"
  20. #include "llvm/Support/FormatVariadic.h"
  21. #include <string>
  22. #include <type_traits>
  23. namespace llvm {
  24. namespace pdb {
  25. #define PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, Text) \
  26. if (Enum::TheOpt == (Value & Mask)) \
  27. Opts.push_back(Text);
  28. #define PUSH_FLAG(Enum, TheOpt, Value, Text) \
  29. PUSH_MASKED_FLAG(Enum, Enum::TheOpt, TheOpt, Value, Text)
  30. #define RETURN_CASE(Enum, X, Ret) \
  31. case Enum::X: \
  32. return Ret;
  33. template <typename T> std::string formatUnknownEnum(T Value) {
  34. return formatv("unknown ({0})", static_cast<std::underlying_type_t<T>>(Value))
  35. .str();
  36. }
  37. std::string formatSegmentOffset(uint16_t Segment, uint32_t Offset);
  38. enum class CharacteristicStyle {
  39. HeaderDefinition, // format as windows header definition
  40. Descriptive, // format as human readable words
  41. };
  42. std::string formatSectionCharacteristics(
  43. uint32_t IndentLevel, uint32_t C, uint32_t FlagsPerLine,
  44. StringRef Separator,
  45. CharacteristicStyle Style = CharacteristicStyle::HeaderDefinition);
  46. std::string typesetItemList(ArrayRef<std::string> Opts, uint32_t IndentLevel,
  47. uint32_t GroupSize, StringRef Sep);
  48. std::string typesetStringList(uint32_t IndentLevel,
  49. ArrayRef<StringRef> Strings);
  50. std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
  51. bool Friendly = true);
  52. std::string formatSymbolKind(codeview::SymbolKind K);
  53. std::string formatTypeLeafKind(codeview::TypeLeafKind K);
  54. /// Returns the number of digits in the given integer.
  55. inline int NumDigits(uint64_t N) {
  56. if (N < 10ULL)
  57. return 1;
  58. if (N < 100ULL)
  59. return 2;
  60. if (N < 1000ULL)
  61. return 3;
  62. if (N < 10000ULL)
  63. return 4;
  64. if (N < 100000ULL)
  65. return 5;
  66. if (N < 1000000ULL)
  67. return 6;
  68. if (N < 10000000ULL)
  69. return 7;
  70. if (N < 100000000ULL)
  71. return 8;
  72. if (N < 1000000000ULL)
  73. return 9;
  74. if (N < 10000000000ULL)
  75. return 10;
  76. if (N < 100000000000ULL)
  77. return 11;
  78. if (N < 1000000000000ULL)
  79. return 12;
  80. if (N < 10000000000000ULL)
  81. return 13;
  82. if (N < 100000000000000ULL)
  83. return 14;
  84. if (N < 1000000000000000ULL)
  85. return 15;
  86. if (N < 10000000000000000ULL)
  87. return 16;
  88. if (N < 100000000000000000ULL)
  89. return 17;
  90. if (N < 1000000000000000000ULL)
  91. return 18;
  92. if (N < 10000000000000000000ULL)
  93. return 19;
  94. return 20;
  95. }
  96. namespace detail {
  97. template <typename T>
  98. struct EndianAdapter final
  99. : public FormatAdapter<support::detail::packed_endian_specific_integral<
  100. T, support::little, support::unaligned>> {
  101. using EndianType =
  102. support::detail::packed_endian_specific_integral<T, support::little,
  103. support::unaligned>;
  104. explicit EndianAdapter(EndianType &&Item)
  105. : FormatAdapter<EndianType>(std::move(Item)) {}
  106. void format(llvm::raw_ostream &Stream, StringRef Style) override {
  107. format_provider<T>::format(static_cast<T>(this->Item), Stream, Style);
  108. }
  109. };
  110. } // namespace detail
  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. } // namespace pdb
  119. } // namespace llvm
  120. #endif
  121. #ifdef __GNUC__
  122. #pragma GCC diagnostic pop
  123. #endif