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 //===----------------------------------------------------------------------===//
698 /// print - Print descriptor.
699 void DIDescriptor::print(raw_ostream &OS) const {
700 OS << "[" << dwarf::TagString(getTag()) << "] ";
701 OS.write_hex((intptr_t) &*DbgNode) << ']';
704 /// print - Print compile unit.
705 void DICompileUnit::print(raw_ostream &OS) const {
707 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
709 OS << " [" << getDirectory() << "/" << getFilename() << "]";
712 /// print - Print type.
713 void DIType::print(raw_ostream &OS) const {
714 if (!DbgNode) return;
716 StringRef Res = getName();
718 OS << " [" << Res << "] ";
720 unsigned Tag = getTag();
721 OS << " [" << dwarf::TagString(Tag) << "] ";
723 // TODO : Print context
725 << "line " << getLineNumber() << ", "
726 << getSizeInBits() << " bits, "
727 << getAlignInBits() << " bit alignment, "
728 << getOffsetInBits() << " bit offset"
733 else if (isProtected())
734 OS << " [protected] ";
740 DIBasicType(DbgNode).print(OS);
741 else if (isDerivedType()) {
742 DIDerivedType DTy = DIDerivedType(DbgNode);
744 DICompositeType CTy = getDICompositeType(DTy);
748 else if (isCompositeType())
749 DICompositeType(DbgNode).print(OS);
751 OS << "Invalid DIType\n";
758 /// print - Print basic type.
759 void DIBasicType::print(raw_ostream &OS) const {
760 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
763 /// print - Print derived type.
764 void DIDerivedType::print(raw_ostream &OS) const {
765 OS << "\n\t Derived From: ";
766 getTypeDerivedFrom().print(OS);
770 /// print - Print composite type.
771 void DICompositeType::print(raw_ostream &OS) const {
772 DIArray A = getTypeArray();
773 OS << " [" << A.getNumElements() << " elements]";
776 /// print - Print subprogram.
777 void DISubprogram::print(raw_ostream &OS) const {
778 StringRef Res = getName();
780 OS << " [" << Res << "] ";
782 unsigned Tag = getTag();
783 OS << " [" << dwarf::TagString(Tag) << "] ";
785 // TODO : Print context
786 OS << " [" << getLineNumber() << "] ";
794 if (getScopeLineNumber() != getLineNumber())
795 OS << " [Scope: " << getScopeLineNumber() << "] ";
800 /// print - Print global variable.
801 void DIGlobalVariable::print(raw_ostream &OS) const {
803 StringRef Res = getName();
805 OS << " [" << Res << "] ";
807 unsigned Tag = getTag();
808 OS << " [" << dwarf::TagString(Tag) << "] ";
810 // TODO : Print context
811 OS << " [" << getLineNumber() << "] ";
819 if (isGlobalVariable())
820 DIGlobalVariable(DbgNode).print(OS);
824 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
825 const LLVMContext &Ctx) {
826 if (!DL.isUnknown()) { // Print source line info.
827 DIScope Scope(DL.getScope(Ctx));
828 // Omit the directory, because it's likely to be long and uninteresting.
830 CommentOS << Scope.getFilename();
832 CommentOS << "<unknown>";
833 CommentOS << ':' << DL.getLine();
834 if (DL.getCol() != 0)
835 CommentOS << ':' << DL.getCol();
836 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
837 if (!InlinedAtDL.isUnknown()) {
839 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
845 void DIVariable::printExtendedName(raw_ostream &OS) const {
846 const LLVMContext &Ctx = DbgNode->getContext();
847 StringRef Res = getName();
849 OS << Res << "," << getLineNumber();
850 if (MDNode *InlinedAt = getInlinedAt()) {
851 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
852 if (!InlinedAtDL.isUnknown()) {
854 printDebugLoc(InlinedAtDL, OS, Ctx);
860 /// print - Print variable.
861 void DIVariable::print(raw_ostream &OS) const {
862 StringRef Res = getName();
864 OS << " [" << Res << "] ";
866 OS << " [" << getLineNumber() << "] ";
870 // FIXME: Dump complex addresses
873 /// dump - Print descriptor to dbgs() with a newline.
874 void DIDescriptor::dump() const {
875 print(dbgs()); dbgs() << '\n';
878 /// dump - Print compile unit to dbgs() with a newline.
879 void DICompileUnit::dump() const {
880 print(dbgs()); dbgs() << '\n';
883 /// dump - Print type to dbgs() with a newline.
884 void DIType::dump() const {
885 print(dbgs()); dbgs() << '\n';
888 /// dump - Print basic type to dbgs() with a newline.
889 void DIBasicType::dump() const {
890 print(dbgs()); dbgs() << '\n';
893 /// dump - Print derived type to dbgs() with a newline.
894 void DIDerivedType::dump() const {
895 print(dbgs()); dbgs() << '\n';
898 /// dump - Print composite type to dbgs() with a newline.
899 void DICompositeType::dump() const {
900 print(dbgs()); dbgs() << '\n';
903 /// dump - Print subprogram to dbgs() with a newline.
904 void DISubprogram::dump() const {
905 print(dbgs()); dbgs() << '\n';
908 /// dump - Print global variable.
909 void DIGlobalVariable::dump() const {
910 print(dbgs()); dbgs() << '\n';
913 /// dump - Print variable.
914 void DIVariable::dump() const {
915 print(dbgs()); dbgs() << '\n';
918 /// fixupObjcLikeName - Replace contains special characters used
919 /// in a typical Objective-C names with '.' in a given string.
920 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
921 bool isObjCLike = false;
922 for (size_t i = 0, e = Str.size(); i < e; ++i) {
927 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
928 C == '+' || C == '(' || C == ')'))
935 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
936 /// suitable to hold function specific information.
937 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
938 SmallString<32> Name = StringRef("llvm.dbg.lv.");
939 StringRef FName = "fn";
940 if (Fn.getFunction())
941 FName = Fn.getFunction()->getName();
943 FName = Fn.getName();
945 if (FName.startswith(StringRef(&One, 1)))
946 FName = FName.substr(1);
947 fixupObjcLikeName(FName, Name);
948 return M.getNamedMetadata(Name.str());
951 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
952 /// to hold function specific information.
953 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
954 SmallString<32> Name = StringRef("llvm.dbg.lv.");
955 StringRef FName = "fn";
956 if (Fn.getFunction())
957 FName = Fn.getFunction()->getName();
959 FName = Fn.getName();
961 if (FName.startswith(StringRef(&One, 1)))
962 FName = FName.substr(1);
963 fixupObjcLikeName(FName, Name);
965 return M.getOrInsertNamedMetadata(Name.str());
968 /// createInlinedVariable - Create a new inlined variable based on current
970 /// @param DV Current Variable.
971 /// @param InlinedScope Location at current variable is inlined.
972 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
973 LLVMContext &VMContext) {
974 SmallVector<Value *, 16> Elts;
975 // Insert inlined scope as 7th element.
976 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
977 i == 7 ? Elts.push_back(InlinedScope) :
978 Elts.push_back(DV->getOperand(i));
979 return DIVariable(MDNode::get(VMContext, Elts));
982 /// cleanseInlinedVariable - Remove inlined scope from the variable.
983 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
984 SmallVector<Value *, 16> Elts;
985 // Insert inlined scope as 7th element.
986 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
988 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
989 Elts.push_back(DV->getOperand(i));
990 return DIVariable(MDNode::get(VMContext, Elts));
993 //===----------------------------------------------------------------------===//
994 // DebugInfoFinder implementations.
995 //===----------------------------------------------------------------------===//
997 /// processModule - Process entire module and collect debug info.
998 void DebugInfoFinder::processModule(Module &M) {
999 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1000 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1001 DICompileUnit CU(CU_Nodes->getOperand(i));
1003 if (CU.getVersion() > LLVMDebugVersion10) {
1004 DIArray GVs = CU.getGlobalVariables();
1005 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1006 DIGlobalVariable DIG(GVs.getElement(i));
1007 if (addGlobalVariable(DIG))
1008 processType(DIG.getType());
1010 DIArray SPs = CU.getSubprograms();
1011 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1012 processSubprogram(DISubprogram(SPs.getElement(i)));
1013 DIArray EnumTypes = CU.getEnumTypes();
1014 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1015 processType(DIType(EnumTypes.getElement(i)));
1016 DIArray RetainedTypes = CU.getRetainedTypes();
1017 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1018 processType(DIType(RetainedTypes.getElement(i)));
1024 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1025 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1026 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1028 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1029 processDeclare(DDI);
1031 DebugLoc Loc = BI->getDebugLoc();
1032 if (Loc.isUnknown())
1035 LLVMContext &Ctx = BI->getContext();
1036 DIDescriptor Scope(Loc.getScope(Ctx));
1038 if (Scope.isCompileUnit())
1039 addCompileUnit(DICompileUnit(Scope));
1040 else if (Scope.isSubprogram())
1041 processSubprogram(DISubprogram(Scope));
1042 else if (Scope.isLexicalBlockFile()) {
1043 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
1044 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1046 else if (Scope.isLexicalBlock())
1047 processLexicalBlock(DILexicalBlock(Scope));
1049 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1050 processLocation(DILocation(IA));
1053 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1054 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1055 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1056 if (addGlobalVariable(DIG)) {
1057 if (DIG.getVersion() <= LLVMDebugVersion10)
1058 addCompileUnit(DIG.getCompileUnit());
1059 processType(DIG.getType());
1064 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1065 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1066 processSubprogram(DISubprogram(NMD->getOperand(i)));
1069 /// processLocation - Process DILocation.
1070 void DebugInfoFinder::processLocation(DILocation Loc) {
1071 if (!Loc.Verify()) return;
1072 DIDescriptor S(Loc.getScope());
1073 if (S.isCompileUnit())
1074 addCompileUnit(DICompileUnit(S));
1075 else if (S.isSubprogram())
1076 processSubprogram(DISubprogram(S));
1077 else if (S.isLexicalBlock())
1078 processLexicalBlock(DILexicalBlock(S));
1079 else if (S.isLexicalBlockFile()) {
1080 DILexicalBlockFile DBF = DILexicalBlockFile(S);
1081 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1083 processLocation(Loc.getOrigLocation());
1086 /// processType - Process DIType.
1087 void DebugInfoFinder::processType(DIType DT) {
1090 if (DT.getVersion() <= LLVMDebugVersion10)
1091 addCompileUnit(DT.getCompileUnit());
1092 if (DT.isCompositeType()) {
1093 DICompositeType DCT(DT);
1094 processType(DCT.getTypeDerivedFrom());
1095 DIArray DA = DCT.getTypeArray();
1096 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1097 DIDescriptor D = DA.getElement(i);
1099 processType(DIType(D));
1100 else if (D.isSubprogram())
1101 processSubprogram(DISubprogram(D));
1103 } else if (DT.isDerivedType()) {
1104 DIDerivedType DDT(DT);
1105 processType(DDT.getTypeDerivedFrom());
1109 /// processLexicalBlock
1110 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1111 DIScope Context = LB.getContext();
1112 if (Context.isLexicalBlock())
1113 return processLexicalBlock(DILexicalBlock(Context));
1114 else if (Context.isLexicalBlockFile()) {
1115 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
1116 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
1119 return processSubprogram(DISubprogram(Context));
1122 /// processSubprogram - Process DISubprogram.
1123 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1124 if (!addSubprogram(SP))
1126 if (SP.getVersion() <= LLVMDebugVersion10)
1127 addCompileUnit(SP.getCompileUnit());
1128 processType(SP.getType());
1131 /// processDeclare - Process DbgDeclareInst.
1132 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1133 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1137 if (!DV.isVariable())
1140 if (!NodesSeen.insert(DV))
1142 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1143 addCompileUnit(DIVariable(N).getCompileUnit());
1144 processType(DIVariable(N).getType());
1147 /// addType - Add type into Tys.
1148 bool DebugInfoFinder::addType(DIType DT) {
1152 if (!NodesSeen.insert(DT))
1159 /// addCompileUnit - Add compile unit into CUs.
1160 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1164 if (!NodesSeen.insert(CU))
1171 /// addGlobalVariable - Add global variable into GVs.
1172 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1173 if (!DIDescriptor(DIG).isGlobalVariable())
1176 if (!NodesSeen.insert(DIG))
1183 // addSubprogram - Add subprgoram into SPs.
1184 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1185 if (!DIDescriptor(SP).isSubprogram())
1188 if (!NodesSeen.insert(SP))
1195 /// getDISubprogram - Find subprogram that is enclosing this scope.
1196 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1197 DIDescriptor D(Scope);
1198 if (D.isSubprogram())
1199 return DISubprogram(Scope);
1201 if (D.isLexicalBlockFile())
1202 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
1204 if (D.isLexicalBlock())
1205 return getDISubprogram(DILexicalBlock(Scope).getContext());
1207 return DISubprogram();
1210 /// getDICompositeType - Find underlying composite type.
1211 DICompositeType llvm::getDICompositeType(DIType T) {
1212 if (T.isCompositeType())
1213 return DICompositeType(T);
1215 if (T.isDerivedType())
1216 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1218 return DICompositeType();
1221 /// isSubprogramContext - Return true if Context is either a subprogram
1222 /// or another context nested inside a subprogram.
1223 bool llvm::isSubprogramContext(const MDNode *Context) {
1226 DIDescriptor D(Context);
1227 if (D.isSubprogram())
1230 return isSubprogramContext(DIType(Context).getContext());