Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
Warning:line 934, column 38
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name DwarfUnit.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/CodeGen/AsmPrinter -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/CodeGen/AsmPrinter -I include -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-10-03-140002-15933-1 -x c++ /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
1//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
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// This file contains support for constructing a dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfUnit.h"
14#include "AddressPool.h"
15#include "DwarfCompileUnit.h"
16#include "DwarfExpression.h"
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APInt.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/iterator_range.h"
21#include "llvm/CodeGen/TargetRegisterInfo.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/GlobalValue.h"
25#include "llvm/IR/Metadata.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCDwarf.h"
29#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Target/TargetLoweringObjectFile.h"
33#include <cassert>
34#include <cstdint>
35#include <string>
36#include <utility>
37
38using namespace llvm;
39
40#define DEBUG_TYPE"dwarfdebug" "dwarfdebug"
41
42DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP,
43 DwarfCompileUnit &CU, DIELoc &DIE)
44 : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
45
46void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
47 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Op);
48}
49
50void DIEDwarfExpression::emitSigned(int64_t Value) {
51 CU.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata, Value);
52}
53
54void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
55 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_udata, Value);
56}
57
58void DIEDwarfExpression::emitData1(uint8_t Value) {
59 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Value);
60}
61
62void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
63 CU.addBaseTypeRef(getActiveDIE(), Idx);
64}
65
66void DIEDwarfExpression::enableTemporaryBuffer() {
67 assert(!IsBuffering && "Already buffering?")(static_cast <bool> (!IsBuffering && "Already buffering?"
) ? void (0) : __assert_fail ("!IsBuffering && \"Already buffering?\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 67, __extension__
__PRETTY_FUNCTION__))
;
68 IsBuffering = true;
69}
70
71void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
72
73unsigned DIEDwarfExpression::getTemporaryBufferSize() {
74 return TmpDIE.computeSize(AP.getDwarfFormParams());
75}
76
77void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(TmpDIE); }
78
79bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
80 llvm::Register MachineReg) {
81 return MachineReg == TRI.getFrameRegister(*AP.MF);
82}
83
84DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
85 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
86 : DIEUnit(UnitTag), CUNode(Node), Asm(A), DD(DW), DU(DWU) {}
87
88DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
89 DwarfDebug *DW, DwarfFile *DWU,
90 MCDwarfDwoLineTable *SplitLineTable)
91 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
92 SplitLineTable(SplitLineTable) {
93}
94
95DwarfUnit::~DwarfUnit() {
96 for (DIEBlock *B : DIEBlocks)
97 B->~DIEBlock();
98 for (DIELoc *L : DIELocs)
99 L->~DIELoc();
100}
101
102int64_t DwarfUnit::getDefaultLowerBound() const {
103 switch (getLanguage()) {
104 default:
105 break;
106
107 // The languages below have valid values in all DWARF versions.
108 case dwarf::DW_LANG_C:
109 case dwarf::DW_LANG_C89:
110 case dwarf::DW_LANG_C_plus_plus:
111 return 0;
112
113 case dwarf::DW_LANG_Fortran77:
114 case dwarf::DW_LANG_Fortran90:
115 return 1;
116
117 // The languages below have valid values only if the DWARF version >= 3.
118 case dwarf::DW_LANG_C99:
119 case dwarf::DW_LANG_ObjC:
120 case dwarf::DW_LANG_ObjC_plus_plus:
121 if (DD->getDwarfVersion() >= 3)
122 return 0;
123 break;
124
125 case dwarf::DW_LANG_Fortran95:
126 if (DD->getDwarfVersion() >= 3)
127 return 1;
128 break;
129
130 // Starting with DWARF v4, all defined languages have valid values.
131 case dwarf::DW_LANG_D:
132 case dwarf::DW_LANG_Java:
133 case dwarf::DW_LANG_Python:
134 case dwarf::DW_LANG_UPC:
135 if (DD->getDwarfVersion() >= 4)
136 return 0;
137 break;
138
139 case dwarf::DW_LANG_Ada83:
140 case dwarf::DW_LANG_Ada95:
141 case dwarf::DW_LANG_Cobol74:
142 case dwarf::DW_LANG_Cobol85:
143 case dwarf::DW_LANG_Modula2:
144 case dwarf::DW_LANG_Pascal83:
145 case dwarf::DW_LANG_PLI:
146 if (DD->getDwarfVersion() >= 4)
147 return 1;
148 break;
149
150 // The languages below are new in DWARF v5.
151 case dwarf::DW_LANG_BLISS:
152 case dwarf::DW_LANG_C11:
153 case dwarf::DW_LANG_C_plus_plus_03:
154 case dwarf::DW_LANG_C_plus_plus_11:
155 case dwarf::DW_LANG_C_plus_plus_14:
156 case dwarf::DW_LANG_Dylan:
157 case dwarf::DW_LANG_Go:
158 case dwarf::DW_LANG_Haskell:
159 case dwarf::DW_LANG_OCaml:
160 case dwarf::DW_LANG_OpenCL:
161 case dwarf::DW_LANG_RenderScript:
162 case dwarf::DW_LANG_Rust:
163 case dwarf::DW_LANG_Swift:
164 if (DD->getDwarfVersion() >= 5)
165 return 0;
166 break;
167
168 case dwarf::DW_LANG_Fortran03:
169 case dwarf::DW_LANG_Fortran08:
170 case dwarf::DW_LANG_Julia:
171 case dwarf::DW_LANG_Modula3:
172 if (DD->getDwarfVersion() >= 5)
173 return 1;
174 break;
175 }
176
177 return -1;
178}
179
180/// Check whether the DIE for this MDNode can be shared across CUs.
181bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
182 // When the MDNode can be part of the type system, the DIE can be shared
183 // across CUs.
184 // Combining type units and cross-CU DIE sharing is lower value (since
185 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
186 // level already) but may be implementable for some value in projects
187 // building multiple independent libraries with LTO and then linking those
188 // together.
189 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
190 return false;
191 return (isa<DIType>(D) ||
192 (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
193 !DD->generateTypeUnits();
194}
195
196DIE *DwarfUnit::getDIE(const DINode *D) const {
197 if (isShareableAcrossCUs(D))
198 return DU->getDIE(D);
199 return MDNodeToDieMap.lookup(D);
200}
201
202void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
203 if (isShareableAcrossCUs(Desc)) {
204 DU->insertDIE(Desc, D);
205 return;
206 }
207 MDNodeToDieMap.insert(std::make_pair(Desc, D));
208}
209
210void DwarfUnit::insertDIE(DIE *D) {
211 MDNodeToDieMap.insert(std::make_pair(nullptr, D));
212}
213
214void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
215 if (DD->getDwarfVersion() >= 4)
216 addAttribute(Die, Attribute, dwarf::DW_FORM_flag_present, DIEInteger(1));
217 else
218 addAttribute(Die, Attribute, dwarf::DW_FORM_flag, DIEInteger(1));
219}
220
221void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
222 Optional<dwarf::Form> Form, uint64_t Integer) {
223 if (!Form)
224 Form = DIEInteger::BestForm(false, Integer);
225 assert(Form != dwarf::DW_FORM_implicit_const &&(static_cast <bool> (Form != dwarf::DW_FORM_implicit_const
&& "DW_FORM_implicit_const is used only for signed integers"
) ? void (0) : __assert_fail ("Form != dwarf::DW_FORM_implicit_const && \"DW_FORM_implicit_const is used only for signed integers\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 226, __extension__
__PRETTY_FUNCTION__))
226 "DW_FORM_implicit_const is used only for signed integers")(static_cast <bool> (Form != dwarf::DW_FORM_implicit_const
&& "DW_FORM_implicit_const is used only for signed integers"
) ? void (0) : __assert_fail ("Form != dwarf::DW_FORM_implicit_const && \"DW_FORM_implicit_const is used only for signed integers\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 226, __extension__
__PRETTY_FUNCTION__))
;
227 addAttribute(Die, Attribute, *Form, DIEInteger(Integer));
228}
229
230void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
231 uint64_t Integer) {
232 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
233}
234
235void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
236 Optional<dwarf::Form> Form, int64_t Integer) {
237 if (!Form)
238 Form = DIEInteger::BestForm(true, Integer);
239 addAttribute(Die, Attribute, *Form, DIEInteger(Integer));
240}
241
242void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
243 int64_t Integer) {
244 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
245}
246
247void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
248 StringRef String) {
249 if (CUNode->isDebugDirectivesOnly())
250 return;
251
252 if (DD->useInlineStrings()) {
253 addAttribute(Die, Attribute, dwarf::DW_FORM_string,
254 new (DIEValueAllocator)
255 DIEInlineString(String, DIEValueAllocator));
256 return;
257 }
258 dwarf::Form IxForm =
259 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
260
261 auto StringPoolEntry =
262 useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
263 ? DU->getStringPool().getIndexedEntry(*Asm, String)
264 : DU->getStringPool().getEntry(*Asm, String);
265
266 // For DWARF v5 and beyond, use the smallest strx? form possible.
267 if (useSegmentedStringOffsetsTable()) {
268 IxForm = dwarf::DW_FORM_strx1;
269 unsigned Index = StringPoolEntry.getIndex();
270 if (Index > 0xffffff)
271 IxForm = dwarf::DW_FORM_strx4;
272 else if (Index > 0xffff)
273 IxForm = dwarf::DW_FORM_strx3;
274 else if (Index > 0xff)
275 IxForm = dwarf::DW_FORM_strx2;
276 }
277 addAttribute(Die, Attribute, IxForm, DIEString(StringPoolEntry));
278}
279
280void DwarfUnit::addLabel(DIEValueList &Die, dwarf::Attribute Attribute,
281 dwarf::Form Form, const MCSymbol *Label) {
282 addAttribute(Die, Attribute, Form, DIELabel(Label));
283}
284
285void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
286 addLabel(Die, (dwarf::Attribute)0, Form, Label);
287}
288
289void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
290 uint64_t Integer) {
291 addUInt(Die, Attribute, DD->getDwarfSectionOffsetForm(), Integer);
292}
293
294unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
295 if (!SplitLineTable)
296 return getCU().getOrCreateSourceID(File);
297 if (!UsedLineTable) {
298 UsedLineTable = true;
299 // This is a split type unit that needs a line table.
300 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
301 }
302 return SplitLineTable->getFile(
303 File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File),
304 Asm->OutContext.getDwarfVersion(), File->getSource());
305}
306
307void DwarfUnit::addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label) {
308 bool UseAddrOffsetFormOrExpressions =
309 DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
310
311 const MCSymbol *Base = nullptr;
312 if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
313 Base = DD->getSectionLabel(&Label->getSection());
314
315 uint32_t Index = DD->getAddressPool().getIndex(Base ? Base : Label);
316
317 if (DD->getDwarfVersion() >= 5) {
318 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx);
319 addUInt(Die, dwarf::DW_FORM_addrx, Index);
320 } else {
321 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
322 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, Index);
323 }
324
325 if (Base && Base != Label) {
326 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_const4u);
327 addLabelDelta(Die, (dwarf::Attribute)0, Label, Base);
328 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
329 }
330}
331
332void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
333 if (DD->getDwarfVersion() >= 5) {
334 addPoolOpAddress(Die, Sym);
335 return;
336 }
337
338 if (DD->useSplitDwarf()) {
339 addPoolOpAddress(Die, Sym);
340 return;
341 }
342
343 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
344 addLabel(Die, dwarf::DW_FORM_addr, Sym);
345}
346
347void DwarfUnit::addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute,
348 const MCSymbol *Hi, const MCSymbol *Lo) {
349 addAttribute(Die, Attribute, dwarf::DW_FORM_data4,
350 new (DIEValueAllocator) DIEDelta(Hi, Lo));
351}
352
353void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
354 addDIEEntry(Die, Attribute, DIEEntry(Entry));
355}
356
357void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
358 // Flag the type unit reference as a declaration so that if it contains
359 // members (implicit special members, static data member definitions, member
360 // declarations for definitions in this CU, etc) consumers don't get confused
361 // and think this is a full definition.
362 addFlag(Die, dwarf::DW_AT_declaration);
363
364 addAttribute(Die, dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
365 DIEInteger(Signature));
366}
367
368void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
369 DIEEntry Entry) {
370 const DIEUnit *CU = Die.getUnit();
371 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
372 if (!CU)
373 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
374 CU = getUnitDie().getUnit();
375 if (!EntryCU)
376 EntryCU = getUnitDie().getUnit();
377 assert(EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() ||(static_cast <bool> (EntryCU == CU || !DD->useSplitDwarf
() || DD->shareAcrossDWOCUs() || !static_cast<const DwarfUnit
*>(CU)->isDwoUnit()) ? void (0) : __assert_fail ("EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() || !static_cast<const DwarfUnit*>(CU)->isDwoUnit()"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 378, __extension__
__PRETTY_FUNCTION__))
378 !static_cast<const DwarfUnit*>(CU)->isDwoUnit())(static_cast <bool> (EntryCU == CU || !DD->useSplitDwarf
() || DD->shareAcrossDWOCUs() || !static_cast<const DwarfUnit
*>(CU)->isDwoUnit()) ? void (0) : __assert_fail ("EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() || !static_cast<const DwarfUnit*>(CU)->isDwoUnit()"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 378, __extension__
__PRETTY_FUNCTION__))
;
379 addAttribute(Die, Attribute,
380 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
381 Entry);
382}
383
384DIE &DwarfUnit::createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N) {
385 DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, Tag));
386 if (N)
387 insertDIE(N, &Die);
388 return Die;
389}
390
391void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
392 Loc->computeSize(Asm->getDwarfFormParams());
393 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
394 addAttribute(Die, Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
395}
396
397void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
398 DIEBlock *Block) {
399 Block->computeSize(Asm->getDwarfFormParams());
400 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
401 addAttribute(Die, Attribute, Form, Block);
402}
403
404void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
405 DIEBlock *Block) {
406 addBlock(Die, Attribute, Block->BestForm(), Block);
407}
408
409void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) {
410 if (Line == 0)
411 return;
412
413 unsigned FileID = getOrCreateSourceID(File);
414 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
415 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
416}
417
418void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
419 assert(V)(static_cast <bool> (V) ? void (0) : __assert_fail ("V"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 419, __extension__
__PRETTY_FUNCTION__))
;
420
421 addSourceLine(Die, V->getLine(), V->getFile());
422}
423
424void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
425 assert(G)(static_cast <bool> (G) ? void (0) : __assert_fail ("G"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 425, __extension__
__PRETTY_FUNCTION__))
;
426
427 addSourceLine(Die, G->getLine(), G->getFile());
428}
429
430void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
431 assert(SP)(static_cast <bool> (SP) ? void (0) : __assert_fail ("SP"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 431, __extension__
__PRETTY_FUNCTION__))
;
432
433 addSourceLine(Die, SP->getLine(), SP->getFile());
434}
435
436void DwarfUnit::addSourceLine(DIE &Die, const DILabel *L) {
437 assert(L)(static_cast <bool> (L) ? void (0) : __assert_fail ("L"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 437, __extension__
__PRETTY_FUNCTION__))
;
438
439 addSourceLine(Die, L->getLine(), L->getFile());
440}
441
442void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
443 assert(Ty)(static_cast <bool> (Ty) ? void (0) : __assert_fail ("Ty"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 443, __extension__
__PRETTY_FUNCTION__))
;
444
445 addSourceLine(Die, Ty->getLine(), Ty->getFile());
446}
447
448void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
449 assert(Ty)(static_cast <bool> (Ty) ? void (0) : __assert_fail ("Ty"
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 449, __extension__
__PRETTY_FUNCTION__))
;
450
451 addSourceLine(Die, Ty->getLine(), Ty->getFile());
452}
453
454void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
455 // Pass this down to addConstantValue as an unsigned bag of bits.
456 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
457}
458
459void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
460 const DIType *Ty) {
461 addConstantValue(Die, CI->getValue(), Ty);
462}
463
464void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) {
465 addConstantValue(Die, DD->isUnsignedDIType(Ty), Val);
466}
467
468void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
469 // FIXME: This is a bit conservative/simple - it emits negative values always
470 // sign extended to 64 bits rather than minimizing the number of bytes.
471 addUInt(Die, dwarf::DW_AT_const_value,
472 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
473}
474
475void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
476 addConstantValue(Die, Val, DD->isUnsignedDIType(Ty));
477}
478
479void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
480 unsigned CIBitWidth = Val.getBitWidth();
481 if (CIBitWidth <= 64) {
482 addConstantValue(Die, Unsigned,
483 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
484 return;
485 }
486
487 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
488
489 // Get the raw data form of the large APInt.
490 const uint64_t *Ptr64 = Val.getRawData();
491
492 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
493 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
494
495 // Output the constant to DWARF one byte at a time.
496 for (int i = 0; i < NumBytes; i++) {
497 uint8_t c;
498 if (LittleEndian)
499 c = Ptr64[i / 8] >> (8 * (i & 7));
500 else
501 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
502 addUInt(*Block, dwarf::DW_FORM_data1, c);
503 }
504
505 addBlock(Die, dwarf::DW_AT_const_value, Block);
506}
507
508void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
509 if (!LinkageName.empty())
510 addString(Die,
511 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
512 : dwarf::DW_AT_MIPS_linkage_name,
513 GlobalValue::dropLLVMManglingEscape(LinkageName));
514}
515
516void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
517 // Add template parameters.
518 for (const auto *Element : TParams) {
519 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
520 constructTemplateTypeParameterDIE(Buffer, TTP);
521 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
522 constructTemplateValueParameterDIE(Buffer, TVP);
523 }
524}
525
526/// Add thrown types.
527void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
528 for (const auto *Ty : ThrownTypes) {
529 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
530 addType(TT, cast<DIType>(Ty));
531 }
532}
533
534void DwarfUnit::addAccess(DIE &Die, DINode::DIFlags Flags) {
535 if ((Flags & DINode::FlagAccessibility) == DINode::FlagProtected)
536 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
537 dwarf::DW_ACCESS_protected);
538 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPrivate)
539 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
540 dwarf::DW_ACCESS_private);
541 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPublic)
542 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
543 dwarf::DW_ACCESS_public);
544}
545
546DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
547 if (!Context
1.1
'Context' is non-null
|| isa<DIFile>(Context))
2
Assuming 'Context' is not a 'DIFile'
3
Taking false branch
548 return &getUnitDie();
549 if (auto *T
4.1
'T' is non-null
= dyn_cast<DIType>(Context))
4
Assuming 'Context' is a 'CastReturnType'
5
Taking true branch
550 return getOrCreateTypeDIE(T);
6
Calling 'DwarfUnit::getOrCreateTypeDIE'
551 if (auto *NS = dyn_cast<DINamespace>(Context))
552 return getOrCreateNameSpace(NS);
553 if (auto *SP = dyn_cast<DISubprogram>(Context))
554 return getOrCreateSubprogramDIE(SP);
555 if (auto *M = dyn_cast<DIModule>(Context))
556 return getOrCreateModule(M);
557 return getDIE(Context);
558}
559
560DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) {
561 auto *Context = Ty->getScope();
562 DIE *ContextDIE = getOrCreateContextDIE(Context);
1
Calling 'DwarfUnit::getOrCreateContextDIE'
563
564 if (DIE *TyDIE = getDIE(Ty))
565 return TyDIE;
566
567 // Create new type.
568 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
569
570 constructTypeDIE(TyDIE, cast<DICompositeType>(Ty));
571
572 updateAcceleratorTables(Context, Ty, TyDIE);
573 return &TyDIE;
574}
575
576DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE,
577 const DIType *Ty) {
578 // Create new type.
579 DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty);
580
581 updateAcceleratorTables(Context, Ty, TyDIE);
582
583 if (auto *BT
15.1
'BT' is null
= dyn_cast<DIBasicType>(Ty))
15
Assuming 'Ty' is not a 'CastReturnType'
16
Taking false branch
584 constructTypeDIE(TyDIE, BT);
585 else if (auto *ST
17.1
'ST' is null
= dyn_cast<DIStringType>(Ty))
17
Assuming 'Ty' is not a 'CastReturnType'
18
Taking false branch
586 constructTypeDIE(TyDIE, ST);
587 else if (auto *STy
19.1
'STy' is null
= dyn_cast<DISubroutineType>(Ty))
19
Assuming 'Ty' is not a 'CastReturnType'
20
Taking false branch
588 constructTypeDIE(TyDIE, STy);
589 else if (auto *CTy
21.1
'CTy' is non-null
= dyn_cast<DICompositeType>(Ty)) {
21
Assuming 'Ty' is a 'CastReturnType'
590 if (DD->generateTypeUnits() && !Ty->isForwardDecl() &&
22
Assuming the condition is false
591 (Ty->getRawName() || CTy->getRawIdentifier())) {
592 // Skip updating the accelerator tables since this is not the full type.
593 if (MDString *TypeId = CTy->getRawIdentifier())
594 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
595 else
596 finishNonUnitTypeDIE(TyDIE, CTy);
597 return &TyDIE;
598 }
599 constructTypeDIE(TyDIE, CTy);
23
Calling 'DwarfUnit::constructTypeDIE'
600 } else {
601 constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty));
602 }
603
604 return &TyDIE;
605}
606
607DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
608 if (!TyNode
6.1
'TyNode' is non-null
)
7
Taking false branch
609 return nullptr;
610
611 auto *Ty = cast<DIType>(TyNode);
8
'TyNode' is a 'CastReturnType'
612
613 // DW_TAG_restrict_type is not supported in DWARF2
614 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
9
Assuming the condition is false
615 return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType());
616
617 // DW_TAG_atomic_type is not supported in DWARF < 5
618 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
10
Assuming the condition is false
619 return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType());
620
621 // Construct the context before querying for the existence of the DIE in case
622 // such construction creates the DIE.
623 auto *Context = Ty->getScope();
624 DIE *ContextDIE = getOrCreateContextDIE(Context);
625 assert(ContextDIE)(static_cast <bool> (ContextDIE) ? void (0) : __assert_fail
("ContextDIE", "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 625
, __extension__ __PRETTY_FUNCTION__))
;
11
'?' condition is true
626
627 if (DIE *TyDIE = getDIE(Ty))
12
Assuming 'TyDIE' is null
13
Taking false branch
628 return TyDIE;
629
630 return static_cast<DwarfUnit *>(ContextDIE->getUnit())
14
Calling 'DwarfUnit::createTypeDIE'
631 ->createTypeDIE(Context, *ContextDIE, Ty);
632}
633
634void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
635 const DIType *Ty, const DIE &TyDIE) {
636 if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
637 bool IsImplementation = false;
638 if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
639 // A runtime language of 0 actually means C/C++ and that any
640 // non-negative value is some version of Objective-C/C++.
641 IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
642 }
643 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
644 DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags);
645
646 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
647 isa<DINamespace>(Context) || isa<DICommonBlock>(Context))
648 addGlobalType(Ty, TyDIE, Context);
649 }
650}
651
652void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
653 dwarf::Attribute Attribute) {
654 assert(Ty && "Trying to add a type that doesn't exist?")(static_cast <bool> (Ty && "Trying to add a type that doesn't exist?"
) ? void (0) : __assert_fail ("Ty && \"Trying to add a type that doesn't exist?\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 654, __extension__
__PRETTY_FUNCTION__))
;
655 addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty)));
656}
657
658std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
659 if (!Context)
660 return "";
661
662 // FIXME: Decide whether to implement this for non-C++ languages.
663 if (!dwarf::isCPlusPlus((dwarf::SourceLanguage)getLanguage()))
664 return "";
665
666 std::string CS;
667 SmallVector<const DIScope *, 1> Parents;
668 while (!isa<DICompileUnit>(Context)) {
669 Parents.push_back(Context);
670 if (const DIScope *S = Context->getScope())
671 Context = S;
672 else
673 // Structure, etc types will have a NULL context if they're at the top
674 // level.
675 break;
676 }
677
678 // Reverse iterate over our list to go from the outermost construct to the
679 // innermost.
680 for (const DIScope *Ctx : llvm::reverse(Parents)) {
681 StringRef Name = Ctx->getName();
682 if (Name.empty() && isa<DINamespace>(Ctx))
683 Name = "(anonymous namespace)";
684 if (!Name.empty()) {
685 CS += Name;
686 CS += "::";
687 }
688 }
689 return CS;
690}
691
692void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
693 // Get core information.
694 StringRef Name = BTy->getName();
695 // Add name if not anonymous or intermediate type.
696 if (!Name.empty())
697 addString(Buffer, dwarf::DW_AT_name, Name);
698
699 // An unspecified type only has a name attribute.
700 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
701 return;
702
703 if (BTy->getTag() != dwarf::DW_TAG_string_type)
704 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
705 BTy->getEncoding());
706
707 uint64_t Size = BTy->getSizeInBits() >> 3;
708 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
709
710 if (BTy->isBigEndian())
711 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_big);
712 else if (BTy->isLittleEndian())
713 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_little);
714}
715
716void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
717 // Get core information.
718 StringRef Name = STy->getName();
719 // Add name if not anonymous or intermediate type.
720 if (!Name.empty())
721 addString(Buffer, dwarf::DW_AT_name, Name);
722
723 if (DIVariable *Var = STy->getStringLength()) {
724 if (auto *VarDIE = getDIE(Var))
725 addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE);
726 } else if (DIExpression *Expr = STy->getStringLengthExp()) {
727 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
728 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
729 // This is to describe the memory location of the
730 // length of a Fortran deferred length string, so
731 // lock it down as such.
732 DwarfExpr.setMemoryLocationKind();
733 DwarfExpr.addExpression(Expr);
734 addBlock(Buffer, dwarf::DW_AT_string_length, DwarfExpr.finalize());
735 } else {
736 uint64_t Size = STy->getSizeInBits() >> 3;
737 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
738 }
739
740 if (DIExpression *Expr = STy->getStringLocationExp()) {
741 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
742 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
743 // This is to describe the memory location of the
744 // string, so lock it down as such.
745 DwarfExpr.setMemoryLocationKind();
746 DwarfExpr.addExpression(Expr);
747 addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
748 }
749
750 if (STy->getEncoding()) {
751 // For eventual Unicode support.
752 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
753 STy->getEncoding());
754 }
755}
756
757void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
758 // Get core information.
759 StringRef Name = DTy->getName();
760 uint64_t Size = DTy->getSizeInBits() >> 3;
761 uint16_t Tag = Buffer.getTag();
762
763 // Map to main type, void will not have a type.
764 const DIType *FromTy = DTy->getBaseType();
765 if (FromTy)
766 addType(Buffer, FromTy);
767
768 // Add name if not anonymous or intermediate type.
769 if (!Name.empty())
770 addString(Buffer, dwarf::DW_AT_name, Name);
771
772 addAnnotation(Buffer, DTy->getAnnotations());
773
774 // If alignment is specified for a typedef , create and insert DW_AT_alignment
775 // attribute in DW_TAG_typedef DIE.
776 if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {
777 uint32_t AlignInBytes = DTy->getAlignInBytes();
778 if (AlignInBytes > 0)
779 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
780 AlignInBytes);
781 }
782
783 // Add size if non-zero (derived types might be zero-sized.)
784 if (Size && Tag != dwarf::DW_TAG_pointer_type
785 && Tag != dwarf::DW_TAG_ptr_to_member_type
786 && Tag != dwarf::DW_TAG_reference_type
787 && Tag != dwarf::DW_TAG_rvalue_reference_type)
788 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
789
790 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
791 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
792 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType()));
793
794 addAccess(Buffer, DTy->getFlags());
795
796 // Add source line info if available and TyDesc is not a forward declaration.
797 if (!DTy->isForwardDecl())
798 addSourceLine(Buffer, DTy);
799
800 // If DWARF address space value is other than None, add it. The IR
801 // verifier checks that DWARF address space only exists for pointer
802 // or reference types.
803 if (DTy->getDWARFAddressSpace())
804 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
805 *DTy->getDWARFAddressSpace());
806}
807
808void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
809 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
810 const DIType *Ty = Args[i];
811 if (!Ty) {
812 assert(i == N-1 && "Unspecified parameter must be the last argument")(static_cast <bool> (i == N-1 && "Unspecified parameter must be the last argument"
) ? void (0) : __assert_fail ("i == N-1 && \"Unspecified parameter must be the last argument\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 812, __extension__
__PRETTY_FUNCTION__))
;
813 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
814 } else {
815 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
816 addType(Arg, Ty);
817 if (Ty->isArtificial())
818 addFlag(Arg, dwarf::DW_AT_artificial);
819 }
820 }
821}
822
823void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
824 // Add return type. A void return won't have a type.
825 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
826 if (Elements.size())
827 if (auto RTy = Elements[0])
828 addType(Buffer, RTy);
829
830 bool isPrototyped = true;
831 if (Elements.size() == 2 && !Elements[1])
832 isPrototyped = false;
833
834 constructSubprogramArguments(Buffer, Elements);
835
836 // Add prototype flag if we're dealing with a C language and the function has
837 // been prototyped.
838 uint16_t Language = getLanguage();
839 if (isPrototyped &&
840 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
841 Language == dwarf::DW_LANG_ObjC))
842 addFlag(Buffer, dwarf::DW_AT_prototyped);
843
844 // Add a DW_AT_calling_convention if this has an explicit convention.
845 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
846 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
847 CTy->getCC());
848
849 if (CTy->isLValueReference())
850 addFlag(Buffer, dwarf::DW_AT_reference);
851
852 if (CTy->isRValueReference())
853 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
854}
855
856void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
857 if (!Annotations)
858 return;
859
860 for (const Metadata *Annotation : Annotations->operands()) {
861 const MDNode *MD = cast<MDNode>(Annotation);
862 const MDString *Name = cast<MDString>(MD->getOperand(0));
863 const auto &Value = MD->getOperand(1);
864
865 DIE &AnnotationDie = createAndAddDIE(dwarf::DW_TAG_LLVM_annotation, Buffer);
866 addString(AnnotationDie, dwarf::DW_AT_name, Name->getString());
867 if (const auto *Data = dyn_cast<MDString>(Value))
868 addString(AnnotationDie, dwarf::DW_AT_const_value, Data->getString());
869 else if (const auto *Data = dyn_cast<ConstantAsMetadata>(Value))
870 addConstantValue(AnnotationDie, Data->getValue()->getUniqueInteger(),
871 /*Unsigned=*/true);
872 else
873 assert(false && "Unsupported annotation value type")(static_cast <bool> (false && "Unsupported annotation value type"
) ? void (0) : __assert_fail ("false && \"Unsupported annotation value type\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 873, __extension__
__PRETTY_FUNCTION__))
;
874 }
875}
876
877void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
878 // Add name if not anonymous or intermediate type.
879 StringRef Name = CTy->getName();
880
881 uint64_t Size = CTy->getSizeInBits() >> 3;
882 uint16_t Tag = Buffer.getTag();
883
884 switch (Tag) {
24
Control jumps to 'case DW_TAG_variant_part:' at line 891
885 case dwarf::DW_TAG_array_type:
886 constructArrayTypeDIE(Buffer, CTy);
887 break;
888 case dwarf::DW_TAG_enumeration_type:
889 constructEnumTypeDIE(Buffer, CTy);
890 break;
891 case dwarf::DW_TAG_variant_part:
892 case dwarf::DW_TAG_structure_type:
893 case dwarf::DW_TAG_union_type:
894 case dwarf::DW_TAG_class_type:
895 case dwarf::DW_TAG_namelist: {
896 // Emit the discriminator for a variant part.
897 DIDerivedType *Discriminator = nullptr;
898 if (Tag
24.1
'Tag' is equal to DW_TAG_variant_part
== dwarf::DW_TAG_variant_part) {
25
Taking true branch
899 Discriminator = CTy->getDiscriminator();
26
Value assigned to 'Discriminator'
900 if (Discriminator) {
27
Assuming 'Discriminator' is null
901 // DWARF says:
902 // If the variant part has a discriminant, the discriminant is
903 // represented by a separate debugging information entry which is
904 // a child of the variant part entry.
905 DIE &DiscMember = constructMemberDIE(Buffer, Discriminator);
906 addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember);
907 }
908 }
909
910 // Add template parameters to a class, structure or union types.
911 if (Tag
27.1
'Tag' is not equal to DW_TAG_class_type
== dwarf::DW_TAG_class_type ||
28
Taking false branch
912 Tag
27.2
'Tag' is not equal to DW_TAG_structure_type
== dwarf::DW_TAG_structure_type || Tag
27.3
'Tag' is not equal to DW_TAG_union_type
== dwarf::DW_TAG_union_type)
913 addTemplateParams(Buffer, CTy->getTemplateParams());
914
915 // Add elements to structure type.
916 DINodeArray Elements = CTy->getElements();
917 for (const auto *Element : Elements) {
918 if (!Element)
29
Assuming 'Element' is non-null
30
Taking false branch
919 continue;
920 if (auto *SP
31.1
'SP' is null
= dyn_cast<DISubprogram>(Element))
31
Assuming 'Element' is not a 'CastReturnType'
32
Taking false branch
921 getOrCreateSubprogramDIE(SP);
922 else if (auto *DDTy
33.1
'DDTy' is non-null
= dyn_cast<DIDerivedType>(Element)) {
33
Assuming 'Element' is a 'CastReturnType'
34
Taking true branch
923 if (DDTy->getTag() == dwarf::DW_TAG_friend) {
35
Assuming the condition is false
36
Taking false branch
924 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
925 addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend);
926 } else if (DDTy->isStaticMember()) {
37
Assuming the condition is false
38
Taking false branch
927 getOrCreateStaticMemberDIE(DDTy);
928 } else if (Tag
38.1
'Tag' is equal to DW_TAG_variant_part
== dwarf::DW_TAG_variant_part) {
39
Taking true branch
929 // When emitting a variant part, wrap each member in
930 // DW_TAG_variant.
931 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer);
932 if (const ConstantInt *CI
40.1
'CI' is non-null
=
41
Taking true branch
933 dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) {
40
Assuming the object is a 'CastReturnType'
934 if (DD->isUnsignedDIType(Discriminator->getBaseType()))
42
Called C++ object pointer is null
935 addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue());
936 else
937 addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue());
938 }
939 constructMemberDIE(Variant, DDTy);
940 } else {
941 constructMemberDIE(Buffer, DDTy);
942 }
943 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
944 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
945 StringRef PropertyName = Property->getName();
946 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
947 if (Property->getType())
948 addType(ElemDie, Property->getType());
949 addSourceLine(ElemDie, Property);
950 StringRef GetterName = Property->getGetterName();
951 if (!GetterName.empty())
952 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
953 StringRef SetterName = Property->getSetterName();
954 if (!SetterName.empty())
955 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
956 if (unsigned PropertyAttributes = Property->getAttributes())
957 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
958 PropertyAttributes);
959 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
960 if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
961 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer);
962 constructTypeDIE(VariantPart, Composite);
963 }
964 } else if (Tag == dwarf::DW_TAG_namelist) {
965 auto *Var = dyn_cast<DINode>(Element);
966 auto *VarDIE = getDIE(Var);
967 if (VarDIE) {
968 DIE &ItemDie = createAndAddDIE(dwarf::DW_TAG_namelist_item, Buffer);
969 addDIEEntry(ItemDie, dwarf::DW_AT_namelist_item, *VarDIE);
970 }
971 }
972 }
973
974 if (CTy->isAppleBlockExtension())
975 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
976
977 if (CTy->getExportSymbols())
978 addFlag(Buffer, dwarf::DW_AT_export_symbols);
979
980 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
981 // inside C++ composite types to point to the base class with the vtable.
982 // Rust uses DW_AT_containing_type to link a vtable to the type
983 // for which it was created.
984 if (auto *ContainingType = CTy->getVTableHolder())
985 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
986 *getOrCreateTypeDIE(ContainingType));
987
988 if (CTy->isObjcClassComplete())
989 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
990
991 // Add the type's non-standard calling convention.
992 // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
993 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) {
994 uint8_t CC = 0;
995 if (CTy->isTypePassByValue())
996 CC = dwarf::DW_CC_pass_by_value;
997 else if (CTy->isTypePassByReference())
998 CC = dwarf::DW_CC_pass_by_reference;
999 if (CC)
1000 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
1001 CC);
1002 }
1003 break;
1004 }
1005 default:
1006 break;
1007 }
1008
1009 // Add name if not anonymous or intermediate type.
1010 if (!Name.empty())
1011 addString(Buffer, dwarf::DW_AT_name, Name);
1012
1013 addAnnotation(Buffer, CTy->getAnnotations());
1014
1015 if (Tag == dwarf::DW_TAG_enumeration_type ||
1016 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1017 Tag == dwarf::DW_TAG_union_type) {
1018 // Add size if non-zero (derived types might be zero-sized.)
1019 // Ignore the size if it's a non-enum forward decl.
1020 // TODO: Do we care about size for enum forward declarations?
1021 if (Size &&
1022 (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type))
1023 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
1024 else if (!CTy->isForwardDecl())
1025 // Add zero size if it is not a forward declaration.
1026 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
1027
1028 // If we're a forward decl, say so.
1029 if (CTy->isForwardDecl())
1030 addFlag(Buffer, dwarf::DW_AT_declaration);
1031
1032 // Add accessibility info if available.
1033 addAccess(Buffer, CTy->getFlags());
1034
1035 // Add source line info if available.
1036 if (!CTy->isForwardDecl())
1037 addSourceLine(Buffer, CTy);
1038
1039 // No harm in adding the runtime language to the declaration.
1040 unsigned RLang = CTy->getRuntimeLang();
1041 if (RLang)
1042 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1043 RLang);
1044
1045 // Add align info if available.
1046 if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1047 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1048 AlignInBytes);
1049 }
1050}
1051
1052void DwarfUnit::constructTemplateTypeParameterDIE(
1053 DIE &Buffer, const DITemplateTypeParameter *TP) {
1054 DIE &ParamDIE =
1055 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1056 // Add the type if it exists, it could be void and therefore no type.
1057 if (TP->getType())
1058 addType(ParamDIE, TP->getType());
1059 if (!TP->getName().empty())
1060 addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1061 if (TP->isDefault() && (DD->getDwarfVersion() >= 5))
1062 addFlag(ParamDIE, dwarf::DW_AT_default_value);
1063}
1064
1065void DwarfUnit::constructTemplateValueParameterDIE(
1066 DIE &Buffer, const DITemplateValueParameter *VP) {
1067 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1068
1069 // Add the type if there is one, template template and template parameter
1070 // packs will not have a type.
1071 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1072 addType(ParamDIE, VP->getType());
1073 if (!VP->getName().empty())
1074 addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1075 if (VP->isDefault() && (DD->getDwarfVersion() >= 5))
1076 addFlag(ParamDIE, dwarf::DW_AT_default_value);
1077 if (Metadata *Val = VP->getValue()) {
1078 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
1079 addConstantValue(ParamDIE, CI, VP->getType());
1080 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1081 // We cannot describe the location of dllimport'd entities: the
1082 // computation of their address requires loads from the IAT.
1083 if (!GV->hasDLLImportStorageClass()) {
1084 // For declaration non-type template parameters (such as global values
1085 // and functions)
1086 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1087 addOpAddress(*Loc, Asm->getSymbol(GV));
1088 // Emit DW_OP_stack_value to use the address as the immediate value of
1089 // the parameter, rather than a pointer to it.
1090 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1091 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1092 }
1093 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1094 assert(isa<MDString>(Val))(static_cast <bool> (isa<MDString>(Val)) ? void (
0) : __assert_fail ("isa<MDString>(Val)", "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp"
, 1094, __extension__ __PRETTY_FUNCTION__))
;
1095 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1096 cast<MDString>(Val)->getString());
1097 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1098 addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1099 }
1100 }
1101}
1102
1103DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1104 // Construct the context before querying for the existence of the DIE in case
1105 // such construction creates the DIE.
1106 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1107
1108 if (DIE *NDie = getDIE(NS))
1109 return NDie;
1110 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1111
1112 StringRef Name = NS->getName();
1113 if (!Name.empty())
1114 addString(NDie, dwarf::DW_AT_name, NS->getName());
1115 else
1116 Name = "(anonymous namespace)";
1117 DD->addAccelNamespace(*CUNode, Name, NDie);
1118 addGlobalName(Name, NDie, NS->getScope());
1119 if (NS->getExportSymbols())
1120 addFlag(NDie, dwarf::DW_AT_export_symbols);
1121 return &NDie;
1122}
1123
1124DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1125 // Construct the context before querying for the existence of the DIE in case
1126 // such construction creates the DIE.
1127 DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1128
1129 if (DIE *MDie = getDIE(M))
1130 return MDie;
1131 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1132
1133 if (!M->getName().empty()) {
1134 addString(MDie, dwarf::DW_AT_name, M->getName());
1135 addGlobalName(M->getName(), MDie, M->getScope());
1136 }
1137 if (!M->getConfigurationMacros().empty())
1138 addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1139 M->getConfigurationMacros());
1140 if (!M->getIncludePath().empty())
1141 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1142 if (!M->getAPINotesFile().empty())
1143 addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile());
1144 if (M->getFile())
1145 addUInt(MDie, dwarf::DW_AT_decl_file, None,
1146 getOrCreateSourceID(M->getFile()));
1147 if (M->getLineNo())
1148 addUInt(MDie, dwarf::DW_AT_decl_line, None, M->getLineNo());
1149 if (M->getIsDecl())
1150 addFlag(MDie, dwarf::DW_AT_declaration);
1151
1152 return &MDie;
1153}
1154
1155DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1156 // Construct the context before querying for the existence of the DIE in case
1157 // such construction creates the DIE (as is the case for member function
1158 // declarations).
1159 DIE *ContextDIE =
1160 Minimal ? &getUnitDie() : getOrCreateContextDIE(SP->getScope());
1161
1162 if (DIE *SPDie = getDIE(SP))
1163 return SPDie;
1164
1165 if (auto *SPDecl = SP->getDeclaration()) {
1166 if (!Minimal) {
1167 // Add subprogram definitions to the CU die directly.
1168 ContextDIE = &getUnitDie();
1169 // Build the decl now to ensure it precedes the definition.
1170 getOrCreateSubprogramDIE(SPDecl);
1171 }
1172 }
1173
1174 // DW_TAG_inlined_subroutine may refer to this DIE.
1175 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1176
1177 // Stop here and fill this in later, depending on whether or not this
1178 // subprogram turns out to have inlined instances or not.
1179 if (SP->isDefinition())
1180 return &SPDie;
1181
1182 static_cast<DwarfUnit *>(SPDie.getUnit())
1183 ->applySubprogramAttributes(SP, SPDie);
1184 return &SPDie;
1185}
1186
1187bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1188 DIE &SPDie, bool Minimal) {
1189 DIE *DeclDie = nullptr;
1190 StringRef DeclLinkageName;
1191 if (auto *SPDecl = SP->getDeclaration()) {
1192 if (!Minimal) {
1193 DITypeRefArray DeclArgs, DefinitionArgs;
1194 DeclArgs = SPDecl->getType()->getTypeArray();
1195 DefinitionArgs = SP->getType()->getTypeArray();
1196
1197 if (DeclArgs.size() && DefinitionArgs.size())
1198 if (DefinitionArgs[0] != nullptr && DeclArgs[0] != DefinitionArgs[0])
1199 addType(SPDie, DefinitionArgs[0]);
1200
1201 DeclDie = getDIE(SPDecl);
1202 assert(DeclDie && "This DIE should've already been constructed when the "(static_cast <bool> (DeclDie && "This DIE should've already been constructed when the "
"definition DIE was created in " "getOrCreateSubprogramDIE")
? void (0) : __assert_fail ("DeclDie && \"This DIE should've already been constructed when the \" \"definition DIE was created in \" \"getOrCreateSubprogramDIE\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1204, __extension__
__PRETTY_FUNCTION__))
1203 "definition DIE was created in "(static_cast <bool> (DeclDie && "This DIE should've already been constructed when the "
"definition DIE was created in " "getOrCreateSubprogramDIE")
? void (0) : __assert_fail ("DeclDie && \"This DIE should've already been constructed when the \" \"definition DIE was created in \" \"getOrCreateSubprogramDIE\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1204, __extension__
__PRETTY_FUNCTION__))
1204 "getOrCreateSubprogramDIE")(static_cast <bool> (DeclDie && "This DIE should've already been constructed when the "
"definition DIE was created in " "getOrCreateSubprogramDIE")
? void (0) : __assert_fail ("DeclDie && \"This DIE should've already been constructed when the \" \"definition DIE was created in \" \"getOrCreateSubprogramDIE\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1204, __extension__
__PRETTY_FUNCTION__))
;
1205 // Look at the Decl's linkage name only if we emitted it.
1206 if (DD->useAllLinkageNames())
1207 DeclLinkageName = SPDecl->getLinkageName();
1208 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile());
1209 unsigned DefID = getOrCreateSourceID(SP->getFile());
1210 if (DeclID != DefID)
1211 addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID);
1212
1213 if (SP->getLine() != SPDecl->getLine())
1214 addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine());
1215 }
1216 }
1217
1218 // Add function template parameters.
1219 addTemplateParams(SPDie, SP->getTemplateParams());
1220
1221 // Add the linkage name if we have one and it isn't in the Decl.
1222 StringRef LinkageName = SP->getLinkageName();
1223 assert(((LinkageName.empty() || DeclLinkageName.empty()) ||(static_cast <bool> (((LinkageName.empty() || DeclLinkageName
.empty()) || LinkageName == DeclLinkageName) && "decl has a linkage name and it is different"
) ? void (0) : __assert_fail ("((LinkageName.empty() || DeclLinkageName.empty()) || LinkageName == DeclLinkageName) && \"decl has a linkage name and it is different\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1225, __extension__
__PRETTY_FUNCTION__))
1224 LinkageName == DeclLinkageName) &&(static_cast <bool> (((LinkageName.empty() || DeclLinkageName
.empty()) || LinkageName == DeclLinkageName) && "decl has a linkage name and it is different"
) ? void (0) : __assert_fail ("((LinkageName.empty() || DeclLinkageName.empty()) || LinkageName == DeclLinkageName) && \"decl has a linkage name and it is different\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1225, __extension__
__PRETTY_FUNCTION__))
1225 "decl has a linkage name and it is different")(static_cast <bool> (((LinkageName.empty() || DeclLinkageName
.empty()) || LinkageName == DeclLinkageName) && "decl has a linkage name and it is different"
) ? void (0) : __assert_fail ("((LinkageName.empty() || DeclLinkageName.empty()) || LinkageName == DeclLinkageName) && \"decl has a linkage name and it is different\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1225, __extension__
__PRETTY_FUNCTION__))
;
1226 if (DeclLinkageName.empty() &&
1227 // Always emit it for abstract subprograms.
1228 (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP)))
1229 addLinkageName(SPDie, LinkageName);
1230
1231 if (!DeclDie)
1232 return false;
1233
1234 // Refer to the function declaration where all the other attributes will be
1235 // found.
1236 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1237 return true;
1238}
1239
1240void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1241 bool SkipSPAttributes) {
1242 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1243 // and its source location.
1244 bool SkipSPSourceLocation = SkipSPAttributes &&
1245 !CUNode->getDebugInfoForProfiling();
1246 if (!SkipSPSourceLocation)
1247 if (applySubprogramDefinitionAttributes(SP, SPDie, SkipSPAttributes))
1248 return;
1249
1250 // Constructors and operators for anonymous aggregates do not have names.
1251 if (!SP->getName().empty())
1252 addString(SPDie, dwarf::DW_AT_name, SP->getName());
1253
1254 addAnnotation(SPDie, SP->getAnnotations());
1255
1256 if (!SkipSPSourceLocation)
1257 addSourceLine(SPDie, SP);
1258
1259 // Skip the rest of the attributes under -gmlt to save space.
1260 if (SkipSPAttributes)
1261 return;
1262
1263 // Add the prototype if we have a prototype and we have a C like
1264 // language.
1265 uint16_t Language = getLanguage();
1266 if (SP->isPrototyped() &&
1267 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1268 Language == dwarf::DW_LANG_ObjC))
1269 addFlag(SPDie, dwarf::DW_AT_prototyped);
1270
1271 if (SP->isObjCDirect())
1272 addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct);
1273
1274 unsigned CC = 0;
1275 DITypeRefArray Args;
1276 if (const DISubroutineType *SPTy = SP->getType()) {
1277 Args = SPTy->getTypeArray();
1278 CC = SPTy->getCC();
1279 }
1280
1281 // Add a DW_AT_calling_convention if this has an explicit convention.
1282 if (CC && CC != dwarf::DW_CC_normal)
1283 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1284
1285 // Add a return type. If this is a type like a C/C++ void type we don't add a
1286 // return type.
1287 if (Args.size())
1288 if (auto Ty = Args[0])
1289 addType(SPDie, Ty);
1290
1291 unsigned VK = SP->getVirtuality();
1292 if (VK) {
1293 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1294 if (SP->getVirtualIndex() != -1u) {
1295 DIELoc *Block = getDIELoc();
1296 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1297 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1298 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1299 }
1300 ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType()));
1301 }
1302
1303 if (!SP->isDefinition()) {
1304 addFlag(SPDie, dwarf::DW_AT_declaration);
1305
1306 // Add arguments. Do not add arguments for subprogram definition. They will
1307 // be handled while processing variables.
1308 constructSubprogramArguments(SPDie, Args);
1309 }
1310
1311 addThrownTypes(SPDie, SP->getThrownTypes());
1312
1313 if (SP->isArtificial())
1314 addFlag(SPDie, dwarf::DW_AT_artificial);
1315
1316 if (!SP->isLocalToUnit())
1317 addFlag(SPDie, dwarf::DW_AT_external);
1318
1319 if (DD->useAppleExtensionAttributes()) {
1320 if (SP->isOptimized())
1321 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1322
1323 if (unsigned isa = Asm->getISAEncoding())
1324 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1325 }
1326
1327 if (SP->isLValueReference())
1328 addFlag(SPDie, dwarf::DW_AT_reference);
1329
1330 if (SP->isRValueReference())
1331 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1332
1333 if (SP->isNoReturn())
1334 addFlag(SPDie, dwarf::DW_AT_noreturn);
1335
1336 addAccess(SPDie, SP->getFlags());
1337
1338 if (SP->isExplicit())
1339 addFlag(SPDie, dwarf::DW_AT_explicit);
1340
1341 if (SP->isMainSubprogram())
1342 addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1343 if (SP->isPure())
1344 addFlag(SPDie, dwarf::DW_AT_pure);
1345 if (SP->isElemental())
1346 addFlag(SPDie, dwarf::DW_AT_elemental);
1347 if (SP->isRecursive())
1348 addFlag(SPDie, dwarf::DW_AT_recursive);
1349
1350 if (!SP->getTargetFuncName().empty())
1351 addString(SPDie, dwarf::DW_AT_trampoline, SP->getTargetFuncName());
1352
1353 if (DD->getDwarfVersion() >= 5 && SP->isDeleted())
1354 addFlag(SPDie, dwarf::DW_AT_deleted);
1355}
1356
1357void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1358 DIE *IndexTy) {
1359 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1360 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
1361
1362 // The LowerBound value defines the lower bounds which is typically zero for
1363 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1364 // Count == -1 then the array is unbounded and we do not emit
1365 // DW_AT_lower_bound and DW_AT_count attributes.
1366 int64_t DefaultLowerBound = getDefaultLowerBound();
1367
1368 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1369 DISubrange::BoundType Bound) -> void {
1370 if (auto *BV = Bound.dyn_cast<DIVariable *>()) {
1371 if (auto *VarDIE = getDIE(BV))
1372 addDIEEntry(DW_Subrange, Attr, *VarDIE);
1373 } else if (auto *BE = Bound.dyn_cast<DIExpression *>()) {
1374 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1375 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1376 DwarfExpr.setMemoryLocationKind();
1377 DwarfExpr.addExpression(BE);
1378 addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
1379 } else if (auto *BI = Bound.dyn_cast<ConstantInt *>()) {
1380 if (Attr == dwarf::DW_AT_count) {
1381 if (BI->getSExtValue() != -1)
1382 addUInt(DW_Subrange, Attr, None, BI->getSExtValue());
1383 } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1384 BI->getSExtValue() != DefaultLowerBound)
1385 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1386 }
1387 };
1388
1389 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1390
1391 AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount());
1392
1393 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1394
1395 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride());
1396}
1397
1398void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
1399 const DIGenericSubrange *GSR,
1400 DIE *IndexTy) {
1401 DIE &DwGenericSubrange =
1402 createAndAddDIE(dwarf::DW_TAG_generic_subrange, Buffer);
1403 addDIEEntry(DwGenericSubrange, dwarf::DW_AT_type, *IndexTy);
1404
1405 int64_t DefaultLowerBound = getDefaultLowerBound();
1406
1407 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1408 DIGenericSubrange::BoundType Bound) -> void {
1409 if (auto *BV = Bound.dyn_cast<DIVariable *>()) {
1410 if (auto *VarDIE = getDIE(BV))
1411 addDIEEntry(DwGenericSubrange, Attr, *VarDIE);
1412 } else if (auto *BE = Bound.dyn_cast<DIExpression *>()) {
1413 if (BE->isConstant() &&
1414 DIExpression::SignedOrUnsignedConstant::SignedConstant ==
1415 *BE->isConstant()) {
1416 if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1417 static_cast<int64_t>(BE->getElement(1)) != DefaultLowerBound)
1418 addSInt(DwGenericSubrange, Attr, dwarf::DW_FORM_sdata,
1419 BE->getElement(1));
1420 } else {
1421 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1422 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1423 DwarfExpr.setMemoryLocationKind();
1424 DwarfExpr.addExpression(BE);
1425 addBlock(DwGenericSubrange, Attr, DwarfExpr.finalize());
1426 }
1427 }
1428 };
1429
1430 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, GSR->getLowerBound());
1431 AddBoundTypeEntry(dwarf::DW_AT_count, GSR->getCount());
1432 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, GSR->getUpperBound());
1433 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, GSR->getStride());
1434}
1435
1436DIE *DwarfUnit::getIndexTyDie() {
1437 if (IndexTyDie)
1438 return IndexTyDie;
1439 // Construct an integer type to use for indexes.
1440 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1441 StringRef Name = "__ARRAY_SIZE_TYPE__";
1442 addString(*IndexTyDie, dwarf::DW_AT_name, Name);
1443 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1444 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1445 dwarf::getArrayIndexTypeEncoding(
1446 (dwarf::SourceLanguage)getLanguage()));
1447 DD->addAccelType(*CUNode, Name, *IndexTyDie, /*Flags*/ 0);
1448 return IndexTyDie;
1449}
1450
1451/// Returns true if the vector's size differs from the sum of sizes of elements
1452/// the user specified. This can occur if the vector has been rounded up to
1453/// fit memory alignment constraints.
1454static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1455 assert(CTy && CTy->isVector() && "Composite type is not a vector")(static_cast <bool> (CTy && CTy->isVector() &&
"Composite type is not a vector") ? void (0) : __assert_fail
("CTy && CTy->isVector() && \"Composite type is not a vector\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1455, __extension__
__PRETTY_FUNCTION__))
;
1456 const uint64_t ActualSize = CTy->getSizeInBits();
1457
1458 // Obtain the size of each element in the vector.
1459 DIType *BaseTy = CTy->getBaseType();
1460 assert(BaseTy && "Unknown vector element type.")(static_cast <bool> (BaseTy && "Unknown vector element type."
) ? void (0) : __assert_fail ("BaseTy && \"Unknown vector element type.\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1460, __extension__
__PRETTY_FUNCTION__))
;
1461 const uint64_t ElementSize = BaseTy->getSizeInBits();
1462
1463 // Locate the number of elements in the vector.
1464 const DINodeArray Elements = CTy->getElements();
1465 assert(Elements.size() == 1 &&(static_cast <bool> (Elements.size() == 1 && Elements
[0]->getTag() == dwarf::DW_TAG_subrange_type && "Invalid vector element array, expected one element of type subrange"
) ? void (0) : __assert_fail ("Elements.size() == 1 && Elements[0]->getTag() == dwarf::DW_TAG_subrange_type && \"Invalid vector element array, expected one element of type subrange\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1467, __extension__
__PRETTY_FUNCTION__))
1466 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&(static_cast <bool> (Elements.size() == 1 && Elements
[0]->getTag() == dwarf::DW_TAG_subrange_type && "Invalid vector element array, expected one element of type subrange"
) ? void (0) : __assert_fail ("Elements.size() == 1 && Elements[0]->getTag() == dwarf::DW_TAG_subrange_type && \"Invalid vector element array, expected one element of type subrange\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1467, __extension__
__PRETTY_FUNCTION__))
1467 "Invalid vector element array, expected one element of type subrange")(static_cast <bool> (Elements.size() == 1 && Elements
[0]->getTag() == dwarf::DW_TAG_subrange_type && "Invalid vector element array, expected one element of type subrange"
) ? void (0) : __assert_fail ("Elements.size() == 1 && Elements[0]->getTag() == dwarf::DW_TAG_subrange_type && \"Invalid vector element array, expected one element of type subrange\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1467, __extension__
__PRETTY_FUNCTION__))
;
1468 const auto Subrange = cast<DISubrange>(Elements[0]);
1469 const auto NumVecElements =
1470 Subrange->getCount()
1471 ? Subrange->getCount().get<ConstantInt *>()->getSExtValue()
1472 : 0;
1473
1474 // Ensure we found the element count and that the actual size is wide
1475 // enough to contain the requested size.
1476 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size")(static_cast <bool> (ActualSize >= (NumVecElements *
ElementSize) && "Invalid vector size") ? void (0) : __assert_fail
("ActualSize >= (NumVecElements * ElementSize) && \"Invalid vector size\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1476, __extension__
__PRETTY_FUNCTION__))
;
1477 return ActualSize != (NumVecElements * ElementSize);
1478}
1479
1480void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1481 if (CTy->isVector()) {
1482 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1483 if (hasVectorBeenPadded(CTy))
1484 addUInt(Buffer, dwarf::DW_AT_byte_size, None,
1485 CTy->getSizeInBits() / CHAR_BIT8);
1486 }
1487
1488 if (DIVariable *Var = CTy->getDataLocation()) {
1489 if (auto *VarDIE = getDIE(Var))
1490 addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE);
1491 } else if (DIExpression *Expr = CTy->getDataLocationExp()) {
1492 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1493 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1494 DwarfExpr.setMemoryLocationKind();
1495 DwarfExpr.addExpression(Expr);
1496 addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
1497 }
1498
1499 if (DIVariable *Var = CTy->getAssociated()) {
1500 if (auto *VarDIE = getDIE(Var))
1501 addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE);
1502 } else if (DIExpression *Expr = CTy->getAssociatedExp()) {
1503 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1504 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1505 DwarfExpr.setMemoryLocationKind();
1506 DwarfExpr.addExpression(Expr);
1507 addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize());
1508 }
1509
1510 if (DIVariable *Var = CTy->getAllocated()) {
1511 if (auto *VarDIE = getDIE(Var))
1512 addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE);
1513 } else if (DIExpression *Expr = CTy->getAllocatedExp()) {
1514 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1515 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1516 DwarfExpr.setMemoryLocationKind();
1517 DwarfExpr.addExpression(Expr);
1518 addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize());
1519 }
1520
1521 if (auto *RankConst = CTy->getRankConst()) {
1522 addSInt(Buffer, dwarf::DW_AT_rank, dwarf::DW_FORM_sdata,
1523 RankConst->getSExtValue());
1524 } else if (auto *RankExpr = CTy->getRankExp()) {
1525 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1526 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1527 DwarfExpr.setMemoryLocationKind();
1528 DwarfExpr.addExpression(RankExpr);
1529 addBlock(Buffer, dwarf::DW_AT_rank, DwarfExpr.finalize());
1530 }
1531
1532 // Emit the element type.
1533 addType(Buffer, CTy->getBaseType());
1534
1535 // Get an anonymous type for index type.
1536 // FIXME: This type should be passed down from the front end
1537 // as different languages may have different sizes for indexes.
1538 DIE *IdxTy = getIndexTyDie();
1539
1540 // Add subranges to array type.
1541 DINodeArray Elements = CTy->getElements();
1542 for (DINode *E : Elements) {
1543 // FIXME: Should this really be such a loose cast?
1544 if (auto *Element = dyn_cast_or_null<DINode>(E)) {
1545 if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1546 constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy);
1547 else if (Element->getTag() == dwarf::DW_TAG_generic_subrange)
1548 constructGenericSubrangeDIE(Buffer, cast<DIGenericSubrange>(Element),
1549 IdxTy);
1550 }
1551 }
1552}
1553
1554void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1555 const DIType *DTy = CTy->getBaseType();
1556 bool IsUnsigned = DTy && DD->isUnsignedDIType(DTy);
1557 if (DTy) {
1558 if (DD->getDwarfVersion() >= 3)
1559 addType(Buffer, DTy);
1560 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1561 addFlag(Buffer, dwarf::DW_AT_enum_class);
1562 }
1563
1564 auto *Context = CTy->getScope();
1565 bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
1566 isa<DINamespace>(Context) || isa<DICommonBlock>(Context);
1567 DINodeArray Elements = CTy->getElements();
1568
1569 // Add enumerators to enumeration type.
1570 for (const DINode *E : Elements) {
1571 auto *Enum = dyn_cast_or_null<DIEnumerator>(E);
1572 if (Enum) {
1573 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1574 StringRef Name = Enum->getName();
1575 addString(Enumerator, dwarf::DW_AT_name, Name);
1576 addConstantValue(Enumerator, Enum->getValue(), IsUnsigned);
1577 if (IndexEnumerators)
1578 addGlobalName(Name, Enumerator, Context);
1579 }
1580 }
1581}
1582
1583void DwarfUnit::constructContainingTypeDIEs() {
1584 for (auto &P : ContainingTypeMap) {
1585 DIE &SPDie = *P.first;
1586 const DINode *D = P.second;
1587 if (!D)
1588 continue;
1589 DIE *NDie = getDIE(D);
1590 if (!NDie)
1591 continue;
1592 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1593 }
1594}
1595
1596DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1597 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer);
1598 StringRef Name = DT->getName();
1599 if (!Name.empty())
1600 addString(MemberDie, dwarf::DW_AT_name, Name);
1601
1602 addAnnotation(MemberDie, DT->getAnnotations());
1603
1604 if (DIType *Resolved = DT->getBaseType())
1605 addType(MemberDie, Resolved);
1606
1607 addSourceLine(MemberDie, DT);
1608
1609 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1610
1611 // For C++, virtual base classes are not at fixed offset. Use following
1612 // expression to extract appropriate offset from vtable.
1613 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1614
1615 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1616 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1617 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1618 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1619 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1620 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1621 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1622 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1623
1624 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1625 } else {
1626 uint64_t Size = DT->getSizeInBits();
1627 uint64_t FieldSize = DD->getBaseTypeSize(DT);
1628 uint32_t AlignInBytes = DT->getAlignInBytes();
1629 uint64_t OffsetInBytes;
1630
1631 bool IsBitfield = FieldSize && Size != FieldSize;
1632 if (IsBitfield) {
1633 // Handle bitfield, assume bytes are 8 bits.
1634 if (DD->useDWARF2Bitfields())
1635 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1636 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1637
1638 uint64_t Offset = DT->getOffsetInBits();
1639 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1640 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1641 // which can't be done with bitfields. Thus we use FieldSize here.
1642 uint32_t AlignInBits = FieldSize;
1643 uint32_t AlignMask = ~(AlignInBits - 1);
1644 // The bits from the start of the storage unit to the start of the field.
1645 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1646 // The byte offset of the field's aligned storage unit inside the struct.
1647 OffsetInBytes = (Offset - StartBitOffset) / 8;
1648
1649 if (DD->useDWARF2Bitfields()) {
1650 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1651 uint64_t FieldOffset = (HiMark - FieldSize);
1652 Offset -= FieldOffset;
1653
1654 // Maybe we need to work from the other end.
1655 if (Asm->getDataLayout().isLittleEndian())
1656 Offset = FieldSize - (Offset + Size);
1657
1658 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1659 OffsetInBytes = FieldOffset >> 3;
1660 } else {
1661 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset);
1662 }
1663 } else {
1664 // This is not a bitfield.
1665 OffsetInBytes = DT->getOffsetInBits() / 8;
1666 if (AlignInBytes)
1667 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1668 AlignInBytes);
1669 }
1670
1671 if (DD->getDwarfVersion() <= 2) {
1672 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1673 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1674 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1675 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1676 } else if (!IsBitfield || DD->useDWARF2Bitfields()) {
1677 // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
1678 // interpreted as location-list pointers. Interpreting constants as
1679 // pointers is not expected, so we use DW_FORM_udata to encode the
1680 // constants here.
1681 if (DD->getDwarfVersion() == 3)
1682 addUInt(MemberDie, dwarf::DW_AT_data_member_location,
1683 dwarf::DW_FORM_udata, OffsetInBytes);
1684 else
1685 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1686 OffsetInBytes);
1687 }
1688 }
1689
1690 addAccess(MemberDie, DT->getFlags());
1691
1692 if (DT->isVirtual())
1693 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1694 dwarf::DW_VIRTUALITY_virtual);
1695
1696 // Objective-C properties.
1697 if (DINode *PNode = DT->getObjCProperty())
1698 if (DIE *PDie = getDIE(PNode))
1699 addAttribute(MemberDie, dwarf::DW_AT_APPLE_property,
1700 dwarf::DW_FORM_ref4, DIEEntry(*PDie));
1701
1702 if (DT->isArtificial())
1703 addFlag(MemberDie, dwarf::DW_AT_artificial);
1704
1705 return MemberDie;
1706}
1707
1708DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1709 if (!DT)
1710 return nullptr;
1711
1712 // Construct the context before querying for the existence of the DIE in case
1713 // such construction creates the DIE.
1714 DIE *ContextDIE = getOrCreateContextDIE(DT->getScope());
1715 assert(dwarf::isType(ContextDIE->getTag()) &&(static_cast <bool> (dwarf::isType(ContextDIE->getTag
()) && "Static member should belong to a type.") ? void
(0) : __assert_fail ("dwarf::isType(ContextDIE->getTag()) && \"Static member should belong to a type.\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1716, __extension__
__PRETTY_FUNCTION__))
1716 "Static member should belong to a type.")(static_cast <bool> (dwarf::isType(ContextDIE->getTag
()) && "Static member should belong to a type.") ? void
(0) : __assert_fail ("dwarf::isType(ContextDIE->getTag()) && \"Static member should belong to a type.\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1716, __extension__
__PRETTY_FUNCTION__))
;
1717
1718 if (DIE *StaticMemberDIE = getDIE(DT))
1719 return StaticMemberDIE;
1720
1721 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
1722
1723 const DIType *Ty = DT->getBaseType();
1724
1725 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
1726 addType(StaticMemberDIE, Ty);
1727 addSourceLine(StaticMemberDIE, DT);
1728 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1729 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1730
1731 // FIXME: We could omit private if the parent is a class_type, and
1732 // public if the parent is something else.
1733 addAccess(StaticMemberDIE, DT->getFlags());
1734
1735 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
1736 addConstantValue(StaticMemberDIE, CI, Ty);
1737 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
1738 addConstantFPValue(StaticMemberDIE, CFP);
1739
1740 if (uint32_t AlignInBytes = DT->getAlignInBytes())
1741 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1742 AlignInBytes);
1743
1744 return &StaticMemberDIE;
1745}
1746
1747void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1748 // Emit size of content not including length itself
1749 if (!DD->useSectionsAsReferences())
1750 EndLabel = Asm->emitDwarfUnitLength(
1751 isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
1752 else
1753 Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
1754 "Length of Unit");
1755
1756 Asm->OutStreamer->AddComment("DWARF version number");
1757 unsigned Version = DD->getDwarfVersion();
1758 Asm->emitInt16(Version);
1759
1760 // DWARF v5 reorders the address size and adds a unit type.
1761 if (Version >= 5) {
1762 Asm->OutStreamer->AddComment("DWARF Unit Type");
1763 Asm->emitInt8(UT);
1764 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1765 Asm->emitInt8(Asm->MAI->getCodePointerSize());
1766 }
1767
1768 // We share one abbreviations table across all units so it's always at the
1769 // start of the section. Use a relocatable offset where needed to ensure
1770 // linking doesn't invalidate that offset.
1771 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
1772 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1773 if (UseOffsets)
1774 Asm->emitDwarfLengthOrOffset(0);
1775 else
1776 Asm->emitDwarfSymbolReference(
1777 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
1778
1779 if (Version <= 4) {
1780 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1781 Asm->emitInt8(Asm->MAI->getCodePointerSize());
1782 }
1783}
1784
1785void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1786 DwarfUnit::emitCommonHeader(UseOffsets,
1787 DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1788 : dwarf::DW_UT_type);
1789 Asm->OutStreamer->AddComment("Type Signature");
1790 Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature));
1791 Asm->OutStreamer->AddComment("Type DIE Offset");
1792 // In a skeleton type unit there is no type DIE so emit a zero offset.
1793 Asm->emitDwarfLengthOrOffset(Ty ? Ty->getOffset() : 0);
1794}
1795
1796void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
1797 const MCSymbol *Hi, const MCSymbol *Lo) {
1798 addAttribute(Die, Attribute, DD->getDwarfSectionOffsetForm(),
1799 new (DIEValueAllocator) DIEDelta(Hi, Lo));
1800}
1801
1802void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
1803 const MCSymbol *Label, const MCSymbol *Sec) {
1804 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1805 addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label);
1806 else
1807 addSectionDelta(Die, Attribute, Label, Sec);
1808}
1809
1810bool DwarfTypeUnit::isDwoUnit() const {
1811 // Since there are no skeleton type units, all type units are dwo type units
1812 // when split DWARF is being used.
1813 return DD->useSplitDwarf();
1814}
1815
1816void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1817 const DIScope *Context) {
1818 getCU().addGlobalNameForTypeUnit(Name, Context);
1819}
1820
1821void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1822 const DIScope *Context) {
1823 getCU().addGlobalTypeUnitType(Ty, Context);
1824}
1825
1826const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1827 if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1828 return nullptr;
1829 if (isDwoUnit())
1830 return nullptr;
1831 return getSection()->getBeginSymbol();
1832}
1833
1834void DwarfUnit::addStringOffsetsStart() {
1835 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1836 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base,
1837 DU->getStringOffsetsStartSym(),
1838 TLOF.getDwarfStrOffSection()->getBeginSymbol());
1839}
1840
1841void DwarfUnit::addRnglistsBase() {
1842 assert(DD->getDwarfVersion() >= 5 &&(static_cast <bool> (DD->getDwarfVersion() >= 5 &&
"DW_AT_rnglists_base requires DWARF version 5 or later") ? void
(0) : __assert_fail ("DD->getDwarfVersion() >= 5 && \"DW_AT_rnglists_base requires DWARF version 5 or later\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1843, __extension__
__PRETTY_FUNCTION__))
1843 "DW_AT_rnglists_base requires DWARF version 5 or later")(static_cast <bool> (DD->getDwarfVersion() >= 5 &&
"DW_AT_rnglists_base requires DWARF version 5 or later") ? void
(0) : __assert_fail ("DD->getDwarfVersion() >= 5 && \"DW_AT_rnglists_base requires DWARF version 5 or later\""
, "llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp", 1843, __extension__
__PRETTY_FUNCTION__))
;
1844 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1845 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base,
1846 DU->getRnglistsTableBaseSym(),
1847 TLOF.getDwarfRnglistsSection()->getBeginSymbol());
1848}
1849
1850void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1851 DD->getAddressPool().resetUsedFlag(true);
1852}