1 //===--- 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 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/DebugInfo.h"
16 #include "LLVMContextImpl.h"
17 #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/IR/ValueHandle.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm;
32 using namespace llvm::dwarf;
34 //===----------------------------------------------------------------------===//
35 // DIDescriptor
36 //===----------------------------------------------------------------------===//
38 bool 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() ||
49 DIUnspecifiedParameter(DbgNode).Verify() ||
50 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
52 DIImportedEntity(DbgNode).Verify());
53 }
55 static Value *getField(const MDNode *DbgNode, unsigned Elt) {
56 if (DbgNode == 0 || Elt >= DbgNode->getNumOperands())
57 return 0;
58 return DbgNode->getOperand(Elt);
59 }
61 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
63 }
65 static 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 }
71 StringRef DIDescriptor::getStringField(unsigned Elt) const {
72 return ::getStringField(DbgNode, Elt);
73 }
75 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
76 if (DbgNode == 0)
77 return 0;
79 if (Elt < DbgNode->getNumOperands())
80 if (ConstantInt *CI =
81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
82 return CI->getZExtValue();
84 return 0;
85 }
87 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
88 if (DbgNode == 0)
89 return 0;
91 if (Elt < DbgNode->getNumOperands())
92 if (ConstantInt *CI =
93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
94 return CI->getSExtValue();
96 return 0;
97 }
99 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
100 MDNode *Field = getNodeField(DbgNode, Elt);
101 return DIDescriptor(Field);
102 }
104 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
105 if (DbgNode == 0)
106 return 0;
108 if (Elt < DbgNode->getNumOperands())
109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
110 return 0;
111 }
113 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
114 if (DbgNode == 0)
115 return 0;
117 if (Elt < DbgNode->getNumOperands())
118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
119 return 0;
120 }
122 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
123 if (DbgNode == 0)
124 return 0;
126 if (Elt < DbgNode->getNumOperands())
127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
128 return 0;
129 }
131 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
132 if (DbgNode == 0)
133 return;
135 if (Elt < DbgNode->getNumOperands()) {
136 MDNode *Node = const_cast<MDNode *>(DbgNode);
137 Node->replaceOperandWith(Elt, F);
138 }
139 }
141 unsigned DIVariable::getNumAddrElements() const {
142 return DbgNode->getNumOperands() - 8;
143 }
145 /// getInlinedAt - If this variable is inlined then return inline location.
146 MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
148 //===----------------------------------------------------------------------===//
149 // Predicates
150 //===----------------------------------------------------------------------===//
152 /// isBasicType - Return true if the specified tag is legal for
153 /// DIBasicType.
154 bool 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 }
166 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
167 bool 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 }
189 /// isCompositeType - Return true if the specified tag is legal for
190 /// DICompositeType.
191 bool 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 }
207 /// isVariable - Return true if the specified tag is legal for DIVariable.
208 bool 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 }
220 /// isType - Return true if the specified tag is legal for DIType.
221 bool DIDescriptor::isType() const {
222 return isBasicType() || isCompositeType() || isDerivedType();
223 }
225 /// isSubprogram - Return true if the specified tag is legal for
226 /// DISubprogram.
227 bool DIDescriptor::isSubprogram() const {
228 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
229 }
231 /// isGlobalVariable - Return true if the specified tag is legal for
232 /// DIGlobalVariable.
233 bool DIDescriptor::isGlobalVariable() const {
234 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
235 getTag() == dwarf::DW_TAG_constant);
236 }
238 /// isUnspecifiedParmeter - Return true if the specified tag is
239 /// DW_TAG_unspecified_parameters.
240 bool DIDescriptor::isUnspecifiedParameter() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
242 }
244 /// isScope - Return true if the specified tag is one of the scope
245 /// related tag.
246 bool 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 }
262 /// isTemplateTypeParameter - Return true if the specified tag is
263 /// DW_TAG_template_type_parameter.
264 bool DIDescriptor::isTemplateTypeParameter() const {
265 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
266 }
268 /// isTemplateValueParameter - Return true if the specified tag is
269 /// DW_TAG_template_value_parameter.
270 bool 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 }
276 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
277 bool DIDescriptor::isCompileUnit() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
279 }
281 /// isFile - Return true if the specified tag is DW_TAG_file_type.
282 bool DIDescriptor::isFile() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
284 }
286 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
287 bool DIDescriptor::isNameSpace() const {
288 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
289 }
291 /// isLexicalBlockFile - Return true if the specified descriptor is a
292 /// lexical block with an extra file.
293 bool DIDescriptor::isLexicalBlockFile() const {
294 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
295 (DbgNode->getNumOperands() == 3);
296 }
298 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
299 bool DIDescriptor::isLexicalBlock() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
301 (DbgNode->getNumOperands() > 3);
302 }
304 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
305 bool DIDescriptor::isSubrange() const {
306 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
307 }
309 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
310 bool DIDescriptor::isEnumerator() const {
311 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
312 }
314 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
315 bool DIDescriptor::isObjCProperty() const {
316 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
317 }
319 /// \brief Return true if the specified tag is DW_TAG_imported_module or
320 /// DW_TAG_imported_declaration.
321 bool DIDescriptor::isImportedEntity() const {
322 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
323 getTag() == dwarf::DW_TAG_imported_declaration);
324 }
326 //===----------------------------------------------------------------------===//
327 // Simple Descriptor Constructors and other Methods
328 //===----------------------------------------------------------------------===//
330 unsigned DIArray::getNumElements() const {
331 if (!DbgNode)
332 return 0;
333 return DbgNode->getNumOperands();
334 }
336 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
337 /// type with the one in the passed descriptor.
338 void DIType::replaceAllUsesWith(DIDescriptor &D) {
340 assert(DbgNode && "Trying to replace an unverified type!");
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 }
356 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
357 /// type with the one in D.
358 void DIType::replaceAllUsesWith(MDNode *D) {
360 assert(DbgNode && "Trying to replace an unverified type!");
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 }
376 /// Verify - Verify that a compile unit is well formed.
377 bool DICompileUnit::Verify() const {
378 if (!isCompileUnit())
379 return false;
381 // Don't bother verifying the compilation directory or producer string
382 // as those could be empty.
383 if (getFilename().empty())
384 return false;
386 return DbgNode->getNumOperands() == 14;
387 }
389 /// Verify - Verify that an ObjC property is well formed.
390 bool DIObjCProperty::Verify() const {
391 if (!isObjCProperty())
392 return false;
394 // Don't worry about the rest of the strings for now.
395 return DbgNode->getNumOperands() == 8;
396 }
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.
401 static 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 }
410 /// Check if a field at position Elt of a MDNode is a MDString.
411 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
412 Value *Fld = getField(DbgNode, Elt);
413 return !Fld || isa<MDString>(Fld);
414 }
416 /// Check if a value can be a reference to a type.
417 static 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 }
423 /// Check if a field at position Elt of a MDNode can be a reference to a type.
424 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
425 Value *Fld = getField(DbgNode, Elt);
426 return isTypeRef(Fld);
427 }
429 /// Check if a value can be a ScopeRef.
430 static 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 }
436 /// Check if a field at position Elt of a MDNode can be a ScopeRef.
437 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
438 Value *Fld = getField(DbgNode, Elt);
439 return isScopeRef(Fld);
440 }
442 /// Verify - Verify that a type descriptor is well formed.
443 bool 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;
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())
466 return DIBasicType(DbgNode).Verify();
467 else if (isCompositeType())
468 return DICompositeType(DbgNode).Verify();
469 else if (isDerivedType())
470 return DIDerivedType(DbgNode).Verify();
471 else
472 return false;
473 }
475 /// Verify - Verify that a basic type descriptor is well formed.
476 bool DIBasicType::Verify() const {
477 return isBasicType() && DbgNode->getNumOperands() == 10;
478 }
480 /// Verify - Verify that a derived type descriptor is well formed.
481 bool 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;
490 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
491 DbgNode->getNumOperands() <= 14;
492 }
494 /// Verify - Verify that a composite type descriptor is well formed.
495 bool DICompositeType::Verify() const {
496 if (!isCompositeType())
497 return false;
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;
505 // Make sure the type identifier at field 14 is MDString, it can be null.
506 if (!fieldIsMDString(DbgNode, 14))
507 return false;
509 // A subroutine type can't be both & and &&.
510 if (isLValueReference() && isRValueReference())
511 return false;
513 return DbgNode->getNumOperands() == 15;
514 }
516 /// Verify - Verify that a subprogram descriptor is well formed.
517 bool DISubprogram::Verify() const {
518 if (!isSubprogram())
519 return false;
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;
530 // A subprogram can't be both & and &&.
531 if (isLValueReference() && isRValueReference())
532 return false;
534 return DbgNode->getNumOperands() == 20;
535 }
537 /// Verify - Verify that a global variable descriptor is well formed.
538 bool DIGlobalVariable::Verify() const {
539 if (!isGlobalVariable())
540 return false;
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;
553 return DbgNode->getNumOperands() == 13;
554 }
556 /// Verify - Verify that a variable descriptor is well formed.
557 bool DIVariable::Verify() const {
558 if (!isVariable())
559 return false;
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 }
569 /// Verify - Verify that a location descriptor is well formed.
570 bool DILocation::Verify() const {
571 if (!DbgNode)
572 return false;
574 return DbgNode->getNumOperands() == 4;
575 }
577 /// Verify - Verify that a namespace descriptor is well formed.
578 bool DINameSpace::Verify() const {
579 if (!isNameSpace())
580 return false;
581 return DbgNode->getNumOperands() == 5;
582 }
584 /// \brief Retrieve the MDNode for the directory/file pair.
585 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
587 /// \brief Verify that the file descriptor is well formed.
588 bool DIFile::Verify() const {
589 return isFile() && DbgNode->getNumOperands() == 2;
590 }
592 /// \brief Verify that the enumerator descriptor is well formed.
593 bool DIEnumerator::Verify() const {
594 return isEnumerator() && DbgNode->getNumOperands() == 3;
595 }
597 /// \brief Verify that the subrange descriptor is well formed.
598 bool DISubrange::Verify() const {
599 return isSubrange() && DbgNode->getNumOperands() == 3;
600 }
602 /// \brief Verify that the lexical block descriptor is well formed.
603 bool DILexicalBlock::Verify() const {
604 return isLexicalBlock() && DbgNode->getNumOperands() == 7;
605 }
607 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
608 bool DILexicalBlockFile::Verify() const {
609 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
610 }
612 /// \brief Verify that an unspecified parameter descriptor is well formed.
613 bool DIUnspecifiedParameter::Verify() const {
614 return isUnspecifiedParameter() && DbgNode->getNumOperands() == 1;
615 }
617 /// \brief Verify that the template type parameter descriptor is well formed.
618 bool DITemplateTypeParameter::Verify() const {
619 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
620 }
622 /// \brief Verify that the template value parameter descriptor is well formed.
623 bool DITemplateValueParameter::Verify() const {
624 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
625 }
627 /// \brief Verify that the imported module descriptor is well formed.
628 bool DIImportedEntity::Verify() const {
629 return isImportedEntity() &&
630 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
631 }
633 /// getObjCProperty - Return property node, if this ivar is associated with one.
634 MDNode *DIDerivedType::getObjCProperty() const {
635 return getNodeField(DbgNode, 10);
636 }
638 MDString *DICompositeType::getIdentifier() const {
639 return cast_or_null<MDString>(getField(DbgNode, 14));
640 }
642 #ifndef NDEBUG
643 static 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
657 /// \brief Set the array of member DITypes.
658 void 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 }
676 /// Generate a reference to this DIType. Uses the type identifier instead
677 /// of the actual MDNode if possible, to help type uniquing.
678 DIScopeRef 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 }
687 /// \brief Set the containing type.
688 void DICompositeType::setContainingType(DICompositeType ContainingType) {
689 TrackingVH<MDNode> N(*this);
690 N->replaceOperandWith(12, ContainingType.getRef());
691 DbgNode = N;
692 }
694 /// isInlinedFnArgument - Return true if this variable provides debugging
695 /// information for an inlined function arguments.
696 bool 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 }
705 /// describes - Return true if this subprogram provides debugging
706 /// information for the function F.
707 bool 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 }
719 unsigned DISubprogram::isOptimized() const {
720 assert(DbgNode && "Invalid subprogram descriptor!");
721 if (DbgNode->getNumOperands() == 15)
722 return getUnsignedField(14);
723 return 0;
724 }
726 MDNode *DISubprogram::getVariablesNodes() const {
727 return getNodeField(DbgNode, 18);
728 }
730 DIArray DISubprogram::getVariables() const {
731 return DIArray(getNodeField(DbgNode, 18));
732 }
734 Value *DITemplateValueParameter::getValue() const {
735 return getField(DbgNode, 4);
736 }
738 // If the current node has a parent scope then return that,
739 // else return an empty scope.
740 DIScopeRef DIScope::getContext() const {
742 if (isType())
743 return DIType(DbgNode).getContext();
745 if (isSubprogram())
746 return DIScopeRef(DISubprogram(DbgNode).getContext());
748 if (isLexicalBlock())
749 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
751 if (isLexicalBlockFile())
752 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
754 if (isNameSpace())
755 return DIScopeRef(DINameSpace(DbgNode).getContext());
757 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
758 return DIScopeRef(NULL);
759 }
761 // If the scope node has a name, return that, else return an empty string.
762 StringRef 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 }
775 StringRef DIScope::getFilename() const {
776 if (!DbgNode)
777 return StringRef();
778 return ::getStringField(getNodeField(DbgNode, 1), 0);
779 }
781 StringRef DIScope::getDirectory() const {
782 if (!DbgNode)
783 return StringRef();
784 return ::getStringField(getNodeField(DbgNode, 1), 1);
785 }
787 DIArray DICompileUnit::getEnumTypes() const {
788 if (!DbgNode || DbgNode->getNumOperands() < 13)
789 return DIArray();
791 return DIArray(getNodeField(DbgNode, 7));
792 }
794 DIArray DICompileUnit::getRetainedTypes() const {
795 if (!DbgNode || DbgNode->getNumOperands() < 13)
796 return DIArray();
798 return DIArray(getNodeField(DbgNode, 8));
799 }
801 DIArray DICompileUnit::getSubprograms() const {
802 if (!DbgNode || DbgNode->getNumOperands() < 13)
803 return DIArray();
805 return DIArray(getNodeField(DbgNode, 9));
806 }
808 DIArray DICompileUnit::getGlobalVariables() const {
809 if (!DbgNode || DbgNode->getNumOperands() < 13)
810 return DIArray();
812 return DIArray(getNodeField(DbgNode, 10));
813 }
815 DIArray DICompileUnit::getImportedEntities() const {
816 if (!DbgNode || DbgNode->getNumOperands() < 13)
817 return DIArray();
819 return DIArray(getNodeField(DbgNode, 11));
820 }
822 /// copyWithNewScope - Return a copy of this location, replacing the
823 /// current scope with the given one.
824 DILocation 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 }
838 /// computeNewDiscriminator - Generate a new discriminator value for this
839 /// file and line location.
840 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
841 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
842 return ++Ctx.pImpl->DiscriminatorTable[Key];
843 }
845 /// fixupSubprogramName - Replace contains special characters used
846 /// in a typical Objective-C names with '.' in a given string.
847 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
848 StringRef FName =
849 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
850 FName = Function::getRealLinkageName(FName);
852 StringRef Prefix("llvm.dbg.lv.");
853 Out.reserve(FName.size() + Prefix.size());
854 Out.append(Prefix.begin(), Prefix.end());
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;
862 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
863 C == '+' || C == '(' || C == ')'))
864 Out.push_back('.');
865 else
866 Out.push_back(C);
867 }
868 }
870 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
871 /// suitable to hold function specific information.
872 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
873 SmallString<32> Name;
874 fixupSubprogramName(Fn, Name);
875 return M.getNamedMetadata(Name.str());
876 }
878 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
879 /// to hold function specific information.
880 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
881 SmallString<32> Name;
882 fixupSubprogramName(Fn, Name);
883 return M.getOrInsertNamedMetadata(Name.str());
884 }
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.
890 DIVariable 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 }
899 /// cleanseInlinedVariable - Remove inlined scope from the variable.
900 DIVariable 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 }
909 /// getDISubprogram - Find subprogram that is enclosing this scope.
910 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
911 DIDescriptor D(Scope);
912 if (D.isSubprogram())
913 return DISubprogram(Scope);
915 if (D.isLexicalBlockFile())
916 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
918 if (D.isLexicalBlock())
919 return getDISubprogram(DILexicalBlock(Scope).getContext());
921 return DISubprogram();
922 }
924 /// getDICompositeType - Find underlying composite type.
925 DICompositeType llvm::getDICompositeType(DIType T) {
926 if (T.isCompositeType())
927 return DICompositeType(T);
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 }
938 return DICompositeType();
939 }
941 /// Update DITypeIdentifierMap by going through retained types of each CU.
942 DITypeIdentifierMap
943 llvm::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 }
967 //===----------------------------------------------------------------------===//
968 // DebugInfoFinder implementations.
969 //===----------------------------------------------------------------------===//
971 void DebugInfoFinder::reset() {
972 CUs.clear();
973 SPs.clear();
974 GVs.clear();
975 TYs.clear();
976 Scopes.clear();
977 NodesSeen.clear();
978 TypeIdentifierMap.clear();
979 TypeMapInitialized = false;
980 }
982 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
983 if (!TypeMapInitialized)
984 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
985 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
986 TypeMapInitialized = true;
987 }
988 }
990 /// processModule - Process entire module and collect debug info.
991 void DebugInfoFinder::processModule(const Module &M) {
992 InitializeTypeMap(M);
993 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
994 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 }
1029 /// processLocation - Process DILocation.
1030 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1031 if (!Loc)
1032 return;
1033 InitializeTypeMap(M);
1034 processScope(Loc.getScope());
1035 processLocation(M, Loc.getOrigLocation());
1036 }
1038 /// processType - Process DIType.
1039 void 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 }
1060 void 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 }
1088 /// processLexicalBlock
1089 void 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 }
1100 /// processSubprogram - Process DISubprogram.
1101 void 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 }
1121 /// processDeclare - Process DbgDeclareInst.
1122 void DebugInfoFinder::processDeclare(const Module &M,
1123 const DbgDeclareInst *DDI) {
1124 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1125 if (!N)
1126 return;
1127 InitializeTypeMap(M);
1129 DIDescriptor DV(N);
1130 if (!DV.isVariable())
1131 return;
1133 if (!NodesSeen.insert(DV))
1134 return;
1135 processScope(DIVariable(N).getContext());
1136 processType(DIVariable(N).getType());
1137 }
1139 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1140 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1141 if (!N)
1142 return;
1143 InitializeTypeMap(M);
1145 DIDescriptor DV(N);
1146 if (!DV.isVariable())
1147 return;
1149 if (!NodesSeen.insert(DV))
1150 return;
1151 processScope(DIVariable(N).getContext());
1152 processType(DIVariable(N).getType());
1153 }
1155 /// addType - Add type into Tys.
1156 bool DebugInfoFinder::addType(DIType DT) {
1157 if (!DT)
1158 return false;
1160 if (!NodesSeen.insert(DT))
1161 return false;
1163 TYs.push_back(DT);
1164 return true;
1165 }
1167 /// addCompileUnit - Add compile unit into CUs.
1168 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1169 if (!CU)
1170 return false;
1171 if (!NodesSeen.insert(CU))
1172 return false;
1174 CUs.push_back(CU);
1175 return true;
1176 }
1178 /// addGlobalVariable - Add global variable into GVs.
1179 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1180 if (!DIG)
1181 return false;
1183 if (!NodesSeen.insert(DIG))
1184 return false;
1186 GVs.push_back(DIG);
1187 return true;
1188 }
1190 // addSubprogram - Add subprgoram into SPs.
1191 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1192 if (!SP)
1193 return false;
1195 if (!NodesSeen.insert(SP))
1196 return false;
1198 SPs.push_back(SP);
1199 return true;
1200 }
1202 bool 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 }
1215 //===----------------------------------------------------------------------===//
1216 // DIDescriptor: dump routines for all descriptors.
1217 //===----------------------------------------------------------------------===//
1219 /// dump - Print descriptor to dbgs() with a newline.
1220 void DIDescriptor::dump() const {
1221 print(dbgs());
1222 dbgs() << '\n';
1223 }
1225 /// print - Print descriptor.
1226 void DIDescriptor::print(raw_ostream &OS) const {
1227 if (!DbgNode)
1228 return;
1230 if (const char *Tag = dwarf::TagString(getTag()))
1231 OS << "[ " << Tag << " ]";
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 }
1262 void 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 }
1270 void DIScope::printInternal(raw_ostream &OS) const {
1271 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1272 }
1274 void 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 }
1285 void DIEnumerator::printInternal(raw_ostream &OS) const {
1286 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1287 }
1289 void DIType::printInternal(raw_ostream &OS) const {
1290 if (!DbgNode)
1291 return;
1293 StringRef Res = getName();
1294 if (!Res.empty())
1295 OS << " [" << Res << "]";
1297 // TODO: Print context?
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 << "]";
1307 if (isPrivate())
1308 OS << " [private]";
1309 else if (isProtected())
1310 OS << " [protected]";
1312 if (isArtificial())
1313 OS << " [artificial]";
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]";
1327 if (isLValueReference())
1328 OS << " [reference]";
1330 if (isRValueReference())
1331 OS << " [rvalue reference]";
1332 }
1334 void DIDerivedType::printInternal(raw_ostream &OS) const {
1335 DIType::printInternal(OS);
1336 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1337 }
1339 void DICompositeType::printInternal(raw_ostream &OS) const {
1340 DIType::printInternal(OS);
1341 DIArray A = getTypeArray();
1342 OS << " [" << A.getNumElements() << " elements]";
1343 }
1345 void DINameSpace::printInternal(raw_ostream &OS) const {
1346 StringRef Name = getName();
1347 if (!Name.empty())
1348 OS << " [" << Name << ']';
1350 OS << " [line " << getLineNumber() << ']';
1351 }
1353 void DISubprogram::printInternal(raw_ostream &OS) const {
1354 // TODO : Print context
1355 OS << " [line " << getLineNumber() << ']';
1357 if (isLocalToUnit())
1358 OS << " [local]";
1360 if (isDefinition())
1361 OS << " [def]";
1363 if (getScopeLineNumber() != getLineNumber())
1364 OS << " [scope " << getScopeLineNumber() << "]";
1366 if (isPrivate())
1367 OS << " [private]";
1368 else if (isProtected())
1369 OS << " [protected]";
1371 if (isLValueReference())
1372 OS << " [reference]";
1374 if (isRValueReference())
1375 OS << " [rvalue reference]";
1377 StringRef Res = getName();
1378 if (!Res.empty())
1379 OS << " [" << Res << ']';
1380 }
1382 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1383 StringRef Res = getName();
1384 if (!Res.empty())
1385 OS << " [" << Res << ']';
1387 OS << " [line " << getLineNumber() << ']';
1389 // TODO : Print context
1391 if (isLocalToUnit())
1392 OS << " [local]";
1394 if (isDefinition())
1395 OS << " [def]";
1396 }
1398 void DIVariable::printInternal(raw_ostream &OS) const {
1399 StringRef Res = getName();
1400 if (!Res.empty())
1401 OS << " [" << Res << ']';
1403 OS << " [line " << getLineNumber() << ']';
1404 }
1406 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1407 StringRef Name = getObjCPropertyName();
1408 if (!Name.empty())
1409 OS << " [" << Name << ']';
1411 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1412 << ']';
1413 }
1415 static 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 }
1434 void 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 }
1449 /// Specialize constructor to make sure it has the correct type.
1450 template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1451 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1452 }
1453 template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1454 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1455 }
1457 /// Specialize getFieldAs to handle fields that are references to DIScopes.
1458 template <>
1459 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1460 return DIScopeRef(getField(DbgNode, Elt));
1461 }
1462 /// Specialize getFieldAs to handle fields that are references to DITypes.
1463 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1464 return DITypeRef(getField(DbgNode, Elt));
1465 }
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.
1471 bool llvm::StripDebugInfo(Module &M) {
1473 bool Changed = false;
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 }
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 }
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 }
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 }
1516 return Changed;
1517 }
1519 /// Return Debug Info Metadata Version by checking module flags.
1520 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1521 Value *Val = M.getModuleFlag("Debug Info Version");
1522 if (!Val)
1523 return 0;
1524 return cast<ConstantInt>(Val)->getZExtValue();
1525 }