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/Analysis/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 DILexicalBlock F) : DbgNode(F.DbgNode) {
45 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
48 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52 DIDescriptor::getStringField(unsigned Elt) const {
56 if (Elt < DbgNode->getNumOperands())
57 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
58 return MDS->getString();
63 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
67 if (Elt < DbgNode->getNumOperands())
68 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
69 return CI->getZExtValue();
74 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
76 return DIDescriptor();
78 if (Elt < DbgNode->getNumOperands())
80 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
81 return DIDescriptor();
84 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
88 if (Elt < DbgNode->getNumOperands())
89 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
93 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
97 if (Elt < DbgNode->getNumOperands())
98 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
102 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
106 if (Elt < DbgNode->getNumOperands())
107 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
111 unsigned DIVariable::getNumAddrElements() const {
112 if (getVersion() <= llvm::LLVMDebugVersion8)
113 return DbgNode->getNumOperands()-6;
114 if (getVersion() == llvm::LLVMDebugVersion9)
115 return DbgNode->getNumOperands()-7;
116 return DbgNode->getNumOperands()-8;
119 /// getInlinedAt - If this variable is inlined then return inline location.
120 MDNode *DIVariable::getInlinedAt() {
121 if (getVersion() <= llvm::LLVMDebugVersion9)
123 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
126 //===----------------------------------------------------------------------===//
128 //===----------------------------------------------------------------------===//
130 /// isBasicType - Return true if the specified tag is legal for
132 bool DIDescriptor::isBasicType() const {
133 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
136 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137 bool DIDescriptor::isDerivedType() const {
138 if (!DbgNode) return false;
140 case dwarf::DW_TAG_typedef:
141 case dwarf::DW_TAG_pointer_type:
142 case dwarf::DW_TAG_reference_type:
143 case dwarf::DW_TAG_const_type:
144 case dwarf::DW_TAG_volatile_type:
145 case dwarf::DW_TAG_restrict_type:
146 case dwarf::DW_TAG_member:
147 case dwarf::DW_TAG_inheritance:
148 case dwarf::DW_TAG_friend:
151 // CompositeTypes are currently modelled as DerivedTypes.
152 return isCompositeType();
156 /// isCompositeType - Return true if the specified tag is legal for
158 bool DIDescriptor::isCompositeType() const {
159 if (!DbgNode) return false;
161 case dwarf::DW_TAG_array_type:
162 case dwarf::DW_TAG_structure_type:
163 case dwarf::DW_TAG_union_type:
164 case dwarf::DW_TAG_enumeration_type:
165 case dwarf::DW_TAG_vector_type:
166 case dwarf::DW_TAG_subroutine_type:
167 case dwarf::DW_TAG_class_type:
174 /// isVariable - Return true if the specified tag is legal for DIVariable.
175 bool DIDescriptor::isVariable() const {
176 if (!DbgNode) return false;
178 case dwarf::DW_TAG_auto_variable:
179 case dwarf::DW_TAG_arg_variable:
180 case dwarf::DW_TAG_return_variable:
187 /// isType - Return true if the specified tag is legal for DIType.
188 bool DIDescriptor::isType() const {
189 return isBasicType() || isCompositeType() || isDerivedType();
192 /// isSubprogram - Return true if the specified tag is legal for
194 bool DIDescriptor::isSubprogram() const {
195 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
198 /// isGlobalVariable - Return true if the specified tag is legal for
199 /// DIGlobalVariable.
200 bool DIDescriptor::isGlobalVariable() const {
201 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
202 getTag() == dwarf::DW_TAG_constant);
205 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
206 bool DIDescriptor::isGlobal() const {
207 return isGlobalVariable();
210 /// isUnspecifiedParmeter - Return true if the specified tag is
211 /// DW_TAG_unspecified_parameters.
212 bool DIDescriptor::isUnspecifiedParameter() const {
213 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
216 /// isScope - Return true if the specified tag is one of the scope
218 bool DIDescriptor::isScope() const {
219 if (!DbgNode) return false;
221 case dwarf::DW_TAG_compile_unit:
222 case dwarf::DW_TAG_lexical_block:
223 case dwarf::DW_TAG_subprogram:
224 case dwarf::DW_TAG_namespace:
232 /// isTemplateTypeParameter - Return true if the specified tag is
233 /// DW_TAG_template_type_parameter.
234 bool DIDescriptor::isTemplateTypeParameter() const {
235 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
238 /// isTemplateValueParameter - Return true if the specified tag is
239 /// DW_TAG_template_value_parameter.
240 bool DIDescriptor::isTemplateValueParameter() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
244 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
245 bool DIDescriptor::isCompileUnit() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
249 /// isFile - Return true if the specified tag is DW_TAG_file_type.
250 bool DIDescriptor::isFile() const {
251 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
254 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
255 bool DIDescriptor::isNameSpace() const {
256 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
259 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
260 bool DIDescriptor::isLexicalBlock() const {
261 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
264 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
265 bool DIDescriptor::isSubrange() const {
266 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
269 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
270 bool DIDescriptor::isEnumerator() const {
271 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
274 //===----------------------------------------------------------------------===//
275 // Simple Descriptor Constructors and other Methods
276 //===----------------------------------------------------------------------===//
278 DIType::DIType(const MDNode *N) : DIScope(N) {
280 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
285 unsigned DIArray::getNumElements() const {
288 return DbgNode->getNumOperands();
291 /// replaceAllUsesWith - Replace all uses of debug info referenced by
293 void DIType::replaceAllUsesWith(DIDescriptor &D) {
297 // Since we use a TrackingVH for the node, its easy for clients to manufacture
298 // legitimate situations where they want to replaceAllUsesWith() on something
299 // which, due to uniquing, has merged with the source. We shield clients from
300 // this detail by allowing a value to be replaced with replaceAllUsesWith()
303 MDNode *Node = const_cast<MDNode*>(DbgNode);
304 const MDNode *DN = D;
305 const Value *V = cast_or_null<Value>(DN);
306 Node->replaceAllUsesWith(const_cast<Value*>(V));
307 MDNode::deleteTemporary(Node);
311 /// replaceAllUsesWith - Replace all uses of debug info referenced by
313 void DIType::replaceAllUsesWith(MDNode *D) {
317 // Since we use a TrackingVH for the node, its easy for clients to manufacture
318 // legitimate situations where they want to replaceAllUsesWith() on something
319 // which, due to uniquing, has merged with the source. We shield clients from
320 // this detail by allowing a value to be replaced with replaceAllUsesWith()
323 MDNode *Node = const_cast<MDNode*>(DbgNode);
324 const MDNode *DN = D;
325 const Value *V = cast_or_null<Value>(DN);
326 Node->replaceAllUsesWith(const_cast<Value*>(V));
327 MDNode::deleteTemporary(Node);
331 /// Verify - Verify that a compile unit is well formed.
332 bool DICompileUnit::Verify() const {
335 StringRef N = getFilename();
338 // It is possible that directory and produce string is empty.
342 /// Verify - Verify that a type descriptor is well formed.
343 bool DIType::Verify() const {
346 if (!getContext().Verify())
348 unsigned Tag = getTag();
349 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
350 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
351 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
352 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
353 && Tag != dwarf::DW_TAG_enumeration_type
354 && getFilename().empty())
359 /// Verify - Verify that a basic type descriptor is well formed.
360 bool DIBasicType::Verify() const {
361 return isBasicType();
364 /// Verify - Verify that a derived type descriptor is well formed.
365 bool DIDerivedType::Verify() const {
366 return isDerivedType();
369 /// Verify - Verify that a composite type descriptor is well formed.
370 bool DICompositeType::Verify() const {
373 if (!getContext().Verify())
376 DICompileUnit CU = getCompileUnit();
382 /// Verify - Verify that a subprogram descriptor is well formed.
383 bool DISubprogram::Verify() const {
387 if (!getContext().Verify())
390 DICompileUnit CU = getCompileUnit();
394 DICompositeType Ty = getType();
400 /// Verify - Verify that a global variable descriptor is well formed.
401 bool DIGlobalVariable::Verify() const {
405 if (getDisplayName().empty())
408 if (!getContext().Verify())
411 DICompileUnit CU = getCompileUnit();
415 DIType Ty = getType();
419 if (!getGlobal() && !getConstant())
425 /// Verify - Verify that a variable descriptor is well formed.
426 bool DIVariable::Verify() const {
430 if (!getContext().Verify())
433 if (!getCompileUnit().Verify())
436 DIType Ty = getType();
443 /// Verify - Verify that a location descriptor is well formed.
444 bool DILocation::Verify() const {
448 return DbgNode->getNumOperands() == 4;
451 /// Verify - Verify that a namespace descriptor is well formed.
452 bool DINameSpace::Verify() const {
455 if (getName().empty())
457 if (!getCompileUnit().Verify())
462 /// getOriginalTypeSize - If this type is derived from a base type then
463 /// return base type size.
464 uint64_t DIDerivedType::getOriginalTypeSize() const {
465 unsigned Tag = getTag();
466 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
467 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
468 Tag == dwarf::DW_TAG_restrict_type) {
469 DIType BaseType = getTypeDerivedFrom();
470 // If this type is not derived from any type then take conservative
472 if (!BaseType.isValid())
473 return getSizeInBits();
474 if (BaseType.isDerivedType())
475 return DIDerivedType(BaseType).getOriginalTypeSize();
477 return BaseType.getSizeInBits();
480 return getSizeInBits();
483 /// isInlinedFnArgument - Return true if this variable provides debugging
484 /// information for an inlined function arguments.
485 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
486 assert(CurFn && "Invalid function");
487 if (!getContext().isSubprogram())
489 // This variable is not inlined function argument if its scope
490 // does not describe current function.
491 return !(DISubprogram(getContext()).describes(CurFn));
494 /// describes - Return true if this subprogram provides debugging
495 /// information for the function F.
496 bool DISubprogram::describes(const Function *F) {
497 assert(F && "Invalid function");
498 if (F == getFunction())
500 StringRef Name = getLinkageName();
503 if (F->getName() == Name)
508 unsigned DISubprogram::isOptimized() const {
509 assert (DbgNode && "Invalid subprogram descriptor!");
510 if (DbgNode->getNumOperands() == 16)
511 return getUnsignedField(15);
515 StringRef DIScope::getFilename() const {
518 if (isLexicalBlock())
519 return DILexicalBlock(DbgNode).getFilename();
521 return DISubprogram(DbgNode).getFilename();
523 return DICompileUnit(DbgNode).getFilename();
525 return DINameSpace(DbgNode).getFilename();
527 return DIType(DbgNode).getFilename();
529 return DIFile(DbgNode).getFilename();
530 assert(0 && "Invalid DIScope!");
534 StringRef DIScope::getDirectory() const {
537 if (isLexicalBlock())
538 return DILexicalBlock(DbgNode).getDirectory();
540 return DISubprogram(DbgNode).getDirectory();
542 return DICompileUnit(DbgNode).getDirectory();
544 return DINameSpace(DbgNode).getDirectory();
546 return DIType(DbgNode).getDirectory();
548 return DIFile(DbgNode).getDirectory();
549 assert(0 && "Invalid DIScope!");
553 //===----------------------------------------------------------------------===//
554 // DIDescriptor: dump routines for all descriptors.
555 //===----------------------------------------------------------------------===//
558 /// print - Print descriptor.
559 void DIDescriptor::print(raw_ostream &OS) const {
560 OS << "[" << dwarf::TagString(getTag()) << "] ";
561 OS.write_hex((intptr_t) &*DbgNode) << ']';
564 /// print - Print compile unit.
565 void DICompileUnit::print(raw_ostream &OS) const {
567 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
569 OS << " [" << getDirectory() << "/" << getFilename() << "]";
572 /// print - Print type.
573 void DIType::print(raw_ostream &OS) const {
574 if (!DbgNode) return;
576 StringRef Res = getName();
578 OS << " [" << Res << "] ";
580 unsigned Tag = getTag();
581 OS << " [" << dwarf::TagString(Tag) << "] ";
583 // TODO : Print context
584 getCompileUnit().print(OS);
586 << "line " << getLineNumber() << ", "
587 << getSizeInBits() << " bits, "
588 << getAlignInBits() << " bit alignment, "
589 << getOffsetInBits() << " bit offset"
594 else if (isProtected())
595 OS << " [protected] ";
601 DIBasicType(DbgNode).print(OS);
602 else if (isDerivedType())
603 DIDerivedType(DbgNode).print(OS);
604 else if (isCompositeType())
605 DICompositeType(DbgNode).print(OS);
607 OS << "Invalid DIType\n";
614 /// print - Print basic type.
615 void DIBasicType::print(raw_ostream &OS) const {
616 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
619 /// print - Print derived type.
620 void DIDerivedType::print(raw_ostream &OS) const {
621 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
624 /// print - Print composite type.
625 void DICompositeType::print(raw_ostream &OS) const {
626 DIArray A = getTypeArray();
627 OS << " [" << A.getNumElements() << " elements]";
630 /// print - Print subprogram.
631 void DISubprogram::print(raw_ostream &OS) const {
632 StringRef Res = getName();
634 OS << " [" << Res << "] ";
636 unsigned Tag = getTag();
637 OS << " [" << dwarf::TagString(Tag) << "] ";
639 // TODO : Print context
640 getCompileUnit().print(OS);
641 OS << " [" << getLineNumber() << "] ";
652 /// print - Print global variable.
653 void DIGlobalVariable::print(raw_ostream &OS) const {
655 StringRef Res = getName();
657 OS << " [" << Res << "] ";
659 unsigned Tag = getTag();
660 OS << " [" << dwarf::TagString(Tag) << "] ";
662 // TODO : Print context
663 getCompileUnit().print(OS);
664 OS << " [" << getLineNumber() << "] ";
672 if (isGlobalVariable())
673 DIGlobalVariable(DbgNode).print(OS);
677 /// print - Print variable.
678 void DIVariable::print(raw_ostream &OS) const {
679 StringRef Res = getName();
681 OS << " [" << Res << "] ";
683 getCompileUnit().print(OS);
684 OS << " [" << getLineNumber() << "] ";
688 // FIXME: Dump complex addresses
691 /// dump - Print descriptor to dbgs() with a newline.
692 void DIDescriptor::dump() const {
693 print(dbgs()); dbgs() << '\n';
696 /// dump - Print compile unit to dbgs() with a newline.
697 void DICompileUnit::dump() const {
698 print(dbgs()); dbgs() << '\n';
701 /// dump - Print type to dbgs() with a newline.
702 void DIType::dump() const {
703 print(dbgs()); dbgs() << '\n';
706 /// dump - Print basic type to dbgs() with a newline.
707 void DIBasicType::dump() const {
708 print(dbgs()); dbgs() << '\n';
711 /// dump - Print derived type to dbgs() with a newline.
712 void DIDerivedType::dump() const {
713 print(dbgs()); dbgs() << '\n';
716 /// dump - Print composite type to dbgs() with a newline.
717 void DICompositeType::dump() const {
718 print(dbgs()); dbgs() << '\n';
721 /// dump - Print subprogram to dbgs() with a newline.
722 void DISubprogram::dump() const {
723 print(dbgs()); dbgs() << '\n';
726 /// dump - Print global variable.
727 void DIGlobalVariable::dump() const {
728 print(dbgs()); dbgs() << '\n';
731 /// dump - Print variable.
732 void DIVariable::dump() const {
733 print(dbgs()); dbgs() << '\n';
736 /// fixupObjcLikeName - Replace contains special characters used
737 /// in a typical Objective-C names with '.' in a given string.
738 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
739 bool isObjCLike = false;
740 for (size_t i = 0, e = Str.size(); i < e; ++i) {
745 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
746 C == '+' || C == '(' || C == ')'))
753 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
754 /// suitable to hold function specific information.
755 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
756 SmallString<32> Name = StringRef("llvm.dbg.lv.");
757 fixupObjcLikeName(FuncName, Name);
759 return M.getNamedMetadata(Name.str());
762 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
763 /// to hold function specific information.
764 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
765 SmallString<32> Name = StringRef("llvm.dbg.lv.");
766 fixupObjcLikeName(FuncName, Name);
768 return M.getOrInsertNamedMetadata(Name.str());
771 /// createInlinedVariable - Create a new inlined variable based on current
773 /// @param DV Current Variable.
774 /// @param InlinedScope Location at current variable is inlined.
775 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
776 LLVMContext &VMContext) {
777 SmallVector<Value *, 16> Elts;
778 // Insert inlined scope as 7th element.
779 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
780 i == 7 ? Elts.push_back(InlinedScope) :
781 Elts.push_back(DV->getOperand(i));
782 return DIVariable(MDNode::get(VMContext, Elts));
785 /// cleanseInlinedVariable - Remove inlined scope from the variable.
786 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
787 SmallVector<Value *, 16> Elts;
788 // Insert inlined scope as 7th element.
789 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
791 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
792 Elts.push_back(DV->getOperand(i));
793 return DIVariable(MDNode::get(VMContext, Elts));
796 //===----------------------------------------------------------------------===//
797 // DebugInfoFinder implementations.
798 //===----------------------------------------------------------------------===//
800 /// processModule - Process entire module and collect debug info.
801 void DebugInfoFinder::processModule(Module &M) {
802 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
803 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
804 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
806 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
809 DebugLoc Loc = BI->getDebugLoc();
813 LLVMContext &Ctx = BI->getContext();
814 DIDescriptor Scope(Loc.getScope(Ctx));
816 if (Scope.isCompileUnit())
817 addCompileUnit(DICompileUnit(Scope));
818 else if (Scope.isSubprogram())
819 processSubprogram(DISubprogram(Scope));
820 else if (Scope.isLexicalBlock())
821 processLexicalBlock(DILexicalBlock(Scope));
823 if (MDNode *IA = Loc.getInlinedAt(Ctx))
824 processLocation(DILocation(IA));
827 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
828 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
829 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
830 if (addGlobalVariable(DIG)) {
831 addCompileUnit(DIG.getCompileUnit());
832 processType(DIG.getType());
837 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
838 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
839 processSubprogram(DISubprogram(NMD->getOperand(i)));
842 /// processLocation - Process DILocation.
843 void DebugInfoFinder::processLocation(DILocation Loc) {
844 if (!Loc.Verify()) return;
845 DIDescriptor S(Loc.getScope());
846 if (S.isCompileUnit())
847 addCompileUnit(DICompileUnit(S));
848 else if (S.isSubprogram())
849 processSubprogram(DISubprogram(S));
850 else if (S.isLexicalBlock())
851 processLexicalBlock(DILexicalBlock(S));
852 processLocation(Loc.getOrigLocation());
855 /// processType - Process DIType.
856 void DebugInfoFinder::processType(DIType DT) {
860 addCompileUnit(DT.getCompileUnit());
861 if (DT.isCompositeType()) {
862 DICompositeType DCT(DT);
863 processType(DCT.getTypeDerivedFrom());
864 DIArray DA = DCT.getTypeArray();
865 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
866 DIDescriptor D = DA.getElement(i);
868 processType(DIType(D));
869 else if (D.isSubprogram())
870 processSubprogram(DISubprogram(D));
872 } else if (DT.isDerivedType()) {
873 DIDerivedType DDT(DT);
874 processType(DDT.getTypeDerivedFrom());
878 /// processLexicalBlock
879 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
880 DIScope Context = LB.getContext();
881 if (Context.isLexicalBlock())
882 return processLexicalBlock(DILexicalBlock(Context));
884 return processSubprogram(DISubprogram(Context));
887 /// processSubprogram - Process DISubprogram.
888 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
889 if (!addSubprogram(SP))
891 addCompileUnit(SP.getCompileUnit());
892 processType(SP.getType());
895 /// processDeclare - Process DbgDeclareInst.
896 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
897 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
901 if (!DV.isVariable())
904 if (!NodesSeen.insert(DV))
907 addCompileUnit(DIVariable(N).getCompileUnit());
908 processType(DIVariable(N).getType());
911 /// addType - Add type into Tys.
912 bool DebugInfoFinder::addType(DIType DT) {
916 if (!NodesSeen.insert(DT))
923 /// addCompileUnit - Add compile unit into CUs.
924 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
928 if (!NodesSeen.insert(CU))
935 /// addGlobalVariable - Add global variable into GVs.
936 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
937 if (!DIDescriptor(DIG).isGlobalVariable())
940 if (!NodesSeen.insert(DIG))
947 // addSubprogram - Add subprgoram into SPs.
948 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
949 if (!DIDescriptor(SP).isSubprogram())
952 if (!NodesSeen.insert(SP))
959 /// getDISubprogram - Find subprogram that is enclosing this scope.
960 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
961 DIDescriptor D(Scope);
962 if (D.isSubprogram())
963 return DISubprogram(Scope);
965 if (D.isLexicalBlock())
966 return getDISubprogram(DILexicalBlock(Scope).getContext());
968 return DISubprogram();
971 /// getDICompositeType - Find underlying composite type.
972 DICompositeType llvm::getDICompositeType(DIType T) {
973 if (T.isCompositeType())
974 return DICompositeType(T);
976 if (T.isDerivedType())
977 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
979 return DICompositeType();