blob: 875737e89d7989449b3f9ac4b31b640fb6ae252e [file] [log] [blame]
Bill Wendling523bea82013-11-08 08:13:15 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "LLVMContextImpl.h"
Bill Wendling523bea82013-11-08 08:13:15 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000022#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000028#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33using namespace llvm::dwarf;
34
35//===----------------------------------------------------------------------===//
36// DIDescriptor
37//===----------------------------------------------------------------------===//
38
39bool DIDescriptor::Verify() const {
40 return DbgNode &&
41 (DIDerivedType(DbgNode).Verify() ||
42 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
43 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
44 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
45 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
46 DILexicalBlock(DbgNode).Verify() ||
47 DILexicalBlockFile(DbgNode).Verify() ||
48 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
49 DIObjCProperty(DbgNode).Verify() ||
50 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
Adrian Prantl87b7eb92014-10-01 18:55:02 +000052 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000053}
54
55static Value *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000056 if (!DbgNode || Elt >= DbgNode->getNumOperands())
57 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000058 return DbgNode->getOperand(Elt);
59}
60
61static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
63}
64
65static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
67 return MDS->getString();
68 return StringRef();
69}
70
71StringRef DIDescriptor::getStringField(unsigned Elt) const {
72 return ::getStringField(DbgNode, Elt);
73}
74
75uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +000076 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +000077 return 0;
78
79 if (Elt < DbgNode->getNumOperands())
80 if (ConstantInt *CI =
81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
82 return CI->getZExtValue();
83
84 return 0;
85}
86
87int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +000088 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +000089 return 0;
90
91 if (Elt < DbgNode->getNumOperands())
92 if (ConstantInt *CI =
93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
94 return CI->getSExtValue();
95
96 return 0;
97}
98
99DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
100 MDNode *Field = getNodeField(DbgNode, Elt);
101 return DIDescriptor(Field);
102}
103
104GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000105 if (!DbgNode)
106 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000107
108 if (Elt < DbgNode->getNumOperands())
109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000110 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000111}
112
113Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000114 if (!DbgNode)
115 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000116
117 if (Elt < DbgNode->getNumOperands())
118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000119 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000120}
121
122Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000123 if (!DbgNode)
124 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000125
126 if (Elt < DbgNode->getNumOperands())
127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000128 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000129}
130
131void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000132 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000133 return;
134
135 if (Elt < DbgNode->getNumOperands()) {
136 MDNode *Node = const_cast<MDNode *>(DbgNode);
137 Node->replaceOperandWith(Elt, F);
138 }
139}
140
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000141static unsigned DIVariableInlinedAtIndex = 4;
142MDNode *DIVariable::getInlinedAt() const {
143 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
144}
Bill Wendling523bea82013-11-08 08:13:15 +0000145
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000146/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000147unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
148 DIType Ty = getType().resolve(Map);
149 // Follow derived types until we reach a type that
150 // reports back a size.
151 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
152 DIDerivedType DT(&*Ty);
153 Ty = DT.getTypeDerivedFrom().resolve(Map);
154 }
155 assert(Ty.getSizeInBits() && "type with size 0");
156 return Ty.getSizeInBits();
157}
158
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000159uint64_t DIExpression::getElement(unsigned Idx) const {
160 unsigned I = Idx + 1;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000161 assert(I < getNumHeaderFields() &&
162 "non-existing complex address element requested");
163 return getHeaderFieldAs<int64_t>(I);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000164}
Adrian Prantlb1416832014-08-01 22:11:58 +0000165
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000166bool DIExpression::isVariablePiece() const {
167 return getNumElements() && getElement(0) == dwarf::DW_OP_piece;
168}
169
170uint64_t DIExpression::getPieceOffset() const {
171 assert(isVariablePiece());
172 return getElement(1);
173}
174
175uint64_t DIExpression::getPieceSize() const {
176 assert(isVariablePiece());
177 return getElement(2);
178}
Adrian Prantlb1416832014-08-01 22:11:58 +0000179
Bill Wendling523bea82013-11-08 08:13:15 +0000180//===----------------------------------------------------------------------===//
181// Predicates
182//===----------------------------------------------------------------------===//
183
Manman Renf8a19672014-07-28 22:24:06 +0000184bool DIDescriptor::isSubroutineType() const {
185 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type;
186}
187
Bill Wendling523bea82013-11-08 08:13:15 +0000188bool DIDescriptor::isBasicType() const {
189 if (!DbgNode)
190 return false;
191 switch (getTag()) {
192 case dwarf::DW_TAG_base_type:
193 case dwarf::DW_TAG_unspecified_type:
194 return true;
195 default:
196 return false;
197 }
198}
199
Bill Wendling523bea82013-11-08 08:13:15 +0000200bool DIDescriptor::isDerivedType() const {
201 if (!DbgNode)
202 return false;
203 switch (getTag()) {
204 case dwarf::DW_TAG_typedef:
205 case dwarf::DW_TAG_pointer_type:
206 case dwarf::DW_TAG_ptr_to_member_type:
207 case dwarf::DW_TAG_reference_type:
208 case dwarf::DW_TAG_rvalue_reference_type:
209 case dwarf::DW_TAG_const_type:
210 case dwarf::DW_TAG_volatile_type:
211 case dwarf::DW_TAG_restrict_type:
212 case dwarf::DW_TAG_member:
213 case dwarf::DW_TAG_inheritance:
214 case dwarf::DW_TAG_friend:
215 return true;
216 default:
217 // CompositeTypes are currently modelled as DerivedTypes.
218 return isCompositeType();
219 }
220}
221
Bill Wendling523bea82013-11-08 08:13:15 +0000222bool DIDescriptor::isCompositeType() const {
223 if (!DbgNode)
224 return false;
225 switch (getTag()) {
226 case dwarf::DW_TAG_array_type:
227 case dwarf::DW_TAG_structure_type:
228 case dwarf::DW_TAG_union_type:
229 case dwarf::DW_TAG_enumeration_type:
230 case dwarf::DW_TAG_subroutine_type:
231 case dwarf::DW_TAG_class_type:
232 return true;
233 default:
234 return false;
235 }
236}
237
Bill Wendling523bea82013-11-08 08:13:15 +0000238bool DIDescriptor::isVariable() const {
239 if (!DbgNode)
240 return false;
241 switch (getTag()) {
242 case dwarf::DW_TAG_auto_variable:
243 case dwarf::DW_TAG_arg_variable:
244 return true;
245 default:
246 return false;
247 }
248}
249
Bill Wendling523bea82013-11-08 08:13:15 +0000250bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000251 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000252}
253
Bill Wendling523bea82013-11-08 08:13:15 +0000254bool DIDescriptor::isSubprogram() const {
255 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
256}
257
Bill Wendling523bea82013-11-08 08:13:15 +0000258bool DIDescriptor::isGlobalVariable() const {
259 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
260 getTag() == dwarf::DW_TAG_constant);
261}
262
Bill Wendling523bea82013-11-08 08:13:15 +0000263bool DIDescriptor::isScope() const {
264 if (!DbgNode)
265 return false;
266 switch (getTag()) {
267 case dwarf::DW_TAG_compile_unit:
268 case dwarf::DW_TAG_lexical_block:
269 case dwarf::DW_TAG_subprogram:
270 case dwarf::DW_TAG_namespace:
271 case dwarf::DW_TAG_file_type:
272 return true;
273 default:
274 break;
275 }
276 return isType();
277}
278
Bill Wendling523bea82013-11-08 08:13:15 +0000279bool DIDescriptor::isTemplateTypeParameter() const {
280 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
281}
282
Bill Wendling523bea82013-11-08 08:13:15 +0000283bool DIDescriptor::isTemplateValueParameter() const {
284 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
285 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
286 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
287}
288
Bill Wendling523bea82013-11-08 08:13:15 +0000289bool DIDescriptor::isCompileUnit() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
291}
292
Bill Wendling523bea82013-11-08 08:13:15 +0000293bool DIDescriptor::isFile() const {
294 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
295}
296
Bill Wendling523bea82013-11-08 08:13:15 +0000297bool DIDescriptor::isNameSpace() const {
298 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
299}
300
Bill Wendling523bea82013-11-08 08:13:15 +0000301bool DIDescriptor::isLexicalBlockFile() const {
302 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000303 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000304}
305
Bill Wendling523bea82013-11-08 08:13:15 +0000306bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000307 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
308 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000309 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000310 DbgNode->getNumOperands() == 3 &&
311 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000312}
313
Bill Wendling523bea82013-11-08 08:13:15 +0000314bool DIDescriptor::isSubrange() const {
315 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
316}
317
Bill Wendling523bea82013-11-08 08:13:15 +0000318bool DIDescriptor::isEnumerator() const {
319 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
320}
321
Bill Wendling523bea82013-11-08 08:13:15 +0000322bool DIDescriptor::isObjCProperty() const {
323 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
324}
325
Bill Wendling523bea82013-11-08 08:13:15 +0000326bool DIDescriptor::isImportedEntity() const {
327 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
328 getTag() == dwarf::DW_TAG_imported_declaration);
329}
330
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000331bool DIDescriptor::isExpression() const {
332 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
333}
334
Bill Wendling523bea82013-11-08 08:13:15 +0000335//===----------------------------------------------------------------------===//
336// Simple Descriptor Constructors and other Methods
337//===----------------------------------------------------------------------===//
338
Frederic Riss36acf0f2014-09-15 07:50:36 +0000339void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000340
341 assert(DbgNode && "Trying to replace an unverified type!");
342
343 // Since we use a TrackingVH for the node, its easy for clients to manufacture
344 // legitimate situations where they want to replaceAllUsesWith() on something
345 // which, due to uniquing, has merged with the source. We shield clients from
346 // this detail by allowing a value to be replaced with replaceAllUsesWith()
347 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000348 const MDNode *DN = D;
349 if (DbgNode == DN) {
350 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
351 for (size_t i = 0; i != Ops.size(); ++i)
352 Ops[i] = DbgNode->getOperand(i);
353 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000354 }
David Blaikied3f094a2014-05-06 03:41:57 +0000355
356 MDNode *Node = const_cast<MDNode *>(DbgNode);
357 const Value *V = cast_or_null<Value>(DN);
358 Node->replaceAllUsesWith(const_cast<Value *>(V));
359 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000360 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000361}
362
Frederic Riss36acf0f2014-09-15 07:50:36 +0000363void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000364
365 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000366 assert(DbgNode != D && "This replacement should always happen");
367 MDNode *Node = const_cast<MDNode *>(DbgNode);
368 const MDNode *DN = D;
369 const Value *V = cast_or_null<Value>(DN);
370 Node->replaceAllUsesWith(const_cast<Value *>(V));
371 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000372}
373
Bill Wendling523bea82013-11-08 08:13:15 +0000374bool DICompileUnit::Verify() const {
375 if (!isCompileUnit())
376 return false;
377
378 // Don't bother verifying the compilation directory or producer string
379 // as those could be empty.
380 if (getFilename().empty())
381 return false;
382
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000383 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000384}
385
Bill Wendling523bea82013-11-08 08:13:15 +0000386bool DIObjCProperty::Verify() const {
387 if (!isObjCProperty())
388 return false;
389
390 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000391 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000392}
393
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000394/// \brief Check if a field at position Elt of a MDNode is a MDNode.
395///
Bill Wendling523bea82013-11-08 08:13:15 +0000396/// We currently allow an empty string and an integer.
397/// But we don't allow a non-empty string in a MDNode field.
398static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
399 // FIXME: This function should return true, if the field is null or the field
400 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
401 Value *Fld = getField(DbgNode, Elt);
402 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
403 return false;
404 return true;
405}
406
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000407/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000408static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
409 Value *Fld = getField(DbgNode, Elt);
410 return !Fld || isa<MDString>(Fld);
411}
412
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000413/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000414static bool isTypeRef(const Metadata *MD) {
415 if (!MD)
416 return true;
417 if (auto *S = dyn_cast<MDString>(MD))
418 return !S->getString().empty();
419 if (auto *N = dyn_cast<MDNode>(MD))
420 return DIType(N).isType();
421 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000422}
423
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000424/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000425static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000426 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000427}
428
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000429/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000430static bool isScopeRef(const Metadata *MD) {
431 if (!MD)
432 return true;
433 if (auto *S = dyn_cast<MDString>(MD))
434 return !S->getString().empty();
435 return isa<MDNode>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000436}
437
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000438/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000439static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000440 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000441}
442
Bill Wendling523bea82013-11-08 08:13:15 +0000443bool DIType::Verify() const {
444 if (!isType())
445 return false;
446 // Make sure Context @ field 2 is MDNode.
447 if (!fieldIsScopeRef(DbgNode, 2))
448 return false;
449
450 // FIXME: Sink this into the various subclass verifies.
451 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000452 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000453 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
454 Tag != dwarf::DW_TAG_ptr_to_member_type &&
455 Tag != dwarf::DW_TAG_reference_type &&
456 Tag != dwarf::DW_TAG_rvalue_reference_type &&
457 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
458 Tag != dwarf::DW_TAG_enumeration_type &&
459 Tag != dwarf::DW_TAG_subroutine_type &&
460 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
461 getFilename().empty())
462 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000463
Bill Wendling523bea82013-11-08 08:13:15 +0000464 // DIType is abstract, it should be a BasicType, a DerivedType or
465 // a CompositeType.
466 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000467 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000468 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000469 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000470 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000471 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000472 else
473 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000474}
475
Bill Wendling523bea82013-11-08 08:13:15 +0000476bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000477 return isBasicType() && DbgNode->getNumOperands() == 3 &&
478 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000479}
480
Bill Wendling523bea82013-11-08 08:13:15 +0000481bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000482 // Make sure DerivedFrom @ field 3 is TypeRef.
483 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000484 return false;
485 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000486 // Make sure ClassType @ field 4 is a TypeRef.
487 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000488 return false;
489
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000490 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
491 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
492 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000493}
494
Bill Wendling523bea82013-11-08 08:13:15 +0000495bool DICompositeType::Verify() const {
496 if (!isCompositeType())
497 return false;
498
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000499 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
500 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000501 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000502 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000503 return false;
504
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000505 // Make sure the type identifier at field 7 is MDString, it can be null.
506 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000507 return false;
508
Adrian Prantl99c7af22013-12-18 21:48:19 +0000509 // A subroutine type can't be both & and &&.
510 if (isLValueReference() && isRValueReference())
511 return false;
512
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000513 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000514}
515
Bill Wendling523bea82013-11-08 08:13:15 +0000516bool DISubprogram::Verify() const {
517 if (!isSubprogram())
518 return false;
519
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000520 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000521 if (!fieldIsScopeRef(DbgNode, 2))
522 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000523 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000524 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000525 // Containing type @ field 4.
526 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000527 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000528
529 // A subprogram can't be both & and &&.
530 if (isLValueReference() && isRValueReference())
531 return false;
532
David Blaikie3dfe4782014-10-14 18:22:52 +0000533 // If a DISubprogram has an llvm::Function*, then scope chains from all
534 // instructions within the function should lead to this DISubprogram.
535 if (auto *F = getFunction()) {
536 LLVMContext &Ctxt = F->getContext();
537 for (auto &BB : *F) {
538 for (auto &I : BB) {
539 DebugLoc DL = I.getDebugLoc();
540 if (DL.isUnknown())
541 continue;
542
543 MDNode *Scope = nullptr;
544 MDNode *IA = nullptr;
545 // walk the inlined-at scopes
546 while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA)
547 DL = DebugLoc::getFromDILocation(IA);
548 DL.getScopeAndInlinedAt(Scope, IA, Ctxt);
549 assert(!IA);
550 while (!DIDescriptor(Scope).isSubprogram()) {
551 DILexicalBlockFile D(Scope);
552 Scope = D.isLexicalBlockFile()
553 ? D.getScope()
554 : DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt);
555 }
556 if (!DISubprogram(Scope).describes(F))
557 return false;
558 }
559 }
560 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000561 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000562}
563
Bill Wendling523bea82013-11-08 08:13:15 +0000564bool DIGlobalVariable::Verify() const {
565 if (!isGlobalVariable())
566 return false;
567
568 if (getDisplayName().empty())
569 return false;
Manman Ren554865d2014-11-18 00:29:08 +0000570 // Make sure context @ field 1 is a ScopeRef.
571 if (!fieldIsScopeRef(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000572 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000573 // Make sure that type @ field 3 is a DITypeRef.
574 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000575 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000576 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
577 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000578 return false;
579
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000580 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000581}
582
Bill Wendling523bea82013-11-08 08:13:15 +0000583bool DIVariable::Verify() const {
584 if (!isVariable())
585 return false;
586
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000587 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000588 if (!fieldIsMDNode(DbgNode, 1))
589 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000590 // Make sure that type @ field 3 is a DITypeRef.
591 if (!fieldIsTypeRef(DbgNode, 3))
592 return false;
593
594 // Check the number of header fields, which is common between complex and
595 // simple variables.
596 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000597 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000598
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000599 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000600 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000601 return true;
602
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000603 // Variable with an inline location.
604 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000605}
606
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000607bool DIExpression::Verify() const {
608 // Empty DIExpressions may be represented as a nullptr.
609 if (!DbgNode)
610 return true;
611
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000612 return isExpression() && DbgNode->getNumOperands() == 1;
Bill Wendling523bea82013-11-08 08:13:15 +0000613}
614
Bill Wendling523bea82013-11-08 08:13:15 +0000615bool DILocation::Verify() const {
616 if (!DbgNode)
617 return false;
618
619 return DbgNode->getNumOperands() == 4;
620}
621
Bill Wendling523bea82013-11-08 08:13:15 +0000622bool DINameSpace::Verify() const {
623 if (!isNameSpace())
624 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000625 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000626}
627
Bill Wendling523bea82013-11-08 08:13:15 +0000628MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
629
Bill Wendling523bea82013-11-08 08:13:15 +0000630bool DIFile::Verify() const {
631 return isFile() && DbgNode->getNumOperands() == 2;
632}
633
Bill Wendling523bea82013-11-08 08:13:15 +0000634bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000635 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
636 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000637}
638
Bill Wendling523bea82013-11-08 08:13:15 +0000639bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000640 return isSubrange() && DbgNode->getNumOperands() == 1 &&
641 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000642}
643
Bill Wendling523bea82013-11-08 08:13:15 +0000644bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000645 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
646 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000647}
648
Bill Wendling523bea82013-11-08 08:13:15 +0000649bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000650 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
651 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000652}
653
Bill Wendling523bea82013-11-08 08:13:15 +0000654bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000655 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
656 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000657}
658
Bill Wendling523bea82013-11-08 08:13:15 +0000659bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000660 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
661 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000662}
663
Bill Wendling523bea82013-11-08 08:13:15 +0000664bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000665 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
666 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000667}
668
Bill Wendling523bea82013-11-08 08:13:15 +0000669MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000670 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000671}
672
673MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000674 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000675}
676
677#ifndef NDEBUG
678static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
679 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
680 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
681 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
682 continue;
683 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
684 bool found = false;
685 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
686 found = E == RHS->getOperand(j);
687 assert(found && "Losing a member during member list replacement");
688 }
689}
690#endif
691
Manman Ren1a125c92014-07-28 19:33:20 +0000692void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Bill Wendling523bea82013-11-08 08:13:15 +0000693 TrackingVH<MDNode> N(*this);
694 if (Elements) {
695#ifndef NDEBUG
696 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000697 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000698 VerifySubsetOf(El, Elements);
699#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000700 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000701 }
702 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000703 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000704 DbgNode = N;
705}
706
Bill Wendling523bea82013-11-08 08:13:15 +0000707DIScopeRef DIScope::getRef() const {
708 if (!isCompositeType())
709 return DIScopeRef(*this);
710 DICompositeType DTy(DbgNode);
711 if (!DTy.getIdentifier())
712 return DIScopeRef(*this);
713 return DIScopeRef(DTy.getIdentifier());
714}
715
Bill Wendling523bea82013-11-08 08:13:15 +0000716void DICompositeType::setContainingType(DICompositeType ContainingType) {
717 TrackingVH<MDNode> N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000718 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000719 DbgNode = N;
720}
721
Bill Wendling523bea82013-11-08 08:13:15 +0000722bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
723 assert(CurFn && "Invalid function");
724 if (!getContext().isSubprogram())
725 return false;
726 // This variable is not inlined function argument if its scope
727 // does not describe current function.
728 return !DISubprogram(getContext()).describes(CurFn);
729}
730
Bill Wendling523bea82013-11-08 08:13:15 +0000731bool DISubprogram::describes(const Function *F) {
732 assert(F && "Invalid function");
733 if (F == getFunction())
734 return true;
735 StringRef Name = getLinkageName();
736 if (Name.empty())
737 Name = getName();
738 if (F->getName() == Name)
739 return true;
740 return false;
741}
742
Bill Wendling523bea82013-11-08 08:13:15 +0000743MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000744 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000745}
746
747DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000748 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000749}
750
751Value *DITemplateValueParameter::getValue() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000752 return getField(DbgNode, 3);
Bill Wendling523bea82013-11-08 08:13:15 +0000753}
754
Bill Wendling523bea82013-11-08 08:13:15 +0000755DIScopeRef DIScope::getContext() const {
756
757 if (isType())
758 return DIType(DbgNode).getContext();
759
760 if (isSubprogram())
761 return DIScopeRef(DISubprogram(DbgNode).getContext());
762
763 if (isLexicalBlock())
764 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
765
766 if (isLexicalBlockFile())
767 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
768
769 if (isNameSpace())
770 return DIScopeRef(DINameSpace(DbgNode).getContext());
771
772 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000773 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000774}
775
Bill Wendling523bea82013-11-08 08:13:15 +0000776StringRef DIScope::getName() const {
777 if (isType())
778 return DIType(DbgNode).getName();
779 if (isSubprogram())
780 return DISubprogram(DbgNode).getName();
781 if (isNameSpace())
782 return DINameSpace(DbgNode).getName();
783 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
784 isCompileUnit()) &&
785 "Unhandled type of scope.");
786 return StringRef();
787}
788
789StringRef DIScope::getFilename() const {
790 if (!DbgNode)
791 return StringRef();
792 return ::getStringField(getNodeField(DbgNode, 1), 0);
793}
794
795StringRef DIScope::getDirectory() const {
796 if (!DbgNode)
797 return StringRef();
798 return ::getStringField(getNodeField(DbgNode, 1), 1);
799}
800
801DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000802 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000803 return DIArray();
804
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000805 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000806}
807
808DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000809 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000810 return DIArray();
811
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000812 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000813}
814
815DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000816 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000817 return DIArray();
818
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000819 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000820}
821
822DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000823 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000824 return DIArray();
825
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000826 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000827}
828
829DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000830 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000831 return DIArray();
832
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000833 return DIArray(getNodeField(DbgNode, 6));
834}
835
836void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
837 assert(Verify() && "Expected compile unit");
838 if (Subprograms == getSubprograms())
839 return;
840
841 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
842}
843
844void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
845 assert(Verify() && "Expected compile unit");
846 if (GlobalVariables == getGlobalVariables())
847 return;
848
849 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000850}
851
Diego Novillof5041ce2014-03-03 20:06:11 +0000852DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000853 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000854 SmallVector<Value *, 10> Elts;
855 assert(Verify());
856 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
857 if (I != 2)
858 Elts.push_back(DbgNode->getOperand(I));
859 else
860 Elts.push_back(NewScope);
861 }
862 MDNode *NewDIL = MDNode::get(Ctx, Elts);
863 return DILocation(NewDIL);
864}
865
Diego Novillof5041ce2014-03-03 20:06:11 +0000866unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
867 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
868 return ++Ctx.pImpl->DiscriminatorTable[Key];
869}
870
Bill Wendling523bea82013-11-08 08:13:15 +0000871DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
872 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000873 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
874 if (!InlinedScope)
875 return cleanseInlinedVariable(DV, VMContext);
876
877 // Insert inlined scope.
878 SmallVector<Value *, 8> Elts;
879 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
880 Elts.push_back(DV->getOperand(I));
881 Elts.push_back(InlinedScope);
882
883 DIVariable Inlined(MDNode::get(VMContext, Elts));
884 assert(Inlined.Verify() && "Expected to create a DIVariable");
885 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000886}
887
Bill Wendling523bea82013-11-08 08:13:15 +0000888DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000889 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
890 if (!DIVariable(DV).getInlinedAt())
891 return DIVariable(DV);
892
893 // Remove inlined scope.
894 SmallVector<Value *, 8> Elts;
895 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
896 Elts.push_back(DV->getOperand(I));
897
898 DIVariable Cleansed(MDNode::get(VMContext, Elts));
899 assert(Cleansed.Verify() && "Expected to create a DIVariable");
900 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000901}
902
Bill Wendling523bea82013-11-08 08:13:15 +0000903DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
904 DIDescriptor D(Scope);
905 if (D.isSubprogram())
906 return DISubprogram(Scope);
907
908 if (D.isLexicalBlockFile())
909 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
910
911 if (D.isLexicalBlock())
912 return getDISubprogram(DILexicalBlock(Scope).getContext());
913
914 return DISubprogram();
915}
916
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000917DISubprogram llvm::getDISubprogram(const Function *F) {
918 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000919 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000920 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
921 return !Inst.getDebugLoc().isUnknown();
922 });
923 if (Inst == BB.end())
924 continue;
925 DebugLoc DLoc = Inst->getDebugLoc();
926 const MDNode *Scope = DLoc.getScopeNode(F->getParent()->getContext());
927 DISubprogram Subprogram = getDISubprogram(Scope);
928 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000929 }
930
931 return DISubprogram();
932}
933
Bill Wendling523bea82013-11-08 08:13:15 +0000934DICompositeType llvm::getDICompositeType(DIType T) {
935 if (T.isCompositeType())
936 return DICompositeType(T);
937
938 if (T.isDerivedType()) {
939 // This function is currently used by dragonegg and dragonegg does
940 // not generate identifier for types, so using an empty map to resolve
941 // DerivedFrom should be fine.
942 DITypeIdentifierMap EmptyMap;
943 return getDICompositeType(
944 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
945 }
946
947 return DICompositeType();
948}
949
Bill Wendling523bea82013-11-08 08:13:15 +0000950DITypeIdentifierMap
951llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
952 DITypeIdentifierMap Map;
953 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000954 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000955 DIArray Retain = CU.getRetainedTypes();
956 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
957 if (!Retain.getElement(Ti).isCompositeType())
958 continue;
959 DICompositeType Ty(Retain.getElement(Ti));
960 if (MDString *TypeId = Ty.getIdentifier()) {
961 // Definition has priority over declaration.
962 // Try to insert (TypeId, Ty) to Map.
963 std::pair<DITypeIdentifierMap::iterator, bool> P =
964 Map.insert(std::make_pair(TypeId, Ty));
965 // If TypeId already exists in Map and this is a definition, replace
966 // whatever we had (declaration or definition) with the definition.
967 if (!P.second && !Ty.isForwardDecl())
968 P.first->second = Ty;
969 }
970 }
971 }
972 return Map;
973}
974
975//===----------------------------------------------------------------------===//
976// DebugInfoFinder implementations.
977//===----------------------------------------------------------------------===//
978
979void DebugInfoFinder::reset() {
980 CUs.clear();
981 SPs.clear();
982 GVs.clear();
983 TYs.clear();
984 Scopes.clear();
985 NodesSeen.clear();
986 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000987 TypeMapInitialized = false;
988}
989
Manman Renb46e5502013-11-17 19:35:03 +0000990void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000991 if (!TypeMapInitialized)
992 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
993 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
994 TypeMapInitialized = true;
995 }
Bill Wendling523bea82013-11-08 08:13:15 +0000996}
997
Bill Wendling523bea82013-11-08 08:13:15 +0000998void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000999 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001000 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001001 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001002 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001003 addCompileUnit(CU);
1004 DIArray GVs = CU.getGlobalVariables();
1005 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1006 DIGlobalVariable DIG(GVs.getElement(i));
1007 if (addGlobalVariable(DIG)) {
Manman Ren554865d2014-11-18 00:29:08 +00001008 processScope(DIG.getContext().resolve(TypeIdentifierMap));
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001009 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001010 }
1011 }
1012 DIArray SPs = CU.getSubprograms();
1013 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1014 processSubprogram(DISubprogram(SPs.getElement(i)));
1015 DIArray EnumTypes = CU.getEnumTypes();
1016 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1017 processType(DIType(EnumTypes.getElement(i)));
1018 DIArray RetainedTypes = CU.getRetainedTypes();
1019 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1020 processType(DIType(RetainedTypes.getElement(i)));
1021 DIArray Imports = CU.getImportedEntities();
1022 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1023 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001024 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001025 if (Entity.isType())
1026 processType(DIType(Entity));
1027 else if (Entity.isSubprogram())
1028 processSubprogram(DISubprogram(Entity));
1029 else if (Entity.isNameSpace())
1030 processScope(DINameSpace(Entity).getContext());
1031 }
1032 }
1033 }
1034}
1035
Manman Ren2085ccc2013-11-17 18:42:37 +00001036void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001037 if (!Loc)
1038 return;
Manman Renb46e5502013-11-17 19:35:03 +00001039 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001040 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001041 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001042}
1043
Bill Wendling523bea82013-11-08 08:13:15 +00001044void DebugInfoFinder::processType(DIType DT) {
1045 if (!addType(DT))
1046 return;
1047 processScope(DT.getContext().resolve(TypeIdentifierMap));
1048 if (DT.isCompositeType()) {
1049 DICompositeType DCT(DT);
1050 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001051 if (DT.isSubroutineType()) {
1052 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1053 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1054 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1055 return;
1056 }
Manman Renab8ffba2014-07-28 19:14:13 +00001057 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001058 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1059 DIDescriptor D = DA.getElement(i);
1060 if (D.isType())
1061 processType(DIType(D));
1062 else if (D.isSubprogram())
1063 processSubprogram(DISubprogram(D));
1064 }
1065 } else if (DT.isDerivedType()) {
1066 DIDerivedType DDT(DT);
1067 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1068 }
1069}
1070
1071void DebugInfoFinder::processScope(DIScope Scope) {
1072 if (Scope.isType()) {
1073 DIType Ty(Scope);
1074 processType(Ty);
1075 return;
1076 }
1077 if (Scope.isCompileUnit()) {
1078 addCompileUnit(DICompileUnit(Scope));
1079 return;
1080 }
1081 if (Scope.isSubprogram()) {
1082 processSubprogram(DISubprogram(Scope));
1083 return;
1084 }
1085 if (!addScope(Scope))
1086 return;
1087 if (Scope.isLexicalBlock()) {
1088 DILexicalBlock LB(Scope);
1089 processScope(LB.getContext());
1090 } else if (Scope.isLexicalBlockFile()) {
1091 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1092 processScope(LBF.getScope());
1093 } else if (Scope.isNameSpace()) {
1094 DINameSpace NS(Scope);
1095 processScope(NS.getContext());
1096 }
1097}
1098
Bill Wendling523bea82013-11-08 08:13:15 +00001099void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1100 if (!addSubprogram(SP))
1101 return;
1102 processScope(SP.getContext().resolve(TypeIdentifierMap));
1103 processType(SP.getType());
1104 DIArray TParams = SP.getTemplateParams();
1105 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1106 DIDescriptor Element = TParams.getElement(I);
1107 if (Element.isTemplateTypeParameter()) {
1108 DITemplateTypeParameter TType(Element);
1109 processScope(TType.getContext().resolve(TypeIdentifierMap));
1110 processType(TType.getType().resolve(TypeIdentifierMap));
1111 } else if (Element.isTemplateValueParameter()) {
1112 DITemplateValueParameter TVal(Element);
1113 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1114 processType(TVal.getType().resolve(TypeIdentifierMap));
1115 }
1116 }
1117}
1118
Manman Ren2085ccc2013-11-17 18:42:37 +00001119void DebugInfoFinder::processDeclare(const Module &M,
1120 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001121 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1122 if (!N)
1123 return;
Manman Renb46e5502013-11-17 19:35:03 +00001124 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001125
1126 DIDescriptor DV(N);
1127 if (!DV.isVariable())
1128 return;
1129
1130 if (!NodesSeen.insert(DV))
1131 return;
1132 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001133 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001134}
1135
Manman Ren2085ccc2013-11-17 18:42:37 +00001136void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001137 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1138 if (!N)
1139 return;
Manman Renb46e5502013-11-17 19:35:03 +00001140 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001141
1142 DIDescriptor DV(N);
1143 if (!DV.isVariable())
1144 return;
1145
1146 if (!NodesSeen.insert(DV))
1147 return;
1148 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001149 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001150}
1151
Bill Wendling523bea82013-11-08 08:13:15 +00001152bool DebugInfoFinder::addType(DIType DT) {
1153 if (!DT)
1154 return false;
1155
1156 if (!NodesSeen.insert(DT))
1157 return false;
1158
1159 TYs.push_back(DT);
1160 return true;
1161}
1162
Bill Wendling523bea82013-11-08 08:13:15 +00001163bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1164 if (!CU)
1165 return false;
1166 if (!NodesSeen.insert(CU))
1167 return false;
1168
1169 CUs.push_back(CU);
1170 return true;
1171}
1172
Bill Wendling523bea82013-11-08 08:13:15 +00001173bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1174 if (!DIG)
1175 return false;
1176
1177 if (!NodesSeen.insert(DIG))
1178 return false;
1179
1180 GVs.push_back(DIG);
1181 return true;
1182}
1183
Bill Wendling523bea82013-11-08 08:13:15 +00001184bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1185 if (!SP)
1186 return false;
1187
1188 if (!NodesSeen.insert(SP))
1189 return false;
1190
1191 SPs.push_back(SP);
1192 return true;
1193}
1194
1195bool DebugInfoFinder::addScope(DIScope Scope) {
1196 if (!Scope)
1197 return false;
1198 // FIXME: Ocaml binding generates a scope with no content, we treat it
1199 // as null for now.
1200 if (Scope->getNumOperands() == 0)
1201 return false;
1202 if (!NodesSeen.insert(Scope))
1203 return false;
1204 Scopes.push_back(Scope);
1205 return true;
1206}
1207
1208//===----------------------------------------------------------------------===//
1209// DIDescriptor: dump routines for all descriptors.
1210//===----------------------------------------------------------------------===//
1211
Bill Wendling523bea82013-11-08 08:13:15 +00001212void DIDescriptor::dump() const {
1213 print(dbgs());
1214 dbgs() << '\n';
1215}
1216
Bill Wendling523bea82013-11-08 08:13:15 +00001217void DIDescriptor::print(raw_ostream &OS) const {
1218 if (!DbgNode)
1219 return;
1220
1221 if (const char *Tag = dwarf::TagString(getTag()))
1222 OS << "[ " << Tag << " ]";
1223
1224 if (this->isSubrange()) {
1225 DISubrange(DbgNode).printInternal(OS);
1226 } else if (this->isCompileUnit()) {
1227 DICompileUnit(DbgNode).printInternal(OS);
1228 } else if (this->isFile()) {
1229 DIFile(DbgNode).printInternal(OS);
1230 } else if (this->isEnumerator()) {
1231 DIEnumerator(DbgNode).printInternal(OS);
1232 } else if (this->isBasicType()) {
1233 DIType(DbgNode).printInternal(OS);
1234 } else if (this->isDerivedType()) {
1235 DIDerivedType(DbgNode).printInternal(OS);
1236 } else if (this->isCompositeType()) {
1237 DICompositeType(DbgNode).printInternal(OS);
1238 } else if (this->isSubprogram()) {
1239 DISubprogram(DbgNode).printInternal(OS);
1240 } else if (this->isGlobalVariable()) {
1241 DIGlobalVariable(DbgNode).printInternal(OS);
1242 } else if (this->isVariable()) {
1243 DIVariable(DbgNode).printInternal(OS);
1244 } else if (this->isObjCProperty()) {
1245 DIObjCProperty(DbgNode).printInternal(OS);
1246 } else if (this->isNameSpace()) {
1247 DINameSpace(DbgNode).printInternal(OS);
1248 } else if (this->isScope()) {
1249 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001250 } else if (this->isExpression()) {
1251 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001252 }
1253}
1254
1255void DISubrange::printInternal(raw_ostream &OS) const {
1256 int64_t Count = getCount();
1257 if (Count != -1)
1258 OS << " [" << getLo() << ", " << Count - 1 << ']';
1259 else
1260 OS << " [unbounded]";
1261}
1262
1263void DIScope::printInternal(raw_ostream &OS) const {
1264 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1265}
1266
1267void DICompileUnit::printInternal(raw_ostream &OS) const {
1268 DIScope::printInternal(OS);
1269 OS << " [";
1270 unsigned Lang = getLanguage();
1271 if (const char *LangStr = dwarf::LanguageString(Lang))
1272 OS << LangStr;
1273 else
1274 (OS << "lang 0x").write_hex(Lang);
1275 OS << ']';
1276}
1277
1278void DIEnumerator::printInternal(raw_ostream &OS) const {
1279 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1280}
1281
1282void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001283 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001284 return;
1285
1286 StringRef Res = getName();
1287 if (!Res.empty())
1288 OS << " [" << Res << "]";
1289
1290 // TODO: Print context?
1291
1292 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1293 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1294 if (isBasicType())
1295 if (const char *Enc =
1296 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1297 OS << ", enc " << Enc;
1298 OS << "]";
1299
1300 if (isPrivate())
1301 OS << " [private]";
1302 else if (isProtected())
1303 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001304 else if (isPublic())
1305 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001306
1307 if (isArtificial())
1308 OS << " [artificial]";
1309
1310 if (isForwardDecl())
1311 OS << " [decl]";
1312 else if (getTag() == dwarf::DW_TAG_structure_type ||
1313 getTag() == dwarf::DW_TAG_union_type ||
1314 getTag() == dwarf::DW_TAG_enumeration_type ||
1315 getTag() == dwarf::DW_TAG_class_type)
1316 OS << " [def]";
1317 if (isVector())
1318 OS << " [vector]";
1319 if (isStaticMember())
1320 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001321
1322 if (isLValueReference())
1323 OS << " [reference]";
1324
1325 if (isRValueReference())
1326 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001327}
1328
1329void DIDerivedType::printInternal(raw_ostream &OS) const {
1330 DIType::printInternal(OS);
1331 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1332}
1333
1334void DICompositeType::printInternal(raw_ostream &OS) const {
1335 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001336 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001337 OS << " [" << A.getNumElements() << " elements]";
1338}
1339
1340void DINameSpace::printInternal(raw_ostream &OS) const {
1341 StringRef Name = getName();
1342 if (!Name.empty())
1343 OS << " [" << Name << ']';
1344
1345 OS << " [line " << getLineNumber() << ']';
1346}
1347
1348void DISubprogram::printInternal(raw_ostream &OS) const {
1349 // TODO : Print context
1350 OS << " [line " << getLineNumber() << ']';
1351
1352 if (isLocalToUnit())
1353 OS << " [local]";
1354
1355 if (isDefinition())
1356 OS << " [def]";
1357
1358 if (getScopeLineNumber() != getLineNumber())
1359 OS << " [scope " << getScopeLineNumber() << "]";
1360
1361 if (isPrivate())
1362 OS << " [private]";
1363 else if (isProtected())
1364 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001365 else if (isPublic())
1366 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001367
Adrian Prantl99c7af22013-12-18 21:48:19 +00001368 if (isLValueReference())
1369 OS << " [reference]";
1370
1371 if (isRValueReference())
1372 OS << " [rvalue reference]";
1373
Bill Wendling523bea82013-11-08 08:13:15 +00001374 StringRef Res = getName();
1375 if (!Res.empty())
1376 OS << " [" << Res << ']';
1377}
1378
1379void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1380 StringRef Res = getName();
1381 if (!Res.empty())
1382 OS << " [" << Res << ']';
1383
1384 OS << " [line " << getLineNumber() << ']';
1385
1386 // TODO : Print context
1387
1388 if (isLocalToUnit())
1389 OS << " [local]";
1390
1391 if (isDefinition())
1392 OS << " [def]";
1393}
1394
1395void DIVariable::printInternal(raw_ostream &OS) const {
1396 StringRef Res = getName();
1397 if (!Res.empty())
1398 OS << " [" << Res << ']';
1399
1400 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001401}
Adrian Prantlb1416832014-08-01 22:11:58 +00001402
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001403void DIExpression::printInternal(raw_ostream &OS) const {
1404 for (unsigned I = 0; I < getNumElements(); ++I) {
1405 uint64_t OpCode = getElement(I);
1406 OS << " [" << OperationEncodingString(OpCode);
1407 switch (OpCode) {
1408 case DW_OP_plus: {
1409 OS << " " << getElement(++I);
1410 break;
1411 }
1412 case DW_OP_piece: {
1413 unsigned Offset = getElement(++I);
1414 unsigned Size = getElement(++I);
Adrian Prantl38666f12014-10-02 16:42:15 +00001415 OS << " offset=" << Offset << ", size=" << Size;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001416 break;
1417 }
1418 default:
Adrian Prantl75a0dac2014-10-02 16:42:13 +00001419 // Else bail out early. This may be a line table entry.
1420 OS << "Unknown]";
1421 return;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001422 }
1423 OS << "]";
1424 }
Bill Wendling523bea82013-11-08 08:13:15 +00001425}
1426
1427void DIObjCProperty::printInternal(raw_ostream &OS) const {
1428 StringRef Name = getObjCPropertyName();
1429 if (!Name.empty())
1430 OS << " [" << Name << ']';
1431
1432 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1433 << ']';
1434}
1435
1436static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1437 const LLVMContext &Ctx) {
1438 if (!DL.isUnknown()) { // Print source line info.
1439 DIScope Scope(DL.getScope(Ctx));
1440 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1441 // Omit the directory, because it's likely to be long and uninteresting.
1442 CommentOS << Scope.getFilename();
1443 CommentOS << ':' << DL.getLine();
1444 if (DL.getCol() != 0)
1445 CommentOS << ':' << DL.getCol();
1446 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1447 if (!InlinedAtDL.isUnknown()) {
1448 CommentOS << " @[ ";
1449 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1450 CommentOS << " ]";
1451 }
1452 }
1453}
1454
1455void DIVariable::printExtendedName(raw_ostream &OS) const {
1456 const LLVMContext &Ctx = DbgNode->getContext();
1457 StringRef Res = getName();
1458 if (!Res.empty())
1459 OS << Res << "," << getLineNumber();
1460 if (MDNode *InlinedAt = getInlinedAt()) {
1461 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1462 if (!InlinedAtDL.isUnknown()) {
1463 OS << " @[";
1464 printDebugLoc(InlinedAtDL, OS, Ctx);
1465 OS << "]";
1466 }
1467 }
1468}
1469
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001470template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001471 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1472}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001473template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001474 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1475}
1476
Bill Wendling523bea82013-11-08 08:13:15 +00001477template <>
1478DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001479 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001480}
Bill Wendling523bea82013-11-08 08:13:15 +00001481template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001482 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001483}
Manman Rencb14bbc2013-11-22 22:06:31 +00001484
Manman Rencb14bbc2013-11-22 22:06:31 +00001485bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001486 bool Changed = false;
1487
1488 // Remove all of the calls to the debugger intrinsics, and remove them from
1489 // the module.
1490 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1491 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001492 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001493 CI->eraseFromParent();
1494 }
1495 Declare->eraseFromParent();
1496 Changed = true;
1497 }
1498
1499 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1500 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001501 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001502 CI->eraseFromParent();
1503 }
1504 DbgVal->eraseFromParent();
1505 Changed = true;
1506 }
1507
1508 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1509 NME = M.named_metadata_end(); NMI != NME;) {
1510 NamedMDNode *NMD = NMI;
1511 ++NMI;
1512 if (NMD->getName().startswith("llvm.dbg.")) {
1513 NMD->eraseFromParent();
1514 Changed = true;
1515 }
1516 }
1517
1518 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1519 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1520 ++FI)
1521 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1522 ++BI) {
1523 if (!BI->getDebugLoc().isUnknown()) {
1524 Changed = true;
1525 BI->setDebugLoc(DebugLoc());
1526 }
1527 }
1528
1529 return Changed;
1530}
Manman Ren8b4306c2013-12-02 21:29:56 +00001531
Manman Renbd4daf82013-12-03 00:12:14 +00001532unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Manman Ren8b4306c2013-12-02 21:29:56 +00001533 Value *Val = M.getModuleFlag("Debug Info Version");
1534 if (!Val)
1535 return 0;
1536 return cast<ConstantInt>(Val)->getZExtValue();
1537}
David Blaikie6876b3b2014-07-01 20:05:26 +00001538
David Blaikiea8c35092014-07-02 18:30:05 +00001539llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1540llvm::makeSubprogramMap(const Module &M) {
1541 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001542
1543 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1544 if (!CU_Nodes)
1545 return R;
1546
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001547 for (MDNode *N : CU_Nodes->operands()) {
1548 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001549 DIArray SPs = CUNode.getSubprograms();
1550 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1551 DISubprogram SP(SPs.getElement(i));
1552 if (Function *F = SP.getFunction())
1553 R.insert(std::make_pair(F, SP));
1554 }
1555 }
1556 return R;
1557}