]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/PDB/Native/InfoStreamBuilder.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / DebugInfo / PDB / Native / InfoStreamBuilder.cpp
1 //===- InfoStreamBuilder.cpp - PDB Info Stream Creation ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
11
12 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
13 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
14 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
15 #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
16 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
17 #include "llvm/DebugInfo/PDB/Native/RawError.h"
18 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
19 #include "llvm/Support/BinaryStreamWriter.h"
20
21 using namespace llvm;
22 using namespace llvm::codeview;
23 using namespace llvm::msf;
24 using namespace llvm::pdb;
25
26 InfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf,
27                                      NamedStreamMap &NamedStreams)
28     : Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Age(0),
29       NamedStreams(NamedStreams) {
30   ::memset(&Guid, 0, sizeof(Guid));
31 }
32
33 void InfoStreamBuilder::setVersion(PdbRaw_ImplVer V) { Ver = V; }
34
35 void InfoStreamBuilder::addFeature(PdbRaw_FeatureSig Sig) {
36   Features.push_back(Sig);
37 }
38
39 void InfoStreamBuilder::setHashPDBContentsToGUID(bool B) {
40   HashPDBContentsToGUID = B;
41 }
42
43 void InfoStreamBuilder::setAge(uint32_t A) { Age = A; }
44
45 void InfoStreamBuilder::setSignature(uint32_t S) { Signature = S; }
46
47 void InfoStreamBuilder::setGuid(GUID G) { Guid = G; }
48
49
50 Error InfoStreamBuilder::finalizeMsfLayout() {
51   uint32_t Length = sizeof(InfoStreamHeader) +
52                     NamedStreams.calculateSerializedLength() +
53                     (Features.size() + 1) * sizeof(uint32_t);
54   if (auto EC = Msf.setStreamSize(StreamPDB, Length))
55     return EC;
56   return Error::success();
57 }
58
59 Error InfoStreamBuilder::commit(const msf::MSFLayout &Layout,
60                                 WritableBinaryStreamRef Buffer) const {
61   auto InfoS = WritableMappedBlockStream::createIndexedStream(
62       Layout, Buffer, StreamPDB, Msf.getAllocator());
63   BinaryStreamWriter Writer(*InfoS);
64
65   InfoStreamHeader H;
66   // Leave the build id fields 0 so they can be set as the last step before
67   // committing the file to disk.
68   ::memset(&H, 0, sizeof(H));
69   H.Version = Ver;
70   if (auto EC = Writer.writeObject(H))
71     return EC;
72
73   if (auto EC = NamedStreams.commit(Writer))
74     return EC;
75   if (auto EC = Writer.writeInteger(0))
76     return EC;
77   for (auto E : Features) {
78     if (auto EC = Writer.writeEnum(E))
79       return EC;
80   }
81   assert(Writer.bytesRemaining() == 0);
82   return Error::success();
83 }