StringsAndChecksums.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StringsAndChecksums.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_CODEVIEW_STRINGSANDCHECKSUMS_H
  14. #define LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H
  15. #include "llvm/DebugInfo/CodeView/CodeView.h"
  16. #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
  17. #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
  18. #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
  19. #include <memory>
  20. namespace llvm {
  21. namespace codeview {
  22. class StringsAndChecksumsRef {
  23. public:
  24. // If no subsections are known about initially, we find as much as we can.
  25. StringsAndChecksumsRef();
  26. // If only a string table subsection is given, we find a checksums subsection.
  27. explicit StringsAndChecksumsRef(const DebugStringTableSubsectionRef &Strings);
  28. // If both subsections are given, we don't need to find anything.
  29. StringsAndChecksumsRef(const DebugStringTableSubsectionRef &Strings,
  30. const DebugChecksumsSubsectionRef &Checksums);
  31. void setStrings(const DebugStringTableSubsectionRef &Strings);
  32. void setChecksums(const DebugChecksumsSubsectionRef &CS);
  33. void reset();
  34. void resetStrings();
  35. void resetChecksums();
  36. template <typename T> void initialize(T &&FragmentRange) {
  37. for (const DebugSubsectionRecord &R : FragmentRange) {
  38. if (Strings && Checksums)
  39. return;
  40. if (R.kind() == DebugSubsectionKind::FileChecksums) {
  41. initializeChecksums(R);
  42. continue;
  43. }
  44. if (R.kind() == DebugSubsectionKind::StringTable && !Strings) {
  45. // While in practice we should never encounter a string table even
  46. // though the string table is already initialized, in theory it's
  47. // possible. PDBs are supposed to have one global string table and
  48. // then this subsection should not appear. Whereas object files are
  49. // supposed to have this subsection appear exactly once. However,
  50. // for testing purposes it's nice to be able to test this subsection
  51. // independently of one format or the other, so for some tests we
  52. // manually construct a PDB that contains this subsection in addition
  53. // to a global string table.
  54. initializeStrings(R);
  55. continue;
  56. }
  57. }
  58. }
  59. const DebugStringTableSubsectionRef &strings() const { return *Strings; }
  60. const DebugChecksumsSubsectionRef &checksums() const { return *Checksums; }
  61. bool hasStrings() const { return Strings != nullptr; }
  62. bool hasChecksums() const { return Checksums != nullptr; }
  63. private:
  64. void initializeStrings(const DebugSubsectionRecord &SR);
  65. void initializeChecksums(const DebugSubsectionRecord &FCR);
  66. std::shared_ptr<DebugStringTableSubsectionRef> OwnedStrings;
  67. std::shared_ptr<DebugChecksumsSubsectionRef> OwnedChecksums;
  68. const DebugStringTableSubsectionRef *Strings = nullptr;
  69. const DebugChecksumsSubsectionRef *Checksums = nullptr;
  70. };
  71. class StringsAndChecksums {
  72. public:
  73. using StringsPtr = std::shared_ptr<DebugStringTableSubsection>;
  74. using ChecksumsPtr = std::shared_ptr<DebugChecksumsSubsection>;
  75. // If no subsections are known about initially, we find as much as we can.
  76. StringsAndChecksums() = default;
  77. void setStrings(const StringsPtr &SP) { Strings = SP; }
  78. void setChecksums(const ChecksumsPtr &CP) { Checksums = CP; }
  79. const StringsPtr &strings() const { return Strings; }
  80. const ChecksumsPtr &checksums() const { return Checksums; }
  81. bool hasStrings() const { return Strings != nullptr; }
  82. bool hasChecksums() const { return Checksums != nullptr; }
  83. private:
  84. StringsPtr Strings;
  85. ChecksumsPtr Checksums;
  86. };
  87. } // end namespace codeview
  88. } // end namespace llvm
  89. #endif // LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H
  90. #ifdef __GNUC__
  91. #pragma GCC diagnostic pop
  92. #endif