1 //===- DebugInlineeLinesSubsection.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
13 #include "llvm/Support/BinaryStreamReader.h"
14 #include "llvm/Support/BinaryStreamWriter.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Error.h"
21 using namespace llvm::codeview
;
23 Error VarStreamArrayExtractor
<InlineeSourceLine
>::
24 operator()(BinaryStreamRef Stream
, uint32_t &Len
, InlineeSourceLine
&Item
) {
25 BinaryStreamReader
Reader(Stream
);
27 if (auto EC
= Reader
.readObject(Item
.Header
))
31 uint32_t ExtraFileCount
;
32 if (auto EC
= Reader
.readInteger(ExtraFileCount
))
34 if (auto EC
= Reader
.readArray(Item
.ExtraFiles
, ExtraFileCount
))
38 Len
= Reader
.getOffset();
39 return Error::success();
42 DebugInlineeLinesSubsectionRef::DebugInlineeLinesSubsectionRef()
43 : DebugSubsectionRef(DebugSubsectionKind::InlineeLines
) {}
45 Error
DebugInlineeLinesSubsectionRef::initialize(BinaryStreamReader Reader
) {
46 if (auto EC
= Reader
.readEnum(Signature
))
49 Lines
.getExtractor().HasExtraFiles
= hasExtraFiles();
50 if (auto EC
= Reader
.readArray(Lines
, Reader
.bytesRemaining()))
53 assert(Reader
.bytesRemaining() == 0);
54 return Error::success();
57 bool DebugInlineeLinesSubsectionRef::hasExtraFiles() const {
58 return Signature
== InlineeLinesSignature::ExtraFiles
;
61 DebugInlineeLinesSubsection::DebugInlineeLinesSubsection(
62 DebugChecksumsSubsection
&Checksums
, bool HasExtraFiles
)
63 : DebugSubsection(DebugSubsectionKind::InlineeLines
), Checksums(Checksums
),
64 HasExtraFiles(HasExtraFiles
) {}
66 uint32_t DebugInlineeLinesSubsection::calculateSerializedSize() const {
67 // 4 bytes for the signature
68 uint32_t Size
= sizeof(InlineeLinesSignature
);
70 // one header for each entry.
71 Size
+= Entries
.size() * sizeof(InlineeSourceLineHeader
);
73 // If extra files are enabled, one count for each entry.
74 Size
+= Entries
.size() * sizeof(uint32_t);
76 // And one file id for each file.
77 Size
+= ExtraFileCount
* sizeof(uint32_t);
79 assert(Size
% 4 == 0);
83 Error
DebugInlineeLinesSubsection::commit(BinaryStreamWriter
&Writer
) const {
84 InlineeLinesSignature Sig
= InlineeLinesSignature::Normal
;
86 Sig
= InlineeLinesSignature::ExtraFiles
;
88 if (auto EC
= Writer
.writeEnum(Sig
))
91 for (const auto &E
: Entries
) {
92 if (auto EC
= Writer
.writeObject(E
.Header
))
98 if (auto EC
= Writer
.writeInteger
<uint32_t>(E
.ExtraFiles
.size()))
100 if (auto EC
= Writer
.writeArray(makeArrayRef(E
.ExtraFiles
)))
104 return Error::success();
107 void DebugInlineeLinesSubsection::addExtraFile(StringRef FileName
) {
108 uint32_t Offset
= Checksums
.mapChecksumOffset(FileName
);
110 auto &Entry
= Entries
.back();
111 Entry
.ExtraFiles
.push_back(ulittle32_t(Offset
));
115 void DebugInlineeLinesSubsection::addInlineSite(TypeIndex FuncId
,
117 uint32_t SourceLine
) {
118 uint32_t Offset
= Checksums
.mapChecksumOffset(FileName
);
120 Entries
.emplace_back();
121 auto &Entry
= Entries
.back();
122 Entry
.Header
.FileID
= Offset
;
123 Entry
.Header
.SourceLineNum
= SourceLine
;
124 Entry
.Header
.Inlinee
= FuncId
;