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 unsigned DIVariable::getNumAddrElements() const {
115 if (getVersion() <= LLVMDebugVersion8)
116 return DbgNode->getNumOperands()-6;
117 if (getVersion() == LLVMDebugVersion9)
118 return DbgNode->getNumOperands()-7;
119 return DbgNode->getNumOperands()-8;
122 /// getInlinedAt - If this variable is inlined then return inline location.
123 MDNode *DIVariable::getInlinedAt() const {
124 if (getVersion() <= LLVMDebugVersion9)
126 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
129 //===----------------------------------------------------------------------===//
131 //===----------------------------------------------------------------------===//
133 /// isBasicType - Return true if the specified tag is legal for
135 bool DIDescriptor::isBasicType() const {
136 if (!DbgNode) return false;
138 case dwarf::DW_TAG_base_type:
139 case dwarf::DW_TAG_unspecified_type:
146 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
147 bool DIDescriptor::isDerivedType() const {
148 if (!DbgNode) return false;
150 case dwarf::DW_TAG_typedef:
151 case dwarf::DW_TAG_pointer_type:
152 case dwarf::DW_TAG_reference_type:
153 case dwarf::DW_TAG_rvalue_reference_type:
154 case dwarf::DW_TAG_const_type:
155 case dwarf::DW_TAG_volatile_type:
156 case dwarf::DW_TAG_restrict_type:
157 case dwarf::DW_TAG_member:
158 case dwarf::DW_TAG_inheritance:
159 case dwarf::DW_TAG_friend:
162 // CompositeTypes are currently modelled as DerivedTypes.
163 return isCompositeType();
167 /// isCompositeType - Return true if the specified tag is legal for
169 bool DIDescriptor::isCompositeType() const {
170 if (!DbgNode) return false;
172 case dwarf::DW_TAG_array_type:
173 case dwarf::DW_TAG_structure_type:
174 case dwarf::DW_TAG_union_type:
175 case dwarf::DW_TAG_enumeration_type:
176 case dwarf::DW_TAG_vector_type:
177 case dwarf::DW_TAG_subroutine_type:
178 case dwarf::DW_TAG_class_type:
185 /// isVariable - Return true if the specified tag is legal for DIVariable.
186 bool DIDescriptor::isVariable() const {
187 if (!DbgNode) return false;
189 case dwarf::DW_TAG_auto_variable:
190 case dwarf::DW_TAG_arg_variable:
191 case dwarf::DW_TAG_return_variable:
198 /// isType - Return true if the specified tag is legal for DIType.
199 bool DIDescriptor::isType() const {
200 return isBasicType() || isCompositeType() || isDerivedType();
203 /// isSubprogram - Return true if the specified tag is legal for
205 bool DIDescriptor::isSubprogram() const {
206 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
209 /// isGlobalVariable - Return true if the specified tag is legal for
210 /// DIGlobalVariable.
211 bool DIDescriptor::isGlobalVariable() const {
212 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
213 getTag() == dwarf::DW_TAG_constant);
216 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
217 bool DIDescriptor::isGlobal() const {
218 return isGlobalVariable();
221 /// isUnspecifiedParmeter - Return true if the specified tag is
222 /// DW_TAG_unspecified_parameters.
223 bool DIDescriptor::isUnspecifiedParameter() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
227 /// isScope - Return true if the specified tag is one of the scope
229 bool DIDescriptor::isScope() const {
230 if (!DbgNode) return false;
232 case dwarf::DW_TAG_compile_unit:
233 case dwarf::DW_TAG_lexical_block:
234 case dwarf::DW_TAG_subprogram:
235 case dwarf::DW_TAG_namespace:
243 /// isTemplateTypeParameter - Return true if the specified tag is
244 /// DW_TAG_template_type_parameter.
245 bool DIDescriptor::isTemplateTypeParameter() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
249 /// isTemplateValueParameter - Return true if the specified tag is
250 /// DW_TAG_template_value_parameter.
251 bool DIDescriptor::isTemplateValueParameter() const {
252 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
255 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
256 bool DIDescriptor::isCompileUnit() const {
257 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
260 /// isFile - Return true if the specified tag is DW_TAG_file_type.
261 bool DIDescriptor::isFile() const {
262 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
265 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
266 bool DIDescriptor::isNameSpace() const {
267 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
270 /// isLexicalBlockFile - Return true if the specified descriptor is a
271 /// lexical block with an extra file.
272 bool DIDescriptor::isLexicalBlockFile() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
274 (DbgNode->getNumOperands() == 3);
277 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
278 bool DIDescriptor::isLexicalBlock() const {
279 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
280 (DbgNode->getNumOperands() > 3);
283 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
284 bool DIDescriptor::isSubrange() const {
285 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
288 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
289 bool DIDescriptor::isEnumerator() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
293 /// isObjCProperty - Return true if the specified tag is DW_TAG
294 bool DIDescriptor::isObjCProperty() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
297 //===----------------------------------------------------------------------===//
298 // Simple Descriptor Constructors and other Methods
299 //===----------------------------------------------------------------------===//
301 DIType::DIType(const MDNode *N) : DIScope(N) {
303 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
308 unsigned DIArray::getNumElements() const {
311 return DbgNode->getNumOperands();
314 /// replaceAllUsesWith - Replace all uses of debug info referenced by
316 void DIType::replaceAllUsesWith(DIDescriptor &D) {
320 // Since we use a TrackingVH for the node, its easy for clients to manufacture
321 // legitimate situations where they want to replaceAllUsesWith() on something
322 // which, due to uniquing, has merged with the source. We shield clients from
323 // this detail by allowing a value to be replaced with replaceAllUsesWith()
326 MDNode *Node = const_cast<MDNode*>(DbgNode);
327 const MDNode *DN = D;
328 const Value *V = cast_or_null<Value>(DN);
329 Node->replaceAllUsesWith(const_cast<Value*>(V));
330 MDNode::deleteTemporary(Node);
334 /// replaceAllUsesWith - Replace all uses of debug info referenced by
336 void DIType::replaceAllUsesWith(MDNode *D) {
340 // Since we use a TrackingVH for the node, its easy for clients to manufacture
341 // legitimate situations where they want to replaceAllUsesWith() on something
342 // which, due to uniquing, has merged with the source. We shield clients from
343 // this detail by allowing a value to be replaced with replaceAllUsesWith()
346 MDNode *Node = const_cast<MDNode*>(DbgNode);
347 const MDNode *DN = D;
348 const Value *V = cast_or_null<Value>(DN);
349 Node->replaceAllUsesWith(const_cast<Value*>(V));
350 MDNode::deleteTemporary(Node);
354 /// isUnsignedDIType - Return true if type encoding is unsigned.
355 bool DIType::isUnsignedDIType() {
356 DIDerivedType DTy(DbgNode);
358 return DTy.getTypeDerivedFrom().isUnsignedDIType();
360 DIBasicType BTy(DbgNode);
362 unsigned Encoding = BTy.getEncoding();
363 if (Encoding == dwarf::DW_ATE_unsigned ||
364 Encoding == dwarf::DW_ATE_unsigned_char)
370 /// Verify - Verify that a compile unit is well formed.
371 bool DICompileUnit::Verify() const {
374 StringRef N = getFilename();
377 // It is possible that directory and produce string is empty.
381 /// Verify - Verify that an ObjC property is well formed.
382 bool DIObjCProperty::Verify() const {
385 unsigned Tag = getTag();
386 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
387 DIType Ty = getType();
388 if (!Ty.Verify()) return false;
390 // Don't worry about the rest of the strings for now.
394 /// Verify - Verify that a type descriptor is well formed.
395 bool DIType::Verify() const {
398 if (getContext() && !getContext().Verify())
400 unsigned Tag = getTag();
401 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
402 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
403 Tag != dwarf::DW_TAG_reference_type &&
404 Tag != dwarf::DW_TAG_rvalue_reference_type &&
405 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
406 Tag != dwarf::DW_TAG_array_type &&
407 Tag != dwarf::DW_TAG_enumeration_type &&
408 Tag != dwarf::DW_TAG_subroutine_type &&
409 getFilename().empty())
414 /// Verify - Verify that a basic type descriptor is well formed.
415 bool DIBasicType::Verify() const {
416 return isBasicType();
419 /// Verify - Verify that a derived type descriptor is well formed.
420 bool DIDerivedType::Verify() const {
421 return isDerivedType();
424 /// Verify - Verify that a composite type descriptor is well formed.
425 bool DICompositeType::Verify() const {
428 if (getContext() && !getContext().Verify())
434 /// Verify - Verify that a subprogram descriptor is well formed.
435 bool DISubprogram::Verify() const {
439 if (getContext() && !getContext().Verify())
442 DICompositeType Ty = getType();
448 /// Verify - Verify that a global variable descriptor is well formed.
449 bool DIGlobalVariable::Verify() const {
453 if (getDisplayName().empty())
456 if (getContext() && !getContext().Verify())
459 DIType Ty = getType();
463 if (!getGlobal() && !getConstant())
469 /// Verify - Verify that a variable descriptor is well formed.
470 bool DIVariable::Verify() const {
474 if (getContext() && !getContext().Verify())
477 DIType Ty = getType();
484 /// Verify - Verify that a location descriptor is well formed.
485 bool DILocation::Verify() const {
489 return DbgNode->getNumOperands() == 4;
492 /// Verify - Verify that a namespace descriptor is well formed.
493 bool DINameSpace::Verify() const {
496 if (getName().empty())
501 /// getOriginalTypeSize - If this type is derived from a base type then
502 /// return base type size.
503 uint64_t DIDerivedType::getOriginalTypeSize() const {
504 unsigned Tag = getTag();
506 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
507 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
508 Tag != dwarf::DW_TAG_restrict_type)
509 return getSizeInBits();
511 DIType BaseType = getTypeDerivedFrom();
513 // If this type is not derived from any type then take conservative approach.
514 if (!BaseType.isValid())
515 return getSizeInBits();
517 // If this is a derived type, go ahead and get the base type, unless it's a
518 // reference then it's just the size of the field. Pointer types have no need
519 // of this since they're a different type of qualification on the type.
520 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
521 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
522 return getSizeInBits();
524 if (BaseType.isDerivedType())
525 return DIDerivedType(BaseType).getOriginalTypeSize();
527 return BaseType.getSizeInBits();
530 /// getObjCProperty - Return property node, if this ivar is associated with one.
531 MDNode *DIDerivedType::getObjCProperty() const {
532 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
534 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
537 /// isInlinedFnArgument - Return true if this variable provides debugging
538 /// information for an inlined function arguments.
539 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
540 assert(CurFn && "Invalid function");
541 if (!getContext().isSubprogram())
543 // This variable is not inlined function argument if its scope
544 // does not describe current function.
545 return !DISubprogram(getContext()).describes(CurFn);
548 /// describes - Return true if this subprogram provides debugging
549 /// information for the function F.
550 bool DISubprogram::describes(const Function *F) {
551 assert(F && "Invalid function");
552 if (F == getFunction())
554 StringRef Name = getLinkageName();
557 if (F->getName() == Name)
562 unsigned DISubprogram::isOptimized() const {
563 assert (DbgNode && "Invalid subprogram descriptor!");
564 if (DbgNode->getNumOperands() == 16)
565 return getUnsignedField(15);
569 MDNode *DISubprogram::getVariablesNodes() const {
570 if (!DbgNode || DbgNode->getNumOperands() <= 19)
572 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
573 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
577 DIArray DISubprogram::getVariables() const {
578 if (!DbgNode || DbgNode->getNumOperands() <= 19)
580 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
581 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
586 StringRef DIScope::getFilename() const {
589 if (isLexicalBlockFile())
590 return DILexicalBlockFile(DbgNode).getFilename();
591 if (isLexicalBlock())
592 return DILexicalBlock(DbgNode).getFilename();
594 return DISubprogram(DbgNode).getFilename();
596 return DICompileUnit(DbgNode).getFilename();
598 return DINameSpace(DbgNode).getFilename();
600 return DIType(DbgNode).getFilename();
602 return DIFile(DbgNode).getFilename();
603 llvm_unreachable("Invalid DIScope!");
606 StringRef DIScope::getDirectory() const {
609 if (isLexicalBlockFile())
610 return DILexicalBlockFile(DbgNode).getDirectory();
611 if (isLexicalBlock())
612 return DILexicalBlock(DbgNode).getDirectory();
614 return DISubprogram(DbgNode).getDirectory();
616 return DICompileUnit(DbgNode).getDirectory();
618 return DINameSpace(DbgNode).getDirectory();
620 return DIType(DbgNode).getDirectory();
622 return DIFile(DbgNode).getDirectory();
623 llvm_unreachable("Invalid DIScope!");
626 DIArray DICompileUnit::getEnumTypes() const {
627 if (!DbgNode || DbgNode->getNumOperands() < 14)
630 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
631 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
636 DIArray DICompileUnit::getRetainedTypes() const {
637 if (!DbgNode || DbgNode->getNumOperands() < 14)
640 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
641 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
646 DIArray DICompileUnit::getSubprograms() const {
647 if (!DbgNode || DbgNode->getNumOperands() < 14)
650 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
651 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
657 DIArray DICompileUnit::getGlobalVariables() const {
658 if (!DbgNode || DbgNode->getNumOperands() < 14)
661 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
662 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
667 /// fixupObjcLikeName - Replace contains special characters used
668 /// in a typical Objective-C names with '.' in a given string.
669 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
670 bool isObjCLike = false;
671 for (size_t i = 0, e = Str.size(); i < e; ++i) {
676 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
677 C == '+' || C == '(' || C == ')'))
684 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
685 /// suitable to hold function specific information.
686 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
687 SmallString<32> Name = StringRef("llvm.dbg.lv.");
688 StringRef FName = "fn";
689 if (Fn.getFunction())
690 FName = Fn.getFunction()->getName();
692 FName = Fn.getName();
694 if (FName.startswith(StringRef(&One, 1)))
695 FName = FName.substr(1);
696 fixupObjcLikeName(FName, Name);
697 return M.getNamedMetadata(Name.str());
700 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
701 /// to hold function specific information.
702 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
703 SmallString<32> Name = StringRef("llvm.dbg.lv.");
704 StringRef FName = "fn";
705 if (Fn.getFunction())
706 FName = Fn.getFunction()->getName();
708 FName = Fn.getName();
710 if (FName.startswith(StringRef(&One, 1)))
711 FName = FName.substr(1);
712 fixupObjcLikeName(FName, Name);
714 return M.getOrInsertNamedMetadata(Name.str());
717 /// createInlinedVariable - Create a new inlined variable based on current
719 /// @param DV Current Variable.
720 /// @param InlinedScope Location at current variable is inlined.
721 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
722 LLVMContext &VMContext) {
723 SmallVector<Value *, 16> Elts;
724 // Insert inlined scope as 7th element.
725 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
726 i == 7 ? Elts.push_back(InlinedScope) :
727 Elts.push_back(DV->getOperand(i));
728 return DIVariable(MDNode::get(VMContext, Elts));
731 /// cleanseInlinedVariable - Remove inlined scope from the variable.
732 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, 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)
737 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
738 Elts.push_back(DV->getOperand(i));
739 return DIVariable(MDNode::get(VMContext, Elts));
742 /// getDISubprogram - Find subprogram that is enclosing this scope.
743 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
744 DIDescriptor D(Scope);
745 if (D.isSubprogram())
746 return DISubprogram(Scope);
748 if (D.isLexicalBlockFile())
749 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
751 if (D.isLexicalBlock())
752 return getDISubprogram(DILexicalBlock(Scope).getContext());
754 return DISubprogram();
757 /// getDICompositeType - Find underlying composite type.
758 DICompositeType llvm::getDICompositeType(DIType T) {
759 if (T.isCompositeType())
760 return DICompositeType(T);
762 if (T.isDerivedType())
763 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
765 return DICompositeType();
768 /// isSubprogramContext - Return true if Context is either a subprogram
769 /// or another context nested inside a subprogram.
770 bool llvm::isSubprogramContext(const MDNode *Context) {
773 DIDescriptor D(Context);
774 if (D.isSubprogram())
777 return isSubprogramContext(DIType(Context).getContext());
781 //===----------------------------------------------------------------------===//
782 // DebugInfoFinder implementations.
783 //===----------------------------------------------------------------------===//
785 /// processModule - Process entire module and collect debug info.
786 void DebugInfoFinder::processModule(Module &M) {
787 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
788 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
789 DICompileUnit CU(CU_Nodes->getOperand(i));
791 if (CU.getVersion() > LLVMDebugVersion10) {
792 DIArray GVs = CU.getGlobalVariables();
793 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
794 DIGlobalVariable DIG(GVs.getElement(i));
795 if (addGlobalVariable(DIG))
796 processType(DIG.getType());
798 DIArray SPs = CU.getSubprograms();
799 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
800 processSubprogram(DISubprogram(SPs.getElement(i)));
801 DIArray EnumTypes = CU.getEnumTypes();
802 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
803 processType(DIType(EnumTypes.getElement(i)));
804 DIArray RetainedTypes = CU.getRetainedTypes();
805 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
806 processType(DIType(RetainedTypes.getElement(i)));
812 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
813 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
814 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
816 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
819 DebugLoc Loc = BI->getDebugLoc();
823 LLVMContext &Ctx = BI->getContext();
824 DIDescriptor Scope(Loc.getScope(Ctx));
826 if (Scope.isCompileUnit())
827 addCompileUnit(DICompileUnit(Scope));
828 else if (Scope.isSubprogram())
829 processSubprogram(DISubprogram(Scope));
830 else if (Scope.isLexicalBlockFile()) {
831 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
832 processLexicalBlock(DILexicalBlock(DBF.getScope()));
834 else if (Scope.isLexicalBlock())
835 processLexicalBlock(DILexicalBlock(Scope));
837 if (MDNode *IA = Loc.getInlinedAt(Ctx))
838 processLocation(DILocation(IA));
841 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
842 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
843 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
844 if (addGlobalVariable(DIG)) {
845 if (DIG.getVersion() <= LLVMDebugVersion10)
846 addCompileUnit(DIG.getCompileUnit());
847 processType(DIG.getType());
852 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
853 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
854 processSubprogram(DISubprogram(NMD->getOperand(i)));
857 /// processLocation - Process DILocation.
858 void DebugInfoFinder::processLocation(DILocation Loc) {
859 if (!Loc.Verify()) return;
860 DIDescriptor S(Loc.getScope());
861 if (S.isCompileUnit())
862 addCompileUnit(DICompileUnit(S));
863 else if (S.isSubprogram())
864 processSubprogram(DISubprogram(S));
865 else if (S.isLexicalBlock())
866 processLexicalBlock(DILexicalBlock(S));
867 else if (S.isLexicalBlockFile()) {
868 DILexicalBlockFile DBF = DILexicalBlockFile(S);
869 processLexicalBlock(DILexicalBlock(DBF.getScope()));
871 processLocation(Loc.getOrigLocation());
874 /// processType - Process DIType.
875 void DebugInfoFinder::processType(DIType DT) {
878 if (DT.getVersion() <= LLVMDebugVersion10)
879 addCompileUnit(DT.getCompileUnit());
880 if (DT.isCompositeType()) {
881 DICompositeType DCT(DT);
882 processType(DCT.getTypeDerivedFrom());
883 DIArray DA = DCT.getTypeArray();
884 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
885 DIDescriptor D = DA.getElement(i);
887 processType(DIType(D));
888 else if (D.isSubprogram())
889 processSubprogram(DISubprogram(D));
891 } else if (DT.isDerivedType()) {
892 DIDerivedType DDT(DT);
893 processType(DDT.getTypeDerivedFrom());
897 /// processLexicalBlock
898 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
899 DIScope Context = LB.getContext();
900 if (Context.isLexicalBlock())
901 return processLexicalBlock(DILexicalBlock(Context));
902 else if (Context.isLexicalBlockFile()) {
903 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
904 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
907 return processSubprogram(DISubprogram(Context));
910 /// processSubprogram - Process DISubprogram.
911 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
912 if (!addSubprogram(SP))
914 if (SP.getVersion() <= LLVMDebugVersion10)
915 addCompileUnit(SP.getCompileUnit());
916 processType(SP.getType());
919 /// processDeclare - Process DbgDeclareInst.
920 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
921 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
925 if (!DV.isVariable())
928 if (!NodesSeen.insert(DV))
930 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
931 addCompileUnit(DIVariable(N).getCompileUnit());
932 processType(DIVariable(N).getType());
935 /// addType - Add type into Tys.
936 bool DebugInfoFinder::addType(DIType DT) {
940 if (!NodesSeen.insert(DT))
947 /// addCompileUnit - Add compile unit into CUs.
948 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
952 if (!NodesSeen.insert(CU))
959 /// addGlobalVariable - Add global variable into GVs.
960 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
961 if (!DIDescriptor(DIG).isGlobalVariable())
964 if (!NodesSeen.insert(DIG))
971 // addSubprogram - Add subprgoram into SPs.
972 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
973 if (!DIDescriptor(SP).isSubprogram())
976 if (!NodesSeen.insert(SP))
983 //===----------------------------------------------------------------------===//
984 // DIDescriptor: dump routines for all descriptors.
985 //===----------------------------------------------------------------------===//
987 /// dump - Print descriptor to dbgs() with a newline.
988 void DIDescriptor::dump() const {
989 print(dbgs()); dbgs() << '\n';
992 /// print - Print descriptor.
993 void DIDescriptor::print(raw_ostream &OS) const {
994 if (!DbgNode) return;
996 if (const char *Tag = dwarf::TagString(getTag()))
997 OS << "[ " << Tag << " ]";
999 if (this->isSubrange()) {
1000 DISubrange(DbgNode).printInternal(OS);
1001 } else if (this->isCompileUnit()) {
1002 DICompileUnit(DbgNode).printInternal(OS);
1003 } else if (this->isFile()) {
1004 DIFile(DbgNode).printInternal(OS);
1005 } else if (this->isEnumerator()) {
1006 DIEnumerator(DbgNode).printInternal(OS);
1007 } else if (this->isBasicType()) {
1008 DIType(DbgNode).printInternal(OS);
1009 } else if (this->isDerivedType()) {
1010 DIDerivedType(DbgNode).printInternal(OS);
1011 } else if (this->isCompositeType()) {
1012 DICompositeType(DbgNode).printInternal(OS);
1013 } else if (this->isSubprogram()) {
1014 DISubprogram(DbgNode).printInternal(OS);
1015 } else if (this->isGlobalVariable()) {
1016 DIGlobalVariable(DbgNode).printInternal(OS);
1017 } else if (this->isVariable()) {
1018 DIVariable(DbgNode).printInternal(OS);
1019 } else if (this->isObjCProperty()) {
1020 DIObjCProperty(DbgNode).printInternal(OS);
1021 } else if (this->isScope()) {
1022 DIScope(DbgNode).printInternal(OS);
1026 void DISubrange::printInternal(raw_ostream &OS) const {
1027 OS << " [" << getLo() << ", " << getHi() << ']';
1030 void DIScope::printInternal(raw_ostream &OS) const {
1031 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1034 void DICompileUnit::printInternal(raw_ostream &OS) const {
1035 DIScope::printInternal(OS);
1036 if (unsigned Lang = getLanguage())
1037 OS << " [" << dwarf::LanguageString(Lang) << ']';
1040 void DIEnumerator::printInternal(raw_ostream &OS) const {
1041 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1044 void DIType::printInternal(raw_ostream &OS) const {
1045 if (!DbgNode) return;
1047 StringRef Res = getName();
1049 OS << " [" << Res << "]";
1051 // TODO: Print context?
1053 OS << " [line " << getLineNumber()
1054 << ", size " << getSizeInBits()
1055 << ", align " << getAlignInBits()
1056 << ", offset " << getOffsetInBits();
1058 if (const char *Enc =
1059 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1060 OS << ", enc " << Enc;
1065 else if (isProtected())
1066 OS << " [protected]";
1068 if (isForwardDecl())
1072 void DIDerivedType::printInternal(raw_ostream &OS) const {
1073 DIType::printInternal(OS);
1074 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1077 void DICompositeType::printInternal(raw_ostream &OS) const {
1078 DIType::printInternal(OS);
1079 DIArray A = getTypeArray();
1080 OS << " [" << A.getNumElements() << " elements]";
1083 void DISubprogram::printInternal(raw_ostream &OS) const {
1084 StringRef Res = getName();
1086 OS << " [" << Res << ']';
1088 // TODO : Print context
1090 OS << " [line " << getLineNumber() << ']';
1092 if (isLocalToUnit())
1098 if (getScopeLineNumber() != getLineNumber())
1099 OS << " [scope " << getScopeLineNumber() << "]";
1102 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1103 StringRef Res = getName();
1105 OS << " [" << Res << ']';
1107 OS << " [line " << getLineNumber() << ']';
1109 // TODO : Print context
1111 if (isLocalToUnit())
1118 void DIVariable::printInternal(raw_ostream &OS) const {
1119 StringRef Res = getName();
1121 OS << " [" << Res << ']';
1123 OS << " [line " << getLineNumber() << ']';
1126 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1127 StringRef Name = getObjCPropertyName();
1129 OS << " [" << Name << ']';
1131 OS << " [line " << getLineNumber()
1132 << ", properties " << getUnsignedField(6) << ']';
1135 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1136 const LLVMContext &Ctx) {
1137 if (!DL.isUnknown()) { // Print source line info.
1138 DIScope Scope(DL.getScope(Ctx));
1139 // Omit the directory, because it's likely to be long and uninteresting.
1141 CommentOS << Scope.getFilename();
1143 CommentOS << "<unknown>";
1144 CommentOS << ':' << DL.getLine();
1145 if (DL.getCol() != 0)
1146 CommentOS << ':' << DL.getCol();
1147 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1148 if (!InlinedAtDL.isUnknown()) {
1149 CommentOS << " @[ ";
1150 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1156 void DIVariable::printExtendedName(raw_ostream &OS) const {
1157 const LLVMContext &Ctx = DbgNode->getContext();
1158 StringRef Res = getName();
1160 OS << Res << "," << getLineNumber();
1161 if (MDNode *InlinedAt = getInlinedAt()) {
1162 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1163 if (!InlinedAtDL.isUnknown()) {
1165 printDebugLoc(InlinedAtDL, OS, Ctx);