DebugStringTableSubsection.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DebugStringTableSubsection.h - CodeView String Table -----*- 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_CODEVIEW_DEBUGSTRINGTABLESUBSECTION_H
  14. #define LLVM_DEBUGINFO_CODEVIEW_DEBUGSTRINGTABLESUBSECTION_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/DebugInfo/CodeView/CodeView.h"
  19. #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
  20. #include "llvm/Support/BinaryStreamRef.h"
  21. #include "llvm/Support/Error.h"
  22. #include <cstdint>
  23. namespace llvm {
  24. class BinaryStreamReader;
  25. namespace codeview {
  26. /// Represents a read-only view of a CodeView string table. This is a very
  27. /// simple flat buffer consisting of null-terminated strings, where strings
  28. /// are retrieved by their offset in the buffer. DebugStringTableSubsectionRef
  29. /// does not own the underlying storage for the buffer.
  30. class DebugStringTableSubsectionRef : public DebugSubsectionRef {
  31. public:
  32. DebugStringTableSubsectionRef();
  33. static bool classof(const DebugSubsectionRef *S) {
  34. return S->kind() == DebugSubsectionKind::StringTable;
  35. }
  36. Error initialize(BinaryStreamRef Contents);
  37. Error initialize(BinaryStreamReader &Reader);
  38. Expected<StringRef> getString(uint32_t Offset) const;
  39. bool valid() const { return Stream.valid(); }
  40. BinaryStreamRef getBuffer() const { return Stream; }
  41. private:
  42. BinaryStreamRef Stream;
  43. };
  44. /// Represents a read-write view of a CodeView string table.
  45. /// DebugStringTableSubsection owns the underlying storage for the table, and is
  46. /// capable of serializing the string table into a format understood by
  47. /// DebugStringTableSubsectionRef.
  48. class DebugStringTableSubsection : public DebugSubsection {
  49. public:
  50. DebugStringTableSubsection();
  51. static bool classof(const DebugSubsection *S) {
  52. return S->kind() == DebugSubsectionKind::StringTable;
  53. }
  54. // If string S does not exist in the string table, insert it.
  55. // Returns the ID for S.
  56. uint32_t insert(StringRef S);
  57. // Return the ID for string S. Assumes S exists in the table.
  58. uint32_t getIdForString(StringRef S) const;
  59. StringRef getStringForId(uint32_t Id) const;
  60. uint32_t calculateSerializedSize() const override;
  61. Error commit(BinaryStreamWriter &Writer) const override;
  62. uint32_t size() const;
  63. StringMap<uint32_t>::const_iterator begin() const {
  64. return StringToId.begin();
  65. }
  66. StringMap<uint32_t>::const_iterator end() const { return StringToId.end(); }
  67. std::vector<uint32_t> sortedIds() const;
  68. private:
  69. DenseMap<uint32_t, StringRef> IdToString;
  70. StringMap<uint32_t> StringToId;
  71. uint32_t StringSize = 1;
  72. };
  73. } // end namespace codeview
  74. } // end namespace llvm
  75. #endif // LLVM_DEBUGINFO_CODEVIEW_DEBUGSTRINGTABLESUBSECTION_H
  76. #ifdef __GNUC__
  77. #pragma GCC diagnostic pop
  78. #endif