1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm::dwarf;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
42 DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
45 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
48 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
51 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
55 DIDescriptor::getStringField(unsigned Elt) const {
59 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
61 return MDS->getString();
66 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
70 if (Elt < DbgNode->getNumOperands())
71 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
72 return CI->getZExtValue();
77 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
79 return DIDescriptor();
81 if (Elt < DbgNode->getNumOperands())
83 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
84 return DIDescriptor();
87 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
91 if (Elt < DbgNode->getNumOperands())
92 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
96 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
100 if (Elt < DbgNode->getNumOperands())
101 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
105 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
114 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
118 if (Elt < DbgNode->getNumOperands()) {
119 MDNode *Node = const_cast<MDNode*>(DbgNode);
120 Node->replaceOperandWith(Elt, F);
124 unsigned DIVariable::getNumAddrElements() const {
125 if (getVersion() <= LLVMDebugVersion8)
126 return DbgNode->getNumOperands()-6;
127 if (getVersion() == LLVMDebugVersion9)
128 return DbgNode->getNumOperands()-7;
129 return DbgNode->getNumOperands()-8;
132 /// getInlinedAt - If this variable is inlined then return inline location.
133 MDNode *DIVariable::getInlinedAt() const {
134 if (getVersion() <= LLVMDebugVersion9)
136 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
139 //===----------------------------------------------------------------------===//
141 //===----------------------------------------------------------------------===//
143 /// isBasicType - Return true if the specified tag is legal for
145 bool DIDescriptor::isBasicType() const {
146 if (!DbgNode) return false;
148 case dwarf::DW_TAG_base_type:
149 case dwarf::DW_TAG_unspecified_type:
156 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
157 bool DIDescriptor::isDerivedType() const {
158 if (!DbgNode) return false;
160 case dwarf::DW_TAG_typedef:
161 case dwarf::DW_TAG_pointer_type:
162 case dwarf::DW_TAG_reference_type:
163 case dwarf::DW_TAG_rvalue_reference_type:
164 case dwarf::DW_TAG_const_type:
165 case dwarf::DW_TAG_volatile_type:
166 case dwarf::DW_TAG_restrict_type:
167 case dwarf::DW_TAG_member:
168 case dwarf::DW_TAG_inheritance:
169 case dwarf::DW_TAG_friend:
172 // CompositeTypes are currently modelled as DerivedTypes.
173 return isCompositeType();
177 /// isCompositeType - Return true if the specified tag is legal for
179 bool DIDescriptor::isCompositeType() const {
180 if (!DbgNode) return false;
182 case dwarf::DW_TAG_array_type:
183 case dwarf::DW_TAG_structure_type:
184 case dwarf::DW_TAG_union_type:
185 case dwarf::DW_TAG_enumeration_type:
186 case dwarf::DW_TAG_vector_type:
187 case dwarf::DW_TAG_subroutine_type:
188 case dwarf::DW_TAG_class_type:
195 /// isVariable - Return true if the specified tag is legal for DIVariable.
196 bool DIDescriptor::isVariable() const {
197 if (!DbgNode) return false;
199 case dwarf::DW_TAG_auto_variable:
200 case dwarf::DW_TAG_arg_variable:
201 case dwarf::DW_TAG_return_variable:
208 /// isType - Return true if the specified tag is legal for DIType.
209 bool DIDescriptor::isType() const {
210 return isBasicType() || isCompositeType() || isDerivedType();
213 /// isSubprogram - Return true if the specified tag is legal for
215 bool DIDescriptor::isSubprogram() const {
216 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
219 /// isGlobalVariable - Return true if the specified tag is legal for
220 /// DIGlobalVariable.
221 bool DIDescriptor::isGlobalVariable() const {
222 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
223 getTag() == dwarf::DW_TAG_constant);
226 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
227 bool DIDescriptor::isGlobal() const {
228 return isGlobalVariable();
231 /// isUnspecifiedParmeter - Return true if the specified tag is
232 /// DW_TAG_unspecified_parameters.
233 bool DIDescriptor::isUnspecifiedParameter() const {
234 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
237 /// isScope - Return true if the specified tag is one of the scope
239 bool DIDescriptor::isScope() const {
240 if (!DbgNode) return false;
242 case dwarf::DW_TAG_compile_unit:
243 case dwarf::DW_TAG_lexical_block:
244 case dwarf::DW_TAG_subprogram:
245 case dwarf::DW_TAG_namespace:
253 /// isTemplateTypeParameter - Return true if the specified tag is
254 /// DW_TAG_template_type_parameter.
255 bool DIDescriptor::isTemplateTypeParameter() const {
256 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
259 /// isTemplateValueParameter - Return true if the specified tag is
260 /// DW_TAG_template_value_parameter.
261 bool DIDescriptor::isTemplateValueParameter() const {
262 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
265 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
266 bool DIDescriptor::isCompileUnit() const {
267 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
270 /// isFile - Return true if the specified tag is DW_TAG_file_type.
271 bool DIDescriptor::isFile() const {
272 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
275 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
276 bool DIDescriptor::isNameSpace() const {
277 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
280 /// isLexicalBlockFile - Return true if the specified descriptor is a
281 /// lexical block with an extra file.
282 bool DIDescriptor::isLexicalBlockFile() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
284 (DbgNode->getNumOperands() == 3);
287 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
288 bool DIDescriptor::isLexicalBlock() const {
289 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
290 (DbgNode->getNumOperands() > 3);
293 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
294 bool DIDescriptor::isSubrange() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
298 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
299 bool DIDescriptor::isEnumerator() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
303 /// isObjCProperty - Return true if the specified tag is DW_TAG
304 bool DIDescriptor::isObjCProperty() const {
305 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
307 //===----------------------------------------------------------------------===//
308 // Simple Descriptor Constructors and other Methods
309 //===----------------------------------------------------------------------===//
311 DIType::DIType(const MDNode *N) : DIScope(N) {
313 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
318 unsigned DIArray::getNumElements() const {
321 return DbgNode->getNumOperands();
324 /// replaceAllUsesWith - Replace all uses of debug info referenced by
326 void DIType::replaceAllUsesWith(DIDescriptor &D) {
330 // Since we use a TrackingVH for the node, its easy for clients to manufacture
331 // legitimate situations where they want to replaceAllUsesWith() on something
332 // which, due to uniquing, has merged with the source. We shield clients from
333 // this detail by allowing a value to be replaced with replaceAllUsesWith()
336 MDNode *Node = const_cast<MDNode*>(DbgNode);
337 const MDNode *DN = D;
338 const Value *V = cast_or_null<Value>(DN);
339 Node->replaceAllUsesWith(const_cast<Value*>(V));
340 MDNode::deleteTemporary(Node);
344 /// replaceAllUsesWith - Replace all uses of debug info referenced by
346 void DIType::replaceAllUsesWith(MDNode *D) {
350 // Since we use a TrackingVH for the node, its easy for clients to manufacture
351 // legitimate situations where they want to replaceAllUsesWith() on something
352 // which, due to uniquing, has merged with the source. We shield clients from
353 // this detail by allowing a value to be replaced with replaceAllUsesWith()
356 MDNode *Node = const_cast<MDNode*>(DbgNode);
357 const MDNode *DN = D;
358 const Value *V = cast_or_null<Value>(DN);
359 Node->replaceAllUsesWith(const_cast<Value*>(V));
360 MDNode::deleteTemporary(Node);
364 /// isUnsignedDIType - Return true if type encoding is unsigned.
365 bool DIType::isUnsignedDIType() {
366 DIDerivedType DTy(DbgNode);
368 return DTy.getTypeDerivedFrom().isUnsignedDIType();
370 DIBasicType BTy(DbgNode);
372 unsigned Encoding = BTy.getEncoding();
373 if (Encoding == dwarf::DW_ATE_unsigned ||
374 Encoding == dwarf::DW_ATE_unsigned_char)
380 /// Verify - Verify that a compile unit is well formed.
381 bool DICompileUnit::Verify() const {
384 StringRef N = getFilename();
387 // It is possible that directory and produce string is empty.
391 /// Verify - Verify that an ObjC property is well formed.
392 bool DIObjCProperty::Verify() const {
395 unsigned Tag = getTag();
396 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
397 DIType Ty = getType();
398 if (!Ty.Verify()) return false;
400 // Don't worry about the rest of the strings for now.
404 /// Verify - Verify that a type descriptor is well formed.
405 bool DIType::Verify() const {
408 if (getContext() && !getContext().Verify())
410 unsigned Tag = getTag();
411 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
412 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
413 Tag != dwarf::DW_TAG_reference_type &&
414 Tag != dwarf::DW_TAG_rvalue_reference_type &&
415 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
416 Tag != dwarf::DW_TAG_array_type &&
417 Tag != dwarf::DW_TAG_enumeration_type &&
418 Tag != dwarf::DW_TAG_subroutine_type &&
419 getFilename().empty())
424 /// Verify - Verify that a basic type descriptor is well formed.
425 bool DIBasicType::Verify() const {
426 return isBasicType();
429 /// Verify - Verify that a derived type descriptor is well formed.
430 bool DIDerivedType::Verify() const {
431 return isDerivedType();
434 /// Verify - Verify that a composite type descriptor is well formed.
435 bool DICompositeType::Verify() const {
438 if (getContext() && !getContext().Verify())
444 /// Verify - Verify that a subprogram descriptor is well formed.
445 bool DISubprogram::Verify() const {
449 if (getContext() && !getContext().Verify())
452 DICompositeType Ty = getType();
458 /// Verify - Verify that a global variable descriptor is well formed.
459 bool DIGlobalVariable::Verify() const {
463 if (getDisplayName().empty())
466 if (getContext() && !getContext().Verify())
469 DIType Ty = getType();
473 if (!getGlobal() && !getConstant())
479 /// Verify - Verify that a variable descriptor is well formed.
480 bool DIVariable::Verify() const {
484 if (getContext() && !getContext().Verify())
487 DIType Ty = getType();
494 /// Verify - Verify that a location descriptor is well formed.
495 bool DILocation::Verify() const {
499 return DbgNode->getNumOperands() == 4;
502 /// Verify - Verify that a namespace descriptor is well formed.
503 bool DINameSpace::Verify() const {
506 if (getName().empty())
511 /// getOriginalTypeSize - If this type is derived from a base type then
512 /// return base type size.
513 uint64_t DIDerivedType::getOriginalTypeSize() const {
514 unsigned Tag = getTag();
516 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
517 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
518 Tag != dwarf::DW_TAG_restrict_type)
519 return getSizeInBits();
521 DIType BaseType = getTypeDerivedFrom();
523 // If this type is not derived from any type then take conservative approach.
524 if (!BaseType.isValid())
525 return getSizeInBits();
527 // If this is a derived type, go ahead and get the base type, unless it's a
528 // reference then it's just the size of the field. Pointer types have no need
529 // of this since they're a different type of qualification on the type.
530 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
531 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
532 return getSizeInBits();
534 if (BaseType.isDerivedType())
535 return DIDerivedType(BaseType).getOriginalTypeSize();
537 return BaseType.getSizeInBits();
540 /// getObjCProperty - Return property node, if this ivar is associated with one.
541 MDNode *DIDerivedType::getObjCProperty() const {
542 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
544 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
547 /// isInlinedFnArgument - Return true if this variable provides debugging
548 /// information for an inlined function arguments.
549 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
550 assert(CurFn && "Invalid function");
551 if (!getContext().isSubprogram())
553 // This variable is not inlined function argument if its scope
554 // does not describe current function.
555 return !DISubprogram(getContext()).describes(CurFn);
558 /// describes - Return true if this subprogram provides debugging
559 /// information for the function F.
560 bool DISubprogram::describes(const Function *F) {
561 assert(F && "Invalid function");
562 if (F == getFunction())
564 StringRef Name = getLinkageName();
567 if (F->getName() == Name)
572 unsigned DISubprogram::isOptimized() const {
573 assert (DbgNode && "Invalid subprogram descriptor!");
574 if (DbgNode->getNumOperands() == 16)
575 return getUnsignedField(15);
579 MDNode *DISubprogram::getVariablesNodes() const {
580 if (!DbgNode || DbgNode->getNumOperands() <= 19)
582 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
583 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
587 DIArray DISubprogram::getVariables() const {
588 if (!DbgNode || DbgNode->getNumOperands() <= 19)
590 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
591 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
596 StringRef DIScope::getFilename() const {
599 if (isLexicalBlockFile())
600 return DILexicalBlockFile(DbgNode).getFilename();
601 if (isLexicalBlock())
602 return DILexicalBlock(DbgNode).getFilename();
604 return DISubprogram(DbgNode).getFilename();
606 return DICompileUnit(DbgNode).getFilename();
608 return DINameSpace(DbgNode).getFilename();
610 return DIType(DbgNode).getFilename();
612 return DIFile(DbgNode).getFilename();
613 llvm_unreachable("Invalid DIScope!");
616 StringRef DIScope::getDirectory() const {
619 if (isLexicalBlockFile())
620 return DILexicalBlockFile(DbgNode).getDirectory();
621 if (isLexicalBlock())
622 return DILexicalBlock(DbgNode).getDirectory();
624 return DISubprogram(DbgNode).getDirectory();
626 return DICompileUnit(DbgNode).getDirectory();
628 return DINameSpace(DbgNode).getDirectory();
630 return DIType(DbgNode).getDirectory();
632 return DIFile(DbgNode).getDirectory();
633 llvm_unreachable("Invalid DIScope!");
636 DIArray DICompileUnit::getEnumTypes() const {
637 if (!DbgNode || DbgNode->getNumOperands() < 14)
640 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
641 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
646 DIArray DICompileUnit::getRetainedTypes() const {
647 if (!DbgNode || DbgNode->getNumOperands() < 14)
650 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
651 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
656 DIArray DICompileUnit::getSubprograms() const {
657 if (!DbgNode || DbgNode->getNumOperands() < 14)
660 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
661 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
667 DIArray DICompileUnit::getGlobalVariables() const {
668 if (!DbgNode || DbgNode->getNumOperands() < 14)
671 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
672 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
677 /// fixupObjcLikeName - Replace contains special characters used
678 /// in a typical Objective-C names with '.' in a given string.
679 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
680 bool isObjCLike = false;
681 for (size_t i = 0, e = Str.size(); i < e; ++i) {
686 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
687 C == '+' || C == '(' || C == ')'))
694 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
695 /// suitable to hold function specific information.
696 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
697 SmallString<32> Name = StringRef("llvm.dbg.lv.");
698 StringRef FName = "fn";
699 if (Fn.getFunction())
700 FName = Fn.getFunction()->getName();
702 FName = Fn.getName();
704 if (FName.startswith(StringRef(&One, 1)))
705 FName = FName.substr(1);
706 fixupObjcLikeName(FName, Name);
707 return M.getNamedMetadata(Name.str());
710 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
711 /// to hold function specific information.
712 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
713 SmallString<32> Name = StringRef("llvm.dbg.lv.");
714 StringRef FName = "fn";
715 if (Fn.getFunction())
716 FName = Fn.getFunction()->getName();
718 FName = Fn.getName();
720 if (FName.startswith(StringRef(&One, 1)))
721 FName = FName.substr(1);
722 fixupObjcLikeName(FName, Name);
724 return M.getOrInsertNamedMetadata(Name.str());
727 /// createInlinedVariable - Create a new inlined variable based on current
729 /// @param DV Current Variable.
730 /// @param InlinedScope Location at current variable is inlined.
731 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
732 LLVMContext &VMContext) {
733 SmallVector<Value *, 16> Elts;
734 // Insert inlined scope as 7th element.
735 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
736 i == 7 ? Elts.push_back(InlinedScope) :
737 Elts.push_back(DV->getOperand(i));
738 return DIVariable(MDNode::get(VMContext, Elts));
741 /// cleanseInlinedVariable - Remove inlined scope from the variable.
742 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
743 SmallVector<Value *, 16> Elts;
744 // Insert inlined scope as 7th element.
745 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
747 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
748 Elts.push_back(DV->getOperand(i));
749 return DIVariable(MDNode::get(VMContext, Elts));
752 /// getDISubprogram - Find subprogram that is enclosing this scope.
753 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
754 DIDescriptor D(Scope);
755 if (D.isSubprogram())
756 return DISubprogram(Scope);
758 if (D.isLexicalBlockFile())
759 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
761 if (D.isLexicalBlock())
762 return getDISubprogram(DILexicalBlock(Scope).getContext());
764 return DISubprogram();
767 /// getDICompositeType - Find underlying composite type.
768 DICompositeType llvm::getDICompositeType(DIType T) {
769 if (T.isCompositeType())
770 return DICompositeType(T);
772 if (T.isDerivedType())
773 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
775 return DICompositeType();
778 /// isSubprogramContext - Return true if Context is either a subprogram
779 /// or another context nested inside a subprogram.
780 bool llvm::isSubprogramContext(const MDNode *Context) {
783 DIDescriptor D(Context);
784 if (D.isSubprogram())
787 return isSubprogramContext(DIType(Context).getContext());
791 //===----------------------------------------------------------------------===//
792 // DebugInfoFinder implementations.
793 //===----------------------------------------------------------------------===//
795 /// processModule - Process entire module and collect debug info.
796 void DebugInfoFinder::processModule(Module &M) {
797 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
798 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
799 DICompileUnit CU(CU_Nodes->getOperand(i));
801 if (CU.getVersion() > LLVMDebugVersion10) {
802 DIArray GVs = CU.getGlobalVariables();
803 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
804 DIGlobalVariable DIG(GVs.getElement(i));
805 if (addGlobalVariable(DIG))
806 processType(DIG.getType());
808 DIArray SPs = CU.getSubprograms();
809 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
810 processSubprogram(DISubprogram(SPs.getElement(i)));
811 DIArray EnumTypes = CU.getEnumTypes();
812 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
813 processType(DIType(EnumTypes.getElement(i)));
814 DIArray RetainedTypes = CU.getRetainedTypes();
815 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
816 processType(DIType(RetainedTypes.getElement(i)));
822 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
823 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
824 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
826 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
829 DebugLoc Loc = BI->getDebugLoc();
833 LLVMContext &Ctx = BI->getContext();
834 DIDescriptor Scope(Loc.getScope(Ctx));
836 if (Scope.isCompileUnit())
837 addCompileUnit(DICompileUnit(Scope));
838 else if (Scope.isSubprogram())
839 processSubprogram(DISubprogram(Scope));
840 else if (Scope.isLexicalBlockFile()) {
841 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
842 processLexicalBlock(DILexicalBlock(DBF.getScope()));
844 else if (Scope.isLexicalBlock())
845 processLexicalBlock(DILexicalBlock(Scope));
847 if (MDNode *IA = Loc.getInlinedAt(Ctx))
848 processLocation(DILocation(IA));
851 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
852 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
853 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
854 if (addGlobalVariable(DIG)) {
855 if (DIG.getVersion() <= LLVMDebugVersion10)
856 addCompileUnit(DIG.getCompileUnit());
857 processType(DIG.getType());
862 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
863 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
864 processSubprogram(DISubprogram(NMD->getOperand(i)));
867 /// processLocation - Process DILocation.
868 void DebugInfoFinder::processLocation(DILocation Loc) {
869 if (!Loc.Verify()) return;
870 DIDescriptor S(Loc.getScope());
871 if (S.isCompileUnit())
872 addCompileUnit(DICompileUnit(S));
873 else if (S.isSubprogram())
874 processSubprogram(DISubprogram(S));
875 else if (S.isLexicalBlock())
876 processLexicalBlock(DILexicalBlock(S));
877 else if (S.isLexicalBlockFile()) {
878 DILexicalBlockFile DBF = DILexicalBlockFile(S);
879 processLexicalBlock(DILexicalBlock(DBF.getScope()));
881 processLocation(Loc.getOrigLocation());
884 /// processType - Process DIType.
885 void DebugInfoFinder::processType(DIType DT) {
888 if (DT.getVersion() <= LLVMDebugVersion10)
889 addCompileUnit(DT.getCompileUnit());
890 if (DT.isCompositeType()) {
891 DICompositeType DCT(DT);
892 processType(DCT.getTypeDerivedFrom());
893 DIArray DA = DCT.getTypeArray();
894 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
895 DIDescriptor D = DA.getElement(i);
897 processType(DIType(D));
898 else if (D.isSubprogram())
899 processSubprogram(DISubprogram(D));
901 } else if (DT.isDerivedType()) {
902 DIDerivedType DDT(DT);
903 processType(DDT.getTypeDerivedFrom());
907 /// processLexicalBlock
908 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
909 DIScope Context = LB.getContext();
910 if (Context.isLexicalBlock())
911 return processLexicalBlock(DILexicalBlock(Context));
912 else if (Context.isLexicalBlockFile()) {
913 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
914 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
917 return processSubprogram(DISubprogram(Context));
920 /// processSubprogram - Process DISubprogram.
921 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
922 if (!addSubprogram(SP))
924 if (SP.getVersion() <= LLVMDebugVersion10)
925 addCompileUnit(SP.getCompileUnit());
926 processType(SP.getType());
929 /// processDeclare - Process DbgDeclareInst.
930 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
931 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
935 if (!DV.isVariable())
938 if (!NodesSeen.insert(DV))
940 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
941 addCompileUnit(DIVariable(N).getCompileUnit());
942 processType(DIVariable(N).getType());
945 /// addType - Add type into Tys.
946 bool DebugInfoFinder::addType(DIType DT) {
950 if (!NodesSeen.insert(DT))
957 /// addCompileUnit - Add compile unit into CUs.
958 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
962 if (!NodesSeen.insert(CU))
969 /// addGlobalVariable - Add global variable into GVs.
970 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
971 if (!DIDescriptor(DIG).isGlobalVariable())
974 if (!NodesSeen.insert(DIG))
981 // addSubprogram - Add subprgoram into SPs.
982 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
983 if (!DIDescriptor(SP).isSubprogram())
986 if (!NodesSeen.insert(SP))
993 //===----------------------------------------------------------------------===//
994 // DIDescriptor: dump routines for all descriptors.
995 //===----------------------------------------------------------------------===//
997 /// dump - Print descriptor to dbgs() with a newline.
998 void DIDescriptor::dump() const {
999 print(dbgs()); dbgs() << '\n';
1002 /// print - Print descriptor.
1003 void DIDescriptor::print(raw_ostream &OS) const {
1004 if (!DbgNode) return;
1006 if (const char *Tag = dwarf::TagString(getTag()))
1007 OS << "[ " << Tag << " ]";
1009 if (this->isSubrange()) {
1010 DISubrange(DbgNode).printInternal(OS);
1011 } else if (this->isCompileUnit()) {
1012 DICompileUnit(DbgNode).printInternal(OS);
1013 } else if (this->isFile()) {
1014 DIFile(DbgNode).printInternal(OS);
1015 } else if (this->isEnumerator()) {
1016 DIEnumerator(DbgNode).printInternal(OS);
1017 } else if (this->isBasicType()) {
1018 DIType(DbgNode).printInternal(OS);
1019 } else if (this->isDerivedType()) {
1020 DIDerivedType(DbgNode).printInternal(OS);
1021 } else if (this->isCompositeType()) {
1022 DICompositeType(DbgNode).printInternal(OS);
1023 } else if (this->isSubprogram()) {
1024 DISubprogram(DbgNode).printInternal(OS);
1025 } else if (this->isGlobalVariable()) {
1026 DIGlobalVariable(DbgNode).printInternal(OS);
1027 } else if (this->isVariable()) {
1028 DIVariable(DbgNode).printInternal(OS);
1029 } else if (this->isObjCProperty()) {
1030 DIObjCProperty(DbgNode).printInternal(OS);
1031 } else if (this->isScope()) {
1032 DIScope(DbgNode).printInternal(OS);
1036 void DISubrange::printInternal(raw_ostream &OS) const {
1037 OS << " [" << getLo() << ", " << getHi() << ']';
1040 void DIScope::printInternal(raw_ostream &OS) const {
1041 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1044 void DICompileUnit::printInternal(raw_ostream &OS) const {
1045 DIScope::printInternal(OS);
1046 if (unsigned Lang = getLanguage())
1047 OS << " [" << dwarf::LanguageString(Lang) << ']';
1050 void DIEnumerator::printInternal(raw_ostream &OS) const {
1051 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1054 void DIType::printInternal(raw_ostream &OS) const {
1055 if (!DbgNode) return;
1057 StringRef Res = getName();
1059 OS << " [" << Res << "]";
1061 // TODO: Print context?
1063 OS << " [line " << getLineNumber()
1064 << ", size " << getSizeInBits()
1065 << ", align " << getAlignInBits()
1066 << ", offset " << getOffsetInBits();
1068 if (const char *Enc =
1069 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1070 OS << ", enc " << Enc;
1075 else if (isProtected())
1076 OS << " [protected]";
1078 if (isForwardDecl())
1082 void DIDerivedType::printInternal(raw_ostream &OS) const {
1083 DIType::printInternal(OS);
1084 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1087 void DICompositeType::printInternal(raw_ostream &OS) const {
1088 DIType::printInternal(OS);
1089 DIArray A = getTypeArray();
1090 OS << " [" << A.getNumElements() << " elements]";
1093 void DISubprogram::printInternal(raw_ostream &OS) const {
1094 // TODO : Print context
1095 OS << " [line " << getLineNumber() << ']';
1097 if (isLocalToUnit())
1103 if (getScopeLineNumber() != getLineNumber())
1104 OS << " [scope " << getScopeLineNumber() << "]";
1106 StringRef Res = getName();
1108 OS << " [" << Res << ']';
1111 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1112 StringRef Res = getName();
1114 OS << " [" << Res << ']';
1116 OS << " [line " << getLineNumber() << ']';
1118 // TODO : Print context
1120 if (isLocalToUnit())
1127 void DIVariable::printInternal(raw_ostream &OS) const {
1128 StringRef Res = getName();
1130 OS << " [" << Res << ']';
1132 OS << " [line " << getLineNumber() << ']';
1135 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1136 StringRef Name = getObjCPropertyName();
1138 OS << " [" << Name << ']';
1140 OS << " [line " << getLineNumber()
1141 << ", properties " << getUnsignedField(6) << ']';
1144 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1145 const LLVMContext &Ctx) {
1146 if (!DL.isUnknown()) { // Print source line info.
1147 DIScope Scope(DL.getScope(Ctx));
1148 // Omit the directory, because it's likely to be long and uninteresting.
1150 CommentOS << Scope.getFilename();
1152 CommentOS << "<unknown>";
1153 CommentOS << ':' << DL.getLine();
1154 if (DL.getCol() != 0)
1155 CommentOS << ':' << DL.getCol();
1156 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1157 if (!InlinedAtDL.isUnknown()) {
1158 CommentOS << " @[ ";
1159 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1165 void DIVariable::printExtendedName(raw_ostream &OS) const {
1166 const LLVMContext &Ctx = DbgNode->getContext();
1167 StringRef Res = getName();
1169 OS << Res << "," << getLineNumber();
1170 if (MDNode *InlinedAt = getInlinedAt()) {
1171 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1172 if (!InlinedAtDL.isUnknown()) {
1174 printDebugLoc(InlinedAtDL, OS, Ctx);