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())
72 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
73 return CI->getZExtValue();
78 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
80 return DIDescriptor();
82 if (Elt < DbgNode->getNumOperands())
84 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
85 return DIDescriptor();
88 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
92 if (Elt < DbgNode->getNumOperands())
93 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
97 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
101 if (Elt < DbgNode->getNumOperands())
102 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
106 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
110 if (Elt < DbgNode->getNumOperands())
111 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
115 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
119 if (Elt < DbgNode->getNumOperands()) {
120 MDNode *Node = const_cast<MDNode*>(DbgNode);
121 Node->replaceOperandWith(Elt, F);
125 unsigned DIVariable::getNumAddrElements() const {
126 if (getVersion() <= LLVMDebugVersion8)
127 return DbgNode->getNumOperands()-6;
128 if (getVersion() == LLVMDebugVersion9)
129 return DbgNode->getNumOperands()-7;
130 return DbgNode->getNumOperands()-8;
133 /// getInlinedAt - If this variable is inlined then return inline location.
134 MDNode *DIVariable::getInlinedAt() const {
135 if (getVersion() <= LLVMDebugVersion9)
137 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
140 //===----------------------------------------------------------------------===//
142 //===----------------------------------------------------------------------===//
144 /// isBasicType - Return true if the specified tag is legal for
146 bool DIDescriptor::isBasicType() const {
147 if (!DbgNode) return false;
149 case dwarf::DW_TAG_base_type:
150 case dwarf::DW_TAG_unspecified_type:
157 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
158 bool DIDescriptor::isDerivedType() const {
159 if (!DbgNode) return false;
161 case dwarf::DW_TAG_typedef:
162 case dwarf::DW_TAG_pointer_type:
163 case dwarf::DW_TAG_reference_type:
164 case dwarf::DW_TAG_rvalue_reference_type:
165 case dwarf::DW_TAG_const_type:
166 case dwarf::DW_TAG_volatile_type:
167 case dwarf::DW_TAG_restrict_type:
168 case dwarf::DW_TAG_member:
169 case dwarf::DW_TAG_inheritance:
170 case dwarf::DW_TAG_friend:
173 // CompositeTypes are currently modelled as DerivedTypes.
174 return isCompositeType();
178 /// isCompositeType - Return true if the specified tag is legal for
180 bool DIDescriptor::isCompositeType() const {
181 if (!DbgNode) return false;
183 case dwarf::DW_TAG_array_type:
184 case dwarf::DW_TAG_structure_type:
185 case dwarf::DW_TAG_union_type:
186 case dwarf::DW_TAG_enumeration_type:
187 case dwarf::DW_TAG_vector_type:
188 case dwarf::DW_TAG_subroutine_type:
189 case dwarf::DW_TAG_class_type:
196 /// isVariable - Return true if the specified tag is legal for DIVariable.
197 bool DIDescriptor::isVariable() const {
198 if (!DbgNode) return false;
200 case dwarf::DW_TAG_auto_variable:
201 case dwarf::DW_TAG_arg_variable:
202 case dwarf::DW_TAG_return_variable:
209 /// isType - Return true if the specified tag is legal for DIType.
210 bool DIDescriptor::isType() const {
211 return isBasicType() || isCompositeType() || isDerivedType();
214 /// isSubprogram - Return true if the specified tag is legal for
216 bool DIDescriptor::isSubprogram() const {
217 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
220 /// isGlobalVariable - Return true if the specified tag is legal for
221 /// DIGlobalVariable.
222 bool DIDescriptor::isGlobalVariable() const {
223 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
224 getTag() == dwarf::DW_TAG_constant);
227 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
228 bool DIDescriptor::isGlobal() const {
229 return isGlobalVariable();
232 /// isUnspecifiedParmeter - Return true if the specified tag is
233 /// DW_TAG_unspecified_parameters.
234 bool DIDescriptor::isUnspecifiedParameter() const {
235 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
238 /// isScope - Return true if the specified tag is one of the scope
240 bool DIDescriptor::isScope() const {
241 if (!DbgNode) return false;
243 case dwarf::DW_TAG_compile_unit:
244 case dwarf::DW_TAG_lexical_block:
245 case dwarf::DW_TAG_subprogram:
246 case dwarf::DW_TAG_namespace:
254 /// isTemplateTypeParameter - Return true if the specified tag is
255 /// DW_TAG_template_type_parameter.
256 bool DIDescriptor::isTemplateTypeParameter() const {
257 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
260 /// isTemplateValueParameter - Return true if the specified tag is
261 /// DW_TAG_template_value_parameter.
262 bool DIDescriptor::isTemplateValueParameter() const {
263 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
266 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
267 bool DIDescriptor::isCompileUnit() const {
268 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
271 /// isFile - Return true if the specified tag is DW_TAG_file_type.
272 bool DIDescriptor::isFile() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
276 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
277 bool DIDescriptor::isNameSpace() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
281 /// isLexicalBlockFile - Return true if the specified descriptor is a
282 /// lexical block with an extra file.
283 bool DIDescriptor::isLexicalBlockFile() const {
284 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
285 (DbgNode->getNumOperands() == 3);
288 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
289 bool DIDescriptor::isLexicalBlock() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
291 (DbgNode->getNumOperands() > 3);
294 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
295 bool DIDescriptor::isSubrange() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
299 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
300 bool DIDescriptor::isEnumerator() const {
301 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
304 /// isObjCProperty - Return true if the specified tag is DW_TAG
305 bool DIDescriptor::isObjCProperty() const {
306 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
308 //===----------------------------------------------------------------------===//
309 // Simple Descriptor Constructors and other Methods
310 //===----------------------------------------------------------------------===//
312 DIType::DIType(const MDNode *N) : DIScope(N) {
314 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
319 unsigned DIArray::getNumElements() const {
322 return DbgNode->getNumOperands();
325 /// replaceAllUsesWith - Replace all uses of debug info referenced by
327 void DIType::replaceAllUsesWith(DIDescriptor &D) {
331 // Since we use a TrackingVH for the node, its easy for clients to manufacture
332 // legitimate situations where they want to replaceAllUsesWith() on something
333 // which, due to uniquing, has merged with the source. We shield clients from
334 // this detail by allowing a value to be replaced with replaceAllUsesWith()
337 MDNode *Node = const_cast<MDNode*>(DbgNode);
338 const MDNode *DN = D;
339 const Value *V = cast_or_null<Value>(DN);
340 Node->replaceAllUsesWith(const_cast<Value*>(V));
341 MDNode::deleteTemporary(Node);
345 /// replaceAllUsesWith - Replace all uses of debug info referenced by
347 void DIType::replaceAllUsesWith(MDNode *D) {
351 // Since we use a TrackingVH for the node, its easy for clients to manufacture
352 // legitimate situations where they want to replaceAllUsesWith() on something
353 // which, due to uniquing, has merged with the source. We shield clients from
354 // this detail by allowing a value to be replaced with replaceAllUsesWith()
357 MDNode *Node = const_cast<MDNode*>(DbgNode);
358 const MDNode *DN = D;
359 const Value *V = cast_or_null<Value>(DN);
360 Node->replaceAllUsesWith(const_cast<Value*>(V));
361 MDNode::deleteTemporary(Node);
365 /// isUnsignedDIType - Return true if type encoding is unsigned.
366 bool DIType::isUnsignedDIType() {
367 DIDerivedType DTy(DbgNode);
369 return DTy.getTypeDerivedFrom().isUnsignedDIType();
371 DIBasicType BTy(DbgNode);
373 unsigned Encoding = BTy.getEncoding();
374 if (Encoding == dwarf::DW_ATE_unsigned ||
375 Encoding == dwarf::DW_ATE_unsigned_char)
381 /// Verify - Verify that a compile unit is well formed.
382 bool DICompileUnit::Verify() const {
385 StringRef N = getFilename();
388 // It is possible that directory and produce string is empty.
392 /// Verify - Verify that an ObjC property is well formed.
393 bool DIObjCProperty::Verify() const {
396 unsigned Tag = getTag();
397 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
398 DIType Ty = getType();
399 if (!Ty.Verify()) return false;
401 // Don't worry about the rest of the strings for now.
405 /// Verify - Verify that a type descriptor is well formed.
406 bool DIType::Verify() const {
409 if (getContext() && !getContext().Verify())
411 unsigned Tag = getTag();
412 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
413 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
414 Tag != dwarf::DW_TAG_reference_type &&
415 Tag != dwarf::DW_TAG_rvalue_reference_type &&
416 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
417 Tag != dwarf::DW_TAG_array_type &&
418 Tag != dwarf::DW_TAG_enumeration_type &&
419 Tag != dwarf::DW_TAG_subroutine_type &&
420 getFilename().empty())
425 /// Verify - Verify that a basic type descriptor is well formed.
426 bool DIBasicType::Verify() const {
427 return isBasicType();
430 /// Verify - Verify that a derived type descriptor is well formed.
431 bool DIDerivedType::Verify() const {
432 return isDerivedType();
435 /// Verify - Verify that a composite type descriptor is well formed.
436 bool DICompositeType::Verify() const {
439 if (getContext() && !getContext().Verify())
445 /// Verify - Verify that a subprogram descriptor is well formed.
446 bool DISubprogram::Verify() const {
450 if (getContext() && !getContext().Verify())
453 DICompositeType Ty = getType();
459 /// Verify - Verify that a global variable descriptor is well formed.
460 bool DIGlobalVariable::Verify() const {
464 if (getDisplayName().empty())
467 if (getContext() && !getContext().Verify())
470 DIType Ty = getType();
474 if (!getGlobal() && !getConstant())
480 /// Verify - Verify that a variable descriptor is well formed.
481 bool DIVariable::Verify() const {
485 if (getContext() && !getContext().Verify())
488 DIType Ty = getType();
495 /// Verify - Verify that a location descriptor is well formed.
496 bool DILocation::Verify() const {
500 return DbgNode->getNumOperands() == 4;
503 /// Verify - Verify that a namespace descriptor is well formed.
504 bool DINameSpace::Verify() const {
507 if (getName().empty())
512 /// getOriginalTypeSize - If this type is derived from a base type then
513 /// return base type size.
514 uint64_t DIDerivedType::getOriginalTypeSize() const {
515 unsigned Tag = getTag();
517 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
518 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
519 Tag != dwarf::DW_TAG_restrict_type)
520 return getSizeInBits();
522 DIType BaseType = getTypeDerivedFrom();
524 // If this type is not derived from any type then take conservative approach.
525 if (!BaseType.isValid())
526 return getSizeInBits();
528 // If this is a derived type, go ahead and get the base type, unless it's a
529 // reference then it's just the size of the field. Pointer types have no need
530 // of this since they're a different type of qualification on the type.
531 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
532 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
533 return getSizeInBits();
535 if (BaseType.isDerivedType())
536 return DIDerivedType(BaseType).getOriginalTypeSize();
538 return BaseType.getSizeInBits();
541 /// getObjCProperty - Return property node, if this ivar is associated with one.
542 MDNode *DIDerivedType::getObjCProperty() const {
543 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
545 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
548 /// isInlinedFnArgument - Return true if this variable provides debugging
549 /// information for an inlined function arguments.
550 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
551 assert(CurFn && "Invalid function");
552 if (!getContext().isSubprogram())
554 // This variable is not inlined function argument if its scope
555 // does not describe current function.
556 return !DISubprogram(getContext()).describes(CurFn);
559 /// describes - Return true if this subprogram provides debugging
560 /// information for the function F.
561 bool DISubprogram::describes(const Function *F) {
562 assert(F && "Invalid function");
563 if (F == getFunction())
565 StringRef Name = getLinkageName();
568 if (F->getName() == Name)
573 unsigned DISubprogram::isOptimized() const {
574 assert (DbgNode && "Invalid subprogram descriptor!");
575 if (DbgNode->getNumOperands() == 16)
576 return getUnsignedField(15);
580 MDNode *DISubprogram::getVariablesNodes() const {
581 if (!DbgNode || DbgNode->getNumOperands() <= 19)
583 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
584 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
588 DIArray DISubprogram::getVariables() const {
589 if (!DbgNode || DbgNode->getNumOperands() <= 19)
591 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
592 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
597 StringRef DIScope::getFilename() const {
600 if (isLexicalBlockFile())
601 return DILexicalBlockFile(DbgNode).getFilename();
602 if (isLexicalBlock())
603 return DILexicalBlock(DbgNode).getFilename();
605 return DISubprogram(DbgNode).getFilename();
607 return DICompileUnit(DbgNode).getFilename();
609 return DINameSpace(DbgNode).getFilename();
611 return DIType(DbgNode).getFilename();
613 return DIFile(DbgNode).getFilename();
614 llvm_unreachable("Invalid DIScope!");
617 StringRef DIScope::getDirectory() const {
620 if (isLexicalBlockFile())
621 return DILexicalBlockFile(DbgNode).getDirectory();
622 if (isLexicalBlock())
623 return DILexicalBlock(DbgNode).getDirectory();
625 return DISubprogram(DbgNode).getDirectory();
627 return DICompileUnit(DbgNode).getDirectory();
629 return DINameSpace(DbgNode).getDirectory();
631 return DIType(DbgNode).getDirectory();
633 return DIFile(DbgNode).getDirectory();
634 llvm_unreachable("Invalid DIScope!");
637 DIArray DICompileUnit::getEnumTypes() const {
638 if (!DbgNode || DbgNode->getNumOperands() < 14)
641 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
642 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
647 DIArray DICompileUnit::getRetainedTypes() const {
648 if (!DbgNode || DbgNode->getNumOperands() < 14)
651 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
652 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
657 DIArray DICompileUnit::getSubprograms() const {
658 if (!DbgNode || DbgNode->getNumOperands() < 14)
661 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
662 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
668 DIArray DICompileUnit::getGlobalVariables() const {
669 if (!DbgNode || DbgNode->getNumOperands() < 14)
672 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
673 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
678 /// fixupObjcLikeName - Replace contains special characters used
679 /// in a typical Objective-C names with '.' in a given string.
680 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
681 bool isObjCLike = false;
682 for (size_t i = 0, e = Str.size(); i < e; ++i) {
687 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
688 C == '+' || C == '(' || C == ')'))
695 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
696 /// suitable to hold function specific information.
697 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
698 SmallString<32> Name = StringRef("llvm.dbg.lv.");
699 StringRef FName = "fn";
700 if (Fn.getFunction())
701 FName = Fn.getFunction()->getName();
703 FName = Fn.getName();
705 if (FName.startswith(StringRef(&One, 1)))
706 FName = FName.substr(1);
707 fixupObjcLikeName(FName, Name);
708 return M.getNamedMetadata(Name.str());
711 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
712 /// to hold function specific information.
713 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
714 SmallString<32> Name = StringRef("llvm.dbg.lv.");
715 StringRef FName = "fn";
716 if (Fn.getFunction())
717 FName = Fn.getFunction()->getName();
719 FName = Fn.getName();
721 if (FName.startswith(StringRef(&One, 1)))
722 FName = FName.substr(1);
723 fixupObjcLikeName(FName, Name);
725 return M.getOrInsertNamedMetadata(Name.str());
728 /// createInlinedVariable - Create a new inlined variable based on current
730 /// @param DV Current Variable.
731 /// @param InlinedScope Location at current variable is inlined.
732 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
733 LLVMContext &VMContext) {
734 SmallVector<Value *, 16> Elts;
735 // Insert inlined scope as 7th element.
736 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
737 i == 7 ? Elts.push_back(InlinedScope) :
738 Elts.push_back(DV->getOperand(i));
739 return DIVariable(MDNode::get(VMContext, Elts));
742 /// cleanseInlinedVariable - Remove inlined scope from the variable.
743 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
744 SmallVector<Value *, 16> Elts;
745 // Insert inlined scope as 7th element.
746 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
748 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
749 Elts.push_back(DV->getOperand(i));
750 return DIVariable(MDNode::get(VMContext, Elts));
753 /// getDISubprogram - Find subprogram that is enclosing this scope.
754 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
755 DIDescriptor D(Scope);
756 if (D.isSubprogram())
757 return DISubprogram(Scope);
759 if (D.isLexicalBlockFile())
760 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
762 if (D.isLexicalBlock())
763 return getDISubprogram(DILexicalBlock(Scope).getContext());
765 return DISubprogram();
768 /// getDICompositeType - Find underlying composite type.
769 DICompositeType llvm::getDICompositeType(DIType T) {
770 if (T.isCompositeType())
771 return DICompositeType(T);
773 if (T.isDerivedType())
774 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
776 return DICompositeType();
779 /// isSubprogramContext - Return true if Context is either a subprogram
780 /// or another context nested inside a subprogram.
781 bool llvm::isSubprogramContext(const MDNode *Context) {
784 DIDescriptor D(Context);
785 if (D.isSubprogram())
788 return isSubprogramContext(DIType(Context).getContext());
792 //===----------------------------------------------------------------------===//
793 // DebugInfoFinder implementations.
794 //===----------------------------------------------------------------------===//
796 /// processModule - Process entire module and collect debug info.
797 void DebugInfoFinder::processModule(const Module &M) {
798 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
799 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
800 DICompileUnit CU(CU_Nodes->getOperand(i));
802 if (CU.getVersion() > LLVMDebugVersion10) {
803 DIArray GVs = CU.getGlobalVariables();
804 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
805 DIGlobalVariable DIG(GVs.getElement(i));
806 if (addGlobalVariable(DIG))
807 processType(DIG.getType());
809 DIArray SPs = CU.getSubprograms();
810 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
811 processSubprogram(DISubprogram(SPs.getElement(i)));
812 DIArray EnumTypes = CU.getEnumTypes();
813 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
814 processType(DIType(EnumTypes.getElement(i)));
815 DIArray RetainedTypes = CU.getRetainedTypes();
816 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
817 processType(DIType(RetainedTypes.getElement(i)));
823 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
824 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
826 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
828 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
831 DebugLoc Loc = BI->getDebugLoc();
835 LLVMContext &Ctx = BI->getContext();
836 DIDescriptor Scope(Loc.getScope(Ctx));
838 if (Scope.isCompileUnit())
839 addCompileUnit(DICompileUnit(Scope));
840 else if (Scope.isSubprogram())
841 processSubprogram(DISubprogram(Scope));
842 else if (Scope.isLexicalBlockFile()) {
843 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
844 processLexicalBlock(DILexicalBlock(DBF.getScope()));
846 else if (Scope.isLexicalBlock())
847 processLexicalBlock(DILexicalBlock(Scope));
849 if (MDNode *IA = Loc.getInlinedAt(Ctx))
850 processLocation(DILocation(IA));
853 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
854 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
855 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
856 if (addGlobalVariable(DIG)) {
857 if (DIG.getVersion() <= LLVMDebugVersion10)
858 addCompileUnit(DIG.getCompileUnit());
859 processType(DIG.getType());
864 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
865 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
866 processSubprogram(DISubprogram(NMD->getOperand(i)));
869 /// processLocation - Process DILocation.
870 void DebugInfoFinder::processLocation(DILocation Loc) {
871 if (!Loc.Verify()) return;
872 DIDescriptor S(Loc.getScope());
873 if (S.isCompileUnit())
874 addCompileUnit(DICompileUnit(S));
875 else if (S.isSubprogram())
876 processSubprogram(DISubprogram(S));
877 else if (S.isLexicalBlock())
878 processLexicalBlock(DILexicalBlock(S));
879 else if (S.isLexicalBlockFile()) {
880 DILexicalBlockFile DBF = DILexicalBlockFile(S);
881 processLexicalBlock(DILexicalBlock(DBF.getScope()));
883 processLocation(Loc.getOrigLocation());
886 /// processType - Process DIType.
887 void DebugInfoFinder::processType(DIType DT) {
890 if (DT.getVersion() <= LLVMDebugVersion10)
891 addCompileUnit(DT.getCompileUnit());
892 if (DT.isCompositeType()) {
893 DICompositeType DCT(DT);
894 processType(DCT.getTypeDerivedFrom());
895 DIArray DA = DCT.getTypeArray();
896 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
897 DIDescriptor D = DA.getElement(i);
899 processType(DIType(D));
900 else if (D.isSubprogram())
901 processSubprogram(DISubprogram(D));
903 } else if (DT.isDerivedType()) {
904 DIDerivedType DDT(DT);
905 processType(DDT.getTypeDerivedFrom());
909 /// processLexicalBlock
910 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
911 DIScope Context = LB.getContext();
912 if (Context.isLexicalBlock())
913 return processLexicalBlock(DILexicalBlock(Context));
914 else if (Context.isLexicalBlockFile()) {
915 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
916 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
919 return processSubprogram(DISubprogram(Context));
922 /// processSubprogram - Process DISubprogram.
923 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
924 if (!addSubprogram(SP))
926 if (SP.getVersion() <= LLVMDebugVersion10)
927 addCompileUnit(SP.getCompileUnit());
928 processType(SP.getType());
931 /// processDeclare - Process DbgDeclareInst.
932 void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
933 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
937 if (!DV.isVariable())
940 if (!NodesSeen.insert(DV))
942 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
943 addCompileUnit(DIVariable(N).getCompileUnit());
944 processType(DIVariable(N).getType());
947 /// addType - Add type into Tys.
948 bool DebugInfoFinder::addType(DIType DT) {
952 if (!NodesSeen.insert(DT))
959 /// addCompileUnit - Add compile unit into CUs.
960 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
964 if (!NodesSeen.insert(CU))
971 /// addGlobalVariable - Add global variable into GVs.
972 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
973 if (!DIDescriptor(DIG).isGlobalVariable())
976 if (!NodesSeen.insert(DIG))
983 // addSubprogram - Add subprgoram into SPs.
984 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
985 if (!DIDescriptor(SP).isSubprogram())
988 if (!NodesSeen.insert(SP))
995 //===----------------------------------------------------------------------===//
996 // DIDescriptor: dump routines for all descriptors.
997 //===----------------------------------------------------------------------===//
999 /// dump - Print descriptor to dbgs() with a newline.
1000 void DIDescriptor::dump() const {
1001 print(dbgs()); dbgs() << '\n';
1004 /// print - Print descriptor.
1005 void DIDescriptor::print(raw_ostream &OS) const {
1006 if (!DbgNode) return;
1008 if (const char *Tag = dwarf::TagString(getTag()))
1009 OS << "[ " << Tag << " ]";
1011 if (this->isSubrange()) {
1012 DISubrange(DbgNode).printInternal(OS);
1013 } else if (this->isCompileUnit()) {
1014 DICompileUnit(DbgNode).printInternal(OS);
1015 } else if (this->isFile()) {
1016 DIFile(DbgNode).printInternal(OS);
1017 } else if (this->isEnumerator()) {
1018 DIEnumerator(DbgNode).printInternal(OS);
1019 } else if (this->isBasicType()) {
1020 DIType(DbgNode).printInternal(OS);
1021 } else if (this->isDerivedType()) {
1022 DIDerivedType(DbgNode).printInternal(OS);
1023 } else if (this->isCompositeType()) {
1024 DICompositeType(DbgNode).printInternal(OS);
1025 } else if (this->isSubprogram()) {
1026 DISubprogram(DbgNode).printInternal(OS);
1027 } else if (this->isGlobalVariable()) {
1028 DIGlobalVariable(DbgNode).printInternal(OS);
1029 } else if (this->isVariable()) {
1030 DIVariable(DbgNode).printInternal(OS);
1031 } else if (this->isObjCProperty()) {
1032 DIObjCProperty(DbgNode).printInternal(OS);
1033 } else if (this->isScope()) {
1034 DIScope(DbgNode).printInternal(OS);
1038 void DISubrange::printInternal(raw_ostream &OS) const {
1039 OS << " [" << getLo() << ", " << getHi() << ']';
1042 void DIScope::printInternal(raw_ostream &OS) const {
1043 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1046 void DICompileUnit::printInternal(raw_ostream &OS) const {
1047 DIScope::printInternal(OS);
1048 if (unsigned Lang = getLanguage())
1049 OS << " [" << dwarf::LanguageString(Lang) << ']';
1052 void DIEnumerator::printInternal(raw_ostream &OS) const {
1053 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1056 void DIType::printInternal(raw_ostream &OS) const {
1057 if (!DbgNode) return;
1059 StringRef Res = getName();
1061 OS << " [" << Res << "]";
1063 // TODO: Print context?
1065 OS << " [line " << getLineNumber()
1066 << ", size " << getSizeInBits()
1067 << ", align " << getAlignInBits()
1068 << ", offset " << getOffsetInBits();
1070 if (const char *Enc =
1071 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1072 OS << ", enc " << Enc;
1077 else if (isProtected())
1078 OS << " [protected]";
1080 if (isForwardDecl())
1084 void DIDerivedType::printInternal(raw_ostream &OS) const {
1085 DIType::printInternal(OS);
1086 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1089 void DICompositeType::printInternal(raw_ostream &OS) const {
1090 DIType::printInternal(OS);
1091 DIArray A = getTypeArray();
1092 OS << " [" << A.getNumElements() << " elements]";
1095 void DISubprogram::printInternal(raw_ostream &OS) const {
1096 // TODO : Print context
1097 OS << " [line " << getLineNumber() << ']';
1099 if (isLocalToUnit())
1105 if (getScopeLineNumber() != getLineNumber())
1106 OS << " [scope " << getScopeLineNumber() << "]";
1108 StringRef Res = getName();
1110 OS << " [" << Res << ']';
1113 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1114 StringRef Res = getName();
1116 OS << " [" << Res << ']';
1118 OS << " [line " << getLineNumber() << ']';
1120 // TODO : Print context
1122 if (isLocalToUnit())
1129 void DIVariable::printInternal(raw_ostream &OS) const {
1130 StringRef Res = getName();
1132 OS << " [" << Res << ']';
1134 OS << " [line " << getLineNumber() << ']';
1137 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1138 StringRef Name = getObjCPropertyName();
1140 OS << " [" << Name << ']';
1142 OS << " [line " << getLineNumber()
1143 << ", properties " << getUnsignedField(6) << ']';
1146 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1147 const LLVMContext &Ctx) {
1148 if (!DL.isUnknown()) { // Print source line info.
1149 DIScope Scope(DL.getScope(Ctx));
1150 // Omit the directory, because it's likely to be long and uninteresting.
1152 CommentOS << Scope.getFilename();
1154 CommentOS << "<unknown>";
1155 CommentOS << ':' << DL.getLine();
1156 if (DL.getCol() != 0)
1157 CommentOS << ':' << DL.getCol();
1158 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1159 if (!InlinedAtDL.isUnknown()) {
1160 CommentOS << " @[ ";
1161 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1167 void DIVariable::printExtendedName(raw_ostream &OS) const {
1168 const LLVMContext &Ctx = DbgNode->getContext();
1169 StringRef Res = getName();
1171 OS << Res << "," << getLineNumber();
1172 if (MDNode *InlinedAt = getInlinedAt()) {
1173 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1174 if (!InlinedAtDL.isUnknown()) {
1176 printDebugLoc(InlinedAtDL, OS, Ctx);