1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm::dwarf;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
42 DIDescriptor::DIDescriptor(const 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())
71 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
72 return CI->getZExtValue();
77 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
79 return DIDescriptor();
81 if (Elt < DbgNode->getNumOperands())
83 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
84 return DIDescriptor();
87 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
91 if (Elt < DbgNode->getNumOperands())
92 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
96 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
100 if (Elt < DbgNode->getNumOperands())
101 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
105 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
114 unsigned DIVariable::getNumAddrElements() const {
115 if (getVersion() <= llvm::LLVMDebugVersion8)
116 return DbgNode->getNumOperands()-6;
117 if (getVersion() == llvm::LLVMDebugVersion9)
118 return DbgNode->getNumOperands()-7;
119 return DbgNode->getNumOperands()-8;
122 /// getInlinedAt - If this variable is inlined then return inline location.
123 MDNode *DIVariable::getInlinedAt() const {
124 if (getVersion() <= llvm::LLVMDebugVersion9)
126 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
129 //===----------------------------------------------------------------------===//
131 //===----------------------------------------------------------------------===//
133 /// isBasicType - Return true if the specified tag is legal for
135 bool DIDescriptor::isBasicType() const {
136 if (!DbgNode) return false;
138 case dwarf::DW_TAG_base_type:
139 case dwarf::DW_TAG_unspecified_type:
146 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
147 bool DIDescriptor::isDerivedType() const {
148 if (!DbgNode) return false;
150 case dwarf::DW_TAG_typedef:
151 case dwarf::DW_TAG_pointer_type:
152 case dwarf::DW_TAG_reference_type:
153 case dwarf::DW_TAG_rvalue_reference_type:
154 case dwarf::DW_TAG_const_type:
155 case dwarf::DW_TAG_volatile_type:
156 case dwarf::DW_TAG_restrict_type:
157 case dwarf::DW_TAG_member:
158 case dwarf::DW_TAG_inheritance:
159 case dwarf::DW_TAG_friend:
162 // CompositeTypes are currently modelled as DerivedTypes.
163 return isCompositeType();
167 /// isCompositeType - Return true if the specified tag is legal for
169 bool DIDescriptor::isCompositeType() const {
170 if (!DbgNode) return false;
172 case dwarf::DW_TAG_array_type:
173 case dwarf::DW_TAG_structure_type:
174 case dwarf::DW_TAG_union_type:
175 case dwarf::DW_TAG_enumeration_type:
176 case dwarf::DW_TAG_vector_type:
177 case dwarf::DW_TAG_subroutine_type:
178 case dwarf::DW_TAG_class_type:
185 /// isVariable - Return true if the specified tag is legal for DIVariable.
186 bool DIDescriptor::isVariable() const {
187 if (!DbgNode) return false;
189 case dwarf::DW_TAG_auto_variable:
190 case dwarf::DW_TAG_arg_variable:
191 case dwarf::DW_TAG_return_variable:
198 /// isType - Return true if the specified tag is legal for DIType.
199 bool DIDescriptor::isType() const {
200 return isBasicType() || isCompositeType() || isDerivedType();
203 /// isSubprogram - Return true if the specified tag is legal for
205 bool DIDescriptor::isSubprogram() const {
206 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
209 /// isGlobalVariable - Return true if the specified tag is legal for
210 /// DIGlobalVariable.
211 bool DIDescriptor::isGlobalVariable() const {
212 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
213 getTag() == dwarf::DW_TAG_constant);
216 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
217 bool DIDescriptor::isGlobal() const {
218 return isGlobalVariable();
221 /// isUnspecifiedParmeter - Return true if the specified tag is
222 /// DW_TAG_unspecified_parameters.
223 bool DIDescriptor::isUnspecifiedParameter() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
227 /// isScope - Return true if the specified tag is one of the scope
229 bool DIDescriptor::isScope() const {
230 if (!DbgNode) return false;
232 case dwarf::DW_TAG_compile_unit:
233 case dwarf::DW_TAG_lexical_block:
234 case dwarf::DW_TAG_subprogram:
235 case dwarf::DW_TAG_namespace:
243 /// isTemplateTypeParameter - Return true if the specified tag is
244 /// DW_TAG_template_type_parameter.
245 bool DIDescriptor::isTemplateTypeParameter() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
249 /// isTemplateValueParameter - Return true if the specified tag is
250 /// DW_TAG_template_value_parameter.
251 bool DIDescriptor::isTemplateValueParameter() const {
252 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
255 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
256 bool DIDescriptor::isCompileUnit() const {
257 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
260 /// isFile - Return true if the specified tag is DW_TAG_file_type.
261 bool DIDescriptor::isFile() const {
262 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
265 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
266 bool DIDescriptor::isNameSpace() const {
267 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
270 /// isLexicalBlockFile - Return true if the specified descriptor is a
271 /// lexical block with an extra file.
272 bool DIDescriptor::isLexicalBlockFile() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
274 (DbgNode->getNumOperands() == 3);
277 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
278 bool DIDescriptor::isLexicalBlock() const {
279 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
280 (DbgNode->getNumOperands() > 3);
283 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
284 bool DIDescriptor::isSubrange() const {
285 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
288 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
289 bool DIDescriptor::isEnumerator() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
293 /// isObjCProperty - Return true if the specified tag is DW_TAG
294 bool DIDescriptor::isObjCProperty() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
297 //===----------------------------------------------------------------------===//
298 // Simple Descriptor Constructors and other Methods
299 //===----------------------------------------------------------------------===//
301 DIType::DIType(const MDNode *N) : DIScope(N) {
303 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
308 unsigned DIArray::getNumElements() const {
311 return DbgNode->getNumOperands();
314 /// replaceAllUsesWith - Replace all uses of debug info referenced by
316 void DIType::replaceAllUsesWith(DIDescriptor &D) {
320 // Since we use a TrackingVH for the node, its easy for clients to manufacture
321 // legitimate situations where they want to replaceAllUsesWith() on something
322 // which, due to uniquing, has merged with the source. We shield clients from
323 // this detail by allowing a value to be replaced with replaceAllUsesWith()
326 MDNode *Node = const_cast<MDNode*>(DbgNode);
327 const MDNode *DN = D;
328 const Value *V = cast_or_null<Value>(DN);
329 Node->replaceAllUsesWith(const_cast<Value*>(V));
330 MDNode::deleteTemporary(Node);
334 /// replaceAllUsesWith - Replace all uses of debug info referenced by
336 void DIType::replaceAllUsesWith(MDNode *D) {
340 // Since we use a TrackingVH for the node, its easy for clients to manufacture
341 // legitimate situations where they want to replaceAllUsesWith() on something
342 // which, due to uniquing, has merged with the source. We shield clients from
343 // this detail by allowing a value to be replaced with replaceAllUsesWith()
346 MDNode *Node = const_cast<MDNode*>(DbgNode);
347 const MDNode *DN = D;
348 const Value *V = cast_or_null<Value>(DN);
349 Node->replaceAllUsesWith(const_cast<Value*>(V));
350 MDNode::deleteTemporary(Node);
354 /// isUnsignedDIType - Return true if type encoding is unsigned.
355 bool DIType::isUnsignedDIType() {
356 DIDerivedType DTy(DbgNode);
358 return DTy.getTypeDerivedFrom().isUnsignedDIType();
360 DIBasicType BTy(DbgNode);
362 unsigned Encoding = BTy.getEncoding();
363 if (Encoding == dwarf::DW_ATE_unsigned ||
364 Encoding == dwarf::DW_ATE_unsigned_char)
370 /// Verify - Verify that a compile unit is well formed.
371 bool DICompileUnit::Verify() const {
374 StringRef N = getFilename();
377 // It is possible that directory and produce string is empty.
381 /// Verify - Verify that an ObjC property is well formed.
382 bool DIObjCProperty::Verify() const {
385 unsigned Tag = getTag();
386 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
387 DIType Ty = getType();
388 if (!Ty.Verify()) return false;
390 // Don't worry about the rest of the strings for now.
394 /// Verify - Verify that a type descriptor is well formed.
395 bool DIType::Verify() const {
398 if (getContext() && !getContext().Verify())
400 unsigned Tag = getTag();
401 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
402 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
403 Tag != dwarf::DW_TAG_reference_type &&
404 Tag != dwarf::DW_TAG_rvalue_reference_type &&
405 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
406 Tag != dwarf::DW_TAG_array_type &&
407 Tag != dwarf::DW_TAG_enumeration_type &&
408 Tag != dwarf::DW_TAG_subroutine_type &&
409 getFilename().empty())
414 /// Verify - Verify that a basic type descriptor is well formed.
415 bool DIBasicType::Verify() const {
416 return isBasicType();
419 /// Verify - Verify that a derived type descriptor is well formed.
420 bool DIDerivedType::Verify() const {
421 return isDerivedType();
424 /// Verify - Verify that a composite type descriptor is well formed.
425 bool DICompositeType::Verify() const {
428 if (getContext() && !getContext().Verify())
434 /// Verify - Verify that a subprogram descriptor is well formed.
435 bool DISubprogram::Verify() const {
439 if (getContext() && !getContext().Verify())
442 DICompositeType Ty = getType();
448 /// Verify - Verify that a global variable descriptor is well formed.
449 bool DIGlobalVariable::Verify() const {
453 if (getDisplayName().empty())
456 if (getContext() && !getContext().Verify())
459 DIType Ty = getType();
463 if (!getGlobal() && !getConstant())
469 /// Verify - Verify that a variable descriptor is well formed.
470 bool DIVariable::Verify() const {
474 if (getContext() && !getContext().Verify())
477 DIType Ty = getType();
484 /// Verify - Verify that a location descriptor is well formed.
485 bool DILocation::Verify() const {
489 return DbgNode->getNumOperands() == 4;
492 /// Verify - Verify that a namespace descriptor is well formed.
493 bool DINameSpace::Verify() const {
496 if (getName().empty())
501 /// getOriginalTypeSize - If this type is derived from a base type then
502 /// return base type size.
503 uint64_t DIDerivedType::getOriginalTypeSize() const {
504 unsigned Tag = getTag();
506 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
507 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
508 Tag == dwarf::DW_TAG_restrict_type) {
509 DIType BaseType = getTypeDerivedFrom();
510 // If this type is not derived from any type then take conservative
512 if (!BaseType.isValid())
513 return getSizeInBits();
514 // If this is a derived type, go ahead and get the base type, unless
515 // it's a reference then it's just the size of the field. Pointer types
516 // have no need of this since they're a different type of qualification
518 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
519 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
520 return getSizeInBits();
521 else if (BaseType.isDerivedType())
522 return DIDerivedType(BaseType).getOriginalTypeSize();
524 return BaseType.getSizeInBits();
527 return getSizeInBits();
530 /// getObjCProperty - Return property node, if this ivar is associated with one.
531 MDNode *DIDerivedType::getObjCProperty() const {
532 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
534 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
537 /// isInlinedFnArgument - Return true if this variable provides debugging
538 /// information for an inlined function arguments.
539 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
540 assert(CurFn && "Invalid function");
541 if (!getContext().isSubprogram())
543 // This variable is not inlined function argument if its scope
544 // does not describe current function.
545 return !(DISubprogram(getContext()).describes(CurFn));
548 /// describes - Return true if this subprogram provides debugging
549 /// information for the function F.
550 bool DISubprogram::describes(const Function *F) {
551 assert(F && "Invalid function");
552 if (F == getFunction())
554 StringRef Name = getLinkageName();
557 if (F->getName() == Name)
562 unsigned DISubprogram::isOptimized() const {
563 assert (DbgNode && "Invalid subprogram descriptor!");
564 if (DbgNode->getNumOperands() == 16)
565 return getUnsignedField(15);
569 MDNode *DISubprogram::getVariablesNodes() const {
570 if (!DbgNode || DbgNode->getNumOperands() <= 19)
572 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
573 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
577 DIArray DISubprogram::getVariables() const {
578 if (!DbgNode || DbgNode->getNumOperands() <= 19)
580 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
581 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
586 StringRef DIScope::getFilename() const {
589 if (isLexicalBlockFile())
590 return DILexicalBlockFile(DbgNode).getFilename();
591 if (isLexicalBlock())
592 return DILexicalBlock(DbgNode).getFilename();
594 return DISubprogram(DbgNode).getFilename();
596 return DICompileUnit(DbgNode).getFilename();
598 return DINameSpace(DbgNode).getFilename();
600 return DIType(DbgNode).getFilename();
602 return DIFile(DbgNode).getFilename();
603 llvm_unreachable("Invalid DIScope!");
606 StringRef DIScope::getDirectory() const {
609 if (isLexicalBlockFile())
610 return DILexicalBlockFile(DbgNode).getDirectory();
611 if (isLexicalBlock())
612 return DILexicalBlock(DbgNode).getDirectory();
614 return DISubprogram(DbgNode).getDirectory();
616 return DICompileUnit(DbgNode).getDirectory();
618 return DINameSpace(DbgNode).getDirectory();
620 return DIType(DbgNode).getDirectory();
622 return DIFile(DbgNode).getDirectory();
623 llvm_unreachable("Invalid DIScope!");
626 DIArray DICompileUnit::getEnumTypes() const {
627 if (!DbgNode || DbgNode->getNumOperands() < 14)
630 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
631 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
636 DIArray DICompileUnit::getRetainedTypes() const {
637 if (!DbgNode || DbgNode->getNumOperands() < 14)
640 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
641 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
646 DIArray DICompileUnit::getSubprograms() const {
647 if (!DbgNode || DbgNode->getNumOperands() < 14)
650 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
651 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
657 DIArray DICompileUnit::getGlobalVariables() const {
658 if (!DbgNode || DbgNode->getNumOperands() < 14)
661 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
662 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
667 //===----------------------------------------------------------------------===//
668 // DIDescriptor: vtable anchors for all descriptors.
669 //===----------------------------------------------------------------------===//
671 void DIScope::anchor() { }
673 void DICompileUnit::anchor() { }
675 void DIFile::anchor() { }
677 void DIType::anchor() { }
679 void DIBasicType::anchor() { }
681 void DIDerivedType::anchor() { }
683 void DICompositeType::anchor() { }
685 void DISubprogram::anchor() { }
687 void DILexicalBlock::anchor() { }
689 void DINameSpace::anchor() { }
691 void DILexicalBlockFile::anchor() { }
693 //===----------------------------------------------------------------------===//
694 // DIDescriptor: dump routines for all descriptors.
695 //===----------------------------------------------------------------------===//
697 /// dump - Print descriptor to dbgs() with a newline.
698 void DIDescriptor::dump() const {
699 print(dbgs()); dbgs() << '\n';
702 /// print - Print descriptor.
703 void DIDescriptor::print(raw_ostream &OS) const {
704 if (!DbgNode) return;
706 OS << "[" << dwarf::TagString(getTag()) << ']';
708 if (this->isSubrange()) {
709 DISubrange(DbgNode).printInternal(OS);
710 } else if (this->isScope()) {
711 DIScope(DbgNode).printInternal(OS);
712 } else if (this->isCompileUnit()) {
713 DICompileUnit(DbgNode).printInternal(OS);
714 } else if (this->isFile()) {
715 DIFile(DbgNode).printInternal(OS);
716 } else if (this->isEnumerator()) {
717 DIEnumerator(DbgNode).printInternal(OS);
718 } else if (this->isBasicType()) {
719 DIType(DbgNode).printInternal(OS);
720 } else if (this->isDerivedType()) {
721 DIDerivedType(DbgNode).printInternal(OS);
722 } else if (this->isCompositeType()) {
723 DICompositeType(DbgNode).printInternal(OS);
724 } else if (this->isSubprogram()) {
725 DISubprogram(DbgNode).printInternal(OS);
726 } else if (this->isGlobalVariable()) {
727 DIGlobalVariable(DbgNode).printInternal(OS);
728 } else if (this->isVariable()) {
729 DIVariable(DbgNode).printInternal(OS);
733 void DISubrange::printInternal(raw_ostream &OS) const {
734 OS << " [" << getLo() << ", " << getHi() << ']';
737 void DIScope::printInternal(raw_ostream &OS) const {
738 OS << " [" << getDirectory() << "/" << getFilename() << ']';
741 void DICompileUnit::printInternal(raw_ostream &OS) const {
742 DIScope::printInternal(OS);
743 if (unsigned Lang = getLanguage())
744 OS << " [" << dwarf::LanguageString(Lang) << ']';
747 void DIEnumerator::printInternal(raw_ostream &OS) const {
748 OS << " [" << getName() << " :: " << getEnumValue() << ']';
751 void DIType::printInternal(raw_ostream &OS) const {
752 if (!DbgNode) return;
754 StringRef Res = getName();
756 OS << " [" << Res << "]";
758 // TODO: Print context?
760 OS << " [line " << getLineNumber()
761 << ", size " << getSizeInBits()
762 << ", align " << getAlignInBits()
763 << ", offset " << getOffsetInBits();
766 << dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding());
771 else if (isProtected())
772 OS << " [protected]";
778 void DIDerivedType::printInternal(raw_ostream &OS) const {
779 DIType::printInternal(OS);
780 OS << " [from " << getTypeDerivedFrom().getName() << ']';
783 void DICompositeType::printInternal(raw_ostream &OS) const {
784 DIType::printInternal(OS);
785 DIArray A = getTypeArray();
786 OS << " [" << A.getNumElements() << " elements]";
789 void DISubprogram::printInternal(raw_ostream &OS) const {
790 StringRef Res = getName();
792 OS << " [" << Res << ']';
794 // TODO : Print context
796 OS << " [line " << getLineNumber() << ']';
804 if (getScopeLineNumber() != getLineNumber())
805 OS << " [scope " << getScopeLineNumber() << "]";
808 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
809 StringRef Res = getName();
811 OS << " [" << Res << ']';
813 OS << " [line " << getLineNumber() << ']';
815 // TODO : Print context
824 void DIVariable::printInternal(raw_ostream &OS) const {
825 StringRef Res = getName();
827 OS << " [" << Res << ']';
829 OS << " [line " << getLineNumber() << ']';
832 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
833 const LLVMContext &Ctx) {
834 if (!DL.isUnknown()) { // Print source line info.
835 DIScope Scope(DL.getScope(Ctx));
836 // Omit the directory, because it's likely to be long and uninteresting.
838 CommentOS << Scope.getFilename();
840 CommentOS << "<unknown>";
841 CommentOS << ':' << DL.getLine();
842 if (DL.getCol() != 0)
843 CommentOS << ':' << DL.getCol();
844 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
845 if (!InlinedAtDL.isUnknown()) {
847 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
853 void DIVariable::printExtendedName(raw_ostream &OS) const {
854 const LLVMContext &Ctx = DbgNode->getContext();
855 StringRef Res = getName();
857 OS << Res << "," << getLineNumber();
858 if (MDNode *InlinedAt = getInlinedAt()) {
859 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
860 if (!InlinedAtDL.isUnknown()) {
862 printDebugLoc(InlinedAtDL, OS, Ctx);
868 /// fixupObjcLikeName - Replace contains special characters used
869 /// in a typical Objective-C names with '.' in a given string.
870 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
871 bool isObjCLike = false;
872 for (size_t i = 0, e = Str.size(); i < e; ++i) {
877 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
878 C == '+' || C == '(' || C == ')'))
885 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
886 /// suitable to hold function specific information.
887 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
888 SmallString<32> Name = StringRef("llvm.dbg.lv.");
889 StringRef FName = "fn";
890 if (Fn.getFunction())
891 FName = Fn.getFunction()->getName();
893 FName = Fn.getName();
895 if (FName.startswith(StringRef(&One, 1)))
896 FName = FName.substr(1);
897 fixupObjcLikeName(FName, Name);
898 return M.getNamedMetadata(Name.str());
901 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
902 /// to hold function specific information.
903 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
904 SmallString<32> Name = StringRef("llvm.dbg.lv.");
905 StringRef FName = "fn";
906 if (Fn.getFunction())
907 FName = Fn.getFunction()->getName();
909 FName = Fn.getName();
911 if (FName.startswith(StringRef(&One, 1)))
912 FName = FName.substr(1);
913 fixupObjcLikeName(FName, Name);
915 return M.getOrInsertNamedMetadata(Name.str());
918 /// createInlinedVariable - Create a new inlined variable based on current
920 /// @param DV Current Variable.
921 /// @param InlinedScope Location at current variable is inlined.
922 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
923 LLVMContext &VMContext) {
924 SmallVector<Value *, 16> Elts;
925 // Insert inlined scope as 7th element.
926 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
927 i == 7 ? Elts.push_back(InlinedScope) :
928 Elts.push_back(DV->getOperand(i));
929 return DIVariable(MDNode::get(VMContext, Elts));
932 /// cleanseInlinedVariable - Remove inlined scope from the variable.
933 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
934 SmallVector<Value *, 16> Elts;
935 // Insert inlined scope as 7th element.
936 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
938 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
939 Elts.push_back(DV->getOperand(i));
940 return DIVariable(MDNode::get(VMContext, Elts));
943 //===----------------------------------------------------------------------===//
944 // DebugInfoFinder implementations.
945 //===----------------------------------------------------------------------===//
947 /// processModule - Process entire module and collect debug info.
948 void DebugInfoFinder::processModule(Module &M) {
949 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
950 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
951 DICompileUnit CU(CU_Nodes->getOperand(i));
953 if (CU.getVersion() > LLVMDebugVersion10) {
954 DIArray GVs = CU.getGlobalVariables();
955 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
956 DIGlobalVariable DIG(GVs.getElement(i));
957 if (addGlobalVariable(DIG))
958 processType(DIG.getType());
960 DIArray SPs = CU.getSubprograms();
961 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
962 processSubprogram(DISubprogram(SPs.getElement(i)));
963 DIArray EnumTypes = CU.getEnumTypes();
964 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
965 processType(DIType(EnumTypes.getElement(i)));
966 DIArray RetainedTypes = CU.getRetainedTypes();
967 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
968 processType(DIType(RetainedTypes.getElement(i)));
974 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
975 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
976 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
978 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
981 DebugLoc Loc = BI->getDebugLoc();
985 LLVMContext &Ctx = BI->getContext();
986 DIDescriptor Scope(Loc.getScope(Ctx));
988 if (Scope.isCompileUnit())
989 addCompileUnit(DICompileUnit(Scope));
990 else if (Scope.isSubprogram())
991 processSubprogram(DISubprogram(Scope));
992 else if (Scope.isLexicalBlockFile()) {
993 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
994 processLexicalBlock(DILexicalBlock(DBF.getScope()));
996 else if (Scope.isLexicalBlock())
997 processLexicalBlock(DILexicalBlock(Scope));
999 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1000 processLocation(DILocation(IA));
1003 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1004 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1005 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1006 if (addGlobalVariable(DIG)) {
1007 if (DIG.getVersion() <= LLVMDebugVersion10)
1008 addCompileUnit(DIG.getCompileUnit());
1009 processType(DIG.getType());
1014 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1015 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1016 processSubprogram(DISubprogram(NMD->getOperand(i)));
1019 /// processLocation - Process DILocation.
1020 void DebugInfoFinder::processLocation(DILocation Loc) {
1021 if (!Loc.Verify()) return;
1022 DIDescriptor S(Loc.getScope());
1023 if (S.isCompileUnit())
1024 addCompileUnit(DICompileUnit(S));
1025 else if (S.isSubprogram())
1026 processSubprogram(DISubprogram(S));
1027 else if (S.isLexicalBlock())
1028 processLexicalBlock(DILexicalBlock(S));
1029 else if (S.isLexicalBlockFile()) {
1030 DILexicalBlockFile DBF = DILexicalBlockFile(S);
1031 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1033 processLocation(Loc.getOrigLocation());
1036 /// processType - Process DIType.
1037 void DebugInfoFinder::processType(DIType DT) {
1040 if (DT.getVersion() <= LLVMDebugVersion10)
1041 addCompileUnit(DT.getCompileUnit());
1042 if (DT.isCompositeType()) {
1043 DICompositeType DCT(DT);
1044 processType(DCT.getTypeDerivedFrom());
1045 DIArray DA = DCT.getTypeArray();
1046 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1047 DIDescriptor D = DA.getElement(i);
1049 processType(DIType(D));
1050 else if (D.isSubprogram())
1051 processSubprogram(DISubprogram(D));
1053 } else if (DT.isDerivedType()) {
1054 DIDerivedType DDT(DT);
1055 processType(DDT.getTypeDerivedFrom());
1059 /// processLexicalBlock
1060 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1061 DIScope Context = LB.getContext();
1062 if (Context.isLexicalBlock())
1063 return processLexicalBlock(DILexicalBlock(Context));
1064 else if (Context.isLexicalBlockFile()) {
1065 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
1066 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
1069 return processSubprogram(DISubprogram(Context));
1072 /// processSubprogram - Process DISubprogram.
1073 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1074 if (!addSubprogram(SP))
1076 if (SP.getVersion() <= LLVMDebugVersion10)
1077 addCompileUnit(SP.getCompileUnit());
1078 processType(SP.getType());
1081 /// processDeclare - Process DbgDeclareInst.
1082 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1083 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1087 if (!DV.isVariable())
1090 if (!NodesSeen.insert(DV))
1092 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1093 addCompileUnit(DIVariable(N).getCompileUnit());
1094 processType(DIVariable(N).getType());
1097 /// addType - Add type into Tys.
1098 bool DebugInfoFinder::addType(DIType DT) {
1102 if (!NodesSeen.insert(DT))
1109 /// addCompileUnit - Add compile unit into CUs.
1110 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1114 if (!NodesSeen.insert(CU))
1121 /// addGlobalVariable - Add global variable into GVs.
1122 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1123 if (!DIDescriptor(DIG).isGlobalVariable())
1126 if (!NodesSeen.insert(DIG))
1133 // addSubprogram - Add subprgoram into SPs.
1134 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1135 if (!DIDescriptor(SP).isSubprogram())
1138 if (!NodesSeen.insert(SP))
1145 /// getDISubprogram - Find subprogram that is enclosing this scope.
1146 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1147 DIDescriptor D(Scope);
1148 if (D.isSubprogram())
1149 return DISubprogram(Scope);
1151 if (D.isLexicalBlockFile())
1152 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
1154 if (D.isLexicalBlock())
1155 return getDISubprogram(DILexicalBlock(Scope).getContext());
1157 return DISubprogram();
1160 /// getDICompositeType - Find underlying composite type.
1161 DICompositeType llvm::getDICompositeType(DIType T) {
1162 if (T.isCompositeType())
1163 return DICompositeType(T);
1165 if (T.isDerivedType())
1166 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1168 return DICompositeType();
1171 /// isSubprogramContext - Return true if Context is either a subprogram
1172 /// or another context nested inside a subprogram.
1173 bool llvm::isSubprogramContext(const MDNode *Context) {
1176 DIDescriptor D(Context);
1177 if (D.isSubprogram())
1180 return isSubprogramContext(DIType(Context).getContext());