LLVM  10.0.0svn
DebugSubsectionRecord.cpp
Go to the documentation of this file.
1 //===- DebugSubsectionRecord.cpp ------------------------------------------===//
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 
14 #include "llvm/Support/Error.h"
16 #include <algorithm>
17 #include <cassert>
18 #include <cstdint>
19 
20 using namespace llvm;
21 using namespace llvm::codeview;
22 
24 
26  BinaryStreamRef Data,
27  CodeViewContainer Container)
28  : Container(Container), Kind(Kind), Data(Data) {}
29 
32  CodeViewContainer Container) {
33  const DebugSubsectionHeader *Header;
34  BinaryStreamReader Reader(Stream);
35  if (auto EC = Reader.readObject(Header))
36  return EC;
37 
38  DebugSubsectionKind Kind =
39  static_cast<DebugSubsectionKind>(uint32_t(Header->Kind));
40  if (auto EC = Reader.readStreamRef(Info.Data, Header->Length))
41  return EC;
42  Info.Container = Container;
43  Info.Kind = Kind;
44  return Error::success();
45 }
46 
48  return sizeof(DebugSubsectionHeader) + Data.getLength();
49 }
50 
52 
54 
56  std::shared_ptr<DebugSubsection> Subsection, CodeViewContainer Container)
57  : Subsection(std::move(Subsection)), Container(Container) {}
58 
60  const DebugSubsectionRecord &Contents, CodeViewContainer Container)
61  : Contents(Contents), Container(Container) {}
62 
64  uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
65  : Contents.getRecordData().getLength();
66  // The length of the entire subsection is always padded to 4 bytes,
67  // regardless of the container kind.
68  return sizeof(DebugSubsectionHeader) + alignTo(DataSize, 4);
69 }
70 
72  assert(Writer.getOffset() % alignOf(Container) == 0 &&
73  "Debug Subsection not properly aligned");
74 
75  DebugSubsectionHeader Header;
76  Header.Kind = uint32_t(Subsection ? Subsection->kind() : Contents.kind());
77  // The value written into the Header's Length field is only padded to the
78  // container's alignment
79  uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
80  : Contents.getRecordData().getLength();
81  Header.Length = alignTo(DataSize, alignOf(Container));
82 
83  if (auto EC = Writer.writeObject(Header))
84  return EC;
85  if (Subsection) {
86  if (auto EC = Subsection->commit(Writer))
87  return EC;
88  } else {
89  if (auto EC = Writer.writeStreamRef(Contents.getRecordData()))
90  return EC;
91  }
92  if (auto EC = Writer.padToAlignment(4))
93  return EC;
94 
95  return Error::success();
96 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
uint32_t alignOf(CodeViewContainer Container)
Definition: CodeView.h:606
Definition: BitVector.h:937
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
Provides write only access to a subclass of WritableBinaryStream.
uint32_t getLength() const
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
static Error initialize(BinaryStreamRef Stream, DebugSubsectionRecord &Info, CodeViewContainer Container)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:133
Error padToAlignment(uint32_t Align)
Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Provides read only access to a subclass of BinaryStream.
DebugSubsectionRecordBuilder(std::shared_ptr< DebugSubsection > Subsection, CodeViewContainer Container)
Error commit(BinaryStreamWriter &Writer) const