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.
67 /// \brief Duplicated debug info flags.
69 /// \see DebugNode::DIFlags.
71 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
72 #include "llvm/IR/DebugInfoFlags.def"
73 FlagAccessibility = DebugNode::FlagAccessibility
77 const MDNode *DbgNode;
80 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
81 DIDescriptor(const DebugNode *N) : DbgNode(N) {}
83 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
84 operator MDNode *() const { return get(); }
85 MDNode *operator->() const { return get(); }
86 MDNode &operator*() const {
87 assert(get() && "Expected valid pointer");
91 // An explicit operator bool so that we can do testing of DI values
93 // FIXME: This operator bool isn't actually protecting anything at the
94 // moment due to the conversion operator above making DIDescriptor nodes
95 // implicitly convertable to bool.
96 explicit operator bool() const { return DbgNode != nullptr; }
98 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
99 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
101 uint16_t getTag() const {
102 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
107 void print(raw_ostream &OS) const;
111 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
113 template <> struct simplify_type<const DESC>; \
114 template <> struct simplify_type<DESC>;
115 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
116 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
117 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
118 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
119 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
120 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
121 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
122 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
123 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
124 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
125 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
126 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
127 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
135 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
136 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
137 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
138 #undef DECLARE_SIMPLIFY_DESCRIPTOR
140 typedef DebugNodeArray DIArray;
141 typedef MDTypeRefArray DITypeArray;
143 /// \brief This is used to represent ranges, for array bounds.
144 class DISubrange : public DIDescriptor {
146 DISubrange() = default;
147 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
149 MDSubrange *get() const {
150 return cast_or_null<MDSubrange>(DIDescriptor::get());
152 operator MDSubrange *() const { return get(); }
153 MDSubrange *operator->() const { return get(); }
154 MDSubrange &operator*() const {
155 assert(get() && "Expected valid pointer");
159 int64_t getLo() const { return get()->getLowerBound(); }
160 int64_t getCount() const { return get()->getCount(); }
163 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
165 /// FIXME: it seems strange that this doesn't have either a reference to the
166 /// type/precision or a file/line pair for location info.
167 class DIEnumerator : public DIDescriptor {
169 DIEnumerator() = default;
170 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
172 MDEnumerator *get() const {
173 return cast_or_null<MDEnumerator>(DIDescriptor::get());
175 operator MDEnumerator *() const { return get(); }
176 MDEnumerator *operator->() const { return get(); }
177 MDEnumerator &operator*() const {
178 assert(get() && "Expected valid pointer");
182 StringRef getName() const { return get()->getName(); }
183 int64_t getEnumValue() const { return get()->getValue(); }
186 template <typename T> class DIRef;
187 typedef DIRef<DIDescriptor> DIDescriptorRef;
188 typedef DIRef<DIScope> DIScopeRef;
189 typedef DIRef<DIType> DITypeRef;
191 /// \brief A base class for various scopes.
193 /// Although, implementation-wise, DIScope is the parent class of most
194 /// other DIxxx classes, including DIType and its descendants, most of
195 /// DIScope's descendants are not a substitutable subtype of
196 /// DIScope. The DIDescriptor::isScope() method only is true for
197 /// DIScopes that are scopes in the strict lexical scope sense
198 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
199 class DIScope : public DIDescriptor {
202 DIScope(const MDScope *N) : DIDescriptor(N) {}
204 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
205 operator MDScope *() const { return get(); }
206 MDScope *operator->() const { return get(); }
207 MDScope &operator*() const {
208 assert(get() && "Expected valid pointer");
212 inline DIScopeRef getContext() const;
213 StringRef getName() const { return get()->getName(); }
214 StringRef getFilename() const { return get()->getFilename(); }
215 StringRef getDirectory() const { return get()->getDirectory(); }
217 /// \brief Generate a reference to this DIScope.
219 /// Uses the type identifier instead of the actual MDNode if possible, to
220 /// help type uniquing.
221 DIScopeRef getRef() const;
224 /// \brief Represents reference to a DIDescriptor.
226 /// Abstracts over direct and identifier-based metadata references.
227 template <typename T> class DIRef {
228 /// \brief Val can be either a MDNode or a MDString.
230 /// In the latter, MDString specifies the type identifier.
235 DIRef(const TypedDebugNodeRef<U> &Ref,
236 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
240 T resolve(const DITypeIdentifierMap &Map) const;
241 operator Metadata *() const { return const_cast<Metadata *>(Val); }
245 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
247 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
248 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
250 DIScopeRef DIScope::getContext() const { return get()->getScope(); }
252 /// \brief This is a wrapper for a type.
254 /// FIXME: Types should be factored much better so that CV qualifiers and
255 /// others do not require a huge and empty descriptor full of zeros.
256 class DIType : public DIScope {
259 DIType(const MDType *N) : DIScope(N) {}
261 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
262 operator MDType *() const { return get(); }
263 MDType *operator->() const { return get(); }
264 MDType &operator*() const {
265 assert(get() && "Expected valid pointer");
269 DIScopeRef getContext() const { return get()->getScope(); }
270 StringRef getName() const { return get()->getName(); }
271 unsigned getLineNumber() const { return get()->getLine(); }
272 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
273 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
274 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
275 // carry this is just plain insane.
276 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
277 unsigned getFlags() const { return get()->getFlags(); }
279 bool isPrivate() const { return get()->isPrivate(); }
280 bool isProtected() const { return get()->isProtected(); }
281 bool isPublic() const { return get()->isPublic(); }
282 bool isForwardDecl() const { return get()->isForwardDecl(); }
283 bool isAppleBlockExtension() const { return get()->isAppleBlockExtension(); }
284 bool isBlockByrefStruct() const { return get()->isBlockByrefStruct(); }
285 bool isVirtual() const { return get()->isVirtual(); }
286 bool isArtificial() const { return get()->isArtificial(); }
287 bool isObjectPointer() const { return get()->isObjectPointer(); }
288 bool isObjcClassComplete() const { return get()->isObjcClassComplete(); }
289 bool isVector() const { return get()->isVector(); }
290 bool isStaticMember() const { return get()->isStaticMember(); }
291 bool isLValueReference() const { return get()->isLValueReference(); }
292 bool isRValueReference() const { return get()->isRValueReference(); }
294 bool isValid() const { return DbgNode && isa<MDType>(*this); }
297 /// \brief A basic type, like 'int' or 'float'.
298 class DIBasicType : public DIType {
300 DIBasicType() = default;
301 DIBasicType(const MDBasicType *N) : DIType(N) {}
303 MDBasicType *get() const {
304 return cast_or_null<MDBasicType>(DIDescriptor::get());
306 operator MDBasicType *() const { return get(); }
307 MDBasicType *operator->() const { return get(); }
308 MDBasicType &operator*() const {
309 assert(get() && "Expected valid pointer");
313 unsigned getEncoding() const { return get()->getEncoding(); }
316 /// \brief A simple derived type
318 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
319 /// Or, a data member of a class/struct/union.
320 class DIDerivedType : public DIType {
322 DIDerivedType() = default;
323 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
325 MDDerivedTypeBase *get() const {
326 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
328 operator MDDerivedTypeBase *() const { return get(); }
329 MDDerivedTypeBase *operator->() const { return get(); }
330 MDDerivedTypeBase &operator*() const {
331 assert(get() && "Expected valid pointer");
335 DITypeRef getTypeDerivedFrom() const { return get()->getBaseType(); }
337 /// \brief Return property node, if this ivar is associated with one.
338 MDNode *getObjCProperty() const {
339 if (auto *N = dyn_cast<MDDerivedType>(get()))
340 return dyn_cast_or_null<MDNode>(N->getExtraData());
344 DITypeRef getClassType() const {
345 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
346 if (auto *N = dyn_cast<MDDerivedType>(get()))
347 return MDTypeRef(N->getExtraData());
351 Constant *getConstant() const {
352 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
353 if (auto *N = dyn_cast<MDDerivedType>(get()))
354 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
355 return C->getValue();
361 /// \brief Types that refer to multiple other types.
363 /// This descriptor holds a type that can refer to multiple other types, like a
364 /// function or struct.
366 /// DICompositeType is derived from DIDerivedType because some
367 /// composite types (such as enums) can be derived from basic types
368 // FIXME: Make this derive from DIType directly & just store the
369 // base type in a single DIType field.
370 class DICompositeType : public DIDerivedType {
371 friend class DIBuilder;
374 DICompositeType() = default;
375 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
377 MDCompositeTypeBase *get() const {
378 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
380 operator MDCompositeTypeBase *() const { return get(); }
381 MDCompositeTypeBase *operator->() const { return get(); }
382 MDCompositeTypeBase &operator*() const {
383 assert(get() && "Expected valid pointer");
387 DIArray getElements() const {
388 assert(!isa<MDSubroutineType>(*this) && "no elements for DISubroutineType");
389 return DIArray(get()->getElements());
392 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
393 DITypeRef getContainingType() const { return get()->getVTableHolder(); }
395 DIArray getTemplateParams() const { return get()->getTemplateParams(); }
396 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
399 class DISubroutineType : public DICompositeType {
401 DISubroutineType() = default;
402 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
404 MDSubroutineType *get() const {
405 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
407 operator MDSubroutineType *() const { return get(); }
408 MDSubroutineType *operator->() const { return get(); }
409 MDSubroutineType &operator*() const {
410 assert(get() && "Expected valid pointer");
414 MDTypeRefArray getTypeArray() const { return get()->getTypeArray(); }
417 /// \brief This is a wrapper for a file.
418 class DIFile : public DIScope {
421 DIFile(const MDFile *N) : DIScope(N) {}
423 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
424 operator MDFile *() const { return get(); }
425 MDFile *operator->() const { return get(); }
426 MDFile &operator*() const {
427 assert(get() && "Expected valid pointer");
431 /// \brief Retrieve the MDNode for the directory/file pair.
432 MDNode *getFileNode() const { return get(); }
435 /// \brief A wrapper for a compile unit.
436 class DICompileUnit : public DIScope {
438 DICompileUnit() = default;
439 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
441 MDCompileUnit *get() const {
442 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
444 operator MDCompileUnit *() const { return get(); }
445 MDCompileUnit *operator->() const { return get(); }
446 MDCompileUnit &operator*() const {
447 assert(get() && "Expected valid pointer");
451 dwarf::SourceLanguage getLanguage() const {
452 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
454 StringRef getProducer() const { return get()->getProducer(); }
455 bool isOptimized() const { return get()->isOptimized(); }
456 StringRef getFlags() const { return get()->getFlags(); }
457 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
459 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
460 DIArray getRetainedTypes() const {
461 return DIArray(get()->getRetainedTypes());
463 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
464 DIArray getGlobalVariables() const {
465 return DIArray(get()->getGlobalVariables());
467 DIArray getImportedEntities() const {
468 return DIArray(get()->getImportedEntities());
471 void replaceSubprograms(DIArray Subprograms);
472 void replaceGlobalVariables(DIArray GlobalVariables);
474 StringRef getSplitDebugFilename() const {
475 return get()->getSplitDebugFilename();
477 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
480 /// \brief This is a wrapper for a subprogram (e.g. a function).
481 class DISubprogram : public DIScope {
483 DISubprogram() = default;
484 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
486 MDSubprogram *get() const {
487 return cast_or_null<MDSubprogram>(DIDescriptor::get());
489 operator MDSubprogram *() const { return get(); }
490 MDSubprogram *operator->() const { return get(); }
491 MDSubprogram &operator*() const {
492 assert(get() && "Expected valid pointer");
496 StringRef getName() const { return get()->getName(); }
497 StringRef getDisplayName() const { return get()->getDisplayName(); }
498 StringRef getLinkageName() const { return get()->getLinkageName(); }
499 unsigned getLineNumber() const { return get()->getLine(); }
501 /// \brief Check if this is local (like 'static' in C).
502 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
503 unsigned isDefinition() const { return get()->isDefinition(); }
505 unsigned getVirtuality() const { return get()->getVirtuality(); }
506 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
508 unsigned getFlags() const { return get()->getFlags(); }
510 unsigned isOptimized() const { return get()->isOptimized(); }
512 /// \brief Get the beginning of the scope of the function (not the name).
513 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
515 DIScopeRef getContext() const { return get()->getScope(); }
516 DISubroutineType getType() const { return get()->getType(); }
518 DITypeRef getContainingType() const { return get()->getContainingType(); }
520 /// \brief Check if this provides debugging information for the function F.
521 bool describes(const Function *F) const { return get()->describes(F); }
523 Function *getFunction() const { return get()->getFunction(); }
525 void replaceFunction(Function *F) { get()->replaceFunction(F); }
526 DIArray getTemplateParams() const { return get()->getTemplateParams(); }
527 DISubprogram getFunctionDeclaration() const {
528 return get()->getDeclaration();
530 DIArray getVariables() const { return DIArray(get()->getVariables()); }
532 unsigned isArtificial() const { return get()->isArtificial(); }
533 bool isPrivate() const { return get()->isPrivate(); }
534 bool isProtected() const { return get()->isProtected(); }
535 bool isPublic() const { return get()->isPublic(); }
536 bool isExplicit() const { return get()->isExplicit(); }
537 bool isPrototyped() const { return get()->isPrototyped(); }
538 unsigned isLValueReference() const { return get()->isLValueReference(); }
539 unsigned isRValueReference() const { return get()->isRValueReference(); }
542 /// \brief This is a wrapper for a lexical block.
543 class DILexicalBlock : public DIScope {
545 DILexicalBlock() = default;
546 DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
548 MDLexicalBlockBase *get() const {
549 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
551 operator MDLexicalBlockBase *() const { return get(); }
552 MDLexicalBlockBase *operator->() const { return get(); }
553 MDLexicalBlockBase &operator*() const {
554 assert(get() && "Expected valid pointer");
558 DIScope getContext() const { return DIScope(get()->getScope()); }
559 unsigned getLineNumber() const {
560 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
564 unsigned getColumnNumber() const {
565 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
566 return N->getColumn();
571 /// \brief This is a wrapper for a lexical block with a filename change.
572 class DILexicalBlockFile : public DIScope {
574 DILexicalBlockFile() = default;
575 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
577 MDLexicalBlockFile *get() const {
578 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
580 operator MDLexicalBlockFile *() const { return get(); }
581 MDLexicalBlockFile *operator->() const { return get(); }
582 MDLexicalBlockFile &operator*() const {
583 assert(get() && "Expected valid pointer");
587 DIScope getContext() const { return get()->getScope(); }
588 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
591 /// \brief A wrapper for a C++ style name space.
592 class DINameSpace : public DIScope {
594 DINameSpace() = default;
595 DINameSpace(const MDNamespace *N) : DIScope(N) {}
597 MDNamespace *get() const {
598 return cast_or_null<MDNamespace>(DIDescriptor::get());
600 operator MDNamespace *() const { return get(); }
601 MDNamespace *operator->() const { return get(); }
602 MDNamespace &operator*() const {
603 assert(get() && "Expected valid pointer");
607 StringRef getName() const { return get()->getName(); }
608 unsigned getLineNumber() const { return get()->getLine(); }
609 DIScope getContext() const { return DIScope(get()->getScope()); }
612 /// \brief This is a wrapper for template type parameter.
613 class DITemplateTypeParameter : public DIDescriptor {
615 DITemplateTypeParameter() = default;
616 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
618 MDTemplateTypeParameter *get() const {
619 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
621 operator MDTemplateTypeParameter *() const { return get(); }
622 MDTemplateTypeParameter *operator->() const { return get(); }
623 MDTemplateTypeParameter &operator*() const {
624 assert(get() && "Expected valid pointer");
628 StringRef getName() const { return get()->getName(); }
629 DITypeRef getType() const { return get()->getType(); }
632 /// \brief This is a wrapper for template value parameter.
633 class DITemplateValueParameter : public DIDescriptor {
635 DITemplateValueParameter() = default;
636 DITemplateValueParameter(const MDTemplateValueParameter *N)
639 MDTemplateValueParameter *get() const {
640 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
642 operator MDTemplateValueParameter *() const { return get(); }
643 MDTemplateValueParameter *operator->() const { return get(); }
644 MDTemplateValueParameter &operator*() const {
645 assert(get() && "Expected valid pointer");
649 StringRef getName() const { return get()->getName(); }
650 DITypeRef getType() const { return get()->getType(); }
651 Metadata *getValue() const { return get()->getValue(); }
654 /// \brief This is a wrapper for a global variable.
655 class DIGlobalVariable : public DIDescriptor {
656 DIFile getFile() const { return DIFile(get()->getFile()); }
659 DIGlobalVariable() = default;
660 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
662 MDGlobalVariable *get() const {
663 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
665 operator MDGlobalVariable *() const { return get(); }
666 MDGlobalVariable *operator->() const { return get(); }
667 MDGlobalVariable &operator*() const {
668 assert(get() && "Expected valid pointer");
672 StringRef getName() const { return get()->getName(); }
673 StringRef getDisplayName() const { return get()->getDisplayName(); }
674 StringRef getLinkageName() const { return get()->getLinkageName(); }
675 unsigned getLineNumber() const { return get()->getLine(); }
676 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
677 unsigned isDefinition() const { return get()->isDefinition(); }
679 DIScope getContext() const { return DIScope(get()->getScope()); }
680 StringRef getFilename() const { return get()->getFilename(); }
681 StringRef getDirectory() const { return get()->getDirectory(); }
682 DITypeRef getType() const { return get()->getType(); }
684 Constant *getConstant() const { return get()->getVariable(); }
685 DIDerivedType getStaticDataMemberDeclaration() const {
686 return DIDerivedType(get()->getStaticDataMemberDeclaration());
690 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
691 class DIVariable : public DIDescriptor {
692 unsigned getFlags() const { return get()->getFlags(); }
695 DIVariable() = default;
696 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
698 MDLocalVariable *get() const {
699 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
701 operator MDLocalVariable *() const { return get(); }
702 MDLocalVariable *operator->() const { return get(); }
703 MDLocalVariable &operator*() const {
704 assert(get() && "Expected valid pointer");
708 StringRef getName() const { return get()->getName(); }
709 unsigned getLineNumber() const { return get()->getLine(); }
710 unsigned getArgNumber() const { return get()->getArg(); }
712 DIScope getContext() const { return DIScope(get()->getScope()); }
713 DIFile getFile() const { return DIFile(get()->getFile()); }
714 DITypeRef getType() const { return get()->getType(); }
716 bool isArtificial() const { return get()->isArtificial(); }
717 bool isObjectPointer() const { return get()->isObjectPointer(); }
719 /// \brief If this variable is inlined then return inline location.
720 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
722 /// \brief Check if this is a "__block" variable (Apple Blocks).
723 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
724 return (getType().resolve(Map)).isBlockByrefStruct();
727 void printExtendedName(raw_ostream &OS) const;
730 /// \brief A complex location expression in postfix notation.
732 /// This is (almost) a DWARF expression that modifies the location of a
733 /// variable or (or the location of a single piece of a variable).
735 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
736 /// and have DW_OP_plus consume the topmost elements on the stack.
737 class DIExpression : public DIDescriptor {
739 DIExpression() = default;
740 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
742 MDExpression *get() const {
743 return cast_or_null<MDExpression>(DIDescriptor::get());
745 operator MDExpression *() const { return get(); }
746 MDExpression *operator->() const { return get(); }
747 MDExpression &operator*() const {
748 assert(get() && "Expected valid pointer");
752 unsigned getNumElements() const { return get()->getNumElements(); }
753 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
754 bool isBitPiece() const { return get()->isBitPiece(); }
755 uint64_t getBitPieceOffset() const { return get()->getBitPieceOffset(); }
756 uint64_t getBitPieceSize() const { return get()->getBitPieceSize(); }
759 /// \brief This object holds location information.
761 /// This object is not associated with any DWARF tag.
762 class DILocation : public DIDescriptor {
764 DILocation() = default;
765 DILocation(const MDLocation *N) : DIDescriptor(N) {}
767 MDLocation *get() const {
768 return cast_or_null<MDLocation>(DIDescriptor::get());
770 operator MDLocation *() const { return get(); }
771 MDLocation *operator->() const { return get(); }
772 MDLocation &operator*() const {
773 assert(get() && "Expected valid pointer");
777 unsigned getLineNumber() const { return get()->getLine(); }
778 unsigned getColumnNumber() const { return get()->getColumn(); }
779 DIScope getScope() const { return DIScope(get()->getScope()); }
780 DILocation getOrigLocation() const {
781 return DILocation(get()->getInlinedAt());
783 StringRef getFilename() const { return get()->getFilename(); }
784 StringRef getDirectory() const { return get()->getDirectory(); }
786 /// \brief Get the DWAF discriminator.
788 /// DWARF discriminators are used to distinguish identical file locations for
789 /// instructions that are on different basic blocks. If two instructions are
790 /// inside the same lexical block and are in different basic blocks, we
791 /// create a new lexical block with identical location as the original but
792 /// with a different discriminator value
793 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
794 unsigned getDiscriminator() const {
795 // Since discriminators are associated with lexical blocks, make
796 // sure this location is a lexical block before retrieving its
798 if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
799 return F->getDiscriminator();
803 /// \brief Generate a new discriminator value for this location.
804 unsigned computeNewDiscriminator(LLVMContext &Ctx);
806 /// \brief Return a copy of this location with a different scope.
807 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
810 class DIObjCProperty : public DIDescriptor {
812 DIObjCProperty() = default;
813 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
815 MDObjCProperty *get() const {
816 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
818 operator MDObjCProperty *() const { return get(); }
819 MDObjCProperty *operator->() const { return get(); }
820 MDObjCProperty &operator*() const {
821 assert(get() && "Expected valid pointer");
825 StringRef getObjCPropertyName() const { return get()->getName(); }
826 DIFile getFile() const { return DIFile(get()->getFile()); }
827 unsigned getLineNumber() const { return get()->getLine(); }
829 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
830 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
831 unsigned getAttributes() const { return get()->getAttributes(); }
832 bool isReadOnlyObjCProperty() const {
833 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
835 bool isReadWriteObjCProperty() const {
836 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
838 bool isAssignObjCProperty() const {
839 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
841 bool isRetainObjCProperty() const {
842 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
844 bool isCopyObjCProperty() const {
845 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
847 bool isNonAtomicObjCProperty() const {
848 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
851 /// \brief Get the type.
853 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
854 /// the type as a DITypeRef here.
855 DIType getType() const { return DIType(get()->getType()); }
858 /// \brief An imported module (C++ using directive or similar).
859 class DIImportedEntity : public DIDescriptor {
861 DIImportedEntity() = default;
862 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
864 MDImportedEntity *get() const {
865 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
867 operator MDImportedEntity *() const { return get(); }
868 MDImportedEntity *operator->() const { return get(); }
869 MDImportedEntity &operator*() const {
870 assert(get() && "Expected valid pointer");
874 DIScope getContext() const { return DIScope(get()->getScope()); }
875 DIDescriptorRef getEntity() const { return get()->getEntity(); }
876 unsigned getLineNumber() const { return get()->getLine(); }
877 StringRef getName() const { return get()->getName(); }
880 #define SIMPLIFY_DESCRIPTOR(DESC) \
881 template <> struct simplify_type<const DESC> { \
882 typedef Metadata *SimpleType; \
883 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
885 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
886 SIMPLIFY_DESCRIPTOR(DIDescriptor)
887 SIMPLIFY_DESCRIPTOR(DISubrange)
888 SIMPLIFY_DESCRIPTOR(DIEnumerator)
889 SIMPLIFY_DESCRIPTOR(DIScope)
890 SIMPLIFY_DESCRIPTOR(DIType)
891 SIMPLIFY_DESCRIPTOR(DIBasicType)
892 SIMPLIFY_DESCRIPTOR(DIDerivedType)
893 SIMPLIFY_DESCRIPTOR(DICompositeType)
894 SIMPLIFY_DESCRIPTOR(DISubroutineType)
895 SIMPLIFY_DESCRIPTOR(DIFile)
896 SIMPLIFY_DESCRIPTOR(DICompileUnit)
897 SIMPLIFY_DESCRIPTOR(DISubprogram)
898 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
899 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
900 SIMPLIFY_DESCRIPTOR(DINameSpace)
901 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
902 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
903 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
904 SIMPLIFY_DESCRIPTOR(DIVariable)
905 SIMPLIFY_DESCRIPTOR(DIExpression)
906 SIMPLIFY_DESCRIPTOR(DILocation)
907 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
908 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
909 #undef SIMPLIFY_DESCRIPTOR
911 /// \brief Find subprogram that is enclosing this scope.
912 DISubprogram getDISubprogram(const MDNode *Scope);
914 /// \brief Find debug info for a given function.
915 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
917 DISubprogram getDISubprogram(const Function *F);
919 /// \brief Find underlying composite type.
920 DICompositeType getDICompositeType(DIType T);
922 /// \brief Create a new inlined variable based on current variable.
924 /// @param DV Current Variable.
925 /// @param InlinedScope Location at current variable is inlined.
926 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
927 LLVMContext &VMContext);
929 /// \brief Remove inlined scope from the variable.
930 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
932 /// \brief Generate map by visiting all retained types.
933 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
935 /// \brief Strip debug info in the module if it exists.
937 /// To do this, we remove all calls to the debugger intrinsics and any named
938 /// metadata for debugging. We also remove debug locations for instructions.
939 /// Return true if module is modified.
940 bool StripDebugInfo(Module &M);
941 bool stripDebugInfo(Function &F);
943 /// \brief Return Debug Info Metadata Version by checking module flags.
944 unsigned getDebugMetadataVersionFromModule(const Module &M);
946 /// \brief Utility to find all debug info in a module.
948 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
949 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
950 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
951 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
952 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
954 class DebugInfoFinder {
956 DebugInfoFinder() : TypeMapInitialized(false) {}
958 /// \brief Process entire module and collect debug info anchors.
959 void processModule(const Module &M);
961 /// \brief Process DbgDeclareInst.
962 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
963 /// \brief Process DbgValueInst.
964 void processValue(const Module &M, const DbgValueInst *DVI);
965 /// \brief Process DILocation.
966 void processLocation(const Module &M, DILocation Loc);
968 /// \brief Clear all lists.
972 void InitializeTypeMap(const Module &M);
974 void processType(DIType DT);
975 void processSubprogram(DISubprogram SP);
976 void processScope(DIScope Scope);
977 bool addCompileUnit(DICompileUnit CU);
978 bool addGlobalVariable(DIGlobalVariable DIG);
979 bool addSubprogram(DISubprogram SP);
980 bool addType(DIType DT);
981 bool addScope(DIScope Scope);
984 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
985 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
986 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
987 global_variable_iterator;
988 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
989 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
991 iterator_range<compile_unit_iterator> compile_units() const {
992 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
995 iterator_range<subprogram_iterator> subprograms() const {
996 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
999 iterator_range<global_variable_iterator> global_variables() const {
1000 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1003 iterator_range<type_iterator> types() const {
1004 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1007 iterator_range<scope_iterator> scopes() const {
1008 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1011 unsigned compile_unit_count() const { return CUs.size(); }
1012 unsigned global_variable_count() const { return GVs.size(); }
1013 unsigned subprogram_count() const { return SPs.size(); }
1014 unsigned type_count() const { return TYs.size(); }
1015 unsigned scope_count() const { return Scopes.size(); }
1018 SmallVector<DICompileUnit, 8> CUs;
1019 SmallVector<DISubprogram, 8> SPs;
1020 SmallVector<DIGlobalVariable, 8> GVs;
1021 SmallVector<DIType, 8> TYs;
1022 SmallVector<DIScope, 8> Scopes;
1023 SmallPtrSet<MDNode *, 64> NodesSeen;
1024 DITypeIdentifierMap TypeIdentifierMap;
1026 /// \brief Specify if TypeIdentifierMap is initialized.
1027 bool TypeMapInitialized;
1030 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1032 } // end namespace llvm