StringTable.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StringTable.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_GSYM_STRINGTABLE_H
  14. #define LLVM_DEBUGINFO_GSYM_STRINGTABLE_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/DebugInfo/GSYM/ExtractRanges.h"
  17. #include <stdint.h>
  18. namespace llvm {
  19. namespace gsym {
  20. /// String tables in GSYM files are required to start with an empty
  21. /// string at offset zero. Strings must be UTF8 NULL terminated strings.
  22. struct StringTable {
  23. StringRef Data;
  24. StringTable() = default;
  25. StringTable(StringRef D) : Data(D) {}
  26. StringRef operator[](size_t Offset) const { return getString(Offset); }
  27. StringRef getString(uint32_t Offset) const {
  28. if (Offset < Data.size()) {
  29. auto End = Data.find('\0', Offset);
  30. return Data.substr(Offset, End - Offset);
  31. }
  32. return StringRef();
  33. }
  34. void clear() { Data = StringRef(); }
  35. };
  36. inline raw_ostream &operator<<(raw_ostream &OS, const StringTable &S) {
  37. OS << "String table:\n";
  38. uint32_t Offset = 0;
  39. const size_t Size = S.Data.size();
  40. while (Offset < Size) {
  41. StringRef Str = S.getString(Offset);
  42. OS << HEX32(Offset) << ": \"" << Str << "\"\n";
  43. Offset += Str.size() + 1;
  44. }
  45. return OS;
  46. }
  47. } // namespace gsym
  48. } // namespace llvm
  49. #endif // LLVM_DEBUGINFO_GSYM_STRINGTABLE_H
  50. #ifdef __GNUC__
  51. #pragma GCC diagnostic pop
  52. #endif