blob: df059c4d7fc58eb0d83d2c488c58aa885099471d [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
15#include "llvm/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"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Intrinsics.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/ValueHandle.h"
30#include "llvm/Support/raw_ostream.h"
31using namespace llvm;
32using namespace llvm::dwarf;
33
34//===----------------------------------------------------------------------===//
35// DIDescriptor
36//===----------------------------------------------------------------------===//
37
38bool DIDescriptor::Verify() const {
39 return DbgNode &&
40 (DIDerivedType(DbgNode).Verify() ||
41 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
42 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
43 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
44 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
45 DILexicalBlock(DbgNode).Verify() ||
46 DILexicalBlockFile(DbgNode).Verify() ||
47 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
48 DIObjCProperty(DbgNode).Verify() ||
Adrian Prantlb363c302014-02-25 23:42:11 +000049 DIUnspecifiedParameter(DbgNode).Verify() ||
Bill Wendling523bea82013-11-08 08:13:15 +000050 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
52 DIImportedEntity(DbgNode).Verify());
53}
54
55static Value *getField(const MDNode *DbgNode, unsigned Elt) {
56 if (DbgNode == 0 || Elt >= DbgNode->getNumOperands())
57 return 0;
58 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 {
76 if (DbgNode == 0)
77 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 {
88 if (DbgNode == 0)
89 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 {
105 if (DbgNode == 0)
106 return 0;
107
108 if (Elt < DbgNode->getNumOperands())
109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
110 return 0;
111}
112
113Constant *DIDescriptor::getConstantField(unsigned Elt) const {
114 if (DbgNode == 0)
115 return 0;
116
117 if (Elt < DbgNode->getNumOperands())
118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
119 return 0;
120}
121
122Function *DIDescriptor::getFunctionField(unsigned Elt) const {
123 if (DbgNode == 0)
124 return 0;
125
126 if (Elt < DbgNode->getNumOperands())
127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
128 return 0;
129}
130
131void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
132 if (DbgNode == 0)
133 return;
134
135 if (Elt < DbgNode->getNumOperands()) {
136 MDNode *Node = const_cast<MDNode *>(DbgNode);
137 Node->replaceOperandWith(Elt, F);
138 }
139}
140
141unsigned DIVariable::getNumAddrElements() const {
142 return DbgNode->getNumOperands() - 8;
143}
144
145/// getInlinedAt - If this variable is inlined then return inline location.
146MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
147
148//===----------------------------------------------------------------------===//
149// Predicates
150//===----------------------------------------------------------------------===//
151
152/// isBasicType - Return true if the specified tag is legal for
153/// DIBasicType.
154bool DIDescriptor::isBasicType() const {
155 if (!DbgNode)
156 return false;
157 switch (getTag()) {
158 case dwarf::DW_TAG_base_type:
159 case dwarf::DW_TAG_unspecified_type:
160 return true;
161 default:
162 return false;
163 }
164}
165
166/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
167bool DIDescriptor::isDerivedType() const {
168 if (!DbgNode)
169 return false;
170 switch (getTag()) {
171 case dwarf::DW_TAG_typedef:
172 case dwarf::DW_TAG_pointer_type:
173 case dwarf::DW_TAG_ptr_to_member_type:
174 case dwarf::DW_TAG_reference_type:
175 case dwarf::DW_TAG_rvalue_reference_type:
176 case dwarf::DW_TAG_const_type:
177 case dwarf::DW_TAG_volatile_type:
178 case dwarf::DW_TAG_restrict_type:
179 case dwarf::DW_TAG_member:
180 case dwarf::DW_TAG_inheritance:
181 case dwarf::DW_TAG_friend:
182 return true;
183 default:
184 // CompositeTypes are currently modelled as DerivedTypes.
185 return isCompositeType();
186 }
187}
188
189/// isCompositeType - Return true if the specified tag is legal for
190/// DICompositeType.
191bool DIDescriptor::isCompositeType() const {
192 if (!DbgNode)
193 return false;
194 switch (getTag()) {
195 case dwarf::DW_TAG_array_type:
196 case dwarf::DW_TAG_structure_type:
197 case dwarf::DW_TAG_union_type:
198 case dwarf::DW_TAG_enumeration_type:
199 case dwarf::DW_TAG_subroutine_type:
200 case dwarf::DW_TAG_class_type:
201 return true;
202 default:
203 return false;
204 }
205}
206
207/// isVariable - Return true if the specified tag is legal for DIVariable.
208bool DIDescriptor::isVariable() const {
209 if (!DbgNode)
210 return false;
211 switch (getTag()) {
212 case dwarf::DW_TAG_auto_variable:
213 case dwarf::DW_TAG_arg_variable:
214 return true;
215 default:
216 return false;
217 }
218}
219
220/// isType - Return true if the specified tag is legal for DIType.
221bool DIDescriptor::isType() const {
222 return isBasicType() || isCompositeType() || isDerivedType();
223}
224
225/// isSubprogram - Return true if the specified tag is legal for
226/// DISubprogram.
227bool DIDescriptor::isSubprogram() const {
228 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
229}
230
231/// isGlobalVariable - Return true if the specified tag is legal for
232/// DIGlobalVariable.
233bool DIDescriptor::isGlobalVariable() const {
234 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
235 getTag() == dwarf::DW_TAG_constant);
236}
237
238/// isUnspecifiedParmeter - Return true if the specified tag is
239/// DW_TAG_unspecified_parameters.
240bool DIDescriptor::isUnspecifiedParameter() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
242}
243
244/// isScope - Return true if the specified tag is one of the scope
245/// related tag.
246bool DIDescriptor::isScope() const {
247 if (!DbgNode)
248 return false;
249 switch (getTag()) {
250 case dwarf::DW_TAG_compile_unit:
251 case dwarf::DW_TAG_lexical_block:
252 case dwarf::DW_TAG_subprogram:
253 case dwarf::DW_TAG_namespace:
254 case dwarf::DW_TAG_file_type:
255 return true;
256 default:
257 break;
258 }
259 return isType();
260}
261
262/// isTemplateTypeParameter - Return true if the specified tag is
263/// DW_TAG_template_type_parameter.
264bool DIDescriptor::isTemplateTypeParameter() const {
265 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
266}
267
268/// isTemplateValueParameter - Return true if the specified tag is
269/// DW_TAG_template_value_parameter.
270bool DIDescriptor::isTemplateValueParameter() const {
271 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
272 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
273 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
274}
275
276/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
277bool DIDescriptor::isCompileUnit() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
279}
280
281/// isFile - Return true if the specified tag is DW_TAG_file_type.
282bool DIDescriptor::isFile() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
284}
285
286/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
287bool DIDescriptor::isNameSpace() const {
288 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
289}
290
291/// isLexicalBlockFile - Return true if the specified descriptor is a
292/// lexical block with an extra file.
293bool DIDescriptor::isLexicalBlockFile() const {
294 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
295 (DbgNode->getNumOperands() == 3);
296}
297
298/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
299bool DIDescriptor::isLexicalBlock() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
301 (DbgNode->getNumOperands() > 3);
302}
303
304/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
305bool DIDescriptor::isSubrange() const {
306 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
307}
308
309/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
310bool DIDescriptor::isEnumerator() const {
311 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
312}
313
314/// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
315bool DIDescriptor::isObjCProperty() const {
316 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
317}
318
319/// \brief Return true if the specified tag is DW_TAG_imported_module or
320/// DW_TAG_imported_declaration.
321bool DIDescriptor::isImportedEntity() const {
322 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
323 getTag() == dwarf::DW_TAG_imported_declaration);
324}
325
326//===----------------------------------------------------------------------===//
327// Simple Descriptor Constructors and other Methods
328//===----------------------------------------------------------------------===//
329
330unsigned DIArray::getNumElements() const {
331 if (!DbgNode)
332 return 0;
333 return DbgNode->getNumOperands();
334}
335
336/// replaceAllUsesWith - Replace all uses of the MDNode used by this
337/// type with the one in the passed descriptor.
338void DIType::replaceAllUsesWith(DIDescriptor &D) {
339
340 assert(DbgNode && "Trying to replace an unverified type!");
341
342 // Since we use a TrackingVH for the node, its easy for clients to manufacture
343 // legitimate situations where they want to replaceAllUsesWith() on something
344 // which, due to uniquing, has merged with the source. We shield clients from
345 // this detail by allowing a value to be replaced with replaceAllUsesWith()
346 // itself.
347 if (DbgNode != D) {
348 MDNode *Node = const_cast<MDNode *>(DbgNode);
349 const MDNode *DN = D;
350 const Value *V = cast_or_null<Value>(DN);
351 Node->replaceAllUsesWith(const_cast<Value *>(V));
352 MDNode::deleteTemporary(Node);
353 }
354}
355
356/// replaceAllUsesWith - Replace all uses of the MDNode used by this
357/// type with the one in D.
358void DIType::replaceAllUsesWith(MDNode *D) {
359
360 assert(DbgNode && "Trying to replace an unverified type!");
361
362 // Since we use a TrackingVH for the node, its easy for clients to manufacture
363 // legitimate situations where they want to replaceAllUsesWith() on something
364 // which, due to uniquing, has merged with the source. We shield clients from
365 // this detail by allowing a value to be replaced with replaceAllUsesWith()
366 // itself.
367 if (DbgNode != D) {
368 MDNode *Node = const_cast<MDNode *>(DbgNode);
369 const MDNode *DN = D;
370 const Value *V = cast_or_null<Value>(DN);
371 Node->replaceAllUsesWith(const_cast<Value *>(V));
372 MDNode::deleteTemporary(Node);
373 }
374}
375
376/// Verify - Verify that a compile unit is well formed.
377bool DICompileUnit::Verify() const {
378 if (!isCompileUnit())
379 return false;
380
381 // Don't bother verifying the compilation directory or producer string
382 // as those could be empty.
383 if (getFilename().empty())
384 return false;
385
Eric Christopher75d49db2014-02-27 01:24:56 +0000386 return DbgNode->getNumOperands() == 14;
Bill Wendling523bea82013-11-08 08:13:15 +0000387}
388
389/// Verify - Verify that an ObjC property is well formed.
390bool DIObjCProperty::Verify() const {
391 if (!isObjCProperty())
392 return false;
393
394 // Don't worry about the rest of the strings for now.
395 return DbgNode->getNumOperands() == 8;
396}
397
398/// Check if a field at position Elt of a MDNode is a MDNode.
399/// We currently allow an empty string and an integer.
400/// But we don't allow a non-empty string in a MDNode field.
401static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
402 // FIXME: This function should return true, if the field is null or the field
403 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
404 Value *Fld = getField(DbgNode, Elt);
405 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
406 return false;
407 return true;
408}
409
410/// Check if a field at position Elt of a MDNode is a MDString.
411static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
412 Value *Fld = getField(DbgNode, Elt);
413 return !Fld || isa<MDString>(Fld);
414}
415
416/// Check if a value can be a reference to a type.
417static bool isTypeRef(const Value *Val) {
418 return !Val ||
419 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
420 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
421}
422
423/// Check if a field at position Elt of a MDNode can be a reference to a type.
424static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
425 Value *Fld = getField(DbgNode, Elt);
426 return isTypeRef(Fld);
427}
428
429/// Check if a value can be a ScopeRef.
430static bool isScopeRef(const Value *Val) {
431 return !Val ||
432 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
433 (isa<MDNode>(Val) && DIScope(cast<MDNode>(Val)).isScope());
434}
435
436/// Check if a field at position Elt of a MDNode can be a ScopeRef.
437static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
438 Value *Fld = getField(DbgNode, Elt);
439 return isScopeRef(Fld);
440}
441
442/// Verify - Verify that a type descriptor is well formed.
443bool 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();
452 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
453 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;
463 // DIType is abstract, it should be a BasicType, a DerivedType or
464 // a CompositeType.
465 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000466 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000467 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000468 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000469 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000470 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000471 else
472 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000473}
474
475/// Verify - Verify that a basic type descriptor is well formed.
476bool DIBasicType::Verify() const {
477 return isBasicType() && DbgNode->getNumOperands() == 10;
478}
479
480/// Verify - Verify that a derived type descriptor is well formed.
481bool DIDerivedType::Verify() const {
482 // Make sure DerivedFrom @ field 9 is TypeRef.
483 if (!fieldIsTypeRef(DbgNode, 9))
484 return false;
485 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
486 // Make sure ClassType @ field 10 is a TypeRef.
487 if (!fieldIsTypeRef(DbgNode, 10))
488 return false;
489
490 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
491 DbgNode->getNumOperands() <= 14;
492}
493
494/// Verify - Verify that a composite type descriptor is well formed.
495bool DICompositeType::Verify() const {
496 if (!isCompositeType())
497 return false;
498
499 // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef.
500 if (!fieldIsTypeRef(DbgNode, 9))
501 return false;
502 if (!fieldIsTypeRef(DbgNode, 12))
503 return false;
504
505 // Make sure the type identifier at field 14 is MDString, it can be null.
506 if (!fieldIsMDString(DbgNode, 14))
507 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
Bill Wendling523bea82013-11-08 08:13:15 +0000513 return DbgNode->getNumOperands() == 15;
514}
515
516/// Verify - Verify that a subprogram descriptor is well formed.
517bool DISubprogram::Verify() const {
518 if (!isSubprogram())
519 return false;
520
521 // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode.
522 if (!fieldIsScopeRef(DbgNode, 2))
523 return false;
524 if (!fieldIsMDNode(DbgNode, 7))
525 return false;
526 // Containing type @ field 12.
527 if (!fieldIsTypeRef(DbgNode, 12))
528 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000529
530 // A subprogram can't be both & and &&.
531 if (isLValueReference() && isRValueReference())
532 return false;
533
Bill Wendling523bea82013-11-08 08:13:15 +0000534 return DbgNode->getNumOperands() == 20;
535}
536
537/// Verify - Verify that a global variable descriptor is well formed.
538bool DIGlobalVariable::Verify() const {
539 if (!isGlobalVariable())
540 return false;
541
542 if (getDisplayName().empty())
543 return false;
544 // Make sure context @ field 2 and type @ field 8 are MDNodes.
545 if (!fieldIsMDNode(DbgNode, 2))
546 return false;
547 if (!fieldIsMDNode(DbgNode, 8))
548 return false;
549 // Make sure StaticDataMemberDeclaration @ field 12 is MDNode.
550 if (!fieldIsMDNode(DbgNode, 12))
551 return false;
552
553 return DbgNode->getNumOperands() == 13;
554}
555
556/// Verify - Verify that a variable descriptor is well formed.
557bool DIVariable::Verify() const {
558 if (!isVariable())
559 return false;
560
561 // Make sure context @ field 1 and type @ field 5 are MDNodes.
562 if (!fieldIsMDNode(DbgNode, 1))
563 return false;
564 if (!fieldIsMDNode(DbgNode, 5))
565 return false;
566 return DbgNode->getNumOperands() >= 8;
567}
568
569/// Verify - Verify that a location descriptor is well formed.
570bool DILocation::Verify() const {
571 if (!DbgNode)
572 return false;
573
574 return DbgNode->getNumOperands() == 4;
575}
576
577/// Verify - Verify that a namespace descriptor is well formed.
578bool DINameSpace::Verify() const {
579 if (!isNameSpace())
580 return false;
581 return DbgNode->getNumOperands() == 5;
582}
583
584/// \brief Retrieve the MDNode for the directory/file pair.
585MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
586
587/// \brief Verify that the file descriptor is well formed.
588bool DIFile::Verify() const {
589 return isFile() && DbgNode->getNumOperands() == 2;
590}
591
592/// \brief Verify that the enumerator descriptor is well formed.
593bool DIEnumerator::Verify() const {
594 return isEnumerator() && DbgNode->getNumOperands() == 3;
595}
596
597/// \brief Verify that the subrange descriptor is well formed.
598bool DISubrange::Verify() const {
599 return isSubrange() && DbgNode->getNumOperands() == 3;
600}
601
602/// \brief Verify that the lexical block descriptor is well formed.
603bool DILexicalBlock::Verify() const {
Diego Novillo282450d2014-03-03 18:53:17 +0000604 return isLexicalBlock() && DbgNode->getNumOperands() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000605}
606
607/// \brief Verify that the file-scoped lexical block descriptor is well formed.
608bool DILexicalBlockFile::Verify() const {
609 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
610}
611
Adrian Prantlb363c302014-02-25 23:42:11 +0000612/// \brief Verify that an unspecified parameter descriptor is well formed.
613bool DIUnspecifiedParameter::Verify() const {
614 return isUnspecifiedParameter() && DbgNode->getNumOperands() == 1;
615}
616
Bill Wendling523bea82013-11-08 08:13:15 +0000617/// \brief Verify that the template type parameter descriptor is well formed.
618bool DITemplateTypeParameter::Verify() const {
619 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
620}
621
622/// \brief Verify that the template value parameter descriptor is well formed.
623bool DITemplateValueParameter::Verify() const {
624 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
625}
626
627/// \brief Verify that the imported module descriptor is well formed.
628bool DIImportedEntity::Verify() const {
629 return isImportedEntity() &&
630 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
631}
632
633/// getObjCProperty - Return property node, if this ivar is associated with one.
634MDNode *DIDerivedType::getObjCProperty() const {
635 return getNodeField(DbgNode, 10);
636}
637
638MDString *DICompositeType::getIdentifier() const {
639 return cast_or_null<MDString>(getField(DbgNode, 14));
640}
641
642#ifndef NDEBUG
643static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
644 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
645 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
646 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
647 continue;
648 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
649 bool found = false;
650 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
651 found = E == RHS->getOperand(j);
652 assert(found && "Losing a member during member list replacement");
653 }
654}
655#endif
656
657/// \brief Set the array of member DITypes.
658void DICompositeType::setTypeArray(DIArray Elements, DIArray TParams) {
659 assert((!TParams || DbgNode->getNumOperands() == 15) &&
660 "If you're setting the template parameters this should include a slot "
661 "for that!");
662 TrackingVH<MDNode> N(*this);
663 if (Elements) {
664#ifndef NDEBUG
665 // Check that the new list of members contains all the old members as well.
666 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10)))
667 VerifySubsetOf(El, Elements);
668#endif
669 N->replaceOperandWith(10, Elements);
670 }
671 if (TParams)
672 N->replaceOperandWith(13, TParams);
673 DbgNode = N;
674}
675
Bill Wendling523bea82013-11-08 08:13:15 +0000676/// Generate a reference to this DIType. Uses the type identifier instead
677/// of the actual MDNode if possible, to help type uniquing.
678DIScopeRef DIScope::getRef() const {
679 if (!isCompositeType())
680 return DIScopeRef(*this);
681 DICompositeType DTy(DbgNode);
682 if (!DTy.getIdentifier())
683 return DIScopeRef(*this);
684 return DIScopeRef(DTy.getIdentifier());
685}
686
687/// \brief Set the containing type.
688void DICompositeType::setContainingType(DICompositeType ContainingType) {
689 TrackingVH<MDNode> N(*this);
690 N->replaceOperandWith(12, ContainingType.getRef());
691 DbgNode = N;
692}
693
694/// isInlinedFnArgument - Return true if this variable provides debugging
695/// information for an inlined function arguments.
696bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
697 assert(CurFn && "Invalid function");
698 if (!getContext().isSubprogram())
699 return false;
700 // This variable is not inlined function argument if its scope
701 // does not describe current function.
702 return !DISubprogram(getContext()).describes(CurFn);
703}
704
705/// describes - Return true if this subprogram provides debugging
706/// information for the function F.
707bool DISubprogram::describes(const Function *F) {
708 assert(F && "Invalid function");
709 if (F == getFunction())
710 return true;
711 StringRef Name = getLinkageName();
712 if (Name.empty())
713 Name = getName();
714 if (F->getName() == Name)
715 return true;
716 return false;
717}
718
719unsigned DISubprogram::isOptimized() const {
720 assert(DbgNode && "Invalid subprogram descriptor!");
721 if (DbgNode->getNumOperands() == 15)
722 return getUnsignedField(14);
723 return 0;
724}
725
726MDNode *DISubprogram::getVariablesNodes() const {
727 return getNodeField(DbgNode, 18);
728}
729
730DIArray DISubprogram::getVariables() const {
731 return DIArray(getNodeField(DbgNode, 18));
732}
733
734Value *DITemplateValueParameter::getValue() const {
735 return getField(DbgNode, 4);
736}
737
738// If the current node has a parent scope then return that,
739// else return an empty scope.
740DIScopeRef DIScope::getContext() const {
741
742 if (isType())
743 return DIType(DbgNode).getContext();
744
745 if (isSubprogram())
746 return DIScopeRef(DISubprogram(DbgNode).getContext());
747
748 if (isLexicalBlock())
749 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
750
751 if (isLexicalBlockFile())
752 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
753
754 if (isNameSpace())
755 return DIScopeRef(DINameSpace(DbgNode).getContext());
756
757 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
758 return DIScopeRef(NULL);
759}
760
761// If the scope node has a name, return that, else return an empty string.
762StringRef DIScope::getName() const {
763 if (isType())
764 return DIType(DbgNode).getName();
765 if (isSubprogram())
766 return DISubprogram(DbgNode).getName();
767 if (isNameSpace())
768 return DINameSpace(DbgNode).getName();
769 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
770 isCompileUnit()) &&
771 "Unhandled type of scope.");
772 return StringRef();
773}
774
775StringRef DIScope::getFilename() const {
776 if (!DbgNode)
777 return StringRef();
778 return ::getStringField(getNodeField(DbgNode, 1), 0);
779}
780
781StringRef DIScope::getDirectory() const {
782 if (!DbgNode)
783 return StringRef();
784 return ::getStringField(getNodeField(DbgNode, 1), 1);
785}
786
787DIArray DICompileUnit::getEnumTypes() const {
788 if (!DbgNode || DbgNode->getNumOperands() < 13)
789 return DIArray();
790
791 return DIArray(getNodeField(DbgNode, 7));
792}
793
794DIArray DICompileUnit::getRetainedTypes() const {
795 if (!DbgNode || DbgNode->getNumOperands() < 13)
796 return DIArray();
797
798 return DIArray(getNodeField(DbgNode, 8));
799}
800
801DIArray DICompileUnit::getSubprograms() const {
802 if (!DbgNode || DbgNode->getNumOperands() < 13)
803 return DIArray();
804
805 return DIArray(getNodeField(DbgNode, 9));
806}
807
808DIArray DICompileUnit::getGlobalVariables() const {
809 if (!DbgNode || DbgNode->getNumOperands() < 13)
810 return DIArray();
811
812 return DIArray(getNodeField(DbgNode, 10));
813}
814
815DIArray DICompileUnit::getImportedEntities() const {
816 if (!DbgNode || DbgNode->getNumOperands() < 13)
817 return DIArray();
818
819 return DIArray(getNodeField(DbgNode, 11));
820}
821
Diego Novillof5041ce2014-03-03 20:06:11 +0000822/// copyWithNewScope - Return a copy of this location, replacing the
823/// current scope with the given one.
824DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
825 DILexicalBlock NewScope) {
826 SmallVector<Value *, 10> Elts;
827 assert(Verify());
828 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
829 if (I != 2)
830 Elts.push_back(DbgNode->getOperand(I));
831 else
832 Elts.push_back(NewScope);
833 }
834 MDNode *NewDIL = MDNode::get(Ctx, Elts);
835 return DILocation(NewDIL);
836}
837
838/// computeNewDiscriminator - Generate a new discriminator value for this
839/// file and line location.
840unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
841 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
842 return ++Ctx.pImpl->DiscriminatorTable[Key];
843}
844
Bill Wendling523bea82013-11-08 08:13:15 +0000845/// fixupSubprogramName - Replace contains special characters used
846/// in a typical Objective-C names with '.' in a given string.
847static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
848 StringRef FName =
849 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
850 FName = Function::getRealLinkageName(FName);
851
852 StringRef Prefix("llvm.dbg.lv.");
853 Out.reserve(FName.size() + Prefix.size());
854 Out.append(Prefix.begin(), Prefix.end());
855
856 bool isObjCLike = false;
857 for (size_t i = 0, e = FName.size(); i < e; ++i) {
858 char C = FName[i];
859 if (C == '[')
860 isObjCLike = true;
861
862 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
863 C == '+' || C == '(' || C == ')'))
864 Out.push_back('.');
865 else
866 Out.push_back(C);
867 }
868}
869
870/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
871/// suitable to hold function specific information.
872NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
873 SmallString<32> Name;
874 fixupSubprogramName(Fn, Name);
875 return M.getNamedMetadata(Name.str());
876}
877
878/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
879/// to hold function specific information.
880NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
881 SmallString<32> Name;
882 fixupSubprogramName(Fn, Name);
883 return M.getOrInsertNamedMetadata(Name.str());
884}
885
886/// createInlinedVariable - Create a new inlined variable based on current
887/// variable.
888/// @param DV Current Variable.
889/// @param InlinedScope Location at current variable is inlined.
890DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
891 LLVMContext &VMContext) {
892 SmallVector<Value *, 16> Elts;
893 // Insert inlined scope as 7th element.
894 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
895 i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i));
896 return DIVariable(MDNode::get(VMContext, Elts));
897}
898
899/// cleanseInlinedVariable - Remove inlined scope from the variable.
900DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
901 SmallVector<Value *, 16> Elts;
902 // Insert inlined scope as 7th element.
903 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
904 i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)))
905 : Elts.push_back(DV->getOperand(i));
906 return DIVariable(MDNode::get(VMContext, Elts));
907}
908
909/// getDISubprogram - Find subprogram that is enclosing this scope.
910DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
911 DIDescriptor D(Scope);
912 if (D.isSubprogram())
913 return DISubprogram(Scope);
914
915 if (D.isLexicalBlockFile())
916 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
917
918 if (D.isLexicalBlock())
919 return getDISubprogram(DILexicalBlock(Scope).getContext());
920
921 return DISubprogram();
922}
923
924/// getDICompositeType - Find underlying composite type.
925DICompositeType llvm::getDICompositeType(DIType T) {
926 if (T.isCompositeType())
927 return DICompositeType(T);
928
929 if (T.isDerivedType()) {
930 // This function is currently used by dragonegg and dragonegg does
931 // not generate identifier for types, so using an empty map to resolve
932 // DerivedFrom should be fine.
933 DITypeIdentifierMap EmptyMap;
934 return getDICompositeType(
935 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
936 }
937
938 return DICompositeType();
939}
940
941/// Update DITypeIdentifierMap by going through retained types of each CU.
942DITypeIdentifierMap
943llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
944 DITypeIdentifierMap Map;
945 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
946 DICompileUnit CU(CU_Nodes->getOperand(CUi));
947 DIArray Retain = CU.getRetainedTypes();
948 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
949 if (!Retain.getElement(Ti).isCompositeType())
950 continue;
951 DICompositeType Ty(Retain.getElement(Ti));
952 if (MDString *TypeId = Ty.getIdentifier()) {
953 // Definition has priority over declaration.
954 // Try to insert (TypeId, Ty) to Map.
955 std::pair<DITypeIdentifierMap::iterator, bool> P =
956 Map.insert(std::make_pair(TypeId, Ty));
957 // If TypeId already exists in Map and this is a definition, replace
958 // whatever we had (declaration or definition) with the definition.
959 if (!P.second && !Ty.isForwardDecl())
960 P.first->second = Ty;
961 }
962 }
963 }
964 return Map;
965}
966
967//===----------------------------------------------------------------------===//
968// DebugInfoFinder implementations.
969//===----------------------------------------------------------------------===//
970
971void DebugInfoFinder::reset() {
972 CUs.clear();
973 SPs.clear();
974 GVs.clear();
975 TYs.clear();
976 Scopes.clear();
977 NodesSeen.clear();
978 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000979 TypeMapInitialized = false;
980}
981
Manman Renb46e5502013-11-17 19:35:03 +0000982void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000983 if (!TypeMapInitialized)
984 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
985 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
986 TypeMapInitialized = true;
987 }
Bill Wendling523bea82013-11-08 08:13:15 +0000988}
989
990/// processModule - Process entire module and collect debug info.
991void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000992 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000993 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000994 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
995 DICompileUnit CU(CU_Nodes->getOperand(i));
996 addCompileUnit(CU);
997 DIArray GVs = CU.getGlobalVariables();
998 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
999 DIGlobalVariable DIG(GVs.getElement(i));
1000 if (addGlobalVariable(DIG)) {
1001 processScope(DIG.getContext());
1002 processType(DIG.getType());
1003 }
1004 }
1005 DIArray SPs = CU.getSubprograms();
1006 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1007 processSubprogram(DISubprogram(SPs.getElement(i)));
1008 DIArray EnumTypes = CU.getEnumTypes();
1009 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1010 processType(DIType(EnumTypes.getElement(i)));
1011 DIArray RetainedTypes = CU.getRetainedTypes();
1012 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1013 processType(DIType(RetainedTypes.getElement(i)));
1014 DIArray Imports = CU.getImportedEntities();
1015 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1016 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1017 DIDescriptor Entity = Import.getEntity();
1018 if (Entity.isType())
1019 processType(DIType(Entity));
1020 else if (Entity.isSubprogram())
1021 processSubprogram(DISubprogram(Entity));
1022 else if (Entity.isNameSpace())
1023 processScope(DINameSpace(Entity).getContext());
1024 }
1025 }
1026 }
1027}
1028
1029/// processLocation - Process DILocation.
Manman Ren2085ccc2013-11-17 18:42:37 +00001030void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001031 if (!Loc)
1032 return;
Manman Renb46e5502013-11-17 19:35:03 +00001033 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001034 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001035 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001036}
1037
1038/// processType - Process DIType.
1039void DebugInfoFinder::processType(DIType DT) {
1040 if (!addType(DT))
1041 return;
1042 processScope(DT.getContext().resolve(TypeIdentifierMap));
1043 if (DT.isCompositeType()) {
1044 DICompositeType DCT(DT);
1045 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1046 DIArray DA = DCT.getTypeArray();
1047 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1048 DIDescriptor D = DA.getElement(i);
1049 if (D.isType())
1050 processType(DIType(D));
1051 else if (D.isSubprogram())
1052 processSubprogram(DISubprogram(D));
1053 }
1054 } else if (DT.isDerivedType()) {
1055 DIDerivedType DDT(DT);
1056 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1057 }
1058}
1059
1060void DebugInfoFinder::processScope(DIScope Scope) {
1061 if (Scope.isType()) {
1062 DIType Ty(Scope);
1063 processType(Ty);
1064 return;
1065 }
1066 if (Scope.isCompileUnit()) {
1067 addCompileUnit(DICompileUnit(Scope));
1068 return;
1069 }
1070 if (Scope.isSubprogram()) {
1071 processSubprogram(DISubprogram(Scope));
1072 return;
1073 }
1074 if (!addScope(Scope))
1075 return;
1076 if (Scope.isLexicalBlock()) {
1077 DILexicalBlock LB(Scope);
1078 processScope(LB.getContext());
1079 } else if (Scope.isLexicalBlockFile()) {
1080 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1081 processScope(LBF.getScope());
1082 } else if (Scope.isNameSpace()) {
1083 DINameSpace NS(Scope);
1084 processScope(NS.getContext());
1085 }
1086}
1087
1088/// processLexicalBlock
1089void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1090 DIScope Context = LB.getContext();
1091 if (Context.isLexicalBlock())
1092 return processLexicalBlock(DILexicalBlock(Context));
1093 else if (Context.isLexicalBlockFile()) {
1094 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
1095 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
1096 } else
1097 return processSubprogram(DISubprogram(Context));
1098}
1099
1100/// processSubprogram - Process DISubprogram.
1101void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1102 if (!addSubprogram(SP))
1103 return;
1104 processScope(SP.getContext().resolve(TypeIdentifierMap));
1105 processType(SP.getType());
1106 DIArray TParams = SP.getTemplateParams();
1107 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1108 DIDescriptor Element = TParams.getElement(I);
1109 if (Element.isTemplateTypeParameter()) {
1110 DITemplateTypeParameter TType(Element);
1111 processScope(TType.getContext().resolve(TypeIdentifierMap));
1112 processType(TType.getType().resolve(TypeIdentifierMap));
1113 } else if (Element.isTemplateValueParameter()) {
1114 DITemplateValueParameter TVal(Element);
1115 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1116 processType(TVal.getType().resolve(TypeIdentifierMap));
1117 }
1118 }
1119}
1120
1121/// processDeclare - Process DbgDeclareInst.
Manman Ren2085ccc2013-11-17 18:42:37 +00001122void DebugInfoFinder::processDeclare(const Module &M,
1123 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001124 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1125 if (!N)
1126 return;
Manman Renb46e5502013-11-17 19:35:03 +00001127 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001128
1129 DIDescriptor DV(N);
1130 if (!DV.isVariable())
1131 return;
1132
1133 if (!NodesSeen.insert(DV))
1134 return;
1135 processScope(DIVariable(N).getContext());
1136 processType(DIVariable(N).getType());
1137}
1138
Manman Ren2085ccc2013-11-17 18:42:37 +00001139void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001140 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1141 if (!N)
1142 return;
Manman Renb46e5502013-11-17 19:35:03 +00001143 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001144
1145 DIDescriptor DV(N);
1146 if (!DV.isVariable())
1147 return;
1148
1149 if (!NodesSeen.insert(DV))
1150 return;
1151 processScope(DIVariable(N).getContext());
1152 processType(DIVariable(N).getType());
1153}
1154
1155/// addType - Add type into Tys.
1156bool DebugInfoFinder::addType(DIType DT) {
1157 if (!DT)
1158 return false;
1159
1160 if (!NodesSeen.insert(DT))
1161 return false;
1162
1163 TYs.push_back(DT);
1164 return true;
1165}
1166
1167/// addCompileUnit - Add compile unit into CUs.
1168bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1169 if (!CU)
1170 return false;
1171 if (!NodesSeen.insert(CU))
1172 return false;
1173
1174 CUs.push_back(CU);
1175 return true;
1176}
1177
1178/// addGlobalVariable - Add global variable into GVs.
1179bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1180 if (!DIG)
1181 return false;
1182
1183 if (!NodesSeen.insert(DIG))
1184 return false;
1185
1186 GVs.push_back(DIG);
1187 return true;
1188}
1189
1190// addSubprogram - Add subprgoram into SPs.
1191bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1192 if (!SP)
1193 return false;
1194
1195 if (!NodesSeen.insert(SP))
1196 return false;
1197
1198 SPs.push_back(SP);
1199 return true;
1200}
1201
1202bool DebugInfoFinder::addScope(DIScope Scope) {
1203 if (!Scope)
1204 return false;
1205 // FIXME: Ocaml binding generates a scope with no content, we treat it
1206 // as null for now.
1207 if (Scope->getNumOperands() == 0)
1208 return false;
1209 if (!NodesSeen.insert(Scope))
1210 return false;
1211 Scopes.push_back(Scope);
1212 return true;
1213}
1214
1215//===----------------------------------------------------------------------===//
1216// DIDescriptor: dump routines for all descriptors.
1217//===----------------------------------------------------------------------===//
1218
1219/// dump - Print descriptor to dbgs() with a newline.
1220void DIDescriptor::dump() const {
1221 print(dbgs());
1222 dbgs() << '\n';
1223}
1224
1225/// print - Print descriptor.
1226void DIDescriptor::print(raw_ostream &OS) const {
1227 if (!DbgNode)
1228 return;
1229
1230 if (const char *Tag = dwarf::TagString(getTag()))
1231 OS << "[ " << Tag << " ]";
1232
1233 if (this->isSubrange()) {
1234 DISubrange(DbgNode).printInternal(OS);
1235 } else if (this->isCompileUnit()) {
1236 DICompileUnit(DbgNode).printInternal(OS);
1237 } else if (this->isFile()) {
1238 DIFile(DbgNode).printInternal(OS);
1239 } else if (this->isEnumerator()) {
1240 DIEnumerator(DbgNode).printInternal(OS);
1241 } else if (this->isBasicType()) {
1242 DIType(DbgNode).printInternal(OS);
1243 } else if (this->isDerivedType()) {
1244 DIDerivedType(DbgNode).printInternal(OS);
1245 } else if (this->isCompositeType()) {
1246 DICompositeType(DbgNode).printInternal(OS);
1247 } else if (this->isSubprogram()) {
1248 DISubprogram(DbgNode).printInternal(OS);
1249 } else if (this->isGlobalVariable()) {
1250 DIGlobalVariable(DbgNode).printInternal(OS);
1251 } else if (this->isVariable()) {
1252 DIVariable(DbgNode).printInternal(OS);
1253 } else if (this->isObjCProperty()) {
1254 DIObjCProperty(DbgNode).printInternal(OS);
1255 } else if (this->isNameSpace()) {
1256 DINameSpace(DbgNode).printInternal(OS);
1257 } else if (this->isScope()) {
1258 DIScope(DbgNode).printInternal(OS);
1259 }
1260}
1261
1262void DISubrange::printInternal(raw_ostream &OS) const {
1263 int64_t Count = getCount();
1264 if (Count != -1)
1265 OS << " [" << getLo() << ", " << Count - 1 << ']';
1266 else
1267 OS << " [unbounded]";
1268}
1269
1270void DIScope::printInternal(raw_ostream &OS) const {
1271 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1272}
1273
1274void DICompileUnit::printInternal(raw_ostream &OS) const {
1275 DIScope::printInternal(OS);
1276 OS << " [";
1277 unsigned Lang = getLanguage();
1278 if (const char *LangStr = dwarf::LanguageString(Lang))
1279 OS << LangStr;
1280 else
1281 (OS << "lang 0x").write_hex(Lang);
1282 OS << ']';
1283}
1284
1285void DIEnumerator::printInternal(raw_ostream &OS) const {
1286 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1287}
1288
1289void DIType::printInternal(raw_ostream &OS) const {
1290 if (!DbgNode)
1291 return;
1292
1293 StringRef Res = getName();
1294 if (!Res.empty())
1295 OS << " [" << Res << "]";
1296
1297 // TODO: Print context?
1298
1299 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1300 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1301 if (isBasicType())
1302 if (const char *Enc =
1303 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1304 OS << ", enc " << Enc;
1305 OS << "]";
1306
1307 if (isPrivate())
1308 OS << " [private]";
1309 else if (isProtected())
1310 OS << " [protected]";
1311
1312 if (isArtificial())
1313 OS << " [artificial]";
1314
1315 if (isForwardDecl())
1316 OS << " [decl]";
1317 else if (getTag() == dwarf::DW_TAG_structure_type ||
1318 getTag() == dwarf::DW_TAG_union_type ||
1319 getTag() == dwarf::DW_TAG_enumeration_type ||
1320 getTag() == dwarf::DW_TAG_class_type)
1321 OS << " [def]";
1322 if (isVector())
1323 OS << " [vector]";
1324 if (isStaticMember())
1325 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001326
1327 if (isLValueReference())
1328 OS << " [reference]";
1329
1330 if (isRValueReference())
1331 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001332}
1333
1334void DIDerivedType::printInternal(raw_ostream &OS) const {
1335 DIType::printInternal(OS);
1336 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1337}
1338
1339void DICompositeType::printInternal(raw_ostream &OS) const {
1340 DIType::printInternal(OS);
1341 DIArray A = getTypeArray();
1342 OS << " [" << A.getNumElements() << " elements]";
1343}
1344
1345void DINameSpace::printInternal(raw_ostream &OS) const {
1346 StringRef Name = getName();
1347 if (!Name.empty())
1348 OS << " [" << Name << ']';
1349
1350 OS << " [line " << getLineNumber() << ']';
1351}
1352
1353void DISubprogram::printInternal(raw_ostream &OS) const {
1354 // TODO : Print context
1355 OS << " [line " << getLineNumber() << ']';
1356
1357 if (isLocalToUnit())
1358 OS << " [local]";
1359
1360 if (isDefinition())
1361 OS << " [def]";
1362
1363 if (getScopeLineNumber() != getLineNumber())
1364 OS << " [scope " << getScopeLineNumber() << "]";
1365
1366 if (isPrivate())
1367 OS << " [private]";
1368 else if (isProtected())
1369 OS << " [protected]";
1370
Adrian Prantl99c7af22013-12-18 21:48:19 +00001371 if (isLValueReference())
1372 OS << " [reference]";
1373
1374 if (isRValueReference())
1375 OS << " [rvalue reference]";
1376
Bill Wendling523bea82013-11-08 08:13:15 +00001377 StringRef Res = getName();
1378 if (!Res.empty())
1379 OS << " [" << Res << ']';
1380}
1381
1382void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1383 StringRef Res = getName();
1384 if (!Res.empty())
1385 OS << " [" << Res << ']';
1386
1387 OS << " [line " << getLineNumber() << ']';
1388
1389 // TODO : Print context
1390
1391 if (isLocalToUnit())
1392 OS << " [local]";
1393
1394 if (isDefinition())
1395 OS << " [def]";
1396}
1397
1398void DIVariable::printInternal(raw_ostream &OS) const {
1399 StringRef Res = getName();
1400 if (!Res.empty())
1401 OS << " [" << Res << ']';
1402
1403 OS << " [line " << getLineNumber() << ']';
1404}
1405
1406void DIObjCProperty::printInternal(raw_ostream &OS) const {
1407 StringRef Name = getObjCPropertyName();
1408 if (!Name.empty())
1409 OS << " [" << Name << ']';
1410
1411 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1412 << ']';
1413}
1414
1415static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1416 const LLVMContext &Ctx) {
1417 if (!DL.isUnknown()) { // Print source line info.
1418 DIScope Scope(DL.getScope(Ctx));
1419 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1420 // Omit the directory, because it's likely to be long and uninteresting.
1421 CommentOS << Scope.getFilename();
1422 CommentOS << ':' << DL.getLine();
1423 if (DL.getCol() != 0)
1424 CommentOS << ':' << DL.getCol();
1425 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1426 if (!InlinedAtDL.isUnknown()) {
1427 CommentOS << " @[ ";
1428 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1429 CommentOS << " ]";
1430 }
1431 }
1432}
1433
1434void DIVariable::printExtendedName(raw_ostream &OS) const {
1435 const LLVMContext &Ctx = DbgNode->getContext();
1436 StringRef Res = getName();
1437 if (!Res.empty())
1438 OS << Res << "," << getLineNumber();
1439 if (MDNode *InlinedAt = getInlinedAt()) {
1440 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1441 if (!InlinedAtDL.isUnknown()) {
1442 OS << " @[";
1443 printDebugLoc(InlinedAtDL, OS, Ctx);
1444 OS << "]";
1445 }
1446 }
1447}
1448
1449/// Specialize constructor to make sure it has the correct type.
1450template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1451 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1452}
1453template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1454 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1455}
1456
1457/// Specialize getFieldAs to handle fields that are references to DIScopes.
1458template <>
1459DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1460 return DIScopeRef(getField(DbgNode, Elt));
1461}
1462/// Specialize getFieldAs to handle fields that are references to DITypes.
1463template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1464 return DITypeRef(getField(DbgNode, Elt));
1465}
Manman Rencb14bbc2013-11-22 22:06:31 +00001466
1467/// Strip debug info in the module if it exists.
1468/// To do this, we remove all calls to the debugger intrinsics and any named
1469/// metadata for debugging. We also remove debug locations for instructions.
1470/// Return true if module is modified.
1471bool llvm::StripDebugInfo(Module &M) {
1472
1473 bool Changed = false;
1474
1475 // Remove all of the calls to the debugger intrinsics, and remove them from
1476 // the module.
1477 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1478 while (!Declare->use_empty()) {
1479 CallInst *CI = cast<CallInst>(Declare->use_back());
1480 CI->eraseFromParent();
1481 }
1482 Declare->eraseFromParent();
1483 Changed = true;
1484 }
1485
1486 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1487 while (!DbgVal->use_empty()) {
1488 CallInst *CI = cast<CallInst>(DbgVal->use_back());
1489 CI->eraseFromParent();
1490 }
1491 DbgVal->eraseFromParent();
1492 Changed = true;
1493 }
1494
1495 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1496 NME = M.named_metadata_end(); NMI != NME;) {
1497 NamedMDNode *NMD = NMI;
1498 ++NMI;
1499 if (NMD->getName().startswith("llvm.dbg.")) {
1500 NMD->eraseFromParent();
1501 Changed = true;
1502 }
1503 }
1504
1505 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1506 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1507 ++FI)
1508 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1509 ++BI) {
1510 if (!BI->getDebugLoc().isUnknown()) {
1511 Changed = true;
1512 BI->setDebugLoc(DebugLoc());
1513 }
1514 }
1515
1516 return Changed;
1517}
Manman Ren8b4306c2013-12-02 21:29:56 +00001518
Manman Renbd4daf82013-12-03 00:12:14 +00001519/// Return Debug Info Metadata Version by checking module flags.
1520unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Manman Ren8b4306c2013-12-02 21:29:56 +00001521 Value *Val = M.getModuleFlag("Debug Info Version");
1522 if (!Val)
1523 return 0;
1524 return cast<ConstantInt>(Val)->getZExtValue();
1525}