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/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/Intrinsics.h"
25 #include "llvm/IR/Module.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 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
82 if (Elt < DbgNode->getNumOperands())
84 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
85 return CI->getSExtValue();
90 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
92 return DIDescriptor();
94 if (Elt < DbgNode->getNumOperands())
96 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
97 return DIDescriptor();
100 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
104 if (Elt < DbgNode->getNumOperands())
105 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
109 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
113 if (Elt < DbgNode->getNumOperands())
114 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
118 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
122 if (Elt < DbgNode->getNumOperands())
123 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
127 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
131 if (Elt < DbgNode->getNumOperands()) {
132 MDNode *Node = const_cast<MDNode*>(DbgNode);
133 Node->replaceOperandWith(Elt, F);
137 unsigned DIVariable::getNumAddrElements() const {
138 if (getVersion() <= LLVMDebugVersion8)
139 return DbgNode->getNumOperands()-6;
140 if (getVersion() == LLVMDebugVersion9)
141 return DbgNode->getNumOperands()-7;
142 return DbgNode->getNumOperands()-8;
145 /// getInlinedAt - If this variable is inlined then return inline location.
146 MDNode *DIVariable::getInlinedAt() const {
147 if (getVersion() <= LLVMDebugVersion9)
149 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
152 //===----------------------------------------------------------------------===//
154 //===----------------------------------------------------------------------===//
156 /// isBasicType - Return true if the specified tag is legal for
158 bool DIDescriptor::isBasicType() const {
159 if (!DbgNode) return false;
161 case dwarf::DW_TAG_base_type:
162 case dwarf::DW_TAG_unspecified_type:
169 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
170 bool DIDescriptor::isDerivedType() const {
171 if (!DbgNode) return false;
173 case dwarf::DW_TAG_typedef:
174 case dwarf::DW_TAG_pointer_type:
175 case dwarf::DW_TAG_ptr_to_member_type:
176 case dwarf::DW_TAG_reference_type:
177 case dwarf::DW_TAG_rvalue_reference_type:
178 case dwarf::DW_TAG_const_type:
179 case dwarf::DW_TAG_volatile_type:
180 case dwarf::DW_TAG_restrict_type:
181 case dwarf::DW_TAG_member:
182 case dwarf::DW_TAG_inheritance:
183 case dwarf::DW_TAG_friend:
186 // CompositeTypes are currently modelled as DerivedTypes.
187 return isCompositeType();
191 /// isCompositeType - Return true if the specified tag is legal for
193 bool DIDescriptor::isCompositeType() const {
194 if (!DbgNode) return false;
196 case dwarf::DW_TAG_array_type:
197 case dwarf::DW_TAG_structure_type:
198 case dwarf::DW_TAG_union_type:
199 case dwarf::DW_TAG_enumeration_type:
200 case dwarf::DW_TAG_vector_type:
201 case dwarf::DW_TAG_subroutine_type:
202 case dwarf::DW_TAG_class_type:
209 /// isVariable - Return true if the specified tag is legal for DIVariable.
210 bool DIDescriptor::isVariable() const {
211 if (!DbgNode) return false;
213 case dwarf::DW_TAG_auto_variable:
214 case dwarf::DW_TAG_arg_variable:
215 case dwarf::DW_TAG_return_variable:
222 /// isType - Return true if the specified tag is legal for DIType.
223 bool DIDescriptor::isType() const {
224 return isBasicType() || isCompositeType() || isDerivedType();
227 /// isSubprogram - Return true if the specified tag is legal for
229 bool DIDescriptor::isSubprogram() const {
230 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
233 /// isGlobalVariable - Return true if the specified tag is legal for
234 /// DIGlobalVariable.
235 bool DIDescriptor::isGlobalVariable() const {
236 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
237 getTag() == dwarf::DW_TAG_constant);
240 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
241 bool DIDescriptor::isGlobal() const {
242 return isGlobalVariable();
245 /// isUnspecifiedParmeter - Return true if the specified tag is
246 /// DW_TAG_unspecified_parameters.
247 bool DIDescriptor::isUnspecifiedParameter() const {
248 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
251 /// isScope - Return true if the specified tag is one of the scope
253 bool DIDescriptor::isScope() const {
254 if (!DbgNode) return false;
256 case dwarf::DW_TAG_compile_unit:
257 case dwarf::DW_TAG_lexical_block:
258 case dwarf::DW_TAG_subprogram:
259 case dwarf::DW_TAG_namespace:
267 /// isTemplateTypeParameter - Return true if the specified tag is
268 /// DW_TAG_template_type_parameter.
269 bool DIDescriptor::isTemplateTypeParameter() const {
270 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
273 /// isTemplateValueParameter - Return true if the specified tag is
274 /// DW_TAG_template_value_parameter.
275 bool DIDescriptor::isTemplateValueParameter() const {
276 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
279 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
280 bool DIDescriptor::isCompileUnit() const {
281 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
284 /// isFile - Return true if the specified tag is DW_TAG_file_type.
285 bool DIDescriptor::isFile() const {
286 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
289 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
290 bool DIDescriptor::isNameSpace() const {
291 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
294 /// isLexicalBlockFile - Return true if the specified descriptor is a
295 /// lexical block with an extra file.
296 bool DIDescriptor::isLexicalBlockFile() const {
297 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
298 (DbgNode->getNumOperands() == 3);
301 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
302 bool DIDescriptor::isLexicalBlock() const {
303 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
304 (DbgNode->getNumOperands() > 3);
307 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
308 bool DIDescriptor::isSubrange() const {
309 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
312 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
313 bool DIDescriptor::isEnumerator() const {
314 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
317 /// isObjCProperty - Return true if the specified tag is DW_TAG
318 bool DIDescriptor::isObjCProperty() const {
319 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
321 //===----------------------------------------------------------------------===//
322 // Simple Descriptor Constructors and other Methods
323 //===----------------------------------------------------------------------===//
325 DIType::DIType(const MDNode *N) : DIScope(N) {
327 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
332 unsigned DIArray::getNumElements() const {
335 return DbgNode->getNumOperands();
338 /// replaceAllUsesWith - Replace all uses of debug info referenced by
340 void DIType::replaceAllUsesWith(DIDescriptor &D) {
344 // Since we use a TrackingVH for the node, its easy for clients to manufacture
345 // legitimate situations where they want to replaceAllUsesWith() on something
346 // which, due to uniquing, has merged with the source. We shield clients from
347 // this detail by allowing a value to be replaced with replaceAllUsesWith()
350 MDNode *Node = const_cast<MDNode*>(DbgNode);
351 const MDNode *DN = D;
352 const Value *V = cast_or_null<Value>(DN);
353 Node->replaceAllUsesWith(const_cast<Value*>(V));
354 MDNode::deleteTemporary(Node);
358 /// replaceAllUsesWith - Replace all uses of debug info referenced by
360 void DIType::replaceAllUsesWith(MDNode *D) {
364 // Since we use a TrackingVH for the node, its easy for clients to manufacture
365 // legitimate situations where they want to replaceAllUsesWith() on something
366 // which, due to uniquing, has merged with the source. We shield clients from
367 // this detail by allowing a value to be replaced with replaceAllUsesWith()
370 MDNode *Node = const_cast<MDNode*>(DbgNode);
371 const MDNode *DN = D;
372 const Value *V = cast_or_null<Value>(DN);
373 Node->replaceAllUsesWith(const_cast<Value*>(V));
374 MDNode::deleteTemporary(Node);
378 /// isUnsignedDIType - Return true if type encoding is unsigned.
379 bool DIType::isUnsignedDIType() {
380 DIDerivedType DTy(DbgNode);
382 return DTy.getTypeDerivedFrom().isUnsignedDIType();
384 DIBasicType BTy(DbgNode);
386 unsigned Encoding = BTy.getEncoding();
387 if (Encoding == dwarf::DW_ATE_unsigned ||
388 Encoding == dwarf::DW_ATE_unsigned_char)
394 /// Verify - Verify that a compile unit is well formed.
395 bool DICompileUnit::Verify() const {
398 StringRef N = getFilename();
401 // It is possible that directory and produce string is empty.
405 /// Verify - Verify that an ObjC property is well formed.
406 bool DIObjCProperty::Verify() const {
409 unsigned Tag = getTag();
410 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
411 DIType Ty = getType();
412 if (!Ty.Verify()) return false;
414 // Don't worry about the rest of the strings for now.
418 /// Verify - Verify that a type descriptor is well formed.
419 bool DIType::Verify() const {
422 if (getContext() && !getContext().Verify())
424 unsigned Tag = getTag();
425 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
426 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
427 Tag != dwarf::DW_TAG_ptr_to_member_type &&
428 Tag != dwarf::DW_TAG_reference_type &&
429 Tag != dwarf::DW_TAG_rvalue_reference_type &&
430 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
431 Tag != dwarf::DW_TAG_array_type &&
432 Tag != dwarf::DW_TAG_enumeration_type &&
433 Tag != dwarf::DW_TAG_subroutine_type &&
434 getFilename().empty())
439 /// Verify - Verify that a basic type descriptor is well formed.
440 bool DIBasicType::Verify() const {
441 return isBasicType();
444 /// Verify - Verify that a derived type descriptor is well formed.
445 bool DIDerivedType::Verify() const {
446 return isDerivedType();
449 /// Verify - Verify that a composite type descriptor is well formed.
450 bool DICompositeType::Verify() const {
453 if (getContext() && !getContext().Verify())
459 /// Verify - Verify that a subprogram descriptor is well formed.
460 bool DISubprogram::Verify() const {
464 if (getContext() && !getContext().Verify())
467 DICompositeType Ty = getType();
473 /// Verify - Verify that a global variable descriptor is well formed.
474 bool DIGlobalVariable::Verify() const {
478 if (getDisplayName().empty())
481 if (getContext() && !getContext().Verify())
484 DIType Ty = getType();
488 if (!getGlobal() && !getConstant())
494 /// Verify - Verify that a variable descriptor is well formed.
495 bool DIVariable::Verify() const {
499 if (getContext() && !getContext().Verify())
502 DIType Ty = getType();
509 /// Verify - Verify that a location descriptor is well formed.
510 bool DILocation::Verify() const {
514 return DbgNode->getNumOperands() == 4;
517 /// Verify - Verify that a namespace descriptor is well formed.
518 bool DINameSpace::Verify() const {
521 if (getName().empty())
526 /// getOriginalTypeSize - If this type is derived from a base type then
527 /// return base type size.
528 uint64_t DIDerivedType::getOriginalTypeSize() const {
529 unsigned Tag = getTag();
531 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
532 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
533 Tag != dwarf::DW_TAG_restrict_type)
534 return getSizeInBits();
536 DIType BaseType = getTypeDerivedFrom();
538 // If this type is not derived from any type then take conservative approach.
539 if (!BaseType.isValid())
540 return getSizeInBits();
542 // If this is a derived type, go ahead and get the base type, unless it's a
543 // reference then it's just the size of the field. Pointer types have no need
544 // of this since they're a different type of qualification on the type.
545 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
546 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
547 return getSizeInBits();
549 if (BaseType.isDerivedType())
550 return DIDerivedType(BaseType).getOriginalTypeSize();
552 return BaseType.getSizeInBits();
555 /// getObjCProperty - Return property node, if this ivar is associated with one.
556 MDNode *DIDerivedType::getObjCProperty() const {
557 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
559 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
562 /// isInlinedFnArgument - Return true if this variable provides debugging
563 /// information for an inlined function arguments.
564 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
565 assert(CurFn && "Invalid function");
566 if (!getContext().isSubprogram())
568 // This variable is not inlined function argument if its scope
569 // does not describe current function.
570 return !DISubprogram(getContext()).describes(CurFn);
573 /// describes - Return true if this subprogram provides debugging
574 /// information for the function F.
575 bool DISubprogram::describes(const Function *F) {
576 assert(F && "Invalid function");
577 if (F == getFunction())
579 StringRef Name = getLinkageName();
582 if (F->getName() == Name)
587 unsigned DISubprogram::isOptimized() const {
588 assert (DbgNode && "Invalid subprogram descriptor!");
589 if (DbgNode->getNumOperands() == 16)
590 return getUnsignedField(15);
594 MDNode *DISubprogram::getVariablesNodes() const {
595 if (!DbgNode || DbgNode->getNumOperands() <= 19)
597 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
598 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
602 DIArray DISubprogram::getVariables() const {
603 if (!DbgNode || DbgNode->getNumOperands() <= 19)
605 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
606 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
611 StringRef DIScope::getFilename() const {
614 if (isLexicalBlockFile())
615 return DILexicalBlockFile(DbgNode).getFilename();
616 if (isLexicalBlock())
617 return DILexicalBlock(DbgNode).getFilename();
619 return DISubprogram(DbgNode).getFilename();
621 return DICompileUnit(DbgNode).getFilename();
623 return DINameSpace(DbgNode).getFilename();
625 return DIType(DbgNode).getFilename();
627 return DIFile(DbgNode).getFilename();
628 llvm_unreachable("Invalid DIScope!");
631 StringRef DIScope::getDirectory() const {
634 if (isLexicalBlockFile())
635 return DILexicalBlockFile(DbgNode).getDirectory();
636 if (isLexicalBlock())
637 return DILexicalBlock(DbgNode).getDirectory();
639 return DISubprogram(DbgNode).getDirectory();
641 return DICompileUnit(DbgNode).getDirectory();
643 return DINameSpace(DbgNode).getDirectory();
645 return DIType(DbgNode).getDirectory();
647 return DIFile(DbgNode).getDirectory();
648 llvm_unreachable("Invalid DIScope!");
651 DIArray DICompileUnit::getEnumTypes() const {
652 if (!DbgNode || DbgNode->getNumOperands() < 14)
655 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
656 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
661 DIArray DICompileUnit::getRetainedTypes() const {
662 if (!DbgNode || DbgNode->getNumOperands() < 14)
665 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
666 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
671 DIArray DICompileUnit::getSubprograms() const {
672 if (!DbgNode || DbgNode->getNumOperands() < 14)
675 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
676 if (N->getNumOperands() > 0)
677 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
683 DIArray DICompileUnit::getGlobalVariables() const {
684 if (!DbgNode || DbgNode->getNumOperands() < 14)
687 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
688 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
693 /// fixupObjcLikeName - Replace contains special characters used
694 /// in a typical Objective-C names with '.' in a given string.
695 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
696 bool isObjCLike = false;
697 for (size_t i = 0, e = Str.size(); i < e; ++i) {
702 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
703 C == '+' || C == '(' || C == ')'))
710 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
711 /// suitable to hold function specific information.
712 NamedMDNode *llvm::getFnSpecificMDNode(const 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);
723 return M.getNamedMetadata(Name.str());
726 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
727 /// to hold function specific information.
728 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
729 SmallString<32> Name = StringRef("llvm.dbg.lv.");
730 StringRef FName = "fn";
731 if (Fn.getFunction())
732 FName = Fn.getFunction()->getName();
734 FName = Fn.getName();
736 if (FName.startswith(StringRef(&One, 1)))
737 FName = FName.substr(1);
738 fixupObjcLikeName(FName, Name);
740 return M.getOrInsertNamedMetadata(Name.str());
743 /// createInlinedVariable - Create a new inlined variable based on current
745 /// @param DV Current Variable.
746 /// @param InlinedScope Location at current variable is inlined.
747 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
748 LLVMContext &VMContext) {
749 SmallVector<Value *, 16> Elts;
750 // Insert inlined scope as 7th element.
751 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
752 i == 7 ? Elts.push_back(InlinedScope) :
753 Elts.push_back(DV->getOperand(i));
754 return DIVariable(MDNode::get(VMContext, Elts));
757 /// cleanseInlinedVariable - Remove inlined scope from the variable.
758 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
759 SmallVector<Value *, 16> Elts;
760 // Insert inlined scope as 7th element.
761 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
763 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
764 Elts.push_back(DV->getOperand(i));
765 return DIVariable(MDNode::get(VMContext, Elts));
768 /// getDISubprogram - Find subprogram that is enclosing this scope.
769 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
770 DIDescriptor D(Scope);
771 if (D.isSubprogram())
772 return DISubprogram(Scope);
774 if (D.isLexicalBlockFile())
775 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
777 if (D.isLexicalBlock())
778 return getDISubprogram(DILexicalBlock(Scope).getContext());
780 return DISubprogram();
783 /// getDICompositeType - Find underlying composite type.
784 DICompositeType llvm::getDICompositeType(DIType T) {
785 if (T.isCompositeType())
786 return DICompositeType(T);
788 if (T.isDerivedType())
789 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
791 return DICompositeType();
794 /// isSubprogramContext - Return true if Context is either a subprogram
795 /// or another context nested inside a subprogram.
796 bool llvm::isSubprogramContext(const MDNode *Context) {
799 DIDescriptor D(Context);
800 if (D.isSubprogram())
803 return isSubprogramContext(DIType(Context).getContext());
807 //===----------------------------------------------------------------------===//
808 // DebugInfoFinder implementations.
809 //===----------------------------------------------------------------------===//
811 /// processModule - Process entire module and collect debug info.
812 void DebugInfoFinder::processModule(const Module &M) {
813 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
814 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
815 DICompileUnit CU(CU_Nodes->getOperand(i));
817 if (CU.getVersion() > LLVMDebugVersion10) {
818 DIArray GVs = CU.getGlobalVariables();
819 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
820 DIGlobalVariable DIG(GVs.getElement(i));
821 if (addGlobalVariable(DIG))
822 processType(DIG.getType());
824 DIArray SPs = CU.getSubprograms();
825 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
826 processSubprogram(DISubprogram(SPs.getElement(i)));
827 DIArray EnumTypes = CU.getEnumTypes();
828 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
829 processType(DIType(EnumTypes.getElement(i)));
830 DIArray RetainedTypes = CU.getRetainedTypes();
831 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
832 processType(DIType(RetainedTypes.getElement(i)));
838 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
839 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
841 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
843 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
846 DebugLoc Loc = BI->getDebugLoc();
850 LLVMContext &Ctx = BI->getContext();
851 DIDescriptor Scope(Loc.getScope(Ctx));
853 if (Scope.isCompileUnit())
854 addCompileUnit(DICompileUnit(Scope));
855 else if (Scope.isSubprogram())
856 processSubprogram(DISubprogram(Scope));
857 else if (Scope.isLexicalBlockFile()) {
858 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
859 processLexicalBlock(DILexicalBlock(DBF.getScope()));
861 else if (Scope.isLexicalBlock())
862 processLexicalBlock(DILexicalBlock(Scope));
864 if (MDNode *IA = Loc.getInlinedAt(Ctx))
865 processLocation(DILocation(IA));
868 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
869 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
870 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
871 if (addGlobalVariable(DIG)) {
872 if (DIG.getVersion() <= LLVMDebugVersion10)
873 addCompileUnit(DIG.getCompileUnit());
874 processType(DIG.getType());
879 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
880 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
881 processSubprogram(DISubprogram(NMD->getOperand(i)));
884 /// processLocation - Process DILocation.
885 void DebugInfoFinder::processLocation(DILocation Loc) {
886 if (!Loc.Verify()) return;
887 DIDescriptor S(Loc.getScope());
888 if (S.isCompileUnit())
889 addCompileUnit(DICompileUnit(S));
890 else if (S.isSubprogram())
891 processSubprogram(DISubprogram(S));
892 else if (S.isLexicalBlock())
893 processLexicalBlock(DILexicalBlock(S));
894 else if (S.isLexicalBlockFile()) {
895 DILexicalBlockFile DBF = DILexicalBlockFile(S);
896 processLexicalBlock(DILexicalBlock(DBF.getScope()));
898 processLocation(Loc.getOrigLocation());
901 /// processType - Process DIType.
902 void DebugInfoFinder::processType(DIType DT) {
905 if (DT.getVersion() <= LLVMDebugVersion10)
906 addCompileUnit(DT.getCompileUnit());
907 if (DT.isCompositeType()) {
908 DICompositeType DCT(DT);
909 processType(DCT.getTypeDerivedFrom());
910 DIArray DA = DCT.getTypeArray();
911 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
912 DIDescriptor D = DA.getElement(i);
914 processType(DIType(D));
915 else if (D.isSubprogram())
916 processSubprogram(DISubprogram(D));
918 } else if (DT.isDerivedType()) {
919 DIDerivedType DDT(DT);
920 processType(DDT.getTypeDerivedFrom());
924 /// processLexicalBlock
925 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
926 DIScope Context = LB.getContext();
927 if (Context.isLexicalBlock())
928 return processLexicalBlock(DILexicalBlock(Context));
929 else if (Context.isLexicalBlockFile()) {
930 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
931 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
934 return processSubprogram(DISubprogram(Context));
937 /// processSubprogram - Process DISubprogram.
938 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
939 if (!addSubprogram(SP))
941 if (SP.getVersion() <= LLVMDebugVersion10)
942 addCompileUnit(SP.getCompileUnit());
943 processType(SP.getType());
946 /// processDeclare - Process DbgDeclareInst.
947 void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
948 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
952 if (!DV.isVariable())
955 if (!NodesSeen.insert(DV))
957 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
958 addCompileUnit(DIVariable(N).getCompileUnit());
959 processType(DIVariable(N).getType());
962 /// addType - Add type into Tys.
963 bool DebugInfoFinder::addType(DIType DT) {
967 if (!NodesSeen.insert(DT))
974 /// addCompileUnit - Add compile unit into CUs.
975 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
979 if (!NodesSeen.insert(CU))
986 /// addGlobalVariable - Add global variable into GVs.
987 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
988 if (!DIDescriptor(DIG).isGlobalVariable())
991 if (!NodesSeen.insert(DIG))
998 // addSubprogram - Add subprgoram into SPs.
999 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1000 if (!DIDescriptor(SP).isSubprogram())
1003 if (!NodesSeen.insert(SP))
1010 //===----------------------------------------------------------------------===//
1011 // DIDescriptor: dump routines for all descriptors.
1012 //===----------------------------------------------------------------------===//
1014 /// dump - Print descriptor to dbgs() with a newline.
1015 void DIDescriptor::dump() const {
1016 print(dbgs()); dbgs() << '\n';
1019 /// print - Print descriptor.
1020 void DIDescriptor::print(raw_ostream &OS) const {
1021 if (!DbgNode) return;
1023 if (const char *Tag = dwarf::TagString(getTag()))
1024 OS << "[ " << Tag << " ]";
1026 if (this->isSubrange()) {
1027 DISubrange(DbgNode).printInternal(OS);
1028 } else if (this->isCompileUnit()) {
1029 DICompileUnit(DbgNode).printInternal(OS);
1030 } else if (this->isFile()) {
1031 DIFile(DbgNode).printInternal(OS);
1032 } else if (this->isEnumerator()) {
1033 DIEnumerator(DbgNode).printInternal(OS);
1034 } else if (this->isBasicType()) {
1035 DIType(DbgNode).printInternal(OS);
1036 } else if (this->isDerivedType()) {
1037 DIDerivedType(DbgNode).printInternal(OS);
1038 } else if (this->isCompositeType()) {
1039 DICompositeType(DbgNode).printInternal(OS);
1040 } else if (this->isSubprogram()) {
1041 DISubprogram(DbgNode).printInternal(OS);
1042 } else if (this->isGlobalVariable()) {
1043 DIGlobalVariable(DbgNode).printInternal(OS);
1044 } else if (this->isVariable()) {
1045 DIVariable(DbgNode).printInternal(OS);
1046 } else if (this->isObjCProperty()) {
1047 DIObjCProperty(DbgNode).printInternal(OS);
1048 } else if (this->isScope()) {
1049 DIScope(DbgNode).printInternal(OS);
1053 void DISubrange::printInternal(raw_ostream &OS) const {
1054 int64_t Count = getCount();
1056 OS << " [" << getLo() << ", " << Count - 1 << ']';
1058 OS << " [unbounded]";
1061 void DIScope::printInternal(raw_ostream &OS) const {
1062 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1065 void DICompileUnit::printInternal(raw_ostream &OS) const {
1066 DIScope::printInternal(OS);
1067 if (unsigned Lang = getLanguage())
1068 OS << " [" << dwarf::LanguageString(Lang) << ']';
1071 void DIEnumerator::printInternal(raw_ostream &OS) const {
1072 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1075 void DIType::printInternal(raw_ostream &OS) const {
1076 if (!DbgNode) return;
1078 StringRef Res = getName();
1080 OS << " [" << Res << "]";
1082 // TODO: Print context?
1084 OS << " [line " << getLineNumber()
1085 << ", size " << getSizeInBits()
1086 << ", align " << getAlignInBits()
1087 << ", offset " << getOffsetInBits();
1089 if (const char *Enc =
1090 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1091 OS << ", enc " << Enc;
1096 else if (isProtected())
1097 OS << " [protected]";
1099 if (isForwardDecl())
1103 void DIDerivedType::printInternal(raw_ostream &OS) const {
1104 DIType::printInternal(OS);
1105 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1108 void DICompositeType::printInternal(raw_ostream &OS) const {
1109 DIType::printInternal(OS);
1110 DIArray A = getTypeArray();
1111 OS << " [" << A.getNumElements() << " elements]";
1114 void DISubprogram::printInternal(raw_ostream &OS) const {
1115 // TODO : Print context
1116 OS << " [line " << getLineNumber() << ']';
1118 if (isLocalToUnit())
1124 if (getScopeLineNumber() != getLineNumber())
1125 OS << " [scope " << getScopeLineNumber() << "]";
1129 else if (isProtected())
1130 OS << " [protected]";
1132 StringRef Res = getName();
1134 OS << " [" << Res << ']';
1137 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1138 StringRef Res = getName();
1140 OS << " [" << Res << ']';
1142 OS << " [line " << getLineNumber() << ']';
1144 // TODO : Print context
1146 if (isLocalToUnit())
1153 void DIVariable::printInternal(raw_ostream &OS) const {
1154 StringRef Res = getName();
1156 OS << " [" << Res << ']';
1158 OS << " [line " << getLineNumber() << ']';
1161 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1162 StringRef Name = getObjCPropertyName();
1164 OS << " [" << Name << ']';
1166 OS << " [line " << getLineNumber()
1167 << ", properties " << getUnsignedField(6) << ']';
1170 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1171 const LLVMContext &Ctx) {
1172 if (!DL.isUnknown()) { // Print source line info.
1173 DIScope Scope(DL.getScope(Ctx));
1174 // Omit the directory, because it's likely to be long and uninteresting.
1176 CommentOS << Scope.getFilename();
1178 CommentOS << "<unknown>";
1179 CommentOS << ':' << DL.getLine();
1180 if (DL.getCol() != 0)
1181 CommentOS << ':' << DL.getCol();
1182 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1183 if (!InlinedAtDL.isUnknown()) {
1184 CommentOS << " @[ ";
1185 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1191 void DIVariable::printExtendedName(raw_ostream &OS) const {
1192 const LLVMContext &Ctx = DbgNode->getContext();
1193 StringRef Res = getName();
1195 OS << Res << "," << getLineNumber();
1196 if (MDNode *InlinedAt = getInlinedAt()) {
1197 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1198 if (!InlinedAtDL.isUnknown()) {
1200 printDebugLoc(InlinedAtDL, OS, Ctx);