1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
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 defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/IR/DebugInfoMetadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
52 class DILexicalBlockFile;
58 /// \brief Maps from type identifier to the actual MDNode.
59 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
61 /// \brief A thin wraper around MDNode to access encoded debug info.
63 /// This should not be stored in a container, because the underlying MDNode may
64 /// change in certain situations.
66 // Befriends DIRef so DIRef can befriend the protected member
67 // function: getFieldAs<DIRef>.
68 template <typename T> friend class DIRef;
71 /// \brief Duplicated debug info flags.
73 /// \see DebugNode::DIFlags.
75 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
76 #include "llvm/IR/DebugInfoFlags.def"
77 FlagAccessibility = DebugNode::FlagAccessibility
81 const MDNode *DbgNode;
83 DIDescriptor getDescriptorField(unsigned Elt) const;
84 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
85 return DescTy(getDescriptorField(Elt));
89 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
91 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
92 operator MDNode *() const { return get(); }
93 MDNode *operator->() const { return get(); }
94 MDNode &operator*() const {
95 assert(get() && "Expected valid pointer");
99 // An explicit operator bool so that we can do testing of DI values
101 // FIXME: This operator bool isn't actually protecting anything at the
102 // moment due to the conversion operator above making DIDescriptor nodes
103 // implicitly convertable to bool.
104 explicit operator bool() const { return DbgNode != nullptr; }
106 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
107 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
109 uint16_t getTag() const {
110 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
115 void print(raw_ostream &OS) const;
118 /// \brief Replace all uses of debug info referenced by this descriptor.
119 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
120 void replaceAllUsesWith(MDNode *D);
123 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
125 template <> struct simplify_type<const DESC>; \
126 template <> struct simplify_type<DESC>;
127 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
135 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
136 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
137 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
138 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
139 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
140 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
141 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
142 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
143 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
144 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
145 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
146 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
147 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
148 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
149 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
150 #undef DECLARE_SIMPLIFY_DESCRIPTOR
152 /// \brief This is used to represent ranges, for array bounds.
153 class DISubrange : public DIDescriptor {
155 DISubrange() = default;
156 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
158 MDSubrange *get() const {
159 return cast_or_null<MDSubrange>(DIDescriptor::get());
161 operator MDSubrange *() const { return get(); }
162 MDSubrange *operator->() const { return get(); }
163 MDSubrange &operator*() const {
164 assert(get() && "Expected valid pointer");
168 int64_t getLo() const { return get()->getLowerBound(); }
169 int64_t getCount() const { return get()->getCount(); }
172 /// \brief This descriptor holds an array of nodes with type T.
173 template <typename T> class DITypedArray : public DIDescriptor {
175 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
176 operator MDTuple *() const {
177 return const_cast<MDTuple *>(cast_or_null<MDTuple>(DbgNode));
179 unsigned getNumElements() const {
180 return DbgNode ? DbgNode->getNumOperands() : 0;
182 T getElement(unsigned Idx) const { return getFieldAs<T>(Idx); }
185 typedef DITypedArray<DIDescriptor> DIArray;
187 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
189 /// FIXME: it seems strange that this doesn't have either a reference to the
190 /// type/precision or a file/line pair for location info.
191 class DIEnumerator : public DIDescriptor {
193 DIEnumerator() = default;
194 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
196 MDEnumerator *get() const {
197 return cast_or_null<MDEnumerator>(DIDescriptor::get());
199 operator MDEnumerator *() const { return get(); }
200 MDEnumerator *operator->() const { return get(); }
201 MDEnumerator &operator*() const {
202 assert(get() && "Expected valid pointer");
206 StringRef getName() const { return get()->getName(); }
207 int64_t getEnumValue() const { return get()->getValue(); }
210 template <typename T> class DIRef;
211 typedef DIRef<DIDescriptor> DIDescriptorRef;
212 typedef DIRef<DIScope> DIScopeRef;
213 typedef DIRef<DIType> DITypeRef;
214 typedef DITypedArray<DITypeRef> DITypeArray;
216 /// \brief A base class for various scopes.
218 /// Although, implementation-wise, DIScope is the parent class of most
219 /// other DIxxx classes, including DIType and its descendants, most of
220 /// DIScope's descendants are not a substitutable subtype of
221 /// DIScope. The DIDescriptor::isScope() method only is true for
222 /// DIScopes that are scopes in the strict lexical scope sense
223 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
224 class DIScope : public DIDescriptor {
227 DIScope(const MDScope *N) : DIDescriptor(N) {}
229 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
230 operator MDScope *() const { return get(); }
231 MDScope *operator->() const { return get(); }
232 MDScope &operator*() const {
233 assert(get() && "Expected valid pointer");
237 /// \brief Get the parent scope.
239 /// Gets the parent scope for this scope node or returns a default
240 /// constructed scope.
241 DIScopeRef getContext() const;
242 /// \brief Get the scope name.
244 /// If the scope node has a name, return that, else return an empty string.
245 StringRef getName() const;
246 StringRef getFilename() const;
247 StringRef getDirectory() const;
249 /// \brief Generate a reference to this DIScope.
251 /// Uses the type identifier instead of the actual MDNode if possible, to
252 /// help type uniquing.
253 DIScopeRef getRef() const;
256 /// \brief Represents reference to a DIDescriptor.
258 /// Abstracts over direct and identifier-based metadata references.
259 template <typename T> class DIRef {
260 template <typename DescTy>
261 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
262 friend DIScopeRef DIScope::getContext() const;
263 friend DIScopeRef DIScope::getRef() const;
266 /// \brief Val can be either a MDNode or a MDString.
268 /// In the latter, MDString specifies the type identifier.
270 explicit DIRef(const Metadata *V);
274 DIRef(const TypedDebugNodeRef<U> &Ref,
275 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
279 T resolve(const DITypeIdentifierMap &Map) const;
280 operator Metadata *() const { return const_cast<Metadata *>(Val); }
282 static DIRef get(const Metadata *MD) { return DIRef(MD); }
286 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
288 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
289 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
291 /// \brief Handle fields that are references to DIDescriptors.
293 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
294 /// \brief Specialize DIRef constructor for DIDescriptorRef.
295 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
297 /// \brief Handle fields that are references to DIScopes.
298 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
299 /// \brief Specialize DIRef constructor for DIScopeRef.
300 template <> DIRef<DIScope>::DIRef(const Metadata *V);
302 /// \brief Handle fields that are references to DITypes.
303 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
304 /// \brief Specialize DIRef constructor for DITypeRef.
305 template <> DIRef<DIType>::DIRef(const Metadata *V);
307 /// \brief This is a wrapper for a type.
309 /// FIXME: Types should be factored much better so that CV qualifiers and
310 /// others do not require a huge and empty descriptor full of zeros.
311 class DIType : public DIScope {
314 DIType(const MDType *N) : DIScope(N) {}
316 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
317 operator MDType *() const { return get(); }
318 MDType *operator->() const { return get(); }
319 MDType &operator*() const {
320 assert(get() && "Expected valid pointer");
324 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
325 StringRef getName() const { return get()->getName(); }
326 unsigned getLineNumber() const { return get()->getLine(); }
327 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
328 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
329 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
330 // carry this is just plain insane.
331 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
332 unsigned getFlags() const { return get()->getFlags(); }
334 bool isPrivate() const { return get()->isPrivate(); }
335 bool isProtected() const { return get()->isProtected(); }
336 bool isPublic() const { return get()->isPublic(); }
337 bool isForwardDecl() const { return get()->isForwardDecl(); }
338 bool isAppleBlockExtension() const { return get()->isAppleBlockExtension(); }
339 bool isBlockByrefStruct() const { return get()->isBlockByrefStruct(); }
340 bool isVirtual() const { return get()->isVirtual(); }
341 bool isArtificial() const { return get()->isArtificial(); }
342 bool isObjectPointer() const { return get()->isObjectPointer(); }
343 bool isObjcClassComplete() const { return get()->isObjcClassComplete(); }
344 bool isVector() const { return get()->isVector(); }
345 bool isStaticMember() const { return get()->isStaticMember(); }
346 bool isLValueReference() const { return get()->isLValueReference(); }
347 bool isRValueReference() const { return get()->isRValueReference(); }
349 bool isValid() const { return DbgNode && isa<MDType>(*this); }
352 /// \brief A basic type, like 'int' or 'float'.
353 class DIBasicType : public DIType {
355 DIBasicType() = default;
356 DIBasicType(const MDBasicType *N) : DIType(N) {}
358 MDBasicType *get() const {
359 return cast_or_null<MDBasicType>(DIDescriptor::get());
361 operator MDBasicType *() const { return get(); }
362 MDBasicType *operator->() const { return get(); }
363 MDBasicType &operator*() const {
364 assert(get() && "Expected valid pointer");
368 unsigned getEncoding() const { return get()->getEncoding(); }
371 /// \brief A simple derived type
373 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
374 /// Or, a data member of a class/struct/union.
375 class DIDerivedType : public DIType {
377 DIDerivedType() = default;
378 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
380 MDDerivedTypeBase *get() const {
381 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
383 operator MDDerivedTypeBase *() const { return get(); }
384 MDDerivedTypeBase *operator->() const { return get(); }
385 MDDerivedTypeBase &operator*() const {
386 assert(get() && "Expected valid pointer");
390 DITypeRef getTypeDerivedFrom() const {
391 return DITypeRef::get(get()->getBaseType());
394 /// \brief Return property node, if this ivar is associated with one.
395 MDNode *getObjCProperty() const {
396 if (auto *N = dyn_cast<MDDerivedType>(get()))
397 return dyn_cast_or_null<MDNode>(N->getExtraData());
401 DITypeRef getClassType() const {
402 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
403 if (auto *N = dyn_cast<MDDerivedType>(get()))
404 return DITypeRef::get(N->getExtraData());
405 return DITypeRef::get(nullptr);
408 Constant *getConstant() const {
409 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
410 if (auto *N = dyn_cast<MDDerivedType>(get()))
411 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
412 return C->getValue();
418 /// \brief Types that refer to multiple other types.
420 /// This descriptor holds a type that can refer to multiple other types, like a
421 /// function or struct.
423 /// DICompositeType is derived from DIDerivedType because some
424 /// composite types (such as enums) can be derived from basic types
425 // FIXME: Make this derive from DIType directly & just store the
426 // base type in a single DIType field.
427 class DICompositeType : public DIDerivedType {
428 friend class DIBuilder;
430 /// \brief Set the array of member DITypes.
431 void setArraysHelper(MDNode *Elements, MDNode *TParams);
434 DICompositeType() = default;
435 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
437 MDCompositeTypeBase *get() const {
438 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
440 operator MDCompositeTypeBase *() const { return get(); }
441 MDCompositeTypeBase *operator->() const { return get(); }
442 MDCompositeTypeBase &operator*() const {
443 assert(get() && "Expected valid pointer");
447 DIArray getElements() const {
448 assert(!isa<MDSubroutineType>(*this) && "no elements for DISubroutineType");
449 return DIArray(get()->getElements());
453 template <typename T>
454 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
456 (!TParams || DbgNode->getNumOperands() == 8) &&
457 "If you're setting the template parameters this should include a slot "
459 setArraysHelper(Elements, TParams);
463 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
464 DITypeRef getContainingType() const {
465 return DITypeRef::get(get()->getVTableHolder());
469 /// \brief Set the containing type.
470 void setContainingType(DICompositeType ContainingType);
473 DIArray getTemplateParams() const {
474 return DIArray(get()->getTemplateParams());
476 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
479 class DISubroutineType : public DICompositeType {
481 DISubroutineType() = default;
482 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
484 MDSubroutineType *get() const {
485 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
487 operator MDSubroutineType *() const { return get(); }
488 MDSubroutineType *operator->() const { return get(); }
489 MDSubroutineType &operator*() const {
490 assert(get() && "Expected valid pointer");
494 DITypedArray<DITypeRef> getTypeArray() const {
495 return DITypedArray<DITypeRef>(get()->getTypeArray());
499 /// \brief This is a wrapper for a file.
500 class DIFile : public DIScope {
503 DIFile(const MDFile *N) : DIScope(N) {}
505 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
506 operator MDFile *() const { return get(); }
507 MDFile *operator->() const { return get(); }
508 MDFile &operator*() const {
509 assert(get() && "Expected valid pointer");
513 /// \brief Retrieve the MDNode for the directory/file pair.
514 MDNode *getFileNode() const { return get(); }
517 /// \brief A wrapper for a compile unit.
518 class DICompileUnit : public DIScope {
520 DICompileUnit() = default;
521 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
523 MDCompileUnit *get() const {
524 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
526 operator MDCompileUnit *() const { return get(); }
527 MDCompileUnit *operator->() const { return get(); }
528 MDCompileUnit &operator*() const {
529 assert(get() && "Expected valid pointer");
533 dwarf::SourceLanguage getLanguage() const {
534 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
536 StringRef getProducer() const { return get()->getProducer(); }
537 bool isOptimized() const { return get()->isOptimized(); }
538 StringRef getFlags() const { return get()->getFlags(); }
539 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
541 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
542 DIArray getRetainedTypes() const {
543 return DIArray(get()->getRetainedTypes());
545 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
546 DIArray getGlobalVariables() const {
547 return DIArray(get()->getGlobalVariables());
549 DIArray getImportedEntities() const {
550 return DIArray(get()->getImportedEntities());
553 void replaceSubprograms(DIArray Subprograms);
554 void replaceGlobalVariables(DIArray GlobalVariables);
556 StringRef getSplitDebugFilename() const {
557 return get()->getSplitDebugFilename();
559 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
562 /// \brief This is a wrapper for a subprogram (e.g. a function).
563 class DISubprogram : public DIScope {
565 DISubprogram() = default;
566 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
568 MDSubprogram *get() const {
569 return cast_or_null<MDSubprogram>(DIDescriptor::get());
571 operator MDSubprogram *() const { return get(); }
572 MDSubprogram *operator->() const { return get(); }
573 MDSubprogram &operator*() const {
574 assert(get() && "Expected valid pointer");
578 StringRef getName() const { return get()->getName(); }
579 StringRef getDisplayName() const { return get()->getDisplayName(); }
580 StringRef getLinkageName() const { return get()->getLinkageName(); }
581 unsigned getLineNumber() const { return get()->getLine(); }
583 /// \brief Check if this is local (like 'static' in C).
584 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
585 unsigned isDefinition() const { return get()->isDefinition(); }
587 unsigned getVirtuality() const { return get()->getVirtuality(); }
588 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
590 unsigned getFlags() const { return get()->getFlags(); }
592 unsigned isOptimized() const { return get()->isOptimized(); }
594 /// \brief Get the beginning of the scope of the function (not the name).
595 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
597 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
598 DISubroutineType getType() const {
599 return DISubroutineType(get()->getType());
602 DITypeRef getContainingType() const {
603 return DITypeRef::get(get()->getContainingType());
606 /// \brief Check if this provides debugging information for the function F.
607 bool describes(const Function *F);
609 Function *getFunction() const;
611 void replaceFunction(Function *F) {
613 N->replaceFunction(F);
615 DIArray getTemplateParams() const {
616 return DIArray(get()->getTemplateParams());
618 DISubprogram getFunctionDeclaration() const {
619 return DISubprogram(get()->getDeclaration());
621 MDNode *getVariablesNodes() const { return getVariables(); }
622 DIArray getVariables() const { return DIArray(get()->getVariables()); }
624 unsigned isArtificial() const { return get()->isArtificial(); }
625 bool isPrivate() const { return get()->isPrivate(); }
626 bool isProtected() const { return get()->isProtected(); }
627 bool isPublic() const { return get()->isPublic(); }
628 bool isExplicit() const { return get()->isExplicit(); }
629 bool isPrototyped() const { return get()->isPrototyped(); }
630 unsigned isLValueReference() const { return get()->isLValueReference(); }
631 unsigned isRValueReference() const { return get()->isRValueReference(); }
634 /// \brief This is a wrapper for a lexical block.
635 class DILexicalBlock : public DIScope {
637 DILexicalBlock() = default;
638 DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
640 MDLexicalBlockBase *get() const {
641 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
643 operator MDLexicalBlockBase *() const { return get(); }
644 MDLexicalBlockBase *operator->() const { return get(); }
645 MDLexicalBlockBase &operator*() const {
646 assert(get() && "Expected valid pointer");
650 DIScope getContext() const { return DIScope(get()->getScope()); }
651 unsigned getLineNumber() const {
652 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
656 unsigned getColumnNumber() const {
657 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
658 return N->getColumn();
663 /// \brief This is a wrapper for a lexical block with a filename change.
664 class DILexicalBlockFile : public DIScope {
666 DILexicalBlockFile() = default;
667 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
669 MDLexicalBlockFile *get() const {
670 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
672 operator MDLexicalBlockFile *() const { return get(); }
673 MDLexicalBlockFile *operator->() const { return get(); }
674 MDLexicalBlockFile &operator*() const {
675 assert(get() && "Expected valid pointer");
679 DIScope getContext() const { return get()->getScope(); }
680 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
683 /// \brief A wrapper for a C++ style name space.
684 class DINameSpace : public DIScope {
686 DINameSpace() = default;
687 DINameSpace(const MDNamespace *N) : DIScope(N) {}
689 MDNamespace *get() const {
690 return cast_or_null<MDNamespace>(DIDescriptor::get());
692 operator MDNamespace *() const { return get(); }
693 MDNamespace *operator->() const { return get(); }
694 MDNamespace &operator*() const {
695 assert(get() && "Expected valid pointer");
699 StringRef getName() const { return get()->getName(); }
700 unsigned getLineNumber() const { return get()->getLine(); }
701 DIScope getContext() const { return DIScope(get()->getScope()); }
704 /// \brief This is a wrapper for template type parameter.
705 class DITemplateTypeParameter : public DIDescriptor {
707 DITemplateTypeParameter() = default;
708 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
710 MDTemplateTypeParameter *get() const {
711 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
713 operator MDTemplateTypeParameter *() const { return get(); }
714 MDTemplateTypeParameter *operator->() const { return get(); }
715 MDTemplateTypeParameter &operator*() const {
716 assert(get() && "Expected valid pointer");
720 StringRef getName() const { return get()->getName(); }
722 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
725 /// \brief This is a wrapper for template value parameter.
726 class DITemplateValueParameter : public DIDescriptor {
728 DITemplateValueParameter() = default;
729 DITemplateValueParameter(const MDTemplateValueParameter *N)
732 MDTemplateValueParameter *get() const {
733 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
735 operator MDTemplateValueParameter *() const { return get(); }
736 MDTemplateValueParameter *operator->() const { return get(); }
737 MDTemplateValueParameter &operator*() const {
738 assert(get() && "Expected valid pointer");
742 StringRef getName() const { return get()->getName(); }
743 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
744 Metadata *getValue() const { return get()->getValue(); }
747 /// \brief This is a wrapper for a global variable.
748 class DIGlobalVariable : public DIDescriptor {
749 DIFile getFile() const { return DIFile(get()->getFile()); }
752 DIGlobalVariable() = default;
753 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
755 MDGlobalVariable *get() const {
756 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
758 operator MDGlobalVariable *() const { return get(); }
759 MDGlobalVariable *operator->() const { return get(); }
760 MDGlobalVariable &operator*() const {
761 assert(get() && "Expected valid pointer");
765 StringRef getName() const { return get()->getName(); }
766 StringRef getDisplayName() const { return get()->getDisplayName(); }
767 StringRef getLinkageName() const { return get()->getLinkageName(); }
768 unsigned getLineNumber() const { return get()->getLine(); }
769 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
770 unsigned isDefinition() const { return get()->isDefinition(); }
772 DIScope getContext() const { return DIScope(get()->getScope()); }
773 StringRef getFilename() const { return getFile().getFilename(); }
774 StringRef getDirectory() const { return getFile().getDirectory(); }
775 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
777 GlobalVariable *getGlobal() const;
778 Constant *getConstant() const {
780 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
781 return C->getValue();
784 DIDerivedType getStaticDataMemberDeclaration() const {
785 return DIDerivedType(get()->getStaticDataMemberDeclaration());
789 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
790 class DIVariable : public DIDescriptor {
791 unsigned getFlags() const { return get()->getFlags(); }
794 DIVariable() = default;
795 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
797 MDLocalVariable *get() const {
798 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
800 operator MDLocalVariable *() const { return get(); }
801 MDLocalVariable *operator->() const { return get(); }
802 MDLocalVariable &operator*() const {
803 assert(get() && "Expected valid pointer");
807 StringRef getName() const { return get()->getName(); }
808 unsigned getLineNumber() const { return get()->getLine(); }
809 unsigned getArgNumber() const { return get()->getArg(); }
811 DIScope getContext() const { return DIScope(get()->getScope()); }
812 DIFile getFile() const { return DIFile(get()->getFile()); }
813 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
815 bool isArtificial() const { return get()->isArtificial(); }
816 bool isObjectPointer() const { return get()->isObjectPointer(); }
818 /// \brief If this variable is inlined then return inline location.
819 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
821 /// \brief Check if this is a "__block" variable (Apple Blocks).
822 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
823 return (getType().resolve(Map)).isBlockByrefStruct();
826 /// \brief Check if this is an inlined function argument.
827 bool isInlinedFnArgument(const Function *CurFn);
829 /// \brief Return the size reported by the variable's type.
830 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
832 void printExtendedName(raw_ostream &OS) const;
835 /// \brief A complex location expression in postfix notation.
837 /// This is (almost) a DWARF expression that modifies the location of a
838 /// variable or (or the location of a single piece of a variable).
840 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
841 /// and have DW_OP_plus consume the topmost elements on the stack.
842 class DIExpression : public DIDescriptor {
844 DIExpression() = default;
845 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
847 MDExpression *get() const {
848 return cast_or_null<MDExpression>(DIDescriptor::get());
850 operator MDExpression *() const { return get(); }
851 MDExpression *operator->() const { return get(); }
852 MDExpression &operator*() const {
853 assert(get() && "Expected valid pointer");
857 unsigned getNumElements() const { return get()->getNumElements(); }
858 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
859 bool isBitPiece() const { return get()->isBitPiece(); }
860 uint64_t getBitPieceOffset() const { return get()->getBitPieceOffset(); }
861 uint64_t getBitPieceSize() const { return get()->getBitPieceSize(); }
864 /// \brief This object holds location information.
866 /// This object is not associated with any DWARF tag.
867 class DILocation : public DIDescriptor {
869 DILocation() = default;
870 DILocation(const MDLocation *N) : DIDescriptor(N) {}
872 MDLocation *get() const {
873 return cast_or_null<MDLocation>(DIDescriptor::get());
875 operator MDLocation *() const { return get(); }
876 MDLocation *operator->() const { return get(); }
877 MDLocation &operator*() const {
878 assert(get() && "Expected valid pointer");
882 unsigned getLineNumber() const { return get()->getLine(); }
883 unsigned getColumnNumber() const { return get()->getColumn(); }
884 DIScope getScope() const { return DIScope(get()->getScope()); }
885 DILocation getOrigLocation() const {
886 return DILocation(get()->getInlinedAt());
888 StringRef getFilename() const { return getScope().getFilename(); }
889 StringRef getDirectory() const { return getScope().getDirectory(); }
890 bool atSameLineAs(const DILocation &Other) const {
891 return (getLineNumber() == Other.getLineNumber() &&
892 getFilename() == Other.getFilename());
894 /// \brief Get the DWAF discriminator.
896 /// DWARF discriminators are used to distinguish identical file locations for
897 /// instructions that are on different basic blocks. If two instructions are
898 /// inside the same lexical block and are in different basic blocks, we
899 /// create a new lexical block with identical location as the original but
900 /// with a different discriminator value
901 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
902 unsigned getDiscriminator() const {
903 // Since discriminators are associated with lexical blocks, make
904 // sure this location is a lexical block before retrieving its
906 if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
907 return F->getDiscriminator();
911 /// \brief Generate a new discriminator value for this location.
912 unsigned computeNewDiscriminator(LLVMContext &Ctx);
914 /// \brief Return a copy of this location with a different scope.
915 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
918 class DIObjCProperty : public DIDescriptor {
920 DIObjCProperty() = default;
921 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
923 MDObjCProperty *get() const {
924 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
926 operator MDObjCProperty *() const { return get(); }
927 MDObjCProperty *operator->() const { return get(); }
928 MDObjCProperty &operator*() const {
929 assert(get() && "Expected valid pointer");
933 StringRef getObjCPropertyName() const { return get()->getName(); }
934 DIFile getFile() const { return DIFile(get()->getFile()); }
935 unsigned getLineNumber() const { return get()->getLine(); }
937 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
938 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
939 unsigned getAttributes() const { return get()->getAttributes(); }
940 bool isReadOnlyObjCProperty() const {
941 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
943 bool isReadWriteObjCProperty() const {
944 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
946 bool isAssignObjCProperty() const {
947 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
949 bool isRetainObjCProperty() const {
950 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
952 bool isCopyObjCProperty() const {
953 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
955 bool isNonAtomicObjCProperty() const {
956 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
959 /// \brief Get the type.
961 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
962 /// the type as a DITypeRef here.
963 DIType getType() const { return DIType(get()->getType()); }
966 /// \brief An imported module (C++ using directive or similar).
967 class DIImportedEntity : public DIDescriptor {
969 DIImportedEntity() = default;
970 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
972 MDImportedEntity *get() const {
973 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
975 operator MDImportedEntity *() const { return get(); }
976 MDImportedEntity *operator->() const { return get(); }
977 MDImportedEntity &operator*() const {
978 assert(get() && "Expected valid pointer");
982 DIScope getContext() const { return DIScope(get()->getScope()); }
983 DIDescriptorRef getEntity() const {
984 return DIDescriptorRef::get(get()->getEntity());
986 unsigned getLineNumber() const { return get()->getLine(); }
987 StringRef getName() const { return get()->getName(); }
990 #define SIMPLIFY_DESCRIPTOR(DESC) \
991 template <> struct simplify_type<const DESC> { \
992 typedef Metadata *SimpleType; \
993 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
995 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
996 SIMPLIFY_DESCRIPTOR(DIDescriptor)
997 SIMPLIFY_DESCRIPTOR(DISubrange)
998 SIMPLIFY_DESCRIPTOR(DIEnumerator)
999 SIMPLIFY_DESCRIPTOR(DIScope)
1000 SIMPLIFY_DESCRIPTOR(DIType)
1001 SIMPLIFY_DESCRIPTOR(DIBasicType)
1002 SIMPLIFY_DESCRIPTOR(DIDerivedType)
1003 SIMPLIFY_DESCRIPTOR(DICompositeType)
1004 SIMPLIFY_DESCRIPTOR(DISubroutineType)
1005 SIMPLIFY_DESCRIPTOR(DIFile)
1006 SIMPLIFY_DESCRIPTOR(DICompileUnit)
1007 SIMPLIFY_DESCRIPTOR(DISubprogram)
1008 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
1009 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
1010 SIMPLIFY_DESCRIPTOR(DINameSpace)
1011 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
1012 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
1013 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
1014 SIMPLIFY_DESCRIPTOR(DIVariable)
1015 SIMPLIFY_DESCRIPTOR(DIExpression)
1016 SIMPLIFY_DESCRIPTOR(DILocation)
1017 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
1018 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
1019 #undef SIMPLIFY_DESCRIPTOR
1021 /// \brief Find subprogram that is enclosing this scope.
1022 DISubprogram getDISubprogram(const MDNode *Scope);
1024 /// \brief Find debug info for a given function.
1025 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1027 DISubprogram getDISubprogram(const Function *F);
1029 /// \brief Find underlying composite type.
1030 DICompositeType getDICompositeType(DIType T);
1032 /// \brief Create a new inlined variable based on current variable.
1034 /// @param DV Current Variable.
1035 /// @param InlinedScope Location at current variable is inlined.
1036 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1037 LLVMContext &VMContext);
1039 /// \brief Remove inlined scope from the variable.
1040 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1042 /// \brief Generate map by visiting all retained types.
1043 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1045 /// \brief Strip debug info in the module if it exists.
1047 /// To do this, we remove all calls to the debugger intrinsics and any named
1048 /// metadata for debugging. We also remove debug locations for instructions.
1049 /// Return true if module is modified.
1050 bool StripDebugInfo(Module &M);
1051 bool stripDebugInfo(Function &F);
1053 /// \brief Return Debug Info Metadata Version by checking module flags.
1054 unsigned getDebugMetadataVersionFromModule(const Module &M);
1056 /// \brief Utility to find all debug info in a module.
1058 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1059 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1060 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1061 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1062 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1063 /// used by the CUs.
1064 class DebugInfoFinder {
1066 DebugInfoFinder() : TypeMapInitialized(false) {}
1068 /// \brief Process entire module and collect debug info anchors.
1069 void processModule(const Module &M);
1071 /// \brief Process DbgDeclareInst.
1072 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1073 /// \brief Process DbgValueInst.
1074 void processValue(const Module &M, const DbgValueInst *DVI);
1075 /// \brief Process DILocation.
1076 void processLocation(const Module &M, DILocation Loc);
1078 /// \brief Clear all lists.
1082 void InitializeTypeMap(const Module &M);
1084 void processType(DIType DT);
1085 void processSubprogram(DISubprogram SP);
1086 void processScope(DIScope Scope);
1087 bool addCompileUnit(DICompileUnit CU);
1088 bool addGlobalVariable(DIGlobalVariable DIG);
1089 bool addSubprogram(DISubprogram SP);
1090 bool addType(DIType DT);
1091 bool addScope(DIScope Scope);
1094 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1095 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1096 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1097 global_variable_iterator;
1098 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1099 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1101 iterator_range<compile_unit_iterator> compile_units() const {
1102 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1105 iterator_range<subprogram_iterator> subprograms() const {
1106 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1109 iterator_range<global_variable_iterator> global_variables() const {
1110 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1113 iterator_range<type_iterator> types() const {
1114 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1117 iterator_range<scope_iterator> scopes() const {
1118 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1121 unsigned compile_unit_count() const { return CUs.size(); }
1122 unsigned global_variable_count() const { return GVs.size(); }
1123 unsigned subprogram_count() const { return SPs.size(); }
1124 unsigned type_count() const { return TYs.size(); }
1125 unsigned scope_count() const { return Scopes.size(); }
1128 SmallVector<DICompileUnit, 8> CUs;
1129 SmallVector<DISubprogram, 8> SPs;
1130 SmallVector<DIGlobalVariable, 8> GVs;
1131 SmallVector<DIType, 8> TYs;
1132 SmallVector<DIScope, 8> Scopes;
1133 SmallPtrSet<MDNode *, 64> NodesSeen;
1134 DITypeIdentifierMap TypeIdentifierMap;
1136 /// \brief Specify if TypeIdentifierMap is initialized.
1137 bool TypeMapInitialized;
1140 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1142 } // end namespace llvm