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