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/ValueHandle.h"
29 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm::dwarf;
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 bool DIDescriptor::Verify() const {
39 (DIDerivedType(DbgNode).Verify() ||
40 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
41 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
42 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
43 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
44 DILexicalBlock(DbgNode).Verify() ||
45 DILexicalBlockFile(DbgNode).Verify() ||
46 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
47 DIObjCProperty(DbgNode).Verify() ||
48 DITemplateTypeParameter(DbgNode).Verify() ||
49 DITemplateValueParameter(DbgNode).Verify() ||
50 DIImportedEntity(DbgNode).Verify());
53 static Value *getField(const MDNode *DbgNode, unsigned Elt) {
54 if (DbgNode == 0 || Elt >= DbgNode->getNumOperands())
56 return DbgNode->getOperand(Elt);
59 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
60 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
63 static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
64 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
65 return MDS->getString();
69 StringRef DIDescriptor::getStringField(unsigned Elt) const {
70 return ::getStringField(DbgNode, Elt);
73 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
77 if (Elt < DbgNode->getNumOperands())
79 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
80 return CI->getZExtValue();
85 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
89 if (Elt < DbgNode->getNumOperands())
91 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
92 return CI->getSExtValue();
97 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
98 MDNode *Field = getNodeField(DbgNode, Elt);
99 return DIDescriptor(Field);
102 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
106 if (Elt < DbgNode->getNumOperands())
107 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
111 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
115 if (Elt < DbgNode->getNumOperands())
116 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
120 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
124 if (Elt < DbgNode->getNumOperands())
125 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
129 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
133 if (Elt < DbgNode->getNumOperands()) {
134 MDNode *Node = const_cast<MDNode*>(DbgNode);
135 Node->replaceOperandWith(Elt, F);
139 unsigned DIVariable::getNumAddrElements() const {
140 return DbgNode->getNumOperands()-8;
143 /// getInlinedAt - If this variable is inlined then return inline location.
144 MDNode *DIVariable::getInlinedAt() const {
145 return getNodeField(DbgNode, 7);
148 //===----------------------------------------------------------------------===//
150 //===----------------------------------------------------------------------===//
152 /// isBasicType - Return true if the specified tag is legal for
154 bool DIDescriptor::isBasicType() const {
155 if (!DbgNode) return false;
157 case dwarf::DW_TAG_base_type:
158 case dwarf::DW_TAG_unspecified_type:
165 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
166 bool DIDescriptor::isDerivedType() const {
167 if (!DbgNode) return false;
169 case dwarf::DW_TAG_typedef:
170 case dwarf::DW_TAG_pointer_type:
171 case dwarf::DW_TAG_ptr_to_member_type:
172 case dwarf::DW_TAG_reference_type:
173 case dwarf::DW_TAG_rvalue_reference_type:
174 case dwarf::DW_TAG_const_type:
175 case dwarf::DW_TAG_volatile_type:
176 case dwarf::DW_TAG_restrict_type:
177 case dwarf::DW_TAG_member:
178 case dwarf::DW_TAG_inheritance:
179 case dwarf::DW_TAG_friend:
182 // CompositeTypes are currently modelled as DerivedTypes.
183 return isCompositeType();
187 /// isCompositeType - Return true if the specified tag is legal for
189 bool DIDescriptor::isCompositeType() const {
190 if (!DbgNode) return false;
192 case dwarf::DW_TAG_array_type:
193 case dwarf::DW_TAG_structure_type:
194 case dwarf::DW_TAG_union_type:
195 case dwarf::DW_TAG_enumeration_type:
196 case dwarf::DW_TAG_subroutine_type:
197 case dwarf::DW_TAG_class_type:
204 /// isVariable - Return true if the specified tag is legal for DIVariable.
205 bool DIDescriptor::isVariable() const {
206 if (!DbgNode) return false;
208 case dwarf::DW_TAG_auto_variable:
209 case dwarf::DW_TAG_arg_variable:
216 /// isType - Return true if the specified tag is legal for DIType.
217 bool DIDescriptor::isType() const {
218 return isBasicType() || isCompositeType() || isDerivedType();
221 /// isSubprogram - Return true if the specified tag is legal for
223 bool DIDescriptor::isSubprogram() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
227 /// isGlobalVariable - Return true if the specified tag is legal for
228 /// DIGlobalVariable.
229 bool DIDescriptor::isGlobalVariable() const {
230 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
231 getTag() == dwarf::DW_TAG_constant);
234 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
235 bool DIDescriptor::isGlobal() const {
236 return isGlobalVariable();
239 /// isUnspecifiedParmeter - Return true if the specified tag is
240 /// DW_TAG_unspecified_parameters.
241 bool DIDescriptor::isUnspecifiedParameter() const {
242 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
245 /// isScope - Return true if the specified tag is one of the scope
247 bool DIDescriptor::isScope() const {
248 if (!DbgNode) return false;
250 case dwarf::DW_TAG_compile_unit:
251 case dwarf::DW_TAG_lexical_block:
252 case dwarf::DW_TAG_subprogram:
253 case dwarf::DW_TAG_namespace:
261 /// isTemplateTypeParameter - Return true if the specified tag is
262 /// DW_TAG_template_type_parameter.
263 bool DIDescriptor::isTemplateTypeParameter() const {
264 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
267 /// isTemplateValueParameter - Return true if the specified tag is
268 /// DW_TAG_template_value_parameter.
269 bool DIDescriptor::isTemplateValueParameter() const {
270 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
271 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
272 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
275 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
276 bool DIDescriptor::isCompileUnit() const {
277 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
280 /// isFile - Return true if the specified tag is DW_TAG_file_type.
281 bool DIDescriptor::isFile() const {
282 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
285 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
286 bool DIDescriptor::isNameSpace() const {
287 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
290 /// isLexicalBlockFile - Return true if the specified descriptor is a
291 /// lexical block with an extra file.
292 bool DIDescriptor::isLexicalBlockFile() const {
293 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
294 (DbgNode->getNumOperands() == 3);
297 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
298 bool DIDescriptor::isLexicalBlock() const {
299 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
300 (DbgNode->getNumOperands() > 3);
303 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
304 bool DIDescriptor::isSubrange() const {
305 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
308 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
309 bool DIDescriptor::isEnumerator() const {
310 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
313 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
314 bool DIDescriptor::isObjCProperty() const {
315 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
318 /// \brief Return true if the specified tag is DW_TAG_imported_module or
319 /// DW_TAG_imported_declaration.
320 bool DIDescriptor::isImportedEntity() const {
321 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
322 getTag() == dwarf::DW_TAG_imported_declaration);
325 //===----------------------------------------------------------------------===//
326 // Simple Descriptor Constructors and other Methods
327 //===----------------------------------------------------------------------===//
329 unsigned DIArray::getNumElements() const {
332 return DbgNode->getNumOperands();
335 /// replaceAllUsesWith - Replace all uses of debug info referenced by
337 void DIType::replaceAllUsesWith(DIDescriptor &D) {
341 // Since we use a TrackingVH for the node, its easy for clients to manufacture
342 // legitimate situations where they want to replaceAllUsesWith() on something
343 // which, due to uniquing, has merged with the source. We shield clients from
344 // this detail by allowing a value to be replaced with replaceAllUsesWith()
347 MDNode *Node = const_cast<MDNode*>(DbgNode);
348 const MDNode *DN = D;
349 const Value *V = cast_or_null<Value>(DN);
350 Node->replaceAllUsesWith(const_cast<Value*>(V));
351 MDNode::deleteTemporary(Node);
355 /// replaceAllUsesWith - Replace all uses of debug info referenced by
357 void DIType::replaceAllUsesWith(MDNode *D) {
361 // Since we use a TrackingVH for the node, its easy for clients to manufacture
362 // legitimate situations where they want to replaceAllUsesWith() on something
363 // which, due to uniquing, has merged with the source. We shield clients from
364 // this detail by allowing a value to be replaced with replaceAllUsesWith()
367 MDNode *Node = const_cast<MDNode*>(DbgNode);
368 const MDNode *DN = D;
369 const Value *V = cast_or_null<Value>(DN);
370 Node->replaceAllUsesWith(const_cast<Value*>(V));
371 MDNode::deleteTemporary(Node);
375 /// isUnsignedDIType - Return true if type encoding is unsigned.
376 bool DIType::isUnsignedDIType() {
377 DIDerivedType DTy(DbgNode);
379 return DTy.getTypeDerivedFrom().isUnsignedDIType();
381 DIBasicType BTy(DbgNode);
383 unsigned Encoding = BTy.getEncoding();
384 if (Encoding == dwarf::DW_ATE_unsigned ||
385 Encoding == dwarf::DW_ATE_unsigned_char ||
386 Encoding == dwarf::DW_ATE_boolean)
392 /// Verify - Verify that a compile unit is well formed.
393 bool DICompileUnit::Verify() const {
394 if (!isCompileUnit())
397 // Don't bother verifying the compilation directory or producer string
398 // as those could be empty.
399 if (getFilename().empty())
402 return DbgNode->getNumOperands() == 13;
405 /// Verify - Verify that an ObjC property is well formed.
406 bool DIObjCProperty::Verify() const {
407 if (!isObjCProperty())
410 // Don't worry about the rest of the strings for now.
411 return DbgNode->getNumOperands() == 8;
414 /// Verify - Verify that a type descriptor is well formed.
415 bool DIType::Verify() const {
418 // FIXME: Sink this into the various subclass verifies.
419 unsigned Tag = getTag();
420 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
421 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
422 Tag != dwarf::DW_TAG_ptr_to_member_type &&
423 Tag != dwarf::DW_TAG_reference_type &&
424 Tag != dwarf::DW_TAG_rvalue_reference_type &&
425 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
426 Tag != dwarf::DW_TAG_enumeration_type &&
427 Tag != dwarf::DW_TAG_subroutine_type &&
428 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
429 getFilename().empty())
431 // DIType is abstract, it should be a BasicType, a DerivedType or
434 DIBasicType(DbgNode).Verify();
435 else if (isCompositeType())
436 DICompositeType(DbgNode).Verify();
437 else if (isDerivedType())
438 DIDerivedType(DbgNode).Verify();
444 /// Verify - Verify that a basic type descriptor is well formed.
445 bool DIBasicType::Verify() const {
446 return isBasicType() && DbgNode->getNumOperands() == 10;
449 /// Verify - Verify that a derived type descriptor is well formed.
450 bool DIDerivedType::Verify() const {
451 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
452 DbgNode->getNumOperands() <= 14;
455 /// Verify - Verify that a composite type descriptor is well formed.
456 bool DICompositeType::Verify() const {
457 if (!isCompositeType())
460 return DbgNode->getNumOperands() >= 10 && DbgNode->getNumOperands() <= 14;
463 /// Verify - Verify that a subprogram descriptor is well formed.
464 bool DISubprogram::Verify() const {
468 return DbgNode->getNumOperands() == 20;
471 /// Verify - Verify that a global variable descriptor is well formed.
472 bool DIGlobalVariable::Verify() const {
473 if (!isGlobalVariable())
476 if (getDisplayName().empty())
479 return DbgNode->getNumOperands() == 13;
482 /// Verify - Verify that a variable descriptor is well formed.
483 bool DIVariable::Verify() const {
487 return DbgNode->getNumOperands() >= 8;
490 /// Verify - Verify that a location descriptor is well formed.
491 bool DILocation::Verify() const {
495 return DbgNode->getNumOperands() == 4;
498 /// Verify - Verify that a namespace descriptor is well formed.
499 bool DINameSpace::Verify() const {
502 return DbgNode->getNumOperands() == 5;
505 /// \brief Retrieve the MDNode for the directory/file pair.
506 MDNode *DIFile::getFileNode() const {
507 return getNodeField(DbgNode, 1);
510 /// \brief Verify that the file descriptor is well formed.
511 bool DIFile::Verify() const {
512 return isFile() && DbgNode->getNumOperands() == 2;
515 /// \brief Verify that the enumerator descriptor is well formed.
516 bool DIEnumerator::Verify() const {
517 return isEnumerator() && DbgNode->getNumOperands() == 3;
520 /// \brief Verify that the subrange descriptor is well formed.
521 bool DISubrange::Verify() const {
522 return isSubrange() && DbgNode->getNumOperands() == 3;
525 /// \brief Verify that the lexical block descriptor is well formed.
526 bool DILexicalBlock::Verify() const {
527 return isLexicalBlock() && DbgNode->getNumOperands() == 6;
530 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
531 bool DILexicalBlockFile::Verify() const {
532 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
535 /// \brief Verify that the template type parameter descriptor is well formed.
536 bool DITemplateTypeParameter::Verify() const {
537 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
540 /// \brief Verify that the template value parameter descriptor is well formed.
541 bool DITemplateValueParameter::Verify() const {
542 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
545 /// \brief Verify that the imported module descriptor is well formed.
546 bool DIImportedEntity::Verify() const {
547 return isImportedEntity() &&
548 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
551 /// getOriginalTypeSize - If this type is derived from a base type then
552 /// return base type size.
553 uint64_t DIDerivedType::getOriginalTypeSize() const {
554 unsigned Tag = getTag();
556 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
557 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
558 Tag != dwarf::DW_TAG_restrict_type)
559 return getSizeInBits();
561 DIType BaseType = getTypeDerivedFrom();
563 // If this type is not derived from any type then take conservative approach.
564 if (!BaseType.isValid())
565 return getSizeInBits();
567 // If this is a derived type, go ahead and get the base type, unless it's a
568 // reference then it's just the size of the field. Pointer types have no need
569 // of this since they're a different type of qualification on the type.
570 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
571 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
572 return getSizeInBits();
574 if (BaseType.isDerivedType())
575 return DIDerivedType(BaseType).getOriginalTypeSize();
577 return BaseType.getSizeInBits();
580 /// getObjCProperty - Return property node, if this ivar is associated with one.
581 MDNode *DIDerivedType::getObjCProperty() const {
582 return getNodeField(DbgNode, 10);
585 /// \brief Set the array of member DITypes.
586 void DICompositeType::setTypeArray(DIArray Elements, DIArray TParams) {
587 assert((!TParams || DbgNode->getNumOperands() == 14) &&
588 "If you're setting the template parameters this should include a slot "
590 TrackingVH<MDNode> N(*this);
591 N->replaceOperandWith(10, Elements);
593 N->replaceOperandWith(13, TParams);
597 /// \brief Set the containing type.
598 void DICompositeType::setContainingType(DICompositeType ContainingType) {
599 TrackingVH<MDNode> N(*this);
600 N->replaceOperandWith(12, ContainingType);
604 /// isInlinedFnArgument - Return true if this variable provides debugging
605 /// information for an inlined function arguments.
606 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
607 assert(CurFn && "Invalid function");
608 if (!getContext().isSubprogram())
610 // This variable is not inlined function argument if its scope
611 // does not describe current function.
612 return !DISubprogram(getContext()).describes(CurFn);
615 /// describes - Return true if this subprogram provides debugging
616 /// information for the function F.
617 bool DISubprogram::describes(const Function *F) {
618 assert(F && "Invalid function");
619 if (F == getFunction())
621 StringRef Name = getLinkageName();
624 if (F->getName() == Name)
629 unsigned DISubprogram::isOptimized() const {
630 assert (DbgNode && "Invalid subprogram descriptor!");
631 if (DbgNode->getNumOperands() == 15)
632 return getUnsignedField(14);
636 MDNode *DISubprogram::getVariablesNodes() const {
637 return getNodeField(DbgNode, 18);
640 DIArray DISubprogram::getVariables() const {
641 return DIArray(getNodeField(DbgNode, 18));
644 Value *DITemplateValueParameter::getValue() const {
645 return getField(DbgNode, 4);
648 StringRef DIScope::getFilename() const {
651 return ::getStringField(getNodeField(DbgNode, 1), 0);
654 StringRef DIScope::getDirectory() const {
657 return ::getStringField(getNodeField(DbgNode, 1), 1);
660 DIArray DICompileUnit::getEnumTypes() const {
661 if (!DbgNode || DbgNode->getNumOperands() < 13)
664 return DIArray(getNodeField(DbgNode, 7));
667 DIArray DICompileUnit::getRetainedTypes() const {
668 if (!DbgNode || DbgNode->getNumOperands() < 13)
671 return DIArray(getNodeField(DbgNode, 8));
674 DIArray DICompileUnit::getSubprograms() const {
675 if (!DbgNode || DbgNode->getNumOperands() < 13)
678 return DIArray(getNodeField(DbgNode, 9));
682 DIArray DICompileUnit::getGlobalVariables() const {
683 if (!DbgNode || DbgNode->getNumOperands() < 13)
686 return DIArray(getNodeField(DbgNode, 10));
689 DIArray DICompileUnit::getImportedEntities() const {
690 if (!DbgNode || DbgNode->getNumOperands() < 13)
693 return DIArray(getNodeField(DbgNode, 11));
696 /// fixupSubprogramName - Replace contains special characters used
697 /// in a typical Objective-C names with '.' in a given string.
698 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
700 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
701 FName = Function::getRealLinkageName(FName);
703 StringRef Prefix("llvm.dbg.lv.");
704 Out.reserve(FName.size() + Prefix.size());
705 Out.append(Prefix.begin(), Prefix.end());
707 bool isObjCLike = false;
708 for (size_t i = 0, e = FName.size(); i < e; ++i) {
713 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
714 C == '+' || C == '(' || C == ')'))
721 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
722 /// suitable to hold function specific information.
723 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
724 SmallString<32> Name;
725 fixupSubprogramName(Fn, Name);
726 return M.getNamedMetadata(Name.str());
729 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
730 /// to hold function specific information.
731 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
732 SmallString<32> Name;
733 fixupSubprogramName(Fn, Name);
734 return M.getOrInsertNamedMetadata(Name.str());
737 /// createInlinedVariable - Create a new inlined variable based on current
739 /// @param DV Current Variable.
740 /// @param InlinedScope Location at current variable is inlined.
741 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
742 LLVMContext &VMContext) {
743 SmallVector<Value *, 16> Elts;
744 // Insert inlined scope as 7th element.
745 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
746 i == 7 ? Elts.push_back(InlinedScope) :
747 Elts.push_back(DV->getOperand(i));
748 return DIVariable(MDNode::get(VMContext, Elts));
751 /// cleanseInlinedVariable - Remove inlined scope from the variable.
752 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
753 SmallVector<Value *, 16> Elts;
754 // Insert inlined scope as 7th element.
755 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
757 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
758 Elts.push_back(DV->getOperand(i));
759 return DIVariable(MDNode::get(VMContext, Elts));
762 /// getDISubprogram - Find subprogram that is enclosing this scope.
763 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
764 DIDescriptor D(Scope);
765 if (D.isSubprogram())
766 return DISubprogram(Scope);
768 if (D.isLexicalBlockFile())
769 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
771 if (D.isLexicalBlock())
772 return getDISubprogram(DILexicalBlock(Scope).getContext());
774 return DISubprogram();
777 /// getDICompositeType - Find underlying composite type.
778 DICompositeType llvm::getDICompositeType(DIType T) {
779 if (T.isCompositeType())
780 return DICompositeType(T);
782 if (T.isDerivedType())
783 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
785 return DICompositeType();
788 /// isSubprogramContext - Return true if Context is either a subprogram
789 /// or another context nested inside a subprogram.
790 bool llvm::isSubprogramContext(const MDNode *Context) {
793 DIDescriptor D(Context);
794 if (D.isSubprogram())
797 return isSubprogramContext(DIType(Context).getContext());
801 //===----------------------------------------------------------------------===//
802 // DebugInfoFinder implementations.
803 //===----------------------------------------------------------------------===//
805 void DebugInfoFinder::reset() {
814 /// processModule - Process entire module and collect debug info.
815 void DebugInfoFinder::processModule(const Module &M) {
816 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
817 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
818 DICompileUnit CU(CU_Nodes->getOperand(i));
820 DIArray GVs = CU.getGlobalVariables();
821 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
822 DIGlobalVariable DIG(GVs.getElement(i));
823 if (addGlobalVariable(DIG)) {
824 processScope(DIG.getContext());
825 processType(DIG.getType());
828 DIArray SPs = CU.getSubprograms();
829 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
830 processSubprogram(DISubprogram(SPs.getElement(i)));
831 DIArray EnumTypes = CU.getEnumTypes();
832 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
833 processType(DIType(EnumTypes.getElement(i)));
834 DIArray RetainedTypes = CU.getRetainedTypes();
835 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
836 processType(DIType(RetainedTypes.getElement(i)));
837 // FIXME: We really shouldn't be bailing out after visiting just one CU
843 /// processLocation - Process DILocation.
844 void DebugInfoFinder::processLocation(DILocation Loc) {
845 if (!Loc.Verify()) return;
846 DIDescriptor S(Loc.getScope());
847 if (S.isCompileUnit())
848 addCompileUnit(DICompileUnit(S));
849 else if (S.isSubprogram())
850 processSubprogram(DISubprogram(S));
851 else if (S.isLexicalBlock())
852 processLexicalBlock(DILexicalBlock(S));
853 else if (S.isLexicalBlockFile()) {
854 DILexicalBlockFile DBF = DILexicalBlockFile(S);
855 processLexicalBlock(DILexicalBlock(DBF.getScope()));
857 processLocation(Loc.getOrigLocation());
860 /// processType - Process DIType.
861 void DebugInfoFinder::processType(DIType DT) {
864 processScope(DT.getContext());
865 if (DT.isCompositeType()) {
866 DICompositeType DCT(DT);
867 processType(DCT.getTypeDerivedFrom());
868 DIArray DA = DCT.getTypeArray();
869 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
870 DIDescriptor D = DA.getElement(i);
872 processType(DIType(D));
873 else if (D.isSubprogram())
874 processSubprogram(DISubprogram(D));
876 } else if (DT.isDerivedType()) {
877 DIDerivedType DDT(DT);
878 processType(DDT.getTypeDerivedFrom());
882 void DebugInfoFinder::processScope(DIScope Scope) {
883 if (Scope.isType()) {
888 if (Scope.isCompileUnit()) {
889 addCompileUnit(DICompileUnit(Scope));
892 if (Scope.isSubprogram()) {
893 processSubprogram(DISubprogram(Scope));
896 if (!addScope(Scope))
898 if (Scope.isLexicalBlock()) {
899 DILexicalBlock LB(Scope);
900 processScope(LB.getContext());
901 } else if (Scope.isLexicalBlockFile()) {
902 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
903 processScope(LBF.getScope());
904 } else if (Scope.isNameSpace()) {
905 DINameSpace NS(Scope);
906 processScope(NS.getContext());
910 /// processLexicalBlock
911 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
912 DIScope Context = LB.getContext();
913 if (Context.isLexicalBlock())
914 return processLexicalBlock(DILexicalBlock(Context));
915 else if (Context.isLexicalBlockFile()) {
916 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
917 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
920 return processSubprogram(DISubprogram(Context));
923 /// processSubprogram - Process DISubprogram.
924 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
925 if (!addSubprogram(SP))
927 processScope(SP.getContext());
928 processType(SP.getType());
931 /// processDeclare - Process DbgDeclareInst.
932 void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
933 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
937 if (!DV.isVariable())
940 if (!NodesSeen.insert(DV))
942 processScope(DIVariable(N).getContext());
943 processType(DIVariable(N).getType());
946 void DebugInfoFinder::processValue(const DbgValueInst *DVI) {
947 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
951 if (!DV.isVariable())
954 if (!NodesSeen.insert(DV))
956 processType(DIVariable(N).getType());
959 /// addType - Add type into Tys.
960 bool DebugInfoFinder::addType(DIType DT) {
964 if (!NodesSeen.insert(DT))
971 /// addCompileUnit - Add compile unit into CUs.
972 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
976 if (!NodesSeen.insert(CU))
983 /// addGlobalVariable - Add global variable into GVs.
984 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
985 if (!DIDescriptor(DIG).isGlobalVariable())
988 if (!NodesSeen.insert(DIG))
995 // addSubprogram - Add subprgoram into SPs.
996 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
997 if (!DIDescriptor(SP).isSubprogram())
1000 if (!NodesSeen.insert(SP))
1007 bool DebugInfoFinder::addScope(DIScope Scope) {
1010 if (!NodesSeen.insert(Scope))
1012 Scopes.push_back(Scope);
1016 //===----------------------------------------------------------------------===//
1017 // DIDescriptor: dump routines for all descriptors.
1018 //===----------------------------------------------------------------------===//
1020 /// dump - Print descriptor to dbgs() with a newline.
1021 void DIDescriptor::dump() const {
1022 print(dbgs()); dbgs() << '\n';
1025 /// print - Print descriptor.
1026 void DIDescriptor::print(raw_ostream &OS) const {
1027 if (!DbgNode) return;
1029 if (const char *Tag = dwarf::TagString(getTag()))
1030 OS << "[ " << Tag << " ]";
1032 if (this->isSubrange()) {
1033 DISubrange(DbgNode).printInternal(OS);
1034 } else if (this->isCompileUnit()) {
1035 DICompileUnit(DbgNode).printInternal(OS);
1036 } else if (this->isFile()) {
1037 DIFile(DbgNode).printInternal(OS);
1038 } else if (this->isEnumerator()) {
1039 DIEnumerator(DbgNode).printInternal(OS);
1040 } else if (this->isBasicType()) {
1041 DIType(DbgNode).printInternal(OS);
1042 } else if (this->isDerivedType()) {
1043 DIDerivedType(DbgNode).printInternal(OS);
1044 } else if (this->isCompositeType()) {
1045 DICompositeType(DbgNode).printInternal(OS);
1046 } else if (this->isSubprogram()) {
1047 DISubprogram(DbgNode).printInternal(OS);
1048 } else if (this->isGlobalVariable()) {
1049 DIGlobalVariable(DbgNode).printInternal(OS);
1050 } else if (this->isVariable()) {
1051 DIVariable(DbgNode).printInternal(OS);
1052 } else if (this->isObjCProperty()) {
1053 DIObjCProperty(DbgNode).printInternal(OS);
1054 } else if (this->isNameSpace()) {
1055 DINameSpace(DbgNode).printInternal(OS);
1056 } else if (this->isScope()) {
1057 DIScope(DbgNode).printInternal(OS);
1061 void DISubrange::printInternal(raw_ostream &OS) const {
1062 int64_t Count = getCount();
1064 OS << " [" << getLo() << ", " << Count - 1 << ']';
1066 OS << " [unbounded]";
1069 void DIScope::printInternal(raw_ostream &OS) const {
1070 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1073 void DICompileUnit::printInternal(raw_ostream &OS) const {
1074 DIScope::printInternal(OS);
1076 unsigned Lang = getLanguage();
1077 if (const char *LangStr = dwarf::LanguageString(Lang))
1080 (OS << "lang 0x").write_hex(Lang);
1084 void DIEnumerator::printInternal(raw_ostream &OS) const {
1085 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1088 void DIType::printInternal(raw_ostream &OS) const {
1089 if (!DbgNode) return;
1091 StringRef Res = getName();
1093 OS << " [" << Res << "]";
1095 // TODO: Print context?
1097 OS << " [line " << getLineNumber()
1098 << ", size " << getSizeInBits()
1099 << ", align " << getAlignInBits()
1100 << ", offset " << getOffsetInBits();
1102 if (const char *Enc =
1103 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1104 OS << ", enc " << Enc;
1109 else if (isProtected())
1110 OS << " [protected]";
1113 OS << " [artificial]";
1115 if (isForwardDecl())
1117 else if (getTag() == dwarf::DW_TAG_structure_type ||
1118 getTag() == dwarf::DW_TAG_union_type ||
1119 getTag() == dwarf::DW_TAG_enumeration_type ||
1120 getTag() == dwarf::DW_TAG_class_type)
1124 if (isStaticMember())
1128 void DIDerivedType::printInternal(raw_ostream &OS) const {
1129 DIType::printInternal(OS);
1130 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1133 void DICompositeType::printInternal(raw_ostream &OS) const {
1134 DIType::printInternal(OS);
1135 DIArray A = getTypeArray();
1136 OS << " [" << A.getNumElements() << " elements]";
1139 void DINameSpace::printInternal(raw_ostream &OS) const {
1140 StringRef Name = getName();
1142 OS << " [" << Name << ']';
1144 OS << " [line " << getLineNumber() << ']';
1147 void DISubprogram::printInternal(raw_ostream &OS) const {
1148 // TODO : Print context
1149 OS << " [line " << getLineNumber() << ']';
1151 if (isLocalToUnit())
1157 if (getScopeLineNumber() != getLineNumber())
1158 OS << " [scope " << getScopeLineNumber() << "]";
1162 else if (isProtected())
1163 OS << " [protected]";
1165 StringRef Res = getName();
1167 OS << " [" << Res << ']';
1170 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1171 StringRef Res = getName();
1173 OS << " [" << Res << ']';
1175 OS << " [line " << getLineNumber() << ']';
1177 // TODO : Print context
1179 if (isLocalToUnit())
1186 void DIVariable::printInternal(raw_ostream &OS) const {
1187 StringRef Res = getName();
1189 OS << " [" << Res << ']';
1191 OS << " [line " << getLineNumber() << ']';
1194 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1195 StringRef Name = getObjCPropertyName();
1197 OS << " [" << Name << ']';
1199 OS << " [line " << getLineNumber()
1200 << ", properties " << getUnsignedField(6) << ']';
1203 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1204 const LLVMContext &Ctx) {
1205 if (!DL.isUnknown()) { // Print source line info.
1206 DIScope Scope(DL.getScope(Ctx));
1207 assert(Scope.isScope() &&
1208 "Scope of a DebugLoc should be a DIScope.");
1209 // Omit the directory, because it's likely to be long and uninteresting.
1210 CommentOS << Scope.getFilename();
1211 CommentOS << ':' << DL.getLine();
1212 if (DL.getCol() != 0)
1213 CommentOS << ':' << DL.getCol();
1214 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1215 if (!InlinedAtDL.isUnknown()) {
1216 CommentOS << " @[ ";
1217 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1223 void DIVariable::printExtendedName(raw_ostream &OS) const {
1224 const LLVMContext &Ctx = DbgNode->getContext();
1225 StringRef Res = getName();
1227 OS << Res << "," << getLineNumber();
1228 if (MDNode *InlinedAt = getInlinedAt()) {
1229 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1230 if (!InlinedAtDL.isUnknown()) {
1232 printDebugLoc(InlinedAtDL, OS, Ctx);