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) {
54 bool DIDescriptor::Verify() const {
56 (DIDerivedType(DbgNode).Verify() ||
57 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
58 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
59 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
60 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
61 DILexicalBlock(DbgNode).Verify() ||
62 DILexicalBlockFile(DbgNode).Verify() ||
63 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
64 DIObjCProperty(DbgNode).Verify() ||
65 DITemplateTypeParameter(DbgNode).Verify() ||
66 DITemplateValueParameter(DbgNode).Verify());
70 DIDescriptor::getStringField(unsigned Elt) const {
74 if (Elt < DbgNode->getNumOperands())
75 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
76 return MDS->getString();
81 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
85 if (Elt < DbgNode->getNumOperands())
87 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
88 return CI->getZExtValue();
93 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
97 if (Elt < DbgNode->getNumOperands())
99 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
100 return CI->getSExtValue();
105 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
107 return DIDescriptor();
109 if (Elt < DbgNode->getNumOperands())
111 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
112 return DIDescriptor();
115 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
119 if (Elt < DbgNode->getNumOperands())
120 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
124 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
128 if (Elt < DbgNode->getNumOperands())
129 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
133 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
137 if (Elt < DbgNode->getNumOperands())
138 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
142 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
146 if (Elt < DbgNode->getNumOperands()) {
147 MDNode *Node = const_cast<MDNode*>(DbgNode);
148 Node->replaceOperandWith(Elt, F);
152 unsigned DIVariable::getNumAddrElements() const {
153 return DbgNode->getNumOperands()-8;
156 /// getInlinedAt - If this variable is inlined then return inline location.
157 MDNode *DIVariable::getInlinedAt() const {
158 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
161 //===----------------------------------------------------------------------===//
163 //===----------------------------------------------------------------------===//
165 /// isBasicType - Return true if the specified tag is legal for
167 bool DIDescriptor::isBasicType() const {
168 if (!DbgNode) return false;
170 case dwarf::DW_TAG_base_type:
171 case dwarf::DW_TAG_unspecified_type:
178 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
179 bool DIDescriptor::isDerivedType() const {
180 if (!DbgNode) return false;
182 case dwarf::DW_TAG_typedef:
183 case dwarf::DW_TAG_pointer_type:
184 case dwarf::DW_TAG_ptr_to_member_type:
185 case dwarf::DW_TAG_reference_type:
186 case dwarf::DW_TAG_rvalue_reference_type:
187 case dwarf::DW_TAG_const_type:
188 case dwarf::DW_TAG_volatile_type:
189 case dwarf::DW_TAG_restrict_type:
190 case dwarf::DW_TAG_member:
191 case dwarf::DW_TAG_inheritance:
192 case dwarf::DW_TAG_friend:
195 // CompositeTypes are currently modelled as DerivedTypes.
196 return isCompositeType();
200 /// isCompositeType - Return true if the specified tag is legal for
202 bool DIDescriptor::isCompositeType() const {
203 if (!DbgNode) return false;
205 case dwarf::DW_TAG_array_type:
206 case dwarf::DW_TAG_structure_type:
207 case dwarf::DW_TAG_union_type:
208 case dwarf::DW_TAG_enumeration_type:
209 case dwarf::DW_TAG_subroutine_type:
210 case dwarf::DW_TAG_class_type:
217 /// isVariable - Return true if the specified tag is legal for DIVariable.
218 bool DIDescriptor::isVariable() const {
219 if (!DbgNode) return false;
221 case dwarf::DW_TAG_auto_variable:
222 case dwarf::DW_TAG_arg_variable:
229 /// isType - Return true if the specified tag is legal for DIType.
230 bool DIDescriptor::isType() const {
231 return isBasicType() || isCompositeType() || isDerivedType();
234 /// isSubprogram - Return true if the specified tag is legal for
236 bool DIDescriptor::isSubprogram() const {
237 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
240 /// isGlobalVariable - Return true if the specified tag is legal for
241 /// DIGlobalVariable.
242 bool DIDescriptor::isGlobalVariable() const {
243 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
244 getTag() == dwarf::DW_TAG_constant);
247 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
248 bool DIDescriptor::isGlobal() const {
249 return isGlobalVariable();
252 /// isUnspecifiedParmeter - Return true if the specified tag is
253 /// DW_TAG_unspecified_parameters.
254 bool DIDescriptor::isUnspecifiedParameter() const {
255 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
258 /// isScope - Return true if the specified tag is one of the scope
260 bool DIDescriptor::isScope() const {
261 if (!DbgNode) return false;
263 case dwarf::DW_TAG_compile_unit:
264 case dwarf::DW_TAG_lexical_block:
265 case dwarf::DW_TAG_subprogram:
266 case dwarf::DW_TAG_namespace:
274 /// isTemplateTypeParameter - Return true if the specified tag is
275 /// DW_TAG_template_type_parameter.
276 bool DIDescriptor::isTemplateTypeParameter() const {
277 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
280 /// isTemplateValueParameter - Return true if the specified tag is
281 /// DW_TAG_template_value_parameter.
282 bool DIDescriptor::isTemplateValueParameter() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
286 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
287 bool DIDescriptor::isCompileUnit() const {
288 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
291 /// isFile - Return true if the specified tag is DW_TAG_file_type.
292 bool DIDescriptor::isFile() const {
293 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
296 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
297 bool DIDescriptor::isNameSpace() const {
298 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
301 /// isLexicalBlockFile - Return true if the specified descriptor is a
302 /// lexical block with an extra file.
303 bool DIDescriptor::isLexicalBlockFile() const {
304 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
305 (DbgNode->getNumOperands() == 3);
308 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
309 bool DIDescriptor::isLexicalBlock() const {
310 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
311 (DbgNode->getNumOperands() > 3);
314 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
315 bool DIDescriptor::isSubrange() const {
316 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
319 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
320 bool DIDescriptor::isEnumerator() const {
321 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
324 /// isObjCProperty - Return true if the specified tag is DW_TAG
325 bool DIDescriptor::isObjCProperty() const {
326 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
328 //===----------------------------------------------------------------------===//
329 // Simple Descriptor Constructors and other Methods
330 //===----------------------------------------------------------------------===//
332 DIType::DIType(const MDNode *N) : DIScope(N) {
334 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
339 unsigned DIArray::getNumElements() const {
342 return DbgNode->getNumOperands();
345 /// replaceAllUsesWith - Replace all uses of debug info referenced by
347 void DIType::replaceAllUsesWith(DIDescriptor &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 /// replaceAllUsesWith - Replace all uses of debug info referenced by
367 void DIType::replaceAllUsesWith(MDNode *D) {
371 // Since we use a TrackingVH for the node, its easy for clients to manufacture
372 // legitimate situations where they want to replaceAllUsesWith() on something
373 // which, due to uniquing, has merged with the source. We shield clients from
374 // this detail by allowing a value to be replaced with replaceAllUsesWith()
377 MDNode *Node = const_cast<MDNode*>(DbgNode);
378 const MDNode *DN = D;
379 const Value *V = cast_or_null<Value>(DN);
380 Node->replaceAllUsesWith(const_cast<Value*>(V));
381 MDNode::deleteTemporary(Node);
385 /// isUnsignedDIType - Return true if type encoding is unsigned.
386 bool DIType::isUnsignedDIType() {
387 DIDerivedType DTy(DbgNode);
389 return DTy.getTypeDerivedFrom().isUnsignedDIType();
391 DIBasicType BTy(DbgNode);
393 unsigned Encoding = BTy.getEncoding();
394 if (Encoding == dwarf::DW_ATE_unsigned ||
395 Encoding == dwarf::DW_ATE_unsigned_char ||
396 Encoding == dwarf::DW_ATE_boolean)
402 /// Verify - Verify that a compile unit is well formed.
403 bool DICompileUnit::Verify() const {
404 if (!isCompileUnit())
406 StringRef N = getFilename();
409 // It is possible that directory and produce string is empty.
410 return DbgNode->getNumOperands() == 13;
413 /// Verify - Verify that an ObjC property is well formed.
414 bool DIObjCProperty::Verify() const {
415 if (!isObjCProperty())
418 DIType Ty = getType();
419 if (!Ty.Verify()) return false;
421 // Don't worry about the rest of the strings for now.
422 return DbgNode->getNumOperands() == 8;
425 /// Verify - Verify that a type descriptor is well formed.
426 bool DIType::Verify() const {
429 if (getContext() && !getContext().Verify())
431 unsigned Tag = getTag();
432 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
433 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
434 Tag != dwarf::DW_TAG_ptr_to_member_type &&
435 Tag != dwarf::DW_TAG_reference_type &&
436 Tag != dwarf::DW_TAG_rvalue_reference_type &&
437 Tag != dwarf::DW_TAG_restrict_type &&
438 Tag != dwarf::DW_TAG_array_type &&
439 Tag != dwarf::DW_TAG_enumeration_type &&
440 Tag != dwarf::DW_TAG_subroutine_type &&
441 getFilename().empty())
446 /// Verify - Verify that a basic type descriptor is well formed.
447 bool DIBasicType::Verify() const {
448 return isBasicType() && DbgNode->getNumOperands() == 10;
451 /// Verify - Verify that a derived type descriptor is well formed.
452 bool DIDerivedType::Verify() const {
453 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
454 DbgNode->getNumOperands() <= 14;
457 /// Verify - Verify that a composite type descriptor is well formed.
458 bool DICompositeType::Verify() const {
459 if (!isCompositeType())
461 if (getContext() && !getContext().Verify())
464 return DbgNode->getNumOperands() >= 10 && DbgNode->getNumOperands() <= 14;
467 /// Verify - Verify that a subprogram descriptor is well formed.
468 bool DISubprogram::Verify() const {
472 if (getContext() && !getContext().Verify())
475 DICompositeType Ty = getType();
478 return DbgNode->getNumOperands() == 21;
481 /// Verify - Verify that a global variable descriptor is well formed.
482 bool DIGlobalVariable::Verify() const {
483 if (!isGlobalVariable())
486 if (getDisplayName().empty())
489 if (getContext() && !getContext().Verify())
492 DIType Ty = getType();
496 if (!getGlobal() && !getConstant())
499 return DbgNode->getNumOperands() == 13;
502 /// Verify - Verify that a variable descriptor is well formed.
503 bool DIVariable::Verify() const {
507 if (getContext() && !getContext().Verify())
510 DIType Ty = getType();
514 return DbgNode->getNumOperands() >= 8;
517 /// Verify - Verify that a location descriptor is well formed.
518 bool DILocation::Verify() const {
522 return DbgNode->getNumOperands() == 4;
525 /// Verify - Verify that a namespace descriptor is well formed.
526 bool DINameSpace::Verify() const {
529 return DbgNode->getNumOperands() == 5;
532 /// \brief Verify that the file descriptor is well formed.
533 bool DIFile::Verify() const {
534 return isFile() && DbgNode->getNumOperands() == 4;
537 /// \brief Verify that the enumerator descriptor is well formed.
538 bool DIEnumerator::Verify() const {
539 return isEnumerator() && DbgNode->getNumOperands() == 3;
542 /// \brief Verify that the subrange descriptor is well formed.
543 bool DISubrange::Verify() const {
544 return isSubrange() && DbgNode->getNumOperands() == 3;
547 /// \brief Verify that the lexical block descriptor is well formed.
548 bool DILexicalBlock::Verify() const {
549 return isLexicalBlock() && DbgNode->getNumOperands() == 6;
552 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
553 bool DILexicalBlockFile::Verify() const {
554 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
557 /// \brief Verify that the template type parameter descriptor is well formed.
558 bool DITemplateTypeParameter::Verify() const {
559 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
562 /// \brief Verify that the template value parameter descriptor is well formed.
563 bool DITemplateValueParameter::Verify() const {
564 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
567 /// getOriginalTypeSize - If this type is derived from a base type then
568 /// return base type size.
569 uint64_t DIDerivedType::getOriginalTypeSize() const {
570 unsigned Tag = getTag();
572 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
573 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
574 Tag != dwarf::DW_TAG_restrict_type)
575 return getSizeInBits();
577 DIType BaseType = getTypeDerivedFrom();
579 // If this type is not derived from any type then take conservative approach.
580 if (!BaseType.isValid())
581 return getSizeInBits();
583 // If this is a derived type, go ahead and get the base type, unless it's a
584 // reference then it's just the size of the field. Pointer types have no need
585 // of this since they're a different type of qualification on the type.
586 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
587 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
588 return getSizeInBits();
590 if (BaseType.isDerivedType())
591 return DIDerivedType(BaseType).getOriginalTypeSize();
593 return BaseType.getSizeInBits();
596 /// getObjCProperty - Return property node, if this ivar is associated with one.
597 MDNode *DIDerivedType::getObjCProperty() const {
598 if (DbgNode->getNumOperands() <= 10)
600 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
603 /// isInlinedFnArgument - Return true if this variable provides debugging
604 /// information for an inlined function arguments.
605 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
606 assert(CurFn && "Invalid function");
607 if (!getContext().isSubprogram())
609 // This variable is not inlined function argument if its scope
610 // does not describe current function.
611 return !DISubprogram(getContext()).describes(CurFn);
614 /// describes - Return true if this subprogram provides debugging
615 /// information for the function F.
616 bool DISubprogram::describes(const Function *F) {
617 assert(F && "Invalid function");
618 if (F == getFunction())
620 StringRef Name = getLinkageName();
623 if (F->getName() == Name)
628 unsigned DISubprogram::isOptimized() const {
629 assert (DbgNode && "Invalid subprogram descriptor!");
630 if (DbgNode->getNumOperands() == 16)
631 return getUnsignedField(15);
635 MDNode *DISubprogram::getVariablesNodes() const {
636 if (!DbgNode || DbgNode->getNumOperands() <= 19)
638 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(19));
641 DIArray DISubprogram::getVariables() const {
642 if (!DbgNode || DbgNode->getNumOperands() <= 19)
644 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
649 StringRef DIScope::getFilename() const {
652 if (isLexicalBlockFile())
653 return DILexicalBlockFile(DbgNode).getFilename();
654 if (isLexicalBlock())
655 return DILexicalBlock(DbgNode).getFilename();
657 return DISubprogram(DbgNode).getFilename();
659 return DICompileUnit(DbgNode).getFilename();
661 return DINameSpace(DbgNode).getFilename();
663 return DIType(DbgNode).getFilename();
665 return DIFile(DbgNode).getFilename();
666 llvm_unreachable("Invalid DIScope!");
669 StringRef DIScope::getDirectory() const {
672 if (isLexicalBlockFile())
673 return DILexicalBlockFile(DbgNode).getDirectory();
674 if (isLexicalBlock())
675 return DILexicalBlock(DbgNode).getDirectory();
677 return DISubprogram(DbgNode).getDirectory();
679 return DICompileUnit(DbgNode).getDirectory();
681 return DINameSpace(DbgNode).getDirectory();
683 return DIType(DbgNode).getDirectory();
685 return DIFile(DbgNode).getDirectory();
686 llvm_unreachable("Invalid DIScope!");
689 DIArray DICompileUnit::getEnumTypes() const {
690 if (!DbgNode || DbgNode->getNumOperands() < 13)
693 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(8)))
698 DIArray DICompileUnit::getRetainedTypes() const {
699 if (!DbgNode || DbgNode->getNumOperands() < 13)
702 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(9)))
707 DIArray DICompileUnit::getSubprograms() const {
708 if (!DbgNode || DbgNode->getNumOperands() < 13)
711 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
717 DIArray DICompileUnit::getGlobalVariables() const {
718 if (!DbgNode || DbgNode->getNumOperands() < 13)
721 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
726 /// fixupObjcLikeName - Replace contains special characters used
727 /// in a typical Objective-C names with '.' in a given string.
728 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
729 bool isObjCLike = false;
730 for (size_t i = 0, e = Str.size(); i < e; ++i) {
735 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
736 C == '+' || C == '(' || C == ')'))
743 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
744 /// suitable to hold function specific information.
745 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
746 SmallString<32> Name = StringRef("llvm.dbg.lv.");
747 StringRef FName = "fn";
748 if (Fn.getFunction())
749 FName = Fn.getFunction()->getName();
751 FName = Fn.getName();
753 if (FName.startswith(StringRef(&One, 1)))
754 FName = FName.substr(1);
755 fixupObjcLikeName(FName, Name);
756 return M.getNamedMetadata(Name.str());
759 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
760 /// to hold function specific information.
761 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
762 SmallString<32> Name = StringRef("llvm.dbg.lv.");
763 StringRef FName = "fn";
764 if (Fn.getFunction())
765 FName = Fn.getFunction()->getName();
767 FName = Fn.getName();
769 if (FName.startswith(StringRef(&One, 1)))
770 FName = FName.substr(1);
771 fixupObjcLikeName(FName, Name);
773 return M.getOrInsertNamedMetadata(Name.str());
776 /// createInlinedVariable - Create a new inlined variable based on current
778 /// @param DV Current Variable.
779 /// @param InlinedScope Location at current variable is inlined.
780 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
781 LLVMContext &VMContext) {
782 SmallVector<Value *, 16> Elts;
783 // Insert inlined scope as 7th element.
784 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
785 i == 7 ? Elts.push_back(InlinedScope) :
786 Elts.push_back(DV->getOperand(i));
787 return DIVariable(MDNode::get(VMContext, Elts));
790 /// cleanseInlinedVariable - Remove inlined scope from the variable.
791 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
792 SmallVector<Value *, 16> Elts;
793 // Insert inlined scope as 7th element.
794 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
796 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
797 Elts.push_back(DV->getOperand(i));
798 return DIVariable(MDNode::get(VMContext, Elts));
801 /// getDISubprogram - Find subprogram that is enclosing this scope.
802 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
803 DIDescriptor D(Scope);
804 if (D.isSubprogram())
805 return DISubprogram(Scope);
807 if (D.isLexicalBlockFile())
808 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
810 if (D.isLexicalBlock())
811 return getDISubprogram(DILexicalBlock(Scope).getContext());
813 return DISubprogram();
816 /// getDICompositeType - Find underlying composite type.
817 DICompositeType llvm::getDICompositeType(DIType T) {
818 if (T.isCompositeType())
819 return DICompositeType(T);
821 if (T.isDerivedType())
822 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
824 return DICompositeType();
827 /// isSubprogramContext - Return true if Context is either a subprogram
828 /// or another context nested inside a subprogram.
829 bool llvm::isSubprogramContext(const MDNode *Context) {
832 DIDescriptor D(Context);
833 if (D.isSubprogram())
836 return isSubprogramContext(DIType(Context).getContext());
840 //===----------------------------------------------------------------------===//
841 // DebugInfoFinder implementations.
842 //===----------------------------------------------------------------------===//
844 /// processModule - Process entire module and collect debug info.
845 void DebugInfoFinder::processModule(const Module &M) {
846 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
847 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
848 DICompileUnit CU(CU_Nodes->getOperand(i));
850 DIArray GVs = CU.getGlobalVariables();
851 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
852 DIGlobalVariable DIG(GVs.getElement(i));
853 if (addGlobalVariable(DIG))
854 processType(DIG.getType());
856 DIArray SPs = CU.getSubprograms();
857 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
858 processSubprogram(DISubprogram(SPs.getElement(i)));
859 DIArray EnumTypes = CU.getEnumTypes();
860 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
861 processType(DIType(EnumTypes.getElement(i)));
862 DIArray RetainedTypes = CU.getRetainedTypes();
863 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
864 processType(DIType(RetainedTypes.getElement(i)));
865 // FIXME: We really shouldn't be bailing out after visiting just one CU
871 /// processLocation - Process DILocation.
872 void DebugInfoFinder::processLocation(DILocation Loc) {
873 if (!Loc.Verify()) return;
874 DIDescriptor S(Loc.getScope());
875 if (S.isCompileUnit())
876 addCompileUnit(DICompileUnit(S));
877 else if (S.isSubprogram())
878 processSubprogram(DISubprogram(S));
879 else if (S.isLexicalBlock())
880 processLexicalBlock(DILexicalBlock(S));
881 else if (S.isLexicalBlockFile()) {
882 DILexicalBlockFile DBF = DILexicalBlockFile(S);
883 processLexicalBlock(DILexicalBlock(DBF.getScope()));
885 processLocation(Loc.getOrigLocation());
888 /// processType - Process DIType.
889 void DebugInfoFinder::processType(DIType DT) {
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 processType(SP.getType());
929 /// processDeclare - Process DbgDeclareInst.
930 void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
931 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
935 if (!DV.isVariable())
938 if (!NodesSeen.insert(DV))
940 processType(DIVariable(N).getType());
943 /// addType - Add type into Tys.
944 bool DebugInfoFinder::addType(DIType DT) {
948 if (!NodesSeen.insert(DT))
955 /// addCompileUnit - Add compile unit into CUs.
956 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
960 if (!NodesSeen.insert(CU))
967 /// addGlobalVariable - Add global variable into GVs.
968 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
969 if (!DIDescriptor(DIG).isGlobalVariable())
972 if (!NodesSeen.insert(DIG))
979 // addSubprogram - Add subprgoram into SPs.
980 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
981 if (!DIDescriptor(SP).isSubprogram())
984 if (!NodesSeen.insert(SP))
991 //===----------------------------------------------------------------------===//
992 // DIDescriptor: dump routines for all descriptors.
993 //===----------------------------------------------------------------------===//
995 /// dump - Print descriptor to dbgs() with a newline.
996 void DIDescriptor::dump() const {
997 print(dbgs()); dbgs() << '\n';
1000 /// print - Print descriptor.
1001 void DIDescriptor::print(raw_ostream &OS) const {
1002 if (!DbgNode) return;
1004 if (const char *Tag = dwarf::TagString(getTag()))
1005 OS << "[ " << Tag << " ]";
1007 if (this->isSubrange()) {
1008 DISubrange(DbgNode).printInternal(OS);
1009 } else if (this->isCompileUnit()) {
1010 DICompileUnit(DbgNode).printInternal(OS);
1011 } else if (this->isFile()) {
1012 DIFile(DbgNode).printInternal(OS);
1013 } else if (this->isEnumerator()) {
1014 DIEnumerator(DbgNode).printInternal(OS);
1015 } else if (this->isBasicType()) {
1016 DIType(DbgNode).printInternal(OS);
1017 } else if (this->isDerivedType()) {
1018 DIDerivedType(DbgNode).printInternal(OS);
1019 } else if (this->isCompositeType()) {
1020 DICompositeType(DbgNode).printInternal(OS);
1021 } else if (this->isSubprogram()) {
1022 DISubprogram(DbgNode).printInternal(OS);
1023 } else if (this->isGlobalVariable()) {
1024 DIGlobalVariable(DbgNode).printInternal(OS);
1025 } else if (this->isVariable()) {
1026 DIVariable(DbgNode).printInternal(OS);
1027 } else if (this->isObjCProperty()) {
1028 DIObjCProperty(DbgNode).printInternal(OS);
1029 } else if (this->isScope()) {
1030 DIScope(DbgNode).printInternal(OS);
1034 void DISubrange::printInternal(raw_ostream &OS) const {
1035 int64_t Count = getCount();
1037 OS << " [" << getLo() << ", " << Count - 1 << ']';
1039 OS << " [unbounded]";
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 (const char *Lang = dwarf::LanguageString(getLanguage()))
1049 OS << " [" << 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]";
1081 OS << " [artificial]";
1083 if (isForwardDecl())
1087 if (isStaticMember())
1091 void DIDerivedType::printInternal(raw_ostream &OS) const {
1092 DIType::printInternal(OS);
1093 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1096 void DICompositeType::printInternal(raw_ostream &OS) const {
1097 DIType::printInternal(OS);
1098 DIArray A = getTypeArray();
1099 OS << " [" << A.getNumElements() << " elements]";
1102 void DISubprogram::printInternal(raw_ostream &OS) const {
1103 // TODO : Print context
1104 OS << " [line " << getLineNumber() << ']';
1106 if (isLocalToUnit())
1112 if (getScopeLineNumber() != getLineNumber())
1113 OS << " [scope " << getScopeLineNumber() << "]";
1117 else if (isProtected())
1118 OS << " [protected]";
1120 StringRef Res = getName();
1122 OS << " [" << Res << ']';
1125 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1126 StringRef Res = getName();
1128 OS << " [" << Res << ']';
1130 OS << " [line " << getLineNumber() << ']';
1132 // TODO : Print context
1134 if (isLocalToUnit())
1141 void DIVariable::printInternal(raw_ostream &OS) const {
1142 StringRef Res = getName();
1144 OS << " [" << Res << ']';
1146 OS << " [line " << getLineNumber() << ']';
1149 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1150 StringRef Name = getObjCPropertyName();
1152 OS << " [" << Name << ']';
1154 OS << " [line " << getLineNumber()
1155 << ", properties " << getUnsignedField(6) << ']';
1158 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1159 const LLVMContext &Ctx) {
1160 if (!DL.isUnknown()) { // Print source line info.
1161 DIScope Scope(DL.getScope(Ctx));
1162 // Omit the directory, because it's likely to be long and uninteresting.
1164 CommentOS << Scope.getFilename();
1166 CommentOS << "<unknown>";
1167 CommentOS << ':' << DL.getLine();
1168 if (DL.getCol() != 0)
1169 CommentOS << ':' << DL.getCol();
1170 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1171 if (!InlinedAtDL.isUnknown()) {
1172 CommentOS << " @[ ";
1173 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1179 void DIVariable::printExtendedName(raw_ostream &OS) const {
1180 const LLVMContext &Ctx = DbgNode->getContext();
1181 StringRef Res = getName();
1183 OS << Res << "," << getLineNumber();
1184 if (MDNode *InlinedAt = getInlinedAt()) {
1185 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1186 if (!InlinedAtDL.isUnknown()) {
1188 printDebugLoc(InlinedAtDL, OS, Ctx);