DebugStringTableSubsection.h 2.9 KB

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