reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
//===- StringsAndChecksums.h ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H
#define LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H

#include "llvm/DebugInfo/CodeView/CodeView.h"
#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
#include <memory>

namespace llvm {
namespace codeview {

class StringsAndChecksumsRef {
public:
  // If no subsections are known about initially, we find as much as we can.
  StringsAndChecksumsRef();

  // If only a string table subsection is given, we find a checksums subsection.
  explicit StringsAndChecksumsRef(const DebugStringTableSubsectionRef &Strings);

  // If both subsections are given, we don't need to find anything.
  StringsAndChecksumsRef(const DebugStringTableSubsectionRef &Strings,
                         const DebugChecksumsSubsectionRef &Checksums);

  void setStrings(const DebugStringTableSubsectionRef &Strings);
  void setChecksums(const DebugChecksumsSubsectionRef &CS);

  void reset();
  void resetStrings();
  void resetChecksums();

  template <typename T> void initialize(T &&FragmentRange) {
    for (const DebugSubsectionRecord &R : FragmentRange) {
      if (Strings && Checksums)
        return;
      if (R.kind() == DebugSubsectionKind::FileChecksums) {
        initializeChecksums(R);
        continue;
      }
      if (R.kind() == DebugSubsectionKind::StringTable && !Strings) {
        // While in practice we should never encounter a string table even
        // though the string table is already initialized, in theory it's
        // possible.  PDBs are supposed to have one global string table and
        // then this subsection should not appear.  Whereas object files are
        // supposed to have this subsection appear exactly once.  However,
        // for testing purposes it's nice to be able to test this subsection
        // independently of one format or the other, so for some tests we
        // manually construct a PDB that contains this subsection in addition
        // to a global string table.
        initializeStrings(R);
        continue;
      }
    }
  }

  const DebugStringTableSubsectionRef &strings() const { return *Strings; }
  const DebugChecksumsSubsectionRef &checksums() const { return *Checksums; }

  bool hasStrings() const { return Strings != nullptr; }
  bool hasChecksums() const { return Checksums != nullptr; }

private:
  void initializeStrings(const DebugSubsectionRecord &SR);
  void initializeChecksums(const DebugSubsectionRecord &FCR);

  std::shared_ptr<DebugStringTableSubsectionRef> OwnedStrings;
  std::shared_ptr<DebugChecksumsSubsectionRef> OwnedChecksums;

  const DebugStringTableSubsectionRef *Strings = nullptr;
  const DebugChecksumsSubsectionRef *Checksums = nullptr;
};

class StringsAndChecksums {
public:
  using StringsPtr = std::shared_ptr<DebugStringTableSubsection>;
  using ChecksumsPtr = std::shared_ptr<DebugChecksumsSubsection>;

  // If no subsections are known about initially, we find as much as we can.
  StringsAndChecksums() = default;

  void setStrings(const StringsPtr &SP) { Strings = SP; }
  void setChecksums(const ChecksumsPtr &CP) { Checksums = CP; }

  const StringsPtr &strings() const { return Strings; }
  const ChecksumsPtr &checksums() const { return Checksums; }

  bool hasStrings() const { return Strings != nullptr; }
  bool hasChecksums() const { return Checksums != nullptr; }

private:
  StringsPtr Strings;
  ChecksumsPtr Checksums;
};

} // end namespace codeview
} // end namespace llvm

#endif // LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H