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_reference_type:
176 case dwarf::DW_TAG_rvalue_reference_type:
177 case dwarf::DW_TAG_const_type:
178 case dwarf::DW_TAG_volatile_type:
179 case dwarf::DW_TAG_restrict_type:
180 case dwarf::DW_TAG_member:
181 case dwarf::DW_TAG_inheritance:
182 case dwarf::DW_TAG_friend:
185 // CompositeTypes are currently modelled as DerivedTypes.
186 return isCompositeType();
190 /// isCompositeType - Return true if the specified tag is legal for
192 bool DIDescriptor::isCompositeType() const {
193 if (!DbgNode) return false;
195 case dwarf::DW_TAG_array_type:
196 case dwarf::DW_TAG_structure_type:
197 case dwarf::DW_TAG_union_type:
198 case dwarf::DW_TAG_enumeration_type:
199 case dwarf::DW_TAG_vector_type:
200 case dwarf::DW_TAG_subroutine_type:
201 case dwarf::DW_TAG_class_type:
208 /// isVariable - Return true if the specified tag is legal for DIVariable.
209 bool DIDescriptor::isVariable() const {
210 if (!DbgNode) return false;
212 case dwarf::DW_TAG_auto_variable:
213 case dwarf::DW_TAG_arg_variable:
214 case dwarf::DW_TAG_return_variable:
221 /// isType - Return true if the specified tag is legal for DIType.
222 bool DIDescriptor::isType() const {
223 return isBasicType() || isCompositeType() || isDerivedType();
226 /// isSubprogram - Return true if the specified tag is legal for
228 bool DIDescriptor::isSubprogram() const {
229 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
232 /// isGlobalVariable - Return true if the specified tag is legal for
233 /// DIGlobalVariable.
234 bool DIDescriptor::isGlobalVariable() const {
235 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
236 getTag() == dwarf::DW_TAG_constant);
239 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
240 bool DIDescriptor::isGlobal() const {
241 return isGlobalVariable();
244 /// isUnspecifiedParmeter - Return true if the specified tag is
245 /// DW_TAG_unspecified_parameters.
246 bool DIDescriptor::isUnspecifiedParameter() const {
247 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
250 /// isScope - Return true if the specified tag is one of the scope
252 bool DIDescriptor::isScope() const {
253 if (!DbgNode) return false;
255 case dwarf::DW_TAG_compile_unit:
256 case dwarf::DW_TAG_lexical_block:
257 case dwarf::DW_TAG_subprogram:
258 case dwarf::DW_TAG_namespace:
266 /// isTemplateTypeParameter - Return true if the specified tag is
267 /// DW_TAG_template_type_parameter.
268 bool DIDescriptor::isTemplateTypeParameter() const {
269 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
272 /// isTemplateValueParameter - Return true if the specified tag is
273 /// DW_TAG_template_value_parameter.
274 bool DIDescriptor::isTemplateValueParameter() const {
275 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
278 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
279 bool DIDescriptor::isCompileUnit() const {
280 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
283 /// isFile - Return true if the specified tag is DW_TAG_file_type.
284 bool DIDescriptor::isFile() const {
285 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
288 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
289 bool DIDescriptor::isNameSpace() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
293 /// isLexicalBlockFile - Return true if the specified descriptor is a
294 /// lexical block with an extra file.
295 bool DIDescriptor::isLexicalBlockFile() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
297 (DbgNode->getNumOperands() == 3);
300 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
301 bool DIDescriptor::isLexicalBlock() const {
302 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
303 (DbgNode->getNumOperands() > 3);
306 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
307 bool DIDescriptor::isSubrange() const {
308 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
311 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
312 bool DIDescriptor::isEnumerator() const {
313 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
316 /// isObjCProperty - Return true if the specified tag is DW_TAG
317 bool DIDescriptor::isObjCProperty() const {
318 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
320 //===----------------------------------------------------------------------===//
321 // Simple Descriptor Constructors and other Methods
322 //===----------------------------------------------------------------------===//
324 DIType::DIType(const MDNode *N) : DIScope(N) {
326 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
331 unsigned DIArray::getNumElements() const {
334 return DbgNode->getNumOperands();
337 /// replaceAllUsesWith - Replace all uses of debug info referenced by
339 void DIType::replaceAllUsesWith(DIDescriptor &D) {
343 // Since we use a TrackingVH for the node, its easy for clients to manufacture
344 // legitimate situations where they want to replaceAllUsesWith() on something
345 // which, due to uniquing, has merged with the source. We shield clients from
346 // this detail by allowing a value to be replaced with replaceAllUsesWith()
349 MDNode *Node = const_cast<MDNode*>(DbgNode);
350 const MDNode *DN = D;
351 const Value *V = cast_or_null<Value>(DN);
352 Node->replaceAllUsesWith(const_cast<Value*>(V));
353 MDNode::deleteTemporary(Node);
357 /// replaceAllUsesWith - Replace all uses of debug info referenced by
359 void DIType::replaceAllUsesWith(MDNode *D) {
363 // Since we use a TrackingVH for the node, its easy for clients to manufacture
364 // legitimate situations where they want to replaceAllUsesWith() on something
365 // which, due to uniquing, has merged with the source. We shield clients from
366 // this detail by allowing a value to be replaced with replaceAllUsesWith()
369 MDNode *Node = const_cast<MDNode*>(DbgNode);
370 const MDNode *DN = D;
371 const Value *V = cast_or_null<Value>(DN);
372 Node->replaceAllUsesWith(const_cast<Value*>(V));
373 MDNode::deleteTemporary(Node);
377 /// isUnsignedDIType - Return true if type encoding is unsigned.
378 bool DIType::isUnsignedDIType() {
379 DIDerivedType DTy(DbgNode);
381 return DTy.getTypeDerivedFrom().isUnsignedDIType();
383 DIBasicType BTy(DbgNode);
385 unsigned Encoding = BTy.getEncoding();
386 if (Encoding == dwarf::DW_ATE_unsigned ||
387 Encoding == dwarf::DW_ATE_unsigned_char)
393 /// Verify - Verify that a compile unit is well formed.
394 bool DICompileUnit::Verify() const {
397 StringRef N = getFilename();
400 // It is possible that directory and produce string is empty.
404 /// Verify - Verify that an ObjC property is well formed.
405 bool DIObjCProperty::Verify() const {
408 unsigned Tag = getTag();
409 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
410 DIType Ty = getType();
411 if (!Ty.Verify()) return false;
413 // Don't worry about the rest of the strings for now.
417 /// Verify - Verify that a type descriptor is well formed.
418 bool DIType::Verify() const {
421 if (getContext() && !getContext().Verify())
423 unsigned Tag = getTag();
424 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
425 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
426 Tag != dwarf::DW_TAG_reference_type &&
427 Tag != dwarf::DW_TAG_rvalue_reference_type &&
428 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
429 Tag != dwarf::DW_TAG_array_type &&
430 Tag != dwarf::DW_TAG_enumeration_type &&
431 Tag != dwarf::DW_TAG_subroutine_type &&
432 getFilename().empty())
437 /// Verify - Verify that a basic type descriptor is well formed.
438 bool DIBasicType::Verify() const {
439 return isBasicType();
442 /// Verify - Verify that a derived type descriptor is well formed.
443 bool DIDerivedType::Verify() const {
444 return isDerivedType();
447 /// Verify - Verify that a composite type descriptor is well formed.
448 bool DICompositeType::Verify() const {
451 if (getContext() && !getContext().Verify())
457 /// Verify - Verify that a subprogram descriptor is well formed.
458 bool DISubprogram::Verify() const {
462 if (getContext() && !getContext().Verify())
465 DICompositeType Ty = getType();
471 /// Verify - Verify that a global variable descriptor is well formed.
472 bool DIGlobalVariable::Verify() const {
476 if (getDisplayName().empty())
479 if (getContext() && !getContext().Verify())
482 DIType Ty = getType();
486 if (!getGlobal() && !getConstant())
492 /// Verify - Verify that a variable descriptor is well formed.
493 bool DIVariable::Verify() const {
497 if (getContext() && !getContext().Verify())
500 DIType Ty = getType();
507 /// Verify - Verify that a location descriptor is well formed.
508 bool DILocation::Verify() const {
512 return DbgNode->getNumOperands() == 4;
515 /// Verify - Verify that a namespace descriptor is well formed.
516 bool DINameSpace::Verify() const {
519 if (getName().empty())
524 /// getOriginalTypeSize - If this type is derived from a base type then
525 /// return base type size.
526 uint64_t DIDerivedType::getOriginalTypeSize() const {
527 unsigned Tag = getTag();
529 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
530 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
531 Tag != dwarf::DW_TAG_restrict_type)
532 return getSizeInBits();
534 DIType BaseType = getTypeDerivedFrom();
536 // If this type is not derived from any type then take conservative approach.
537 if (!BaseType.isValid())
538 return getSizeInBits();
540 // If this is a derived type, go ahead and get the base type, unless it's a
541 // reference then it's just the size of the field. Pointer types have no need
542 // of this since they're a different type of qualification on the type.
543 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
544 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
545 return getSizeInBits();
547 if (BaseType.isDerivedType())
548 return DIDerivedType(BaseType).getOriginalTypeSize();
550 return BaseType.getSizeInBits();
553 /// getObjCProperty - Return property node, if this ivar is associated with one.
554 MDNode *DIDerivedType::getObjCProperty() const {
555 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
557 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
560 /// isInlinedFnArgument - Return true if this variable provides debugging
561 /// information for an inlined function arguments.
562 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
563 assert(CurFn && "Invalid function");
564 if (!getContext().isSubprogram())
566 // This variable is not inlined function argument if its scope
567 // does not describe current function.
568 return !DISubprogram(getContext()).describes(CurFn);
571 /// describes - Return true if this subprogram provides debugging
572 /// information for the function F.
573 bool DISubprogram::describes(const Function *F) {
574 assert(F && "Invalid function");
575 if (F == getFunction())
577 StringRef Name = getLinkageName();
580 if (F->getName() == Name)
585 unsigned DISubprogram::isOptimized() const {
586 assert (DbgNode && "Invalid subprogram descriptor!");
587 if (DbgNode->getNumOperands() == 16)
588 return getUnsignedField(15);
592 MDNode *DISubprogram::getVariablesNodes() const {
593 if (!DbgNode || DbgNode->getNumOperands() <= 19)
595 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
596 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
600 DIArray DISubprogram::getVariables() const {
601 if (!DbgNode || DbgNode->getNumOperands() <= 19)
603 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
604 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
609 StringRef DIScope::getFilename() const {
612 if (isLexicalBlockFile())
613 return DILexicalBlockFile(DbgNode).getFilename();
614 if (isLexicalBlock())
615 return DILexicalBlock(DbgNode).getFilename();
617 return DISubprogram(DbgNode).getFilename();
619 return DICompileUnit(DbgNode).getFilename();
621 return DINameSpace(DbgNode).getFilename();
623 return DIType(DbgNode).getFilename();
625 return DIFile(DbgNode).getFilename();
626 llvm_unreachable("Invalid DIScope!");
629 StringRef DIScope::getDirectory() const {
632 if (isLexicalBlockFile())
633 return DILexicalBlockFile(DbgNode).getDirectory();
634 if (isLexicalBlock())
635 return DILexicalBlock(DbgNode).getDirectory();
637 return DISubprogram(DbgNode).getDirectory();
639 return DICompileUnit(DbgNode).getDirectory();
641 return DINameSpace(DbgNode).getDirectory();
643 return DIType(DbgNode).getDirectory();
645 return DIFile(DbgNode).getDirectory();
646 llvm_unreachable("Invalid DIScope!");
649 DIArray DICompileUnit::getEnumTypes() const {
650 if (!DbgNode || DbgNode->getNumOperands() < 14)
653 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
654 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
659 DIArray DICompileUnit::getRetainedTypes() const {
660 if (!DbgNode || DbgNode->getNumOperands() < 14)
663 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
664 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
669 DIArray DICompileUnit::getSubprograms() const {
670 if (!DbgNode || DbgNode->getNumOperands() < 14)
673 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
674 if (N->getNumOperands() > 0)
675 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
681 DIArray DICompileUnit::getGlobalVariables() const {
682 if (!DbgNode || DbgNode->getNumOperands() < 14)
685 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
686 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
691 /// fixupObjcLikeName - Replace contains special characters used
692 /// in a typical Objective-C names with '.' in a given string.
693 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
694 bool isObjCLike = false;
695 for (size_t i = 0, e = Str.size(); i < e; ++i) {
700 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
701 C == '+' || C == '(' || C == ')'))
708 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
709 /// suitable to hold function specific information.
710 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
711 SmallString<32> Name = StringRef("llvm.dbg.lv.");
712 StringRef FName = "fn";
713 if (Fn.getFunction())
714 FName = Fn.getFunction()->getName();
716 FName = Fn.getName();
718 if (FName.startswith(StringRef(&One, 1)))
719 FName = FName.substr(1);
720 fixupObjcLikeName(FName, Name);
721 return M.getNamedMetadata(Name.str());
724 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
725 /// to hold function specific information.
726 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
727 SmallString<32> Name = StringRef("llvm.dbg.lv.");
728 StringRef FName = "fn";
729 if (Fn.getFunction())
730 FName = Fn.getFunction()->getName();
732 FName = Fn.getName();
734 if (FName.startswith(StringRef(&One, 1)))
735 FName = FName.substr(1);
736 fixupObjcLikeName(FName, Name);
738 return M.getOrInsertNamedMetadata(Name.str());
741 /// createInlinedVariable - Create a new inlined variable based on current
743 /// @param DV Current Variable.
744 /// @param InlinedScope Location at current variable is inlined.
745 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
746 LLVMContext &VMContext) {
747 SmallVector<Value *, 16> Elts;
748 // Insert inlined scope as 7th element.
749 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
750 i == 7 ? Elts.push_back(InlinedScope) :
751 Elts.push_back(DV->getOperand(i));
752 return DIVariable(MDNode::get(VMContext, Elts));
755 /// cleanseInlinedVariable - Remove inlined scope from the variable.
756 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
757 SmallVector<Value *, 16> Elts;
758 // Insert inlined scope as 7th element.
759 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
761 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
762 Elts.push_back(DV->getOperand(i));
763 return DIVariable(MDNode::get(VMContext, Elts));
766 /// getDISubprogram - Find subprogram that is enclosing this scope.
767 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
768 DIDescriptor D(Scope);
769 if (D.isSubprogram())
770 return DISubprogram(Scope);
772 if (D.isLexicalBlockFile())
773 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
775 if (D.isLexicalBlock())
776 return getDISubprogram(DILexicalBlock(Scope).getContext());
778 return DISubprogram();
781 /// getDICompositeType - Find underlying composite type.
782 DICompositeType llvm::getDICompositeType(DIType T) {
783 if (T.isCompositeType())
784 return DICompositeType(T);
786 if (T.isDerivedType())
787 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
789 return DICompositeType();
792 /// isSubprogramContext - Return true if Context is either a subprogram
793 /// or another context nested inside a subprogram.
794 bool llvm::isSubprogramContext(const MDNode *Context) {
797 DIDescriptor D(Context);
798 if (D.isSubprogram())
801 return isSubprogramContext(DIType(Context).getContext());
805 //===----------------------------------------------------------------------===//
806 // DebugInfoFinder implementations.
807 //===----------------------------------------------------------------------===//
809 /// processModule - Process entire module and collect debug info.
810 void DebugInfoFinder::processModule(const Module &M) {
811 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
812 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
813 DICompileUnit CU(CU_Nodes->getOperand(i));
815 if (CU.getVersion() > LLVMDebugVersion10) {
816 DIArray GVs = CU.getGlobalVariables();
817 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
818 DIGlobalVariable DIG(GVs.getElement(i));
819 if (addGlobalVariable(DIG))
820 processType(DIG.getType());
822 DIArray SPs = CU.getSubprograms();
823 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
824 processSubprogram(DISubprogram(SPs.getElement(i)));
825 DIArray EnumTypes = CU.getEnumTypes();
826 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
827 processType(DIType(EnumTypes.getElement(i)));
828 DIArray RetainedTypes = CU.getRetainedTypes();
829 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
830 processType(DIType(RetainedTypes.getElement(i)));
836 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
837 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
839 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
841 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
844 DebugLoc Loc = BI->getDebugLoc();
848 LLVMContext &Ctx = BI->getContext();
849 DIDescriptor Scope(Loc.getScope(Ctx));
851 if (Scope.isCompileUnit())
852 addCompileUnit(DICompileUnit(Scope));
853 else if (Scope.isSubprogram())
854 processSubprogram(DISubprogram(Scope));
855 else if (Scope.isLexicalBlockFile()) {
856 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
857 processLexicalBlock(DILexicalBlock(DBF.getScope()));
859 else if (Scope.isLexicalBlock())
860 processLexicalBlock(DILexicalBlock(Scope));
862 if (MDNode *IA = Loc.getInlinedAt(Ctx))
863 processLocation(DILocation(IA));
866 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
867 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
868 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
869 if (addGlobalVariable(DIG)) {
870 if (DIG.getVersion() <= LLVMDebugVersion10)
871 addCompileUnit(DIG.getCompileUnit());
872 processType(DIG.getType());
877 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
878 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
879 processSubprogram(DISubprogram(NMD->getOperand(i)));
882 /// processLocation - Process DILocation.
883 void DebugInfoFinder::processLocation(DILocation Loc) {
884 if (!Loc.Verify()) return;
885 DIDescriptor S(Loc.getScope());
886 if (S.isCompileUnit())
887 addCompileUnit(DICompileUnit(S));
888 else if (S.isSubprogram())
889 processSubprogram(DISubprogram(S));
890 else if (S.isLexicalBlock())
891 processLexicalBlock(DILexicalBlock(S));
892 else if (S.isLexicalBlockFile()) {
893 DILexicalBlockFile DBF = DILexicalBlockFile(S);
894 processLexicalBlock(DILexicalBlock(DBF.getScope()));
896 processLocation(Loc.getOrigLocation());
899 /// processType - Process DIType.
900 void DebugInfoFinder::processType(DIType DT) {
903 if (DT.getVersion() <= LLVMDebugVersion10)
904 addCompileUnit(DT.getCompileUnit());
905 if (DT.isCompositeType()) {
906 DICompositeType DCT(DT);
907 processType(DCT.getTypeDerivedFrom());
908 DIArray DA = DCT.getTypeArray();
909 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
910 DIDescriptor D = DA.getElement(i);
912 processType(DIType(D));
913 else if (D.isSubprogram())
914 processSubprogram(DISubprogram(D));
916 } else if (DT.isDerivedType()) {
917 DIDerivedType DDT(DT);
918 processType(DDT.getTypeDerivedFrom());
922 /// processLexicalBlock
923 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
924 DIScope Context = LB.getContext();
925 if (Context.isLexicalBlock())
926 return processLexicalBlock(DILexicalBlock(Context));
927 else if (Context.isLexicalBlockFile()) {
928 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
929 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
932 return processSubprogram(DISubprogram(Context));
935 /// processSubprogram - Process DISubprogram.
936 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
937 if (!addSubprogram(SP))
939 if (SP.getVersion() <= LLVMDebugVersion10)
940 addCompileUnit(SP.getCompileUnit());
941 processType(SP.getType());
944 /// processDeclare - Process DbgDeclareInst.
945 void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
946 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
950 if (!DV.isVariable())
953 if (!NodesSeen.insert(DV))
955 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
956 addCompileUnit(DIVariable(N).getCompileUnit());
957 processType(DIVariable(N).getType());
960 /// addType - Add type into Tys.
961 bool DebugInfoFinder::addType(DIType DT) {
965 if (!NodesSeen.insert(DT))
972 /// addCompileUnit - Add compile unit into CUs.
973 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
977 if (!NodesSeen.insert(CU))
984 /// addGlobalVariable - Add global variable into GVs.
985 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
986 if (!DIDescriptor(DIG).isGlobalVariable())
989 if (!NodesSeen.insert(DIG))
996 // addSubprogram - Add subprgoram into SPs.
997 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
998 if (!DIDescriptor(SP).isSubprogram())
1001 if (!NodesSeen.insert(SP))
1008 //===----------------------------------------------------------------------===//
1009 // DIDescriptor: dump routines for all descriptors.
1010 //===----------------------------------------------------------------------===//
1012 /// dump - Print descriptor to dbgs() with a newline.
1013 void DIDescriptor::dump() const {
1014 print(dbgs()); dbgs() << '\n';
1017 /// print - Print descriptor.
1018 void DIDescriptor::print(raw_ostream &OS) const {
1019 if (!DbgNode) return;
1021 if (const char *Tag = dwarf::TagString(getTag()))
1022 OS << "[ " << Tag << " ]";
1024 if (this->isSubrange()) {
1025 DISubrange(DbgNode).printInternal(OS);
1026 } else if (this->isCompileUnit()) {
1027 DICompileUnit(DbgNode).printInternal(OS);
1028 } else if (this->isFile()) {
1029 DIFile(DbgNode).printInternal(OS);
1030 } else if (this->isEnumerator()) {
1031 DIEnumerator(DbgNode).printInternal(OS);
1032 } else if (this->isBasicType()) {
1033 DIType(DbgNode).printInternal(OS);
1034 } else if (this->isDerivedType()) {
1035 DIDerivedType(DbgNode).printInternal(OS);
1036 } else if (this->isCompositeType()) {
1037 DICompositeType(DbgNode).printInternal(OS);
1038 } else if (this->isSubprogram()) {
1039 DISubprogram(DbgNode).printInternal(OS);
1040 } else if (this->isGlobalVariable()) {
1041 DIGlobalVariable(DbgNode).printInternal(OS);
1042 } else if (this->isVariable()) {
1043 DIVariable(DbgNode).printInternal(OS);
1044 } else if (this->isObjCProperty()) {
1045 DIObjCProperty(DbgNode).printInternal(OS);
1046 } else if (this->isScope()) {
1047 DIScope(DbgNode).printInternal(OS);
1051 void DISubrange::printInternal(raw_ostream &OS) const {
1052 int64_t Count = getCount();
1054 OS << " [" << getLo() << ", " << Count - 1 << ']';
1056 OS << " [unbounded]";
1059 void DIScope::printInternal(raw_ostream &OS) const {
1060 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1063 void DICompileUnit::printInternal(raw_ostream &OS) const {
1064 DIScope::printInternal(OS);
1065 if (unsigned Lang = getLanguage())
1066 OS << " [" << dwarf::LanguageString(Lang) << ']';
1069 void DIEnumerator::printInternal(raw_ostream &OS) const {
1070 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1073 void DIType::printInternal(raw_ostream &OS) const {
1074 if (!DbgNode) return;
1076 StringRef Res = getName();
1078 OS << " [" << Res << "]";
1080 // TODO: Print context?
1082 OS << " [line " << getLineNumber()
1083 << ", size " << getSizeInBits()
1084 << ", align " << getAlignInBits()
1085 << ", offset " << getOffsetInBits();
1087 if (const char *Enc =
1088 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1089 OS << ", enc " << Enc;
1094 else if (isProtected())
1095 OS << " [protected]";
1097 if (isForwardDecl())
1101 void DIDerivedType::printInternal(raw_ostream &OS) const {
1102 DIType::printInternal(OS);
1103 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1106 void DICompositeType::printInternal(raw_ostream &OS) const {
1107 DIType::printInternal(OS);
1108 DIArray A = getTypeArray();
1109 OS << " [" << A.getNumElements() << " elements]";
1112 void DISubprogram::printInternal(raw_ostream &OS) const {
1113 // TODO : Print context
1114 OS << " [line " << getLineNumber() << ']';
1116 if (isLocalToUnit())
1122 if (getScopeLineNumber() != getLineNumber())
1123 OS << " [scope " << getScopeLineNumber() << "]";
1127 else if (isProtected())
1128 OS << " [protected]";
1130 StringRef Res = getName();
1132 OS << " [" << Res << ']';
1135 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1136 StringRef Res = getName();
1138 OS << " [" << Res << ']';
1140 OS << " [line " << getLineNumber() << ']';
1142 // TODO : Print context
1144 if (isLocalToUnit())
1151 void DIVariable::printInternal(raw_ostream &OS) const {
1152 StringRef Res = getName();
1154 OS << " [" << Res << ']';
1156 OS << " [line " << getLineNumber() << ']';
1159 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1160 StringRef Name = getObjCPropertyName();
1162 OS << " [" << Name << ']';
1164 OS << " [line " << getLineNumber()
1165 << ", properties " << getUnsignedField(6) << ']';
1168 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1169 const LLVMContext &Ctx) {
1170 if (!DL.isUnknown()) { // Print source line info.
1171 DIScope Scope(DL.getScope(Ctx));
1172 // Omit the directory, because it's likely to be long and uninteresting.
1174 CommentOS << Scope.getFilename();
1176 CommentOS << "<unknown>";
1177 CommentOS << ':' << DL.getLine();
1178 if (DL.getCol() != 0)
1179 CommentOS << ':' << DL.getCol();
1180 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1181 if (!InlinedAtDL.isUnknown()) {
1182 CommentOS << " @[ ";
1183 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1189 void DIVariable::printExtendedName(raw_ostream &OS) const {
1190 const LLVMContext &Ctx = DbgNode->getContext();
1191 StringRef Res = getName();
1193 OS << Res << "," << getLineNumber();
1194 if (MDNode *InlinedAt = getInlinedAt()) {
1195 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1196 if (!InlinedAtDL.isUnknown()) {
1198 printDebugLoc(InlinedAtDL, OS, Ctx);